blob: 0d5543317214ce1897ae52f7e7bfc7825df005e1 [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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "hw/usb.h"
26#include "hw/pcmcia.h"
27#include "hw/pc.h"
28#include "hw/pci.h"
29#include "gdbstub.h"
30#include "net.h"
31#include "qemu-char.h"
32#include "sysemu.h"
33#include "console.h"
34#include "block.h"
35#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000036#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000037#include "balloon.h"
bellard81d09122004-07-14 17:21:37 +000038#include <dirent.h>
balrogc8256f92008-06-08 22:45:01 +000039#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000040#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000041#include "kvm.h"
ths6a5bd302007-12-03 17:05:38 +000042
bellard9dc39cb2004-03-14 21:38:27 +000043//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000044//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000045
bellard9307c4c2004-04-04 12:57:25 +000046/*
47 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000048 *
bellard9307c4c2004-04-04 12:57:25 +000049 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000050 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000051 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000052 * 'i' 32 bit integer
53 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000054 * '/' optional gdb-like print format (like "/10x")
55 *
56 * '?' optional type (for 'F', 's' and 'i')
57 *
58 */
59
bellard9dc39cb2004-03-14 21:38:27 +000060typedef struct term_cmd_t {
61 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000062 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000063 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000064 const char *params;
65 const char *help;
66} term_cmd_t;
67
ths20d8a3e2007-02-18 17:04:49 +000068#define MAX_MON 4
69static CharDriverState *monitor_hd[MAX_MON];
ths86e94de2007-01-05 22:01:59 +000070static int hide_banner;
bellard7e2515e2004-08-01 21:52:19 +000071
blueswir18662d652008-10-02 18:32:44 +000072static const term_cmd_t term_cmds[];
73static const term_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000074
ths60fe76f2007-12-16 03:02:09 +000075static uint8_t term_outbuf[1024];
bellard7e2515e2004-08-01 21:52:19 +000076static int term_outbuf_index;
bellard81d09122004-07-14 17:21:37 +000077
aliguori83ab7952008-08-19 14:44:22 +000078static void monitor_start_input(void);
79
blueswir1b1d8e522008-10-26 13:43:07 +000080static CPUState *mon_cpu = NULL;
bellard6a00d602005-11-21 23:25:50 +000081
bellard9dc39cb2004-03-14 21:38:27 +000082void term_flush(void)
83{
ths20d8a3e2007-02-18 17:04:49 +000084 int i;
bellard7e2515e2004-08-01 21:52:19 +000085 if (term_outbuf_index > 0) {
ths20d8a3e2007-02-18 17:04:49 +000086 for (i = 0; i < MAX_MON; i++)
87 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
88 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
bellard7e2515e2004-08-01 21:52:19 +000089 term_outbuf_index = 0;
90 }
91}
92
93/* flush at every end of line or if the buffer is full */
94void term_puts(const char *str)
95{
ths60fe76f2007-12-16 03:02:09 +000096 char c;
bellard7e2515e2004-08-01 21:52:19 +000097 for(;;) {
98 c = *str++;
99 if (c == '\0')
100 break;
bellard7ba12602006-07-14 20:26:42 +0000101 if (c == '\n')
102 term_outbuf[term_outbuf_index++] = '\r';
bellard7e2515e2004-08-01 21:52:19 +0000103 term_outbuf[term_outbuf_index++] = c;
bellard7ba12602006-07-14 20:26:42 +0000104 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
bellard7e2515e2004-08-01 21:52:19 +0000105 c == '\n')
106 term_flush();
107 }
108}
109
110void term_vprintf(const char *fmt, va_list ap)
111{
112 char buf[4096];
113 vsnprintf(buf, sizeof(buf), fmt, ap);
114 term_puts(buf);
115}
116
117void term_printf(const char *fmt, ...)
118{
119 va_list ap;
120 va_start(ap, fmt);
121 term_vprintf(fmt, ap);
122 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000123}
124
thsfef30742006-12-22 14:11:32 +0000125void term_print_filename(const char *filename)
126{
127 int i;
128
129 for (i = 0; filename[i]; i++) {
130 switch (filename[i]) {
131 case ' ':
132 case '"':
133 case '\\':
134 term_printf("\\%c", filename[i]);
135 break;
136 case '\t':
137 term_printf("\\t");
138 break;
139 case '\r':
140 term_printf("\\r");
141 break;
142 case '\n':
143 term_printf("\\n");
144 break;
145 default:
146 term_printf("%c", filename[i]);
147 break;
148 }
149 }
150}
151
bellard7fe48482004-10-09 18:08:01 +0000152static int monitor_fprintf(FILE *stream, const char *fmt, ...)
153{
154 va_list ap;
155 va_start(ap, fmt);
156 term_vprintf(fmt, ap);
157 va_end(ap);
158 return 0;
159}
160
bellard9dc39cb2004-03-14 21:38:27 +0000161static int compare_cmd(const char *name, const char *list)
162{
163 const char *p, *pstart;
164 int len;
165 len = strlen(name);
166 p = list;
167 for(;;) {
168 pstart = p;
169 p = strchr(p, '|');
170 if (!p)
171 p = pstart + strlen(pstart);
172 if ((p - pstart) == len && !memcmp(pstart, name, len))
173 return 1;
174 if (*p == '\0')
175 break;
176 p++;
177 }
178 return 0;
179}
180
blueswir18662d652008-10-02 18:32:44 +0000181static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000182{
blueswir18662d652008-10-02 18:32:44 +0000183 const term_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000184
185 for(cmd = cmds; cmd->name != NULL; cmd++) {
186 if (!name || !strcmp(name, cmd->name))
187 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
188 }
189}
190
191static void help_cmd(const char *name)
192{
193 if (name && !strcmp(name, "info")) {
194 help_cmd1(info_cmds, "info ", NULL);
195 } else {
196 help_cmd1(term_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000197 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000198 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +0000199 term_printf("Log items (comma separated):\n");
200 term_printf("%-10s %s\n", "none", "remove all logs");
201 for(item = cpu_log_items; item->mask != 0; item++) {
202 term_printf("%-10s %s\n", item->name, item->help);
203 }
204 }
bellard9dc39cb2004-03-14 21:38:27 +0000205 }
206}
207
bellard9307c4c2004-04-04 12:57:25 +0000208static void do_help(const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000209{
bellard9307c4c2004-04-04 12:57:25 +0000210 help_cmd(name);
bellard9dc39cb2004-03-14 21:38:27 +0000211}
212
bellard7954c732006-08-01 15:52:40 +0000213static void do_commit(const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000214{
bellard7954c732006-08-01 15:52:40 +0000215 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000216
bellard7954c732006-08-01 15:52:40 +0000217 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000218 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000219 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000220 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
221 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000222 }
223}
224
bellard9307c4c2004-04-04 12:57:25 +0000225static void do_info(const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000226{
blueswir18662d652008-10-02 18:32:44 +0000227 const term_cmd_t *cmd;
blueswir1a5f1b962008-08-17 20:21:51 +0000228 void (*handler)(void);
bellard9dc39cb2004-03-14 21:38:27 +0000229
bellard9307c4c2004-04-04 12:57:25 +0000230 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000231 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000232 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000233 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000234 goto found;
235 }
236 help:
bellard9307c4c2004-04-04 12:57:25 +0000237 help_cmd("info");
bellard9dc39cb2004-03-14 21:38:27 +0000238 return;
239 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000240 handler = cmd->handler;
241 handler();
bellard9dc39cb2004-03-14 21:38:27 +0000242}
243
bellard9bc9d1c2004-10-10 15:15:51 +0000244static void do_info_version(void)
245{
246 term_printf("%s\n", QEMU_VERSION);
247}
248
thsc35734b2007-03-19 15:17:08 +0000249static void do_info_name(void)
250{
251 if (qemu_name)
252 term_printf("%s\n", qemu_name);
253}
254
blueswir1f1f23ad2008-09-18 18:30:20 +0000255static void do_info_uuid(void)
256{
257 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
258 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
259 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
260 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
261 qemu_uuid[15]);
262}
263
bellard9307c4c2004-04-04 12:57:25 +0000264static void do_info_block(void)
bellard9dc39cb2004-03-14 21:38:27 +0000265{
266 bdrv_info();
267}
268
thsa36e69d2007-12-02 05:18:19 +0000269static void do_info_blockstats(void)
270{
271 bdrv_info_stats();
272}
273
bellard6a00d602005-11-21 23:25:50 +0000274/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000275static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000276{
277 CPUState *env;
278
279 for(env = first_cpu; env != NULL; env = env->next_cpu) {
280 if (env->cpu_index == cpu_index) {
281 mon_cpu = env;
282 return 0;
283 }
284 }
285 return -1;
286}
287
pbrook9596ebb2007-11-18 01:44:38 +0000288static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000289{
290 if (!mon_cpu) {
291 mon_set_cpu(0);
292 }
293 return mon_cpu;
294}
295
bellard9307c4c2004-04-04 12:57:25 +0000296static void do_info_registers(void)
297{
bellard6a00d602005-11-21 23:25:50 +0000298 CPUState *env;
299 env = mon_get_cpu();
300 if (!env)
301 return;
bellard9307c4c2004-04-04 12:57:25 +0000302#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000303 cpu_dump_state(env, NULL, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000304 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000305#else
ths5fafdf22007-09-16 21:08:06 +0000306 cpu_dump_state(env, NULL, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000307 0);
bellard9307c4c2004-04-04 12:57:25 +0000308#endif
309}
310
bellard6a00d602005-11-21 23:25:50 +0000311static void do_info_cpus(void)
312{
313 CPUState *env;
314
315 /* just to set the default cpu if not already done */
316 mon_get_cpu();
317
318 for(env = first_cpu; env != NULL; env = env->next_cpu) {
ths5fafdf22007-09-16 21:08:06 +0000319 term_printf("%c CPU #%d:",
bellard6a00d602005-11-21 23:25:50 +0000320 (env == mon_cpu) ? '*' : ' ',
321 env->cpu_index);
322#if defined(TARGET_I386)
323 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000324#elif defined(TARGET_PPC)
325 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000326#elif defined(TARGET_SPARC)
327 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000328#elif defined(TARGET_MIPS)
thsb5dc7732008-06-27 10:02:35 +0000329 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000330#endif
thsead93602007-09-06 00:18:15 +0000331 if (env->halted)
332 term_printf(" (halted)");
bellard6a00d602005-11-21 23:25:50 +0000333 term_printf("\n");
334 }
335}
336
337static void do_cpu_set(int index)
338{
339 if (mon_set_cpu(index) < 0)
340 term_printf("Invalid CPU index\n");
341}
342
bellarde3db7222005-01-26 22:00:47 +0000343static void do_info_jit(void)
344{
345 dump_exec_info(NULL, monitor_fprintf);
346}
347
bellardaa455482004-04-04 13:07:25 +0000348static void do_info_history (void)
349{
350 int i;
bellard7e2515e2004-08-01 21:52:19 +0000351 const char *str;
ths3b46e622007-09-17 08:09:54 +0000352
bellard7e2515e2004-08-01 21:52:19 +0000353 i = 0;
354 for(;;) {
355 str = readline_get_history(i);
356 if (!str)
357 break;
358 term_printf("%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000359 i++;
bellardaa455482004-04-04 13:07:25 +0000360 }
361}
362
j_mayer76a66252007-03-07 08:32:30 +0000363#if defined(TARGET_PPC)
364/* XXX: not implemented in other targets */
365static void do_info_cpu_stats (void)
366{
367 CPUState *env;
368
369 env = mon_get_cpu();
370 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
371}
372#endif
373
bellard9307c4c2004-04-04 12:57:25 +0000374static void do_quit(void)
bellard9dc39cb2004-03-14 21:38:27 +0000375{
376 exit(0);
377}
378
379static int eject_device(BlockDriverState *bs, int force)
380{
381 if (bdrv_is_inserted(bs)) {
382 if (!force) {
383 if (!bdrv_is_removable(bs)) {
384 term_printf("device is not removable\n");
385 return -1;
386 }
387 if (bdrv_is_locked(bs)) {
388 term_printf("device is locked\n");
389 return -1;
390 }
391 }
392 bdrv_close(bs);
393 }
394 return 0;
395}
396
bellard9307c4c2004-04-04 12:57:25 +0000397static void do_eject(int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000398{
399 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000400
bellard9307c4c2004-04-04 12:57:25 +0000401 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000402 if (!bs) {
403 term_printf("device not found\n");
404 return;
405 }
406 eject_device(bs, force);
407}
408
aurel322ecea9b2008-06-18 22:10:01 +0000409static void do_change_block(const char *device, const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000410{
411 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000412 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000413
bellard9307c4c2004-04-04 12:57:25 +0000414 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000415 if (!bs) {
416 term_printf("device not found\n");
417 return;
418 }
aurel322ecea9b2008-06-18 22:10:01 +0000419 if (fmt) {
420 drv = bdrv_find_format(fmt);
421 if (!drv) {
422 term_printf("invalid format %s\n", fmt);
423 return;
424 }
425 }
bellard9dc39cb2004-03-14 21:38:27 +0000426 if (eject_device(bs, 0) < 0)
427 return;
aurel322ecea9b2008-06-18 22:10:01 +0000428 bdrv_open2(bs, filename, 0, drv);
balrog2bac6012007-04-30 01:34:31 +0000429 qemu_key_check(bs, filename);
bellard9dc39cb2004-03-14 21:38:27 +0000430}
431
aliguori2569da02008-12-10 15:14:13 +0000432static void do_change_vnc(const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000433{
ths70848512007-08-25 01:37:05 +0000434 if (strcmp(target, "passwd") == 0 ||
435 strcmp(target, "password") == 0) {
436 char password[9];
aliguori2569da02008-12-10 15:14:13 +0000437 if (arg) {
438 strncpy(password, arg, sizeof(password));
439 password[sizeof(password) - 1] = '\0';
440 } else
441 monitor_readline("Password: ", 1, password, sizeof(password));
ths70848512007-08-25 01:37:05 +0000442 if (vnc_display_password(NULL, password) < 0)
443 term_printf("could not set VNC server password\n");
444 } else {
445 if (vnc_display_open(NULL, target) < 0)
446 term_printf("could not start VNC server on %s\n", target);
447 }
thse25a5822007-08-25 01:36:20 +0000448}
449
aliguori2569da02008-12-10 15:14:13 +0000450static void do_change(const char *device, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000451{
452 if (strcmp(device, "vnc") == 0) {
aliguori2569da02008-12-10 15:14:13 +0000453 do_change_vnc(target, arg);
thse25a5822007-08-25 01:36:20 +0000454 } else {
aliguori2569da02008-12-10 15:14:13 +0000455 do_change_block(device, target, arg);
thse25a5822007-08-25 01:36:20 +0000456 }
457}
458
bellard9307c4c2004-04-04 12:57:25 +0000459static void do_screen_dump(const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000460{
pbrook95219892006-04-09 01:06:34 +0000461 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000462}
463
pbrooke735b912007-06-30 13:53:24 +0000464static void do_logfile(const char *filename)
465{
466 cpu_set_log_filename(filename);
467}
468
bellard9307c4c2004-04-04 12:57:25 +0000469static void do_log(const char *items)
bellardf193c792004-03-21 17:06:25 +0000470{
471 int mask;
ths3b46e622007-09-17 08:09:54 +0000472
bellard9307c4c2004-04-04 12:57:25 +0000473 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000474 mask = 0;
475 } else {
bellard9307c4c2004-04-04 12:57:25 +0000476 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000477 if (!mask) {
bellard9307c4c2004-04-04 12:57:25 +0000478 help_cmd("log");
bellardf193c792004-03-21 17:06:25 +0000479 return;
480 }
481 }
482 cpu_set_log(mask);
483}
484
bellard9307c4c2004-04-04 12:57:25 +0000485static void do_stop(void)
bellard8a7ddc32004-03-31 19:00:16 +0000486{
487 vm_stop(EXCP_INTERRUPT);
488}
489
bellard9307c4c2004-04-04 12:57:25 +0000490static void do_cont(void)
bellard8a7ddc32004-03-31 19:00:16 +0000491{
492 vm_start();
493}
494
bellard67b915a2004-03-31 23:37:16 +0000495#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +0000496static void do_gdbserver(const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000497{
pbrookcfc34752007-02-22 01:48:01 +0000498 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000499 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000500 if (gdbserver_start(port) < 0) {
501 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000502 } else {
pbrookcfc34752007-02-22 01:48:01 +0000503 qemu_printf("Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000504 }
505}
bellard67b915a2004-03-31 23:37:16 +0000506#endif
bellard8a7ddc32004-03-31 19:00:16 +0000507
bellard9307c4c2004-04-04 12:57:25 +0000508static void term_printc(int c)
509{
510 term_printf("'");
511 switch(c) {
512 case '\'':
513 term_printf("\\'");
514 break;
515 case '\\':
516 term_printf("\\\\");
517 break;
518 case '\n':
519 term_printf("\\n");
520 break;
521 case '\r':
522 term_printf("\\r");
523 break;
524 default:
525 if (c >= 32 && c <= 126) {
526 term_printf("%c", c);
527 } else {
528 term_printf("\\x%02x", c);
529 }
530 break;
531 }
532 term_printf("'");
533}
534
ths5fafdf22007-09-16 21:08:06 +0000535static void memory_dump(int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000536 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000537{
bellard6a00d602005-11-21 23:25:50 +0000538 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000539 int nb_per_line, l, line_size, i, max_digits, len;
540 uint8_t buf[16];
541 uint64_t v;
542
543 if (format == 'i') {
544 int flags;
545 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000546 env = mon_get_cpu();
547 if (!env && !is_physical)
548 return;
bellard9307c4c2004-04-04 12:57:25 +0000549#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000550 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000551 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000552 } else if (wsize == 4) {
553 flags = 0;
554 } else {
bellard6a15fd12006-04-12 21:07:07 +0000555 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000556 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000557 if (env) {
558#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000559 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000560 (env->segs[R_CS].flags & DESC_L_MASK))
561 flags = 2;
562 else
563#endif
564 if (!(env->segs[R_CS].flags & DESC_B_MASK))
565 flags = 1;
566 }
bellard4c27ba22004-04-25 18:05:08 +0000567 }
568#endif
bellard6a00d602005-11-21 23:25:50 +0000569 monitor_disas(env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000570 return;
571 }
572
573 len = wsize * count;
574 if (wsize == 1)
575 line_size = 8;
576 else
577 line_size = 16;
578 nb_per_line = line_size / wsize;
579 max_digits = 0;
580
581 switch(format) {
582 case 'o':
583 max_digits = (wsize * 8 + 2) / 3;
584 break;
585 default:
586 case 'x':
587 max_digits = (wsize * 8) / 4;
588 break;
589 case 'u':
590 case 'd':
591 max_digits = (wsize * 8 * 10 + 32) / 33;
592 break;
593 case 'c':
594 wsize = 1;
595 break;
596 }
597
598 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000599 if (is_physical)
600 term_printf(TARGET_FMT_plx ":", addr);
601 else
602 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000603 l = len;
604 if (l > line_size)
605 l = line_size;
606 if (is_physical) {
607 cpu_physical_memory_rw(addr, buf, l, 0);
608 } else {
bellard6a00d602005-11-21 23:25:50 +0000609 env = mon_get_cpu();
610 if (!env)
611 break;
aliguoric8f79b62008-08-18 14:00:20 +0000612 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
613 term_printf(" Cannot access memory\n");
614 break;
615 }
bellard9307c4c2004-04-04 12:57:25 +0000616 }
ths5fafdf22007-09-16 21:08:06 +0000617 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000618 while (i < l) {
619 switch(wsize) {
620 default:
621 case 1:
622 v = ldub_raw(buf + i);
623 break;
624 case 2:
625 v = lduw_raw(buf + i);
626 break;
627 case 4:
bellard92a31b12005-02-10 22:00:52 +0000628 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000629 break;
630 case 8:
631 v = ldq_raw(buf + i);
632 break;
633 }
634 term_printf(" ");
635 switch(format) {
636 case 'o':
bellard26a76462006-06-25 18:15:32 +0000637 term_printf("%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000638 break;
639 case 'x':
bellard26a76462006-06-25 18:15:32 +0000640 term_printf("0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000641 break;
642 case 'u':
bellard26a76462006-06-25 18:15:32 +0000643 term_printf("%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000644 break;
645 case 'd':
bellard26a76462006-06-25 18:15:32 +0000646 term_printf("%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000647 break;
648 case 'c':
649 term_printc(v);
650 break;
651 }
652 i += wsize;
653 }
654 term_printf("\n");
655 addr += l;
656 len -= l;
657 }
658}
659
bellard92a31b12005-02-10 22:00:52 +0000660#if TARGET_LONG_BITS == 64
661#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
662#else
663#define GET_TLONG(h, l) (l)
664#endif
665
ths5fafdf22007-09-16 21:08:06 +0000666static void do_memory_dump(int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000667 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000668{
bellard92a31b12005-02-10 22:00:52 +0000669 target_long addr = GET_TLONG(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000670 memory_dump(count, format, size, addr, 0);
671}
672
blueswir17743e582007-09-24 18:39:04 +0000673#if TARGET_PHYS_ADDR_BITS > 32
674#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
675#else
676#define GET_TPHYSADDR(h, l) (l)
677#endif
678
bellard92a31b12005-02-10 22:00:52 +0000679static void do_physical_memory_dump(int count, int format, int size,
680 uint32_t addrh, uint32_t addrl)
681
bellard9307c4c2004-04-04 12:57:25 +0000682{
blueswir17743e582007-09-24 18:39:04 +0000683 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000684 memory_dump(count, format, size, addr, 1);
685}
686
bellard92a31b12005-02-10 22:00:52 +0000687static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000688{
blueswir17743e582007-09-24 18:39:04 +0000689 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
690#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000691 switch(format) {
692 case 'o':
693 term_printf("%#o", val);
694 break;
695 case 'x':
696 term_printf("%#x", val);
697 break;
698 case 'u':
699 term_printf("%u", val);
700 break;
701 default:
702 case 'd':
703 term_printf("%d", val);
704 break;
705 case 'c':
706 term_printc(val);
707 break;
708 }
bellard92a31b12005-02-10 22:00:52 +0000709#else
710 switch(format) {
711 case 'o':
bellard26a76462006-06-25 18:15:32 +0000712 term_printf("%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000713 break;
714 case 'x':
bellard26a76462006-06-25 18:15:32 +0000715 term_printf("%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000716 break;
717 case 'u':
bellard26a76462006-06-25 18:15:32 +0000718 term_printf("%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000719 break;
720 default:
721 case 'd':
bellard26a76462006-06-25 18:15:32 +0000722 term_printf("%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000723 break;
724 case 'c':
725 term_printc(val);
726 break;
727 }
728#endif
bellard9307c4c2004-04-04 12:57:25 +0000729 term_printf("\n");
730}
731
ths5fafdf22007-09-16 21:08:06 +0000732static void do_memory_save(unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000733 uint32_t size, const char *filename)
734{
735 FILE *f;
736 target_long addr = GET_TLONG(valh, vall);
737 uint32_t l;
738 CPUState *env;
739 uint8_t buf[1024];
740
741 env = mon_get_cpu();
742 if (!env)
743 return;
744
745 f = fopen(filename, "wb");
746 if (!f) {
747 term_printf("could not open '%s'\n", filename);
748 return;
749 }
750 while (size != 0) {
751 l = sizeof(buf);
752 if (l > size)
753 l = size;
754 cpu_memory_rw_debug(env, addr, buf, l, 0);
755 fwrite(buf, 1, l, f);
756 addr += l;
757 size -= l;
758 }
759 fclose(f);
760}
761
aurel32a8bdf7a2008-04-11 21:36:14 +0000762static void do_physical_memory_save(unsigned int valh, unsigned int vall,
763 uint32_t size, const char *filename)
764{
765 FILE *f;
766 uint32_t l;
767 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000768 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000769
770 f = fopen(filename, "wb");
771 if (!f) {
772 term_printf("could not open '%s'\n", filename);
773 return;
774 }
775 while (size != 0) {
776 l = sizeof(buf);
777 if (l > size)
778 l = size;
779 cpu_physical_memory_rw(addr, buf, l, 0);
780 fwrite(buf, 1, l, f);
781 fflush(f);
782 addr += l;
783 size -= l;
784 }
785 fclose(f);
786}
787
bellarde4cf1ad2005-06-04 20:15:57 +0000788static void do_sum(uint32_t start, uint32_t size)
789{
790 uint32_t addr;
791 uint8_t buf[1];
792 uint16_t sum;
793
794 sum = 0;
795 for(addr = start; addr < (start + size); addr++) {
796 cpu_physical_memory_rw(addr, buf, 1, 0);
797 /* BSD sum algorithm ('sum' Unix command) */
798 sum = (sum >> 1) | (sum << 15);
799 sum += buf[0];
800 }
801 term_printf("%05d\n", sum);
802}
803
bellarda3a91a32004-06-04 11:06:21 +0000804typedef struct {
805 int keycode;
806 const char *name;
807} KeyDef;
808
809static const KeyDef key_defs[] = {
810 { 0x2a, "shift" },
811 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000812
bellarda3a91a32004-06-04 11:06:21 +0000813 { 0x38, "alt" },
814 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000815 { 0x64, "altgr" },
816 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000817 { 0x1d, "ctrl" },
818 { 0x9d, "ctrl_r" },
819
820 { 0xdd, "menu" },
821
822 { 0x01, "esc" },
823
824 { 0x02, "1" },
825 { 0x03, "2" },
826 { 0x04, "3" },
827 { 0x05, "4" },
828 { 0x06, "5" },
829 { 0x07, "6" },
830 { 0x08, "7" },
831 { 0x09, "8" },
832 { 0x0a, "9" },
833 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000834 { 0x0c, "minus" },
835 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000836 { 0x0e, "backspace" },
837
838 { 0x0f, "tab" },
839 { 0x10, "q" },
840 { 0x11, "w" },
841 { 0x12, "e" },
842 { 0x13, "r" },
843 { 0x14, "t" },
844 { 0x15, "y" },
845 { 0x16, "u" },
846 { 0x17, "i" },
847 { 0x18, "o" },
848 { 0x19, "p" },
849
850 { 0x1c, "ret" },
851
852 { 0x1e, "a" },
853 { 0x1f, "s" },
854 { 0x20, "d" },
855 { 0x21, "f" },
856 { 0x22, "g" },
857 { 0x23, "h" },
858 { 0x24, "j" },
859 { 0x25, "k" },
860 { 0x26, "l" },
861
862 { 0x2c, "z" },
863 { 0x2d, "x" },
864 { 0x2e, "c" },
865 { 0x2f, "v" },
866 { 0x30, "b" },
867 { 0x31, "n" },
868 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000869 { 0x33, "comma" },
870 { 0x34, "dot" },
871 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000872
balrog4d3b6f62008-02-10 16:33:14 +0000873 { 0x37, "asterisk" },
874
bellarda3a91a32004-06-04 11:06:21 +0000875 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000876 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000877 { 0x3b, "f1" },
878 { 0x3c, "f2" },
879 { 0x3d, "f3" },
880 { 0x3e, "f4" },
881 { 0x3f, "f5" },
882 { 0x40, "f6" },
883 { 0x41, "f7" },
884 { 0x42, "f8" },
885 { 0x43, "f9" },
886 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000887 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000888 { 0x46, "scroll_lock" },
889
bellard64866c32006-05-07 18:03:31 +0000890 { 0xb5, "kp_divide" },
891 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000892 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000893 { 0x4e, "kp_add" },
894 { 0x9c, "kp_enter" },
895 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000896 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000897
898 { 0x52, "kp_0" },
899 { 0x4f, "kp_1" },
900 { 0x50, "kp_2" },
901 { 0x51, "kp_3" },
902 { 0x4b, "kp_4" },
903 { 0x4c, "kp_5" },
904 { 0x4d, "kp_6" },
905 { 0x47, "kp_7" },
906 { 0x48, "kp_8" },
907 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000908
bellarda3a91a32004-06-04 11:06:21 +0000909 { 0x56, "<" },
910
911 { 0x57, "f11" },
912 { 0x58, "f12" },
913
914 { 0xb7, "print" },
915
916 { 0xc7, "home" },
917 { 0xc9, "pgup" },
918 { 0xd1, "pgdn" },
919 { 0xcf, "end" },
920
921 { 0xcb, "left" },
922 { 0xc8, "up" },
923 { 0xd0, "down" },
924 { 0xcd, "right" },
925
926 { 0xd2, "insert" },
927 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +0000928#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
929 { 0xf0, "stop" },
930 { 0xf1, "again" },
931 { 0xf2, "props" },
932 { 0xf3, "undo" },
933 { 0xf4, "front" },
934 { 0xf5, "copy" },
935 { 0xf6, "open" },
936 { 0xf7, "paste" },
937 { 0xf8, "find" },
938 { 0xf9, "cut" },
939 { 0xfa, "lf" },
940 { 0xfb, "help" },
941 { 0xfc, "meta_l" },
942 { 0xfd, "meta_r" },
943 { 0xfe, "compose" },
944#endif
bellarda3a91a32004-06-04 11:06:21 +0000945 { 0, NULL },
946};
947
948static int get_keycode(const char *key)
949{
950 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +0000951 char *endp;
952 int ret;
bellarda3a91a32004-06-04 11:06:21 +0000953
954 for(p = key_defs; p->name != NULL; p++) {
955 if (!strcmp(key, p->name))
956 return p->keycode;
957 }
bellard64866c32006-05-07 18:03:31 +0000958 if (strstart(key, "0x", NULL)) {
959 ret = strtoul(key, &endp, 0);
960 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
961 return ret;
962 }
bellarda3a91a32004-06-04 11:06:21 +0000963 return -1;
964}
965
balrogc8256f92008-06-08 22:45:01 +0000966#define MAX_KEYCODES 16
967static uint8_t keycodes[MAX_KEYCODES];
968static int nb_pending_keycodes;
969static QEMUTimer *key_timer;
970
971static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +0000972{
balrogc8256f92008-06-08 22:45:01 +0000973 int keycode;
974
975 while (nb_pending_keycodes > 0) {
976 nb_pending_keycodes--;
977 keycode = keycodes[nb_pending_keycodes];
978 if (keycode & 0x80)
979 kbd_put_keycode(0xe0);
980 kbd_put_keycode(keycode | 0x80);
981 }
982}
983
984static void do_sendkey(const char *string, int has_hold_time, int hold_time)
985{
balrog3401c0d2008-06-04 10:05:59 +0000986 char keyname_buf[16];
987 char *separator;
988 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +0000989
balrogc8256f92008-06-08 22:45:01 +0000990 if (nb_pending_keycodes > 0) {
991 qemu_del_timer(key_timer);
992 release_keys(NULL);
993 }
994 if (!has_hold_time)
995 hold_time = 100;
996 i = 0;
balrog3401c0d2008-06-04 10:05:59 +0000997 while (1) {
998 separator = strchr(string, '-');
999 keyname_len = separator ? separator - string : strlen(string);
1000 if (keyname_len > 0) {
1001 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1002 if (keyname_len > sizeof(keyname_buf) - 1) {
1003 term_printf("invalid key: '%s...'\n", keyname_buf);
1004 return;
bellarda3a91a32004-06-04 11:06:21 +00001005 }
balrogc8256f92008-06-08 22:45:01 +00001006 if (i == MAX_KEYCODES) {
balrog3401c0d2008-06-04 10:05:59 +00001007 term_printf("too many keys\n");
1008 return;
1009 }
1010 keyname_buf[keyname_len] = 0;
1011 keycode = get_keycode(keyname_buf);
1012 if (keycode < 0) {
1013 term_printf("unknown key: '%s'\n", keyname_buf);
1014 return;
1015 }
balrogc8256f92008-06-08 22:45:01 +00001016 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001017 }
balrog3401c0d2008-06-04 10:05:59 +00001018 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001019 break;
balrog3401c0d2008-06-04 10:05:59 +00001020 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001021 }
balrogc8256f92008-06-08 22:45:01 +00001022 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001023 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001024 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001025 keycode = keycodes[i];
1026 if (keycode & 0x80)
1027 kbd_put_keycode(0xe0);
1028 kbd_put_keycode(keycode & 0x7f);
1029 }
balrogc8256f92008-06-08 22:45:01 +00001030 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001031 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1032 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001033}
1034
bellard13224a82006-07-14 22:03:35 +00001035static int mouse_button_state;
1036
ths5fafdf22007-09-16 21:08:06 +00001037static void do_mouse_move(const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001038 const char *dz_str)
1039{
1040 int dx, dy, dz;
1041 dx = strtol(dx_str, NULL, 0);
1042 dy = strtol(dy_str, NULL, 0);
1043 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001044 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001045 dz = strtol(dz_str, NULL, 0);
1046 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1047}
1048
1049static void do_mouse_button(int button_state)
1050{
1051 mouse_button_state = button_state;
1052 kbd_mouse_event(0, 0, 0, mouse_button_state);
1053}
1054
bellard34405572004-06-08 00:55:58 +00001055static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1056{
1057 uint32_t val;
1058 int suffix;
1059
1060 if (has_index) {
1061 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1062 addr++;
1063 }
1064 addr &= 0xffff;
1065
1066 switch(size) {
1067 default:
1068 case 1:
1069 val = cpu_inb(NULL, addr);
1070 suffix = 'b';
1071 break;
1072 case 2:
1073 val = cpu_inw(NULL, addr);
1074 suffix = 'w';
1075 break;
1076 case 4:
1077 val = cpu_inl(NULL, addr);
1078 suffix = 'l';
1079 break;
1080 }
1081 term_printf("port%c[0x%04x] = %#0*x\n",
1082 suffix, addr, size * 2, val);
1083}
bellarda3a91a32004-06-04 11:06:21 +00001084
blueswir13b4366d2008-06-20 16:25:06 +00001085/* boot_set handler */
1086static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1087static void *boot_opaque;
1088
1089void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1090{
1091 qemu_boot_set_handler = func;
1092 boot_opaque = opaque;
1093}
1094
aurel320ecdffb2008-05-04 20:11:34 +00001095static void do_boot_set(const char *bootdevice)
1096{
1097 int res;
1098
1099 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001100 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001101 if (res == 0)
1102 term_printf("boot device list now set to %s\n", bootdevice);
1103 else
1104 term_printf("setting boot device list failed with error %i\n", res);
1105 } else {
1106 term_printf("no function defined to set boot device list for this architecture\n");
1107 }
1108}
1109
bellarde4f90822004-06-20 12:35:44 +00001110static void do_system_reset(void)
1111{
1112 qemu_system_reset_request();
1113}
1114
bellard34751872005-07-02 14:31:34 +00001115static void do_system_powerdown(void)
1116{
1117 qemu_system_powerdown_request();
1118}
1119
bellardb86bda52004-09-18 19:32:46 +00001120#if defined(TARGET_I386)
1121static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1122{
ths5fafdf22007-09-16 21:08:06 +00001123 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
bellardb86bda52004-09-18 19:32:46 +00001124 addr,
1125 pte & mask,
1126 pte & PG_GLOBAL_MASK ? 'G' : '-',
1127 pte & PG_PSE_MASK ? 'P' : '-',
1128 pte & PG_DIRTY_MASK ? 'D' : '-',
1129 pte & PG_ACCESSED_MASK ? 'A' : '-',
1130 pte & PG_PCD_MASK ? 'C' : '-',
1131 pte & PG_PWT_MASK ? 'T' : '-',
1132 pte & PG_USER_MASK ? 'U' : '-',
1133 pte & PG_RW_MASK ? 'W' : '-');
1134}
1135
1136static void tlb_info(void)
1137{
bellard6a00d602005-11-21 23:25:50 +00001138 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001139 int l1, l2;
1140 uint32_t pgd, pde, pte;
1141
bellard6a00d602005-11-21 23:25:50 +00001142 env = mon_get_cpu();
1143 if (!env)
1144 return;
1145
bellardb86bda52004-09-18 19:32:46 +00001146 if (!(env->cr[0] & CR0_PG_MASK)) {
1147 term_printf("PG disabled\n");
1148 return;
1149 }
1150 pgd = env->cr[3] & ~0xfff;
1151 for(l1 = 0; l1 < 1024; l1++) {
1152 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1153 pde = le32_to_cpu(pde);
1154 if (pde & PG_PRESENT_MASK) {
1155 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1156 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1157 } else {
1158 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001159 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001160 (uint8_t *)&pte, 4);
1161 pte = le32_to_cpu(pte);
1162 if (pte & PG_PRESENT_MASK) {
ths5fafdf22007-09-16 21:08:06 +00001163 print_pte((l1 << 22) + (l2 << 12),
1164 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001165 ~0xfff);
1166 }
1167 }
1168 }
1169 }
1170 }
1171}
1172
ths5fafdf22007-09-16 21:08:06 +00001173static void mem_print(uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001174 uint32_t end, int prot)
1175{
bellard9746b152004-11-11 18:30:24 +00001176 int prot1;
1177 prot1 = *plast_prot;
1178 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001179 if (*pstart != -1) {
1180 term_printf("%08x-%08x %08x %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001181 *pstart, end, end - *pstart,
bellard9746b152004-11-11 18:30:24 +00001182 prot1 & PG_USER_MASK ? 'u' : '-',
bellardb86bda52004-09-18 19:32:46 +00001183 'r',
bellard9746b152004-11-11 18:30:24 +00001184 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001185 }
1186 if (prot != 0)
1187 *pstart = end;
1188 else
1189 *pstart = -1;
1190 *plast_prot = prot;
1191 }
1192}
1193
1194static void mem_info(void)
1195{
bellard6a00d602005-11-21 23:25:50 +00001196 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001197 int l1, l2, prot, last_prot;
1198 uint32_t pgd, pde, pte, start, end;
1199
bellard6a00d602005-11-21 23:25:50 +00001200 env = mon_get_cpu();
1201 if (!env)
1202 return;
1203
bellardb86bda52004-09-18 19:32:46 +00001204 if (!(env->cr[0] & CR0_PG_MASK)) {
1205 term_printf("PG disabled\n");
1206 return;
1207 }
1208 pgd = env->cr[3] & ~0xfff;
1209 last_prot = 0;
1210 start = -1;
1211 for(l1 = 0; l1 < 1024; l1++) {
1212 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1213 pde = le32_to_cpu(pde);
1214 end = l1 << 22;
1215 if (pde & PG_PRESENT_MASK) {
1216 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1217 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1218 mem_print(&start, &last_prot, end, prot);
1219 } else {
1220 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001221 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001222 (uint8_t *)&pte, 4);
1223 pte = le32_to_cpu(pte);
1224 end = (l1 << 22) + (l2 << 12);
1225 if (pte & PG_PRESENT_MASK) {
1226 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1227 } else {
1228 prot = 0;
1229 }
1230 mem_print(&start, &last_prot, end, prot);
1231 }
1232 }
1233 } else {
1234 prot = 0;
1235 mem_print(&start, &last_prot, end, prot);
1236 }
1237 }
1238}
1239#endif
1240
bellard0f4c6412005-07-24 18:10:56 +00001241static void do_info_kqemu(void)
1242{
1243#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001244 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001245 int val;
1246 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001247 env = mon_get_cpu();
1248 if (!env) {
1249 term_printf("No cpu initialized yet");
1250 return;
1251 }
1252 val = env->kqemu_enabled;
bellard5f1ce942006-02-08 22:40:15 +00001253 term_printf("kqemu support: ");
1254 switch(val) {
1255 default:
1256 case 0:
1257 term_printf("disabled\n");
1258 break;
1259 case 1:
1260 term_printf("enabled for user code\n");
1261 break;
1262 case 2:
1263 term_printf("enabled for user and kernel code\n");
1264 break;
1265 }
bellard0f4c6412005-07-24 18:10:56 +00001266#else
bellard5f1ce942006-02-08 22:40:15 +00001267 term_printf("kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001268#endif
ths5fafdf22007-09-16 21:08:06 +00001269}
bellard0f4c6412005-07-24 18:10:56 +00001270
aliguori7ba1e612008-11-05 16:04:33 +00001271static void do_info_kvm(void)
1272{
1273#ifdef CONFIG_KVM
1274 term_printf("kvm support: ");
1275 if (kvm_enabled())
1276 term_printf("enabled\n");
1277 else
1278 term_printf("disabled\n");
1279#else
1280 term_printf("kvm support: not compiled\n");
1281#endif
1282}
1283
bellard5f1ce942006-02-08 22:40:15 +00001284#ifdef CONFIG_PROFILER
1285
1286int64_t kqemu_time;
1287int64_t qemu_time;
1288int64_t kqemu_exec_count;
1289int64_t dev_time;
1290int64_t kqemu_ret_int_count;
1291int64_t kqemu_ret_excp_count;
1292int64_t kqemu_ret_intr_count;
1293
1294static void do_info_profile(void)
1295{
1296 int64_t total;
1297 total = qemu_time;
1298 if (total == 0)
1299 total = 1;
bellard26a76462006-06-25 18:15:32 +00001300 term_printf("async time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001301 dev_time, dev_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001302 term_printf("qemu time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001303 qemu_time, qemu_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001304 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
bellard5f1ce942006-02-08 22:40:15 +00001305 kqemu_time, kqemu_time / (double)ticks_per_sec,
1306 kqemu_time / (double)total * 100.0,
1307 kqemu_exec_count,
1308 kqemu_ret_int_count,
1309 kqemu_ret_excp_count,
1310 kqemu_ret_intr_count);
1311 qemu_time = 0;
1312 kqemu_time = 0;
1313 kqemu_exec_count = 0;
1314 dev_time = 0;
1315 kqemu_ret_int_count = 0;
1316 kqemu_ret_excp_count = 0;
1317 kqemu_ret_intr_count = 0;
1318#ifdef USE_KQEMU
1319 kqemu_record_dump();
1320#endif
1321}
1322#else
1323static void do_info_profile(void)
1324{
1325 term_printf("Internal profiler not compiled\n");
1326}
1327#endif
1328
bellardec36b692006-07-16 18:57:03 +00001329/* Capture support */
1330static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1331
1332static void do_info_capture (void)
1333{
1334 int i;
1335 CaptureState *s;
1336
1337 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1338 term_printf ("[%d]: ", i);
1339 s->ops.info (s->opaque);
1340 }
1341}
1342
1343static void do_stop_capture (int n)
1344{
1345 int i;
1346 CaptureState *s;
1347
1348 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1349 if (i == n) {
1350 s->ops.destroy (s->opaque);
1351 LIST_REMOVE (s, entries);
1352 qemu_free (s);
1353 return;
1354 }
1355 }
1356}
1357
1358#ifdef HAS_AUDIO
bellardec36b692006-07-16 18:57:03 +00001359static void do_wav_capture (const char *path,
1360 int has_freq, int freq,
1361 int has_bits, int bits,
1362 int has_channels, int nchannels)
1363{
1364 CaptureState *s;
1365
1366 s = qemu_mallocz (sizeof (*s));
1367 if (!s) {
1368 term_printf ("Not enough memory to add wave capture\n");
1369 return;
1370 }
1371
1372 freq = has_freq ? freq : 44100;
1373 bits = has_bits ? bits : 16;
1374 nchannels = has_channels ? nchannels : 2;
1375
1376 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1377 term_printf ("Faied to add wave capture\n");
1378 qemu_free (s);
1379 }
1380 LIST_INSERT_HEAD (&capture_head, s, entries);
1381}
1382#endif
1383
aurel32dc1c0b72008-04-27 23:52:12 +00001384#if defined(TARGET_I386)
1385static void do_inject_nmi(int cpu_index)
1386{
1387 CPUState *env;
1388
1389 for (env = first_cpu; env != NULL; env = env->next_cpu)
1390 if (env->cpu_index == cpu_index) {
1391 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1392 break;
1393 }
1394}
1395#endif
1396
aliguoridf751fa2008-12-04 20:19:35 +00001397static void do_balloon(int value)
1398{
1399 ram_addr_t target = value;
1400 qemu_balloon(target << 20);
1401}
1402
1403static void do_info_balloon(void)
1404{
1405 ram_addr_t actual;
1406
1407 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001408 if (kvm_enabled() && !kvm_has_sync_mmu())
1409 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1410 else if (actual == 0)
aliguoridf751fa2008-12-04 20:19:35 +00001411 term_printf("Ballooning not activated in VM\n");
1412 else
1413 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1414}
1415
blueswir18662d652008-10-02 18:32:44 +00001416static const term_cmd_t term_cmds[] = {
ths5fafdf22007-09-16 21:08:06 +00001417 { "help|?", "s?", do_help,
bellard9dc39cb2004-03-14 21:38:27 +00001418 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001419 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001420 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001421 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001422 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001423 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001424 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001425 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001426 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001427 { "change", "BFs?", do_change,
1428 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001429 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001430 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001431 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001432 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001433 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001434 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001435 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001436 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001437 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001438 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001439 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001440 "tag|id", "delete a VM snapshot from its tag or id" },
1441 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001442 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001443 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001444 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001445#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001446 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001447 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001448#endif
ths5fafdf22007-09-16 21:08:06 +00001449 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001450 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001451 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001452 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001453 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001454 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001455 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001456 "/fmt addr", "I/O port read" },
1457
balrogc8256f92008-06-08 22:45:01 +00001458 { "sendkey", "si?", do_sendkey,
1459 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
ths5fafdf22007-09-16 21:08:06 +00001460 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001461 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001462 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001463 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001464 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001465 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001466 { "usb_add", "s", do_usb_add,
1467 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1468 { "usb_del", "s", do_usb_del,
1469 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001470 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001471 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001472 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001473 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001474 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001475 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001476 { "mouse_set", "i", do_mouse_set,
1477 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001478#ifdef HAS_AUDIO
1479 { "wavcapture", "si?i?i?", do_wav_capture,
1480 "path [frequency bits channels]",
1481 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1482#endif
1483 { "stopcapture", "i", do_stop_capture,
1484 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001485 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001486 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001487 { "pmemsave", "lis", do_physical_memory_save,
1488 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001489 { "boot_set", "s", do_boot_set,
1490 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001491#if defined(TARGET_I386)
1492 { "nmi", "i", do_inject_nmi,
1493 "cpu", "inject an NMI on the given CPU", },
1494#endif
aliguori5bb79102008-10-13 03:12:02 +00001495 { "migrate", "-ds", do_migrate,
1496 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1497 { "migrate_cancel", "", do_migrate_cancel,
1498 "", "cancel the current VM migration" },
1499 { "migrate_set_speed", "s", do_migrate_set_speed,
1500 "value", "set maximum speed (in bytes) for migrations" },
aliguoridf751fa2008-12-04 20:19:35 +00001501 { "balloon", "i", do_balloon,
1502 "target", "request VM to change it's memory allocation (in MB)" },
ths5fafdf22007-09-16 21:08:06 +00001503 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001504};
1505
blueswir18662d652008-10-02 18:32:44 +00001506static const term_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001507 { "version", "", do_info_version,
1508 "", "show the version of qemu" },
bellard9307c4c2004-04-04 12:57:25 +00001509 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001510 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001511 { "chardev", "", qemu_chr_info,
1512 "", "show the character devices" },
bellard9307c4c2004-04-04 12:57:25 +00001513 { "block", "", do_info_block,
bellard9dc39cb2004-03-14 21:38:27 +00001514 "", "show the block devices" },
thsa36e69d2007-12-02 05:18:19 +00001515 { "blockstats", "", do_info_blockstats,
1516 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001517 { "registers", "", do_info_registers,
1518 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001519 { "cpus", "", do_info_cpus,
1520 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001521 { "history", "", do_info_history,
1522 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001523 { "irq", "", irq_info,
1524 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001525 { "pic", "", pic_info,
1526 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001527 { "pci", "", pci_info,
1528 "", "show PCI info", },
bellardb86bda52004-09-18 19:32:46 +00001529#if defined(TARGET_I386)
1530 { "tlb", "", tlb_info,
1531 "", "show virtual to physical memory mappings", },
1532 { "mem", "", mem_info,
1533 "", "show the active virtual memory mappings", },
1534#endif
bellarde3db7222005-01-26 22:00:47 +00001535 { "jit", "", do_info_jit,
1536 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001537 { "kqemu", "", do_info_kqemu,
1538 "", "show kqemu information", },
aliguori7ba1e612008-11-05 16:04:33 +00001539 { "kvm", "", do_info_kvm,
1540 "", "show kvm information", },
bellarda594cfb2005-11-06 16:13:29 +00001541 { "usb", "", usb_info,
1542 "", "show guest USB devices", },
1543 { "usbhost", "", usb_host_info,
1544 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001545 { "profile", "", do_info_profile,
1546 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001547 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001548 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001549 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001550 "", "show the currently saved VM snapshots" },
balrog201a51f2007-04-30 00:51:09 +00001551 { "pcmcia", "", pcmcia_info,
1552 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001553 { "mice", "", do_info_mice,
1554 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001555 { "vnc", "", do_info_vnc,
1556 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001557 { "name", "", do_info_name,
1558 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001559 { "uuid", "", do_info_uuid,
1560 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001561#if defined(TARGET_PPC)
1562 { "cpustats", "", do_info_cpu_stats,
1563 "", "show CPU statistics", },
1564#endif
blueswir131a60e22007-10-26 18:42:59 +00001565#if defined(CONFIG_SLIRP)
1566 { "slirp", "", do_info_slirp,
1567 "", "show SLIRP statistics", },
1568#endif
aliguori5bb79102008-10-13 03:12:02 +00001569 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001570 { "balloon", "", do_info_balloon,
1571 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001572 { NULL, NULL, },
1573};
1574
bellard9307c4c2004-04-04 12:57:25 +00001575/*******************************************************************/
1576
1577static const char *pch;
1578static jmp_buf expr_env;
1579
bellard92a31b12005-02-10 22:00:52 +00001580#define MD_TLONG 0
1581#define MD_I32 1
1582
bellard9307c4c2004-04-04 12:57:25 +00001583typedef struct MonitorDef {
1584 const char *name;
1585 int offset;
blueswir18662d652008-10-02 18:32:44 +00001586 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001587 int type;
bellard9307c4c2004-04-04 12:57:25 +00001588} MonitorDef;
1589
bellard57206fd2004-04-25 18:54:52 +00001590#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001591static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001592{
bellard6a00d602005-11-21 23:25:50 +00001593 CPUState *env = mon_get_cpu();
1594 if (!env)
1595 return 0;
1596 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001597}
1598#endif
1599
bellarda541f292004-04-12 20:39:29 +00001600#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001601static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001602{
bellard6a00d602005-11-21 23:25:50 +00001603 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001604 unsigned int u;
1605 int i;
1606
bellard6a00d602005-11-21 23:25:50 +00001607 if (!env)
1608 return 0;
1609
bellarda541f292004-04-12 20:39:29 +00001610 u = 0;
1611 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001612 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001613
1614 return u;
1615}
1616
blueswir18662d652008-10-02 18:32:44 +00001617static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001618{
bellard6a00d602005-11-21 23:25:50 +00001619 CPUState *env = mon_get_cpu();
1620 if (!env)
1621 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001622 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001623}
1624
blueswir18662d652008-10-02 18:32:44 +00001625static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001626{
bellard6a00d602005-11-21 23:25:50 +00001627 CPUState *env = mon_get_cpu();
1628 if (!env)
1629 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001630 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001631}
bellard9fddaa02004-05-21 12:59:32 +00001632
blueswir18662d652008-10-02 18:32:44 +00001633static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001634{
bellard6a00d602005-11-21 23:25:50 +00001635 CPUState *env = mon_get_cpu();
1636 if (!env)
1637 return 0;
1638 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001639}
1640
blueswir18662d652008-10-02 18:32:44 +00001641static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001642{
bellard6a00d602005-11-21 23:25:50 +00001643 CPUState *env = mon_get_cpu();
1644 if (!env)
1645 return 0;
1646 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001647}
1648
blueswir18662d652008-10-02 18:32:44 +00001649static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001650{
bellard6a00d602005-11-21 23:25:50 +00001651 CPUState *env = mon_get_cpu();
1652 if (!env)
1653 return 0;
1654 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001655}
bellarda541f292004-04-12 20:39:29 +00001656#endif
1657
bellarde95c8d52004-09-30 22:22:08 +00001658#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001659#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001660static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001661{
bellard6a00d602005-11-21 23:25:50 +00001662 CPUState *env = mon_get_cpu();
1663 if (!env)
1664 return 0;
1665 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001666}
bellard7b936c02005-10-30 17:05:13 +00001667#endif
bellarde95c8d52004-09-30 22:22:08 +00001668
blueswir18662d652008-10-02 18:32:44 +00001669static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001670{
bellard6a00d602005-11-21 23:25:50 +00001671 CPUState *env = mon_get_cpu();
1672 if (!env)
1673 return 0;
1674 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001675}
1676#endif
1677
blueswir18662d652008-10-02 18:32:44 +00001678static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001679#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001680
1681#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001682 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001683 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001684 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001685
bellard9307c4c2004-04-04 12:57:25 +00001686 { "eax", offsetof(CPUState, regs[0]) },
1687 { "ecx", offsetof(CPUState, regs[1]) },
1688 { "edx", offsetof(CPUState, regs[2]) },
1689 { "ebx", offsetof(CPUState, regs[3]) },
1690 { "esp|sp", offsetof(CPUState, regs[4]) },
1691 { "ebp|fp", offsetof(CPUState, regs[5]) },
1692 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001693 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001694#ifdef TARGET_X86_64
1695 { "r8", offsetof(CPUState, regs[8]) },
1696 { "r9", offsetof(CPUState, regs[9]) },
1697 { "r10", offsetof(CPUState, regs[10]) },
1698 { "r11", offsetof(CPUState, regs[11]) },
1699 { "r12", offsetof(CPUState, regs[12]) },
1700 { "r13", offsetof(CPUState, regs[13]) },
1701 { "r14", offsetof(CPUState, regs[14]) },
1702 { "r15", offsetof(CPUState, regs[15]) },
1703#endif
bellard9307c4c2004-04-04 12:57:25 +00001704 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001705 { "eip", offsetof(CPUState, eip) },
1706 SEG("cs", R_CS)
1707 SEG("ds", R_DS)
1708 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001709 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001710 SEG("fs", R_FS)
1711 SEG("gs", R_GS)
1712 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001713#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001714 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001715 { "r0", offsetof(CPUState, gpr[0]) },
1716 { "r1", offsetof(CPUState, gpr[1]) },
1717 { "r2", offsetof(CPUState, gpr[2]) },
1718 { "r3", offsetof(CPUState, gpr[3]) },
1719 { "r4", offsetof(CPUState, gpr[4]) },
1720 { "r5", offsetof(CPUState, gpr[5]) },
1721 { "r6", offsetof(CPUState, gpr[6]) },
1722 { "r7", offsetof(CPUState, gpr[7]) },
1723 { "r8", offsetof(CPUState, gpr[8]) },
1724 { "r9", offsetof(CPUState, gpr[9]) },
1725 { "r10", offsetof(CPUState, gpr[10]) },
1726 { "r11", offsetof(CPUState, gpr[11]) },
1727 { "r12", offsetof(CPUState, gpr[12]) },
1728 { "r13", offsetof(CPUState, gpr[13]) },
1729 { "r14", offsetof(CPUState, gpr[14]) },
1730 { "r15", offsetof(CPUState, gpr[15]) },
1731 { "r16", offsetof(CPUState, gpr[16]) },
1732 { "r17", offsetof(CPUState, gpr[17]) },
1733 { "r18", offsetof(CPUState, gpr[18]) },
1734 { "r19", offsetof(CPUState, gpr[19]) },
1735 { "r20", offsetof(CPUState, gpr[20]) },
1736 { "r21", offsetof(CPUState, gpr[21]) },
1737 { "r22", offsetof(CPUState, gpr[22]) },
1738 { "r23", offsetof(CPUState, gpr[23]) },
1739 { "r24", offsetof(CPUState, gpr[24]) },
1740 { "r25", offsetof(CPUState, gpr[25]) },
1741 { "r26", offsetof(CPUState, gpr[26]) },
1742 { "r27", offsetof(CPUState, gpr[27]) },
1743 { "r28", offsetof(CPUState, gpr[28]) },
1744 { "r29", offsetof(CPUState, gpr[29]) },
1745 { "r30", offsetof(CPUState, gpr[30]) },
1746 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001747 /* Floating point registers */
1748 { "f0", offsetof(CPUState, fpr[0]) },
1749 { "f1", offsetof(CPUState, fpr[1]) },
1750 { "f2", offsetof(CPUState, fpr[2]) },
1751 { "f3", offsetof(CPUState, fpr[3]) },
1752 { "f4", offsetof(CPUState, fpr[4]) },
1753 { "f5", offsetof(CPUState, fpr[5]) },
1754 { "f6", offsetof(CPUState, fpr[6]) },
1755 { "f7", offsetof(CPUState, fpr[7]) },
1756 { "f8", offsetof(CPUState, fpr[8]) },
1757 { "f9", offsetof(CPUState, fpr[9]) },
1758 { "f10", offsetof(CPUState, fpr[10]) },
1759 { "f11", offsetof(CPUState, fpr[11]) },
1760 { "f12", offsetof(CPUState, fpr[12]) },
1761 { "f13", offsetof(CPUState, fpr[13]) },
1762 { "f14", offsetof(CPUState, fpr[14]) },
1763 { "f15", offsetof(CPUState, fpr[15]) },
1764 { "f16", offsetof(CPUState, fpr[16]) },
1765 { "f17", offsetof(CPUState, fpr[17]) },
1766 { "f18", offsetof(CPUState, fpr[18]) },
1767 { "f19", offsetof(CPUState, fpr[19]) },
1768 { "f20", offsetof(CPUState, fpr[20]) },
1769 { "f21", offsetof(CPUState, fpr[21]) },
1770 { "f22", offsetof(CPUState, fpr[22]) },
1771 { "f23", offsetof(CPUState, fpr[23]) },
1772 { "f24", offsetof(CPUState, fpr[24]) },
1773 { "f25", offsetof(CPUState, fpr[25]) },
1774 { "f26", offsetof(CPUState, fpr[26]) },
1775 { "f27", offsetof(CPUState, fpr[27]) },
1776 { "f28", offsetof(CPUState, fpr[28]) },
1777 { "f29", offsetof(CPUState, fpr[29]) },
1778 { "f30", offsetof(CPUState, fpr[30]) },
1779 { "f31", offsetof(CPUState, fpr[31]) },
1780 { "fpscr", offsetof(CPUState, fpscr) },
1781 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00001782 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00001783 { "lr", offsetof(CPUState, lr) },
1784 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00001785 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00001786 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00001787 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00001788 { "msr", 0, &monitor_get_msr, },
1789 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00001790 { "tbu", 0, &monitor_get_tbu, },
1791 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00001792#if defined(TARGET_PPC64)
1793 /* Address space register */
1794 { "asr", offsetof(CPUState, asr) },
1795#endif
1796 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00001797 { "sdr1", offsetof(CPUState, sdr1) },
1798 { "sr0", offsetof(CPUState, sr[0]) },
1799 { "sr1", offsetof(CPUState, sr[1]) },
1800 { "sr2", offsetof(CPUState, sr[2]) },
1801 { "sr3", offsetof(CPUState, sr[3]) },
1802 { "sr4", offsetof(CPUState, sr[4]) },
1803 { "sr5", offsetof(CPUState, sr[5]) },
1804 { "sr6", offsetof(CPUState, sr[6]) },
1805 { "sr7", offsetof(CPUState, sr[7]) },
1806 { "sr8", offsetof(CPUState, sr[8]) },
1807 { "sr9", offsetof(CPUState, sr[9]) },
1808 { "sr10", offsetof(CPUState, sr[10]) },
1809 { "sr11", offsetof(CPUState, sr[11]) },
1810 { "sr12", offsetof(CPUState, sr[12]) },
1811 { "sr13", offsetof(CPUState, sr[13]) },
1812 { "sr14", offsetof(CPUState, sr[14]) },
1813 { "sr15", offsetof(CPUState, sr[15]) },
1814 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00001815#elif defined(TARGET_SPARC)
1816 { "g0", offsetof(CPUState, gregs[0]) },
1817 { "g1", offsetof(CPUState, gregs[1]) },
1818 { "g2", offsetof(CPUState, gregs[2]) },
1819 { "g3", offsetof(CPUState, gregs[3]) },
1820 { "g4", offsetof(CPUState, gregs[4]) },
1821 { "g5", offsetof(CPUState, gregs[5]) },
1822 { "g6", offsetof(CPUState, gregs[6]) },
1823 { "g7", offsetof(CPUState, gregs[7]) },
1824 { "o0", 0, monitor_get_reg },
1825 { "o1", 1, monitor_get_reg },
1826 { "o2", 2, monitor_get_reg },
1827 { "o3", 3, monitor_get_reg },
1828 { "o4", 4, monitor_get_reg },
1829 { "o5", 5, monitor_get_reg },
1830 { "o6", 6, monitor_get_reg },
1831 { "o7", 7, monitor_get_reg },
1832 { "l0", 8, monitor_get_reg },
1833 { "l1", 9, monitor_get_reg },
1834 { "l2", 10, monitor_get_reg },
1835 { "l3", 11, monitor_get_reg },
1836 { "l4", 12, monitor_get_reg },
1837 { "l5", 13, monitor_get_reg },
1838 { "l6", 14, monitor_get_reg },
1839 { "l7", 15, monitor_get_reg },
1840 { "i0", 16, monitor_get_reg },
1841 { "i1", 17, monitor_get_reg },
1842 { "i2", 18, monitor_get_reg },
1843 { "i3", 19, monitor_get_reg },
1844 { "i4", 20, monitor_get_reg },
1845 { "i5", 21, monitor_get_reg },
1846 { "i6", 22, monitor_get_reg },
1847 { "i7", 23, monitor_get_reg },
1848 { "pc", offsetof(CPUState, pc) },
1849 { "npc", offsetof(CPUState, npc) },
1850 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00001851#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00001852 { "psr", 0, &monitor_get_psr, },
1853 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00001854#endif
bellarde95c8d52004-09-30 22:22:08 +00001855 { "tbr", offsetof(CPUState, tbr) },
1856 { "fsr", offsetof(CPUState, fsr) },
1857 { "f0", offsetof(CPUState, fpr[0]) },
1858 { "f1", offsetof(CPUState, fpr[1]) },
1859 { "f2", offsetof(CPUState, fpr[2]) },
1860 { "f3", offsetof(CPUState, fpr[3]) },
1861 { "f4", offsetof(CPUState, fpr[4]) },
1862 { "f5", offsetof(CPUState, fpr[5]) },
1863 { "f6", offsetof(CPUState, fpr[6]) },
1864 { "f7", offsetof(CPUState, fpr[7]) },
1865 { "f8", offsetof(CPUState, fpr[8]) },
1866 { "f9", offsetof(CPUState, fpr[9]) },
1867 { "f10", offsetof(CPUState, fpr[10]) },
1868 { "f11", offsetof(CPUState, fpr[11]) },
1869 { "f12", offsetof(CPUState, fpr[12]) },
1870 { "f13", offsetof(CPUState, fpr[13]) },
1871 { "f14", offsetof(CPUState, fpr[14]) },
1872 { "f15", offsetof(CPUState, fpr[15]) },
1873 { "f16", offsetof(CPUState, fpr[16]) },
1874 { "f17", offsetof(CPUState, fpr[17]) },
1875 { "f18", offsetof(CPUState, fpr[18]) },
1876 { "f19", offsetof(CPUState, fpr[19]) },
1877 { "f20", offsetof(CPUState, fpr[20]) },
1878 { "f21", offsetof(CPUState, fpr[21]) },
1879 { "f22", offsetof(CPUState, fpr[22]) },
1880 { "f23", offsetof(CPUState, fpr[23]) },
1881 { "f24", offsetof(CPUState, fpr[24]) },
1882 { "f25", offsetof(CPUState, fpr[25]) },
1883 { "f26", offsetof(CPUState, fpr[26]) },
1884 { "f27", offsetof(CPUState, fpr[27]) },
1885 { "f28", offsetof(CPUState, fpr[28]) },
1886 { "f29", offsetof(CPUState, fpr[29]) },
1887 { "f30", offsetof(CPUState, fpr[30]) },
1888 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00001889#ifdef TARGET_SPARC64
1890 { "f32", offsetof(CPUState, fpr[32]) },
1891 { "f34", offsetof(CPUState, fpr[34]) },
1892 { "f36", offsetof(CPUState, fpr[36]) },
1893 { "f38", offsetof(CPUState, fpr[38]) },
1894 { "f40", offsetof(CPUState, fpr[40]) },
1895 { "f42", offsetof(CPUState, fpr[42]) },
1896 { "f44", offsetof(CPUState, fpr[44]) },
1897 { "f46", offsetof(CPUState, fpr[46]) },
1898 { "f48", offsetof(CPUState, fpr[48]) },
1899 { "f50", offsetof(CPUState, fpr[50]) },
1900 { "f52", offsetof(CPUState, fpr[52]) },
1901 { "f54", offsetof(CPUState, fpr[54]) },
1902 { "f56", offsetof(CPUState, fpr[56]) },
1903 { "f58", offsetof(CPUState, fpr[58]) },
1904 { "f60", offsetof(CPUState, fpr[60]) },
1905 { "f62", offsetof(CPUState, fpr[62]) },
1906 { "asi", offsetof(CPUState, asi) },
1907 { "pstate", offsetof(CPUState, pstate) },
1908 { "cansave", offsetof(CPUState, cansave) },
1909 { "canrestore", offsetof(CPUState, canrestore) },
1910 { "otherwin", offsetof(CPUState, otherwin) },
1911 { "wstate", offsetof(CPUState, wstate) },
1912 { "cleanwin", offsetof(CPUState, cleanwin) },
1913 { "fprs", offsetof(CPUState, fprs) },
1914#endif
bellard9307c4c2004-04-04 12:57:25 +00001915#endif
1916 { NULL },
1917};
1918
ths5fafdf22007-09-16 21:08:06 +00001919static void expr_error(const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001920{
bellard9307c4c2004-04-04 12:57:25 +00001921 term_printf(fmt);
1922 term_printf("\n");
1923 longjmp(expr_env, 1);
1924}
1925
bellard6a00d602005-11-21 23:25:50 +00001926/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00001927static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00001928{
blueswir18662d652008-10-02 18:32:44 +00001929 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00001930 void *ptr;
1931
bellard9307c4c2004-04-04 12:57:25 +00001932 for(md = monitor_defs; md->name != NULL; md++) {
1933 if (compare_cmd(name, md->name)) {
1934 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00001935 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00001936 } else {
bellard6a00d602005-11-21 23:25:50 +00001937 CPUState *env = mon_get_cpu();
1938 if (!env)
1939 return -2;
1940 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00001941 switch(md->type) {
1942 case MD_I32:
1943 *pval = *(int32_t *)ptr;
1944 break;
1945 case MD_TLONG:
1946 *pval = *(target_long *)ptr;
1947 break;
1948 default:
1949 *pval = 0;
1950 break;
1951 }
bellard9307c4c2004-04-04 12:57:25 +00001952 }
1953 return 0;
1954 }
1955 }
1956 return -1;
1957}
1958
1959static void next(void)
1960{
1961 if (pch != '\0') {
1962 pch++;
blueswir1cd390082008-11-16 13:53:32 +00001963 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00001964 pch++;
1965 }
1966}
1967
blueswir1c2efc952007-09-25 17:28:42 +00001968static int64_t expr_sum(void);
bellard9307c4c2004-04-04 12:57:25 +00001969
blueswir1c2efc952007-09-25 17:28:42 +00001970static int64_t expr_unary(void)
bellard9307c4c2004-04-04 12:57:25 +00001971{
blueswir1c2efc952007-09-25 17:28:42 +00001972 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00001973 char *p;
bellard6a00d602005-11-21 23:25:50 +00001974 int ret;
bellard9307c4c2004-04-04 12:57:25 +00001975
1976 switch(*pch) {
1977 case '+':
1978 next();
1979 n = expr_unary();
1980 break;
1981 case '-':
1982 next();
1983 n = -expr_unary();
1984 break;
1985 case '~':
1986 next();
1987 n = ~expr_unary();
1988 break;
1989 case '(':
1990 next();
1991 n = expr_sum();
1992 if (*pch != ')') {
1993 expr_error("')' expected");
1994 }
1995 next();
1996 break;
bellard81d09122004-07-14 17:21:37 +00001997 case '\'':
1998 pch++;
1999 if (*pch == '\0')
2000 expr_error("character constant expected");
2001 n = *pch;
2002 pch++;
2003 if (*pch != '\'')
2004 expr_error("missing terminating \' character");
2005 next();
2006 break;
bellard9307c4c2004-04-04 12:57:25 +00002007 case '$':
2008 {
2009 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002010 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002011
bellard9307c4c2004-04-04 12:57:25 +00002012 pch++;
2013 q = buf;
2014 while ((*pch >= 'a' && *pch <= 'z') ||
2015 (*pch >= 'A' && *pch <= 'Z') ||
2016 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002017 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002018 if ((q - buf) < sizeof(buf) - 1)
2019 *q++ = *pch;
2020 pch++;
2021 }
blueswir1cd390082008-11-16 13:53:32 +00002022 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002023 pch++;
2024 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002025 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002026 if (ret == -1)
bellard9307c4c2004-04-04 12:57:25 +00002027 expr_error("unknown register");
ths5fafdf22007-09-16 21:08:06 +00002028 else if (ret == -2)
bellard6a00d602005-11-21 23:25:50 +00002029 expr_error("no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002030 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002031 }
2032 break;
2033 case '\0':
2034 expr_error("unexpected end of expression");
2035 n = 0;
2036 break;
2037 default:
blueswir17743e582007-09-24 18:39:04 +00002038#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002039 n = strtoull(pch, &p, 0);
2040#else
bellard9307c4c2004-04-04 12:57:25 +00002041 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002042#endif
bellard9307c4c2004-04-04 12:57:25 +00002043 if (pch == p) {
2044 expr_error("invalid char in expression");
2045 }
2046 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002047 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002048 pch++;
2049 break;
2050 }
2051 return n;
2052}
2053
2054
blueswir1c2efc952007-09-25 17:28:42 +00002055static int64_t expr_prod(void)
bellard9307c4c2004-04-04 12:57:25 +00002056{
blueswir1c2efc952007-09-25 17:28:42 +00002057 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002058 int op;
ths3b46e622007-09-17 08:09:54 +00002059
bellard9307c4c2004-04-04 12:57:25 +00002060 val = expr_unary();
2061 for(;;) {
2062 op = *pch;
2063 if (op != '*' && op != '/' && op != '%')
2064 break;
2065 next();
2066 val2 = expr_unary();
2067 switch(op) {
2068 default:
2069 case '*':
2070 val *= val2;
2071 break;
2072 case '/':
2073 case '%':
ths5fafdf22007-09-16 21:08:06 +00002074 if (val2 == 0)
bellard5b602122004-05-22 21:41:05 +00002075 expr_error("division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002076 if (op == '/')
2077 val /= val2;
2078 else
2079 val %= val2;
2080 break;
2081 }
2082 }
2083 return val;
2084}
2085
blueswir1c2efc952007-09-25 17:28:42 +00002086static int64_t expr_logic(void)
bellard9307c4c2004-04-04 12:57:25 +00002087{
blueswir1c2efc952007-09-25 17:28:42 +00002088 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002089 int op;
bellard9307c4c2004-04-04 12:57:25 +00002090
2091 val = expr_prod();
2092 for(;;) {
2093 op = *pch;
2094 if (op != '&' && op != '|' && op != '^')
2095 break;
2096 next();
2097 val2 = expr_prod();
2098 switch(op) {
2099 default:
2100 case '&':
2101 val &= val2;
2102 break;
2103 case '|':
2104 val |= val2;
2105 break;
2106 case '^':
2107 val ^= val2;
2108 break;
2109 }
2110 }
2111 return val;
2112}
2113
blueswir1c2efc952007-09-25 17:28:42 +00002114static int64_t expr_sum(void)
bellard9307c4c2004-04-04 12:57:25 +00002115{
blueswir1c2efc952007-09-25 17:28:42 +00002116 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002117 int op;
bellard9307c4c2004-04-04 12:57:25 +00002118
2119 val = expr_logic();
2120 for(;;) {
2121 op = *pch;
2122 if (op != '+' && op != '-')
2123 break;
2124 next();
2125 val2 = expr_logic();
2126 if (op == '+')
2127 val += val2;
2128 else
2129 val -= val2;
2130 }
2131 return val;
2132}
2133
blueswir1c2efc952007-09-25 17:28:42 +00002134static int get_expr(int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002135{
2136 pch = *pp;
2137 if (setjmp(expr_env)) {
2138 *pp = pch;
2139 return -1;
2140 }
blueswir1cd390082008-11-16 13:53:32 +00002141 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002142 pch++;
2143 *pval = expr_sum();
2144 *pp = pch;
2145 return 0;
2146}
2147
2148static int get_str(char *buf, int buf_size, const char **pp)
2149{
2150 const char *p;
2151 char *q;
2152 int c;
2153
bellard81d09122004-07-14 17:21:37 +00002154 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002155 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002156 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002157 p++;
2158 if (*p == '\0') {
2159 fail:
bellard81d09122004-07-14 17:21:37 +00002160 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002161 *pp = p;
2162 return -1;
2163 }
bellard9307c4c2004-04-04 12:57:25 +00002164 if (*p == '\"') {
2165 p++;
2166 while (*p != '\0' && *p != '\"') {
2167 if (*p == '\\') {
2168 p++;
2169 c = *p++;
2170 switch(c) {
2171 case 'n':
2172 c = '\n';
2173 break;
2174 case 'r':
2175 c = '\r';
2176 break;
2177 case '\\':
2178 case '\'':
2179 case '\"':
2180 break;
2181 default:
2182 qemu_printf("unsupported escape code: '\\%c'\n", c);
2183 goto fail;
2184 }
2185 if ((q - buf) < buf_size - 1) {
2186 *q++ = c;
2187 }
2188 } else {
2189 if ((q - buf) < buf_size - 1) {
2190 *q++ = *p;
2191 }
2192 p++;
2193 }
2194 }
2195 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002196 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002197 goto fail;
2198 }
2199 p++;
2200 } else {
blueswir1cd390082008-11-16 13:53:32 +00002201 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002202 if ((q - buf) < buf_size - 1) {
2203 *q++ = *p;
2204 }
2205 p++;
2206 }
bellard9307c4c2004-04-04 12:57:25 +00002207 }
bellard81d09122004-07-14 17:21:37 +00002208 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002209 *pp = p;
2210 return 0;
2211}
2212
2213static int default_fmt_format = 'x';
2214static int default_fmt_size = 4;
2215
2216#define MAX_ARGS 16
2217
bellard7e2515e2004-08-01 21:52:19 +00002218static void monitor_handle_command(const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002219{
2220 const char *p, *pstart, *typestr;
2221 char *q;
2222 int c, nb_args, len, i, has_arg;
blueswir18662d652008-10-02 18:32:44 +00002223 const term_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002224 char cmdname[256];
2225 char buf[1024];
2226 void *str_allocated[MAX_ARGS];
2227 void *args[MAX_ARGS];
blueswir1a5f1b962008-08-17 20:21:51 +00002228 void (*handler_0)(void);
2229 void (*handler_1)(void *arg0);
2230 void (*handler_2)(void *arg0, void *arg1);
2231 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2232 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2233 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2234 void *arg4);
2235 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2236 void *arg4, void *arg5);
2237 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2238 void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002239
2240#ifdef DEBUG
2241 term_printf("command='%s'\n", cmdline);
2242#endif
ths3b46e622007-09-17 08:09:54 +00002243
bellard9307c4c2004-04-04 12:57:25 +00002244 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002245 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002246 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002247 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002248 p++;
2249 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002250 return;
bellard9307c4c2004-04-04 12:57:25 +00002251 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002252 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002253 p++;
2254 len = p - pstart;
2255 if (len > sizeof(cmdname) - 1)
2256 len = sizeof(cmdname) - 1;
2257 memcpy(cmdname, pstart, len);
2258 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002259
bellard9307c4c2004-04-04 12:57:25 +00002260 /* find the command */
bellard9dc39cb2004-03-14 21:38:27 +00002261 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002262 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002263 goto found;
2264 }
bellard9307c4c2004-04-04 12:57:25 +00002265 term_printf("unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002266 return;
2267 found:
bellard9307c4c2004-04-04 12:57:25 +00002268
2269 for(i = 0; i < MAX_ARGS; i++)
2270 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002271
bellard9307c4c2004-04-04 12:57:25 +00002272 /* parse the parameters */
2273 typestr = cmd->args_type;
2274 nb_args = 0;
2275 for(;;) {
2276 c = *typestr;
2277 if (c == '\0')
2278 break;
2279 typestr++;
2280 switch(c) {
2281 case 'F':
bellard81d09122004-07-14 17:21:37 +00002282 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002283 case 's':
2284 {
2285 int ret;
2286 char *str;
ths3b46e622007-09-17 08:09:54 +00002287
blueswir1cd390082008-11-16 13:53:32 +00002288 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002289 p++;
2290 if (*typestr == '?') {
2291 typestr++;
2292 if (*p == '\0') {
2293 /* no optional string: NULL argument */
2294 str = NULL;
2295 goto add_str;
2296 }
2297 }
2298 ret = get_str(buf, sizeof(buf), &p);
2299 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002300 switch(c) {
2301 case 'F':
bellard9307c4c2004-04-04 12:57:25 +00002302 term_printf("%s: filename expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002303 break;
2304 case 'B':
2305 term_printf("%s: block device name expected\n", cmdname);
2306 break;
2307 default:
bellard9307c4c2004-04-04 12:57:25 +00002308 term_printf("%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002309 break;
2310 }
bellard9307c4c2004-04-04 12:57:25 +00002311 goto fail;
2312 }
2313 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002314 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002315 str_allocated[nb_args] = str;
2316 add_str:
2317 if (nb_args >= MAX_ARGS) {
2318 error_args:
2319 term_printf("%s: too many arguments\n", cmdname);
2320 goto fail;
2321 }
2322 args[nb_args++] = str;
2323 }
2324 break;
2325 case '/':
2326 {
2327 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002328
blueswir1cd390082008-11-16 13:53:32 +00002329 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002330 p++;
2331 if (*p == '/') {
2332 /* format found */
2333 p++;
2334 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002335 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002336 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002337 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002338 count = count * 10 + (*p - '0');
2339 p++;
2340 }
2341 }
2342 size = -1;
2343 format = -1;
2344 for(;;) {
2345 switch(*p) {
2346 case 'o':
2347 case 'd':
2348 case 'u':
2349 case 'x':
2350 case 'i':
2351 case 'c':
2352 format = *p++;
2353 break;
2354 case 'b':
2355 size = 1;
2356 p++;
2357 break;
2358 case 'h':
2359 size = 2;
2360 p++;
2361 break;
2362 case 'w':
2363 size = 4;
2364 p++;
2365 break;
2366 case 'g':
2367 case 'L':
2368 size = 8;
2369 p++;
2370 break;
2371 default:
2372 goto next;
2373 }
2374 }
2375 next:
blueswir1cd390082008-11-16 13:53:32 +00002376 if (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002377 term_printf("invalid char in format: '%c'\n", *p);
2378 goto fail;
2379 }
bellard9307c4c2004-04-04 12:57:25 +00002380 if (format < 0)
2381 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002382 if (format != 'i') {
2383 /* for 'i', not specifying a size gives -1 as size */
2384 if (size < 0)
2385 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002386 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002387 }
bellard9307c4c2004-04-04 12:57:25 +00002388 default_fmt_format = format;
2389 } else {
2390 count = 1;
2391 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002392 if (format != 'i') {
2393 size = default_fmt_size;
2394 } else {
2395 size = -1;
2396 }
bellard9307c4c2004-04-04 12:57:25 +00002397 }
2398 if (nb_args + 3 > MAX_ARGS)
2399 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002400 args[nb_args++] = (void*)(long)count;
2401 args[nb_args++] = (void*)(long)format;
2402 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002403 }
2404 break;
2405 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002406 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002407 {
blueswir1c2efc952007-09-25 17:28:42 +00002408 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002409
blueswir1cd390082008-11-16 13:53:32 +00002410 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002411 p++;
bellard34405572004-06-08 00:55:58 +00002412 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002413 if (*typestr == '?') {
2414 if (*p == '\0')
2415 has_arg = 0;
2416 else
2417 has_arg = 1;
2418 } else {
2419 if (*p == '.') {
2420 p++;
blueswir1cd390082008-11-16 13:53:32 +00002421 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002422 p++;
2423 has_arg = 1;
2424 } else {
2425 has_arg = 0;
2426 }
2427 }
bellard13224a82006-07-14 22:03:35 +00002428 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002429 if (nb_args >= MAX_ARGS)
2430 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002431 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002432 if (!has_arg) {
2433 if (nb_args >= MAX_ARGS)
2434 goto error_args;
2435 val = -1;
2436 goto add_num;
2437 }
2438 }
2439 if (get_expr(&val, &p))
2440 goto fail;
2441 add_num:
bellard92a31b12005-02-10 22:00:52 +00002442 if (c == 'i') {
2443 if (nb_args >= MAX_ARGS)
2444 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002445 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002446 } else {
2447 if ((nb_args + 1) >= MAX_ARGS)
2448 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002449#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002450 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002451#else
2452 args[nb_args++] = (void *)0;
2453#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002454 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002455 }
bellard9307c4c2004-04-04 12:57:25 +00002456 }
2457 break;
2458 case '-':
2459 {
2460 int has_option;
2461 /* option */
ths3b46e622007-09-17 08:09:54 +00002462
bellard9307c4c2004-04-04 12:57:25 +00002463 c = *typestr++;
2464 if (c == '\0')
2465 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002466 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002467 p++;
2468 has_option = 0;
2469 if (*p == '-') {
2470 p++;
2471 if (*p != c) {
ths5fafdf22007-09-16 21:08:06 +00002472 term_printf("%s: unsupported option -%c\n",
bellard9307c4c2004-04-04 12:57:25 +00002473 cmdname, *p);
2474 goto fail;
2475 }
2476 p++;
2477 has_option = 1;
2478 }
2479 if (nb_args >= MAX_ARGS)
2480 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002481 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002482 }
2483 break;
2484 default:
2485 bad_type:
2486 term_printf("%s: unknown type '%c'\n", cmdname, c);
2487 goto fail;
2488 }
2489 }
2490 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002491 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002492 p++;
2493 if (*p != '\0') {
ths5fafdf22007-09-16 21:08:06 +00002494 term_printf("%s: extraneous characters at the end of line\n",
bellard9307c4c2004-04-04 12:57:25 +00002495 cmdname);
2496 goto fail;
2497 }
2498
2499 switch(nb_args) {
2500 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002501 handler_0 = cmd->handler;
2502 handler_0();
bellard9307c4c2004-04-04 12:57:25 +00002503 break;
2504 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002505 handler_1 = cmd->handler;
2506 handler_1(args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002507 break;
2508 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002509 handler_2 = cmd->handler;
2510 handler_2(args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002511 break;
2512 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002513 handler_3 = cmd->handler;
2514 handler_3(args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002515 break;
2516 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002517 handler_4 = cmd->handler;
2518 handler_4(args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002519 break;
2520 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002521 handler_5 = cmd->handler;
2522 handler_5(args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002523 break;
bellard34405572004-06-08 00:55:58 +00002524 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002525 handler_6 = cmd->handler;
2526 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002527 break;
bellardec36b692006-07-16 18:57:03 +00002528 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002529 handler_7 = cmd->handler;
2530 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
bellardec36b692006-07-16 18:57:03 +00002531 break;
bellard9307c4c2004-04-04 12:57:25 +00002532 default:
2533 term_printf("unsupported number of arguments: %d\n", nb_args);
2534 goto fail;
2535 }
2536 fail:
2537 for(i = 0; i < MAX_ARGS; i++)
2538 qemu_free(str_allocated[i]);
2539 return;
bellard9dc39cb2004-03-14 21:38:27 +00002540}
2541
bellard81d09122004-07-14 17:21:37 +00002542static void cmd_completion(const char *name, const char *list)
2543{
2544 const char *p, *pstart;
2545 char cmd[128];
2546 int len;
2547
2548 p = list;
2549 for(;;) {
2550 pstart = p;
2551 p = strchr(p, '|');
2552 if (!p)
2553 p = pstart + strlen(pstart);
2554 len = p - pstart;
2555 if (len > sizeof(cmd) - 2)
2556 len = sizeof(cmd) - 2;
2557 memcpy(cmd, pstart, len);
2558 cmd[len] = '\0';
2559 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2560 add_completion(cmd);
2561 }
2562 if (*p == '\0')
2563 break;
2564 p++;
2565 }
2566}
2567
2568static void file_completion(const char *input)
2569{
2570 DIR *ffs;
2571 struct dirent *d;
2572 char path[1024];
2573 char file[1024], file_prefix[1024];
2574 int input_path_len;
2575 const char *p;
2576
ths5fafdf22007-09-16 21:08:06 +00002577 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002578 if (!p) {
2579 input_path_len = 0;
2580 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002581 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002582 } else {
2583 input_path_len = p - input + 1;
2584 memcpy(path, input, input_path_len);
2585 if (input_path_len > sizeof(path) - 1)
2586 input_path_len = sizeof(path) - 1;
2587 path[input_path_len] = '\0';
2588 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2589 }
2590#ifdef DEBUG_COMPLETION
2591 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2592#endif
2593 ffs = opendir(path);
2594 if (!ffs)
2595 return;
2596 for(;;) {
2597 struct stat sb;
2598 d = readdir(ffs);
2599 if (!d)
2600 break;
2601 if (strstart(d->d_name, file_prefix, NULL)) {
2602 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002603 if (input_path_len < sizeof(file))
2604 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2605 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002606 /* stat the file to find out if it's a directory.
2607 * In that case add a slash to speed up typing long paths
2608 */
2609 stat(file, &sb);
2610 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002611 pstrcat(file, sizeof(file), "/");
bellard81d09122004-07-14 17:21:37 +00002612 add_completion(file);
2613 }
2614 }
2615 closedir(ffs);
2616}
2617
2618static void block_completion_it(void *opaque, const char *name)
2619{
2620 const char *input = opaque;
2621
2622 if (input[0] == '\0' ||
2623 !strncmp(name, (char *)input, strlen(input))) {
2624 add_completion(name);
2625 }
2626}
2627
2628/* NOTE: this parser is an approximate form of the real command parser */
2629static void parse_cmdline(const char *cmdline,
2630 int *pnb_args, char **args)
2631{
2632 const char *p;
2633 int nb_args, ret;
2634 char buf[1024];
2635
2636 p = cmdline;
2637 nb_args = 0;
2638 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002639 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002640 p++;
2641 if (*p == '\0')
2642 break;
2643 if (nb_args >= MAX_ARGS)
2644 break;
2645 ret = get_str(buf, sizeof(buf), &p);
2646 args[nb_args] = qemu_strdup(buf);
2647 nb_args++;
2648 if (ret < 0)
2649 break;
2650 }
2651 *pnb_args = nb_args;
2652}
2653
bellard7e2515e2004-08-01 21:52:19 +00002654void readline_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002655{
2656 const char *cmdname;
2657 char *args[MAX_ARGS];
2658 int nb_args, i, len;
2659 const char *ptype, *str;
blueswir18662d652008-10-02 18:32:44 +00002660 const term_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002661 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002662
2663 parse_cmdline(cmdline, &nb_args, args);
2664#ifdef DEBUG_COMPLETION
2665 for(i = 0; i < nb_args; i++) {
2666 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2667 }
2668#endif
2669
2670 /* if the line ends with a space, it means we want to complete the
2671 next arg */
2672 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002673 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002674 if (nb_args >= MAX_ARGS)
2675 return;
2676 args[nb_args++] = qemu_strdup("");
2677 }
2678 if (nb_args <= 1) {
2679 /* command completion */
2680 if (nb_args == 0)
2681 cmdname = "";
2682 else
2683 cmdname = args[0];
2684 completion_index = strlen(cmdname);
2685 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2686 cmd_completion(cmdname, cmd->name);
2687 }
2688 } else {
2689 /* find the command */
2690 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2691 if (compare_cmd(args[0], cmd->name))
2692 goto found;
2693 }
2694 return;
2695 found:
2696 ptype = cmd->args_type;
2697 for(i = 0; i < nb_args - 2; i++) {
2698 if (*ptype != '\0') {
2699 ptype++;
2700 while (*ptype == '?')
2701 ptype++;
2702 }
2703 }
2704 str = args[nb_args - 1];
2705 switch(*ptype) {
2706 case 'F':
2707 /* file completion */
2708 completion_index = strlen(str);
2709 file_completion(str);
2710 break;
2711 case 'B':
2712 /* block device name completion */
2713 completion_index = strlen(str);
2714 bdrv_iterate(block_completion_it, (void *)str);
2715 break;
bellard7fe48482004-10-09 18:08:01 +00002716 case 's':
2717 /* XXX: more generic ? */
2718 if (!strcmp(cmd->name, "info")) {
2719 completion_index = strlen(str);
2720 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2721 cmd_completion(str, cmd->name);
2722 }
bellard64866c32006-05-07 18:03:31 +00002723 } else if (!strcmp(cmd->name, "sendkey")) {
2724 completion_index = strlen(str);
2725 for(key = key_defs; key->name != NULL; key++) {
2726 cmd_completion(str, key->name);
2727 }
bellard7fe48482004-10-09 18:08:01 +00002728 }
2729 break;
bellard81d09122004-07-14 17:21:37 +00002730 default:
2731 break;
2732 }
2733 }
2734 for(i = 0; i < nb_args; i++)
2735 qemu_free(args[i]);
2736}
2737
bellard9dc39cb2004-03-14 21:38:27 +00002738static int term_can_read(void *opaque)
2739{
bellard81d09122004-07-14 17:21:37 +00002740 return 128;
bellard9dc39cb2004-03-14 21:38:27 +00002741}
2742
2743static void term_read(void *opaque, const uint8_t *buf, int size)
2744{
2745 int i;
2746 for(i = 0; i < size; i++)
bellard7e2515e2004-08-01 21:52:19 +00002747 readline_handle_byte(buf[i]);
2748}
2749
aliguorid8f44602008-10-06 13:52:44 +00002750static int monitor_suspended;
2751
bellard7e2515e2004-08-01 21:52:19 +00002752static void monitor_handle_command1(void *opaque, const char *cmdline)
2753{
2754 monitor_handle_command(cmdline);
aliguorid8f44602008-10-06 13:52:44 +00002755 if (!monitor_suspended)
2756 monitor_start_input();
2757 else
2758 monitor_suspended = 2;
2759}
2760
2761void monitor_suspend(void)
2762{
2763 monitor_suspended = 1;
2764}
2765
2766void monitor_resume(void)
2767{
2768 if (monitor_suspended == 2)
2769 monitor_start_input();
2770 monitor_suspended = 0;
bellard7e2515e2004-08-01 21:52:19 +00002771}
2772
aliguori83ab7952008-08-19 14:44:22 +00002773static void monitor_start_input(void)
bellard7e2515e2004-08-01 21:52:19 +00002774{
2775 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
bellard9dc39cb2004-03-14 21:38:27 +00002776}
2777
ths86e94de2007-01-05 22:01:59 +00002778static void term_event(void *opaque, int event)
2779{
2780 if (event != CHR_EVENT_RESET)
2781 return;
2782
2783 if (!hide_banner)
2784 term_printf("QEMU %s monitor - type 'help' for more information\n",
2785 QEMU_VERSION);
2786 monitor_start_input();
2787}
2788
ths20d8a3e2007-02-18 17:04:49 +00002789static int is_first_init = 1;
2790
bellard81d09122004-07-14 17:21:37 +00002791void monitor_init(CharDriverState *hd, int show_banner)
bellard9dc39cb2004-03-14 21:38:27 +00002792{
ths20d8a3e2007-02-18 17:04:49 +00002793 int i;
2794
2795 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00002796 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2797 if (!key_timer)
2798 return;
ths20d8a3e2007-02-18 17:04:49 +00002799 for (i = 0; i < MAX_MON; i++) {
2800 monitor_hd[i] = NULL;
2801 }
2802 is_first_init = 0;
2803 }
2804 for (i = 0; i < MAX_MON; i++) {
2805 if (monitor_hd[i] == NULL) {
2806 monitor_hd[i] = hd;
2807 break;
2808 }
2809 }
2810
ths86e94de2007-01-05 22:01:59 +00002811 hide_banner = !show_banner;
2812
pbrooke5b0bc42007-01-27 23:46:43 +00002813 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
aliguori83ab7952008-08-19 14:44:22 +00002814
2815 readline_start("", 0, monitor_handle_command1, NULL);
bellard7e2515e2004-08-01 21:52:19 +00002816}
2817
2818/* XXX: use threads ? */
2819/* modal monitor readline */
2820static int monitor_readline_started;
2821static char *monitor_readline_buf;
2822static int monitor_readline_buf_size;
2823
2824static void monitor_readline_cb(void *opaque, const char *input)
2825{
2826 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2827 monitor_readline_started = 0;
2828}
2829
2830void monitor_readline(const char *prompt, int is_password,
2831 char *buf, int buf_size)
2832{
ths20d8a3e2007-02-18 17:04:49 +00002833 int i;
aliguoribc0129d2008-08-01 15:12:34 +00002834 int old_focus[MAX_MON];
ths20d8a3e2007-02-18 17:04:49 +00002835
bellard7e2515e2004-08-01 21:52:19 +00002836 if (is_password) {
aliguoribc0129d2008-08-01 15:12:34 +00002837 for (i = 0; i < MAX_MON; i++) {
2838 old_focus[i] = 0;
2839 if (monitor_hd[i]) {
2840 old_focus[i] = monitor_hd[i]->focus;
2841 monitor_hd[i]->focus = 0;
ths20d8a3e2007-02-18 17:04:49 +00002842 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
aliguoribc0129d2008-08-01 15:12:34 +00002843 }
2844 }
bellard7e2515e2004-08-01 21:52:19 +00002845 }
aliguoribc0129d2008-08-01 15:12:34 +00002846
bellard7e2515e2004-08-01 21:52:19 +00002847 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2848 monitor_readline_buf = buf;
2849 monitor_readline_buf_size = buf_size;
2850 monitor_readline_started = 1;
2851 while (monitor_readline_started) {
2852 main_loop_wait(10);
2853 }
aliguoribc0129d2008-08-01 15:12:34 +00002854 /* restore original focus */
2855 if (is_password) {
2856 for (i = 0; i < MAX_MON; i++)
2857 if (old_focus[i])
2858 monitor_hd[i]->focus = old_focus[i];
2859 }
bellard9dc39cb2004-03-14 21:38:27 +00002860}