blob: 183c846847d3d09dba78bc9cfedda793faa2d823 [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;
aliguoribb5fc202009-03-05 23:01:15 +000077static BlockDriverCompletionFunc *password_completion_cb;
78static void *password_opaque;
bellard81d09122004-07-14 17:21:37 +000079
aliguori83ab7952008-08-19 14:44:22 +000080static void monitor_start_input(void);
81
blueswir1b1d8e522008-10-26 13:43:07 +000082static CPUState *mon_cpu = NULL;
bellard6a00d602005-11-21 23:25:50 +000083
aliguoribb5fc202009-03-05 23:01:15 +000084static void monitor_read_password(ReadLineFunc *readline_func, void *opaque)
85{
86 readline_start("Password: ", 1, readline_func, opaque);
87}
88
bellard9dc39cb2004-03-14 21:38:27 +000089void term_flush(void)
90{
ths20d8a3e2007-02-18 17:04:49 +000091 int i;
bellard7e2515e2004-08-01 21:52:19 +000092 if (term_outbuf_index > 0) {
ths20d8a3e2007-02-18 17:04:49 +000093 for (i = 0; i < MAX_MON; i++)
94 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
95 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
bellard7e2515e2004-08-01 21:52:19 +000096 term_outbuf_index = 0;
97 }
98}
99
100/* flush at every end of line or if the buffer is full */
101void term_puts(const char *str)
102{
ths60fe76f2007-12-16 03:02:09 +0000103 char c;
bellard7e2515e2004-08-01 21:52:19 +0000104 for(;;) {
105 c = *str++;
106 if (c == '\0')
107 break;
bellard7ba12602006-07-14 20:26:42 +0000108 if (c == '\n')
109 term_outbuf[term_outbuf_index++] = '\r';
bellard7e2515e2004-08-01 21:52:19 +0000110 term_outbuf[term_outbuf_index++] = c;
bellard7ba12602006-07-14 20:26:42 +0000111 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
bellard7e2515e2004-08-01 21:52:19 +0000112 c == '\n')
113 term_flush();
114 }
115}
116
117void term_vprintf(const char *fmt, va_list ap)
118{
119 char buf[4096];
120 vsnprintf(buf, sizeof(buf), fmt, ap);
121 term_puts(buf);
122}
123
124void term_printf(const char *fmt, ...)
125{
126 va_list ap;
127 va_start(ap, fmt);
128 term_vprintf(fmt, ap);
129 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000130}
131
thsfef30742006-12-22 14:11:32 +0000132void term_print_filename(const char *filename)
133{
134 int i;
135
136 for (i = 0; filename[i]; i++) {
137 switch (filename[i]) {
138 case ' ':
139 case '"':
140 case '\\':
141 term_printf("\\%c", filename[i]);
142 break;
143 case '\t':
144 term_printf("\\t");
145 break;
146 case '\r':
147 term_printf("\\r");
148 break;
149 case '\n':
150 term_printf("\\n");
151 break;
152 default:
153 term_printf("%c", filename[i]);
154 break;
155 }
156 }
157}
158
bellard7fe48482004-10-09 18:08:01 +0000159static int monitor_fprintf(FILE *stream, const char *fmt, ...)
160{
161 va_list ap;
162 va_start(ap, fmt);
163 term_vprintf(fmt, ap);
164 va_end(ap);
165 return 0;
166}
167
bellard9dc39cb2004-03-14 21:38:27 +0000168static int compare_cmd(const char *name, const char *list)
169{
170 const char *p, *pstart;
171 int len;
172 len = strlen(name);
173 p = list;
174 for(;;) {
175 pstart = p;
176 p = strchr(p, '|');
177 if (!p)
178 p = pstart + strlen(pstart);
179 if ((p - pstart) == len && !memcmp(pstart, name, len))
180 return 1;
181 if (*p == '\0')
182 break;
183 p++;
184 }
185 return 0;
186}
187
blueswir18662d652008-10-02 18:32:44 +0000188static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000189{
blueswir18662d652008-10-02 18:32:44 +0000190 const term_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000191
192 for(cmd = cmds; cmd->name != NULL; cmd++) {
193 if (!name || !strcmp(name, cmd->name))
194 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
195 }
196}
197
198static void help_cmd(const char *name)
199{
200 if (name && !strcmp(name, "info")) {
201 help_cmd1(info_cmds, "info ", NULL);
202 } else {
203 help_cmd1(term_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000204 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000205 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +0000206 term_printf("Log items (comma separated):\n");
207 term_printf("%-10s %s\n", "none", "remove all logs");
208 for(item = cpu_log_items; item->mask != 0; item++) {
209 term_printf("%-10s %s\n", item->name, item->help);
210 }
211 }
bellard9dc39cb2004-03-14 21:38:27 +0000212 }
213}
214
bellard9307c4c2004-04-04 12:57:25 +0000215static void do_help(const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000216{
bellard9307c4c2004-04-04 12:57:25 +0000217 help_cmd(name);
bellard9dc39cb2004-03-14 21:38:27 +0000218}
219
bellard7954c732006-08-01 15:52:40 +0000220static void do_commit(const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000221{
bellard7954c732006-08-01 15:52:40 +0000222 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000223
bellard7954c732006-08-01 15:52:40 +0000224 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000225 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000226 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000227 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
228 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000229 }
230}
231
bellard9307c4c2004-04-04 12:57:25 +0000232static void do_info(const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000233{
blueswir18662d652008-10-02 18:32:44 +0000234 const term_cmd_t *cmd;
blueswir1a5f1b962008-08-17 20:21:51 +0000235 void (*handler)(void);
bellard9dc39cb2004-03-14 21:38:27 +0000236
bellard9307c4c2004-04-04 12:57:25 +0000237 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000238 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000239 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000240 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000241 goto found;
242 }
243 help:
bellard9307c4c2004-04-04 12:57:25 +0000244 help_cmd("info");
bellard9dc39cb2004-03-14 21:38:27 +0000245 return;
246 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000247 handler = cmd->handler;
248 handler();
bellard9dc39cb2004-03-14 21:38:27 +0000249}
250
bellard9bc9d1c2004-10-10 15:15:51 +0000251static void do_info_version(void)
252{
253 term_printf("%s\n", QEMU_VERSION);
254}
255
thsc35734b2007-03-19 15:17:08 +0000256static void do_info_name(void)
257{
258 if (qemu_name)
259 term_printf("%s\n", qemu_name);
260}
261
aurel32bf4f74c2008-12-18 22:42:34 +0000262#if defined(TARGET_I386)
aliguori16b29ae2008-12-17 23:28:44 +0000263static void do_info_hpet(void)
264{
265 term_printf("HPET is %s by QEMU\n", (no_hpet) ? "disabled" : "enabled");
266}
aurel32bf4f74c2008-12-18 22:42:34 +0000267#endif
aliguori16b29ae2008-12-17 23:28:44 +0000268
blueswir1f1f23ad2008-09-18 18:30:20 +0000269static void do_info_uuid(void)
270{
271 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
272 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
273 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
274 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
275 qemu_uuid[15]);
276}
277
bellard9307c4c2004-04-04 12:57:25 +0000278static void do_info_block(void)
bellard9dc39cb2004-03-14 21:38:27 +0000279{
280 bdrv_info();
281}
282
thsa36e69d2007-12-02 05:18:19 +0000283static void do_info_blockstats(void)
284{
285 bdrv_info_stats();
286}
287
bellard6a00d602005-11-21 23:25:50 +0000288/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000289static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000290{
291 CPUState *env;
292
293 for(env = first_cpu; env != NULL; env = env->next_cpu) {
294 if (env->cpu_index == cpu_index) {
295 mon_cpu = env;
296 return 0;
297 }
298 }
299 return -1;
300}
301
pbrook9596ebb2007-11-18 01:44:38 +0000302static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000303{
304 if (!mon_cpu) {
305 mon_set_cpu(0);
306 }
307 return mon_cpu;
308}
309
bellard9307c4c2004-04-04 12:57:25 +0000310static void do_info_registers(void)
311{
bellard6a00d602005-11-21 23:25:50 +0000312 CPUState *env;
313 env = mon_get_cpu();
314 if (!env)
315 return;
bellard9307c4c2004-04-04 12:57:25 +0000316#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000317 cpu_dump_state(env, NULL, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000318 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000319#else
ths5fafdf22007-09-16 21:08:06 +0000320 cpu_dump_state(env, NULL, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000321 0);
bellard9307c4c2004-04-04 12:57:25 +0000322#endif
323}
324
bellard6a00d602005-11-21 23:25:50 +0000325static void do_info_cpus(void)
326{
327 CPUState *env;
328
329 /* just to set the default cpu if not already done */
330 mon_get_cpu();
331
332 for(env = first_cpu; env != NULL; env = env->next_cpu) {
ths5fafdf22007-09-16 21:08:06 +0000333 term_printf("%c CPU #%d:",
bellard6a00d602005-11-21 23:25:50 +0000334 (env == mon_cpu) ? '*' : ' ',
335 env->cpu_index);
336#if defined(TARGET_I386)
337 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000338#elif defined(TARGET_PPC)
339 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000340#elif defined(TARGET_SPARC)
341 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000342#elif defined(TARGET_MIPS)
thsb5dc7732008-06-27 10:02:35 +0000343 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000344#endif
thsead93602007-09-06 00:18:15 +0000345 if (env->halted)
346 term_printf(" (halted)");
bellard6a00d602005-11-21 23:25:50 +0000347 term_printf("\n");
348 }
349}
350
351static void do_cpu_set(int index)
352{
353 if (mon_set_cpu(index) < 0)
354 term_printf("Invalid CPU index\n");
355}
356
bellarde3db7222005-01-26 22:00:47 +0000357static void do_info_jit(void)
358{
359 dump_exec_info(NULL, monitor_fprintf);
360}
361
bellardaa455482004-04-04 13:07:25 +0000362static void do_info_history (void)
363{
364 int i;
bellard7e2515e2004-08-01 21:52:19 +0000365 const char *str;
ths3b46e622007-09-17 08:09:54 +0000366
bellard7e2515e2004-08-01 21:52:19 +0000367 i = 0;
368 for(;;) {
369 str = readline_get_history(i);
370 if (!str)
371 break;
372 term_printf("%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000373 i++;
bellardaa455482004-04-04 13:07:25 +0000374 }
375}
376
j_mayer76a66252007-03-07 08:32:30 +0000377#if defined(TARGET_PPC)
378/* XXX: not implemented in other targets */
379static void do_info_cpu_stats (void)
380{
381 CPUState *env;
382
383 env = mon_get_cpu();
384 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
385}
386#endif
387
bellard9307c4c2004-04-04 12:57:25 +0000388static void do_quit(void)
bellard9dc39cb2004-03-14 21:38:27 +0000389{
390 exit(0);
391}
392
393static int eject_device(BlockDriverState *bs, int force)
394{
395 if (bdrv_is_inserted(bs)) {
396 if (!force) {
397 if (!bdrv_is_removable(bs)) {
398 term_printf("device is not removable\n");
399 return -1;
400 }
401 if (bdrv_is_locked(bs)) {
402 term_printf("device is locked\n");
403 return -1;
404 }
405 }
406 bdrv_close(bs);
407 }
408 return 0;
409}
410
bellard9307c4c2004-04-04 12:57:25 +0000411static void do_eject(int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000412{
413 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000414
bellard9307c4c2004-04-04 12:57:25 +0000415 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000416 if (!bs) {
417 term_printf("device not found\n");
418 return;
419 }
420 eject_device(bs, force);
421}
422
aurel322ecea9b2008-06-18 22:10:01 +0000423static void do_change_block(const char *device, const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000424{
425 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000426 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000427
bellard9307c4c2004-04-04 12:57:25 +0000428 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000429 if (!bs) {
430 term_printf("device not found\n");
431 return;
432 }
aurel322ecea9b2008-06-18 22:10:01 +0000433 if (fmt) {
434 drv = bdrv_find_format(fmt);
435 if (!drv) {
436 term_printf("invalid format %s\n", fmt);
437 return;
438 }
439 }
bellard9dc39cb2004-03-14 21:38:27 +0000440 if (eject_device(bs, 0) < 0)
441 return;
aurel322ecea9b2008-06-18 22:10:01 +0000442 bdrv_open2(bs, filename, 0, drv);
aliguoribb5fc202009-03-05 23:01:15 +0000443 monitor_read_bdrv_key_start(bs, NULL, NULL);
444}
445
446static void change_vnc_password_cb(void *opaque, const char *password)
447{
448 if (vnc_display_password(NULL, password) < 0)
449 term_printf("could not set VNC server password\n");
450
451 monitor_start_input();
bellard9dc39cb2004-03-14 21:38:27 +0000452}
453
aliguori2569da02008-12-10 15:14:13 +0000454static void do_change_vnc(const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000455{
ths70848512007-08-25 01:37:05 +0000456 if (strcmp(target, "passwd") == 0 ||
457 strcmp(target, "password") == 0) {
aliguori2569da02008-12-10 15:14:13 +0000458 if (arg) {
aliguoribb5fc202009-03-05 23:01:15 +0000459 char password[9];
aliguori2569da02008-12-10 15:14:13 +0000460 strncpy(password, arg, sizeof(password));
461 password[sizeof(password) - 1] = '\0';
aliguoribb5fc202009-03-05 23:01:15 +0000462 change_vnc_password_cb(NULL, password);
463 } else {
464 monitor_read_password(change_vnc_password_cb, NULL);
465 }
ths70848512007-08-25 01:37:05 +0000466 } else {
467 if (vnc_display_open(NULL, target) < 0)
468 term_printf("could not start VNC server on %s\n", target);
469 }
thse25a5822007-08-25 01:36:20 +0000470}
471
aliguori2569da02008-12-10 15:14:13 +0000472static void do_change(const char *device, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000473{
474 if (strcmp(device, "vnc") == 0) {
aliguori2569da02008-12-10 15:14:13 +0000475 do_change_vnc(target, arg);
thse25a5822007-08-25 01:36:20 +0000476 } else {
aliguori2569da02008-12-10 15:14:13 +0000477 do_change_block(device, target, arg);
thse25a5822007-08-25 01:36:20 +0000478 }
479}
480
bellard9307c4c2004-04-04 12:57:25 +0000481static void do_screen_dump(const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000482{
pbrook95219892006-04-09 01:06:34 +0000483 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000484}
485
pbrooke735b912007-06-30 13:53:24 +0000486static void do_logfile(const char *filename)
487{
488 cpu_set_log_filename(filename);
489}
490
bellard9307c4c2004-04-04 12:57:25 +0000491static void do_log(const char *items)
bellardf193c792004-03-21 17:06:25 +0000492{
493 int mask;
ths3b46e622007-09-17 08:09:54 +0000494
bellard9307c4c2004-04-04 12:57:25 +0000495 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000496 mask = 0;
497 } else {
bellard9307c4c2004-04-04 12:57:25 +0000498 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000499 if (!mask) {
bellard9307c4c2004-04-04 12:57:25 +0000500 help_cmd("log");
bellardf193c792004-03-21 17:06:25 +0000501 return;
502 }
503 }
504 cpu_set_log(mask);
505}
506
bellard9307c4c2004-04-04 12:57:25 +0000507static void do_stop(void)
bellard8a7ddc32004-03-31 19:00:16 +0000508{
509 vm_stop(EXCP_INTERRUPT);
510}
511
aliguoribb5fc202009-03-05 23:01:15 +0000512static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
aliguoric0f4ce72009-03-05 23:01:01 +0000513
bellard9307c4c2004-04-04 12:57:25 +0000514static void do_cont(void)
bellard8a7ddc32004-03-31 19:00:16 +0000515{
aliguoric0f4ce72009-03-05 23:01:01 +0000516 int err = 0;
517
518 bdrv_iterate(encrypted_bdrv_it, &err);
519 /* only resume the vm if all keys are set and valid */
520 if (!err)
521 vm_start();
bellard8a7ddc32004-03-31 19:00:16 +0000522}
523
aliguoribb5fc202009-03-05 23:01:15 +0000524static void bdrv_key_cb(void *opaque, int err)
525{
526 /* another key was set successfully, retry to continue */
527 if (!err)
528 do_cont();
529}
530
531static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
532{
533 int *err = opaque;
534
535 if (!*err && bdrv_key_required(bs)) {
536 *err = -EBUSY;
537 monitor_read_bdrv_key_start(bs, bdrv_key_cb, NULL);
538 }
539}
540
bellard67b915a2004-03-31 23:37:16 +0000541#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +0000542static void do_gdbserver(const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000543{
pbrookcfc34752007-02-22 01:48:01 +0000544 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000545 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000546 if (gdbserver_start(port) < 0) {
547 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000548 } else {
pbrookcfc34752007-02-22 01:48:01 +0000549 qemu_printf("Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000550 }
551}
bellard67b915a2004-03-31 23:37:16 +0000552#endif
bellard8a7ddc32004-03-31 19:00:16 +0000553
bellard9307c4c2004-04-04 12:57:25 +0000554static void term_printc(int c)
555{
556 term_printf("'");
557 switch(c) {
558 case '\'':
559 term_printf("\\'");
560 break;
561 case '\\':
562 term_printf("\\\\");
563 break;
564 case '\n':
565 term_printf("\\n");
566 break;
567 case '\r':
568 term_printf("\\r");
569 break;
570 default:
571 if (c >= 32 && c <= 126) {
572 term_printf("%c", c);
573 } else {
574 term_printf("\\x%02x", c);
575 }
576 break;
577 }
578 term_printf("'");
579}
580
ths5fafdf22007-09-16 21:08:06 +0000581static void memory_dump(int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000582 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000583{
bellard6a00d602005-11-21 23:25:50 +0000584 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000585 int nb_per_line, l, line_size, i, max_digits, len;
586 uint8_t buf[16];
587 uint64_t v;
588
589 if (format == 'i') {
590 int flags;
591 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000592 env = mon_get_cpu();
593 if (!env && !is_physical)
594 return;
bellard9307c4c2004-04-04 12:57:25 +0000595#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000596 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000597 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000598 } else if (wsize == 4) {
599 flags = 0;
600 } else {
bellard6a15fd12006-04-12 21:07:07 +0000601 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000602 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000603 if (env) {
604#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000605 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000606 (env->segs[R_CS].flags & DESC_L_MASK))
607 flags = 2;
608 else
609#endif
610 if (!(env->segs[R_CS].flags & DESC_B_MASK))
611 flags = 1;
612 }
bellard4c27ba22004-04-25 18:05:08 +0000613 }
614#endif
bellard6a00d602005-11-21 23:25:50 +0000615 monitor_disas(env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000616 return;
617 }
618
619 len = wsize * count;
620 if (wsize == 1)
621 line_size = 8;
622 else
623 line_size = 16;
624 nb_per_line = line_size / wsize;
625 max_digits = 0;
626
627 switch(format) {
628 case 'o':
629 max_digits = (wsize * 8 + 2) / 3;
630 break;
631 default:
632 case 'x':
633 max_digits = (wsize * 8) / 4;
634 break;
635 case 'u':
636 case 'd':
637 max_digits = (wsize * 8 * 10 + 32) / 33;
638 break;
639 case 'c':
640 wsize = 1;
641 break;
642 }
643
644 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000645 if (is_physical)
646 term_printf(TARGET_FMT_plx ":", addr);
647 else
648 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000649 l = len;
650 if (l > line_size)
651 l = line_size;
652 if (is_physical) {
653 cpu_physical_memory_rw(addr, buf, l, 0);
654 } else {
bellard6a00d602005-11-21 23:25:50 +0000655 env = mon_get_cpu();
656 if (!env)
657 break;
aliguoric8f79b62008-08-18 14:00:20 +0000658 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
659 term_printf(" Cannot access memory\n");
660 break;
661 }
bellard9307c4c2004-04-04 12:57:25 +0000662 }
ths5fafdf22007-09-16 21:08:06 +0000663 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000664 while (i < l) {
665 switch(wsize) {
666 default:
667 case 1:
668 v = ldub_raw(buf + i);
669 break;
670 case 2:
671 v = lduw_raw(buf + i);
672 break;
673 case 4:
bellard92a31b12005-02-10 22:00:52 +0000674 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000675 break;
676 case 8:
677 v = ldq_raw(buf + i);
678 break;
679 }
680 term_printf(" ");
681 switch(format) {
682 case 'o':
bellard26a76462006-06-25 18:15:32 +0000683 term_printf("%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000684 break;
685 case 'x':
bellard26a76462006-06-25 18:15:32 +0000686 term_printf("0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000687 break;
688 case 'u':
bellard26a76462006-06-25 18:15:32 +0000689 term_printf("%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000690 break;
691 case 'd':
bellard26a76462006-06-25 18:15:32 +0000692 term_printf("%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000693 break;
694 case 'c':
695 term_printc(v);
696 break;
697 }
698 i += wsize;
699 }
700 term_printf("\n");
701 addr += l;
702 len -= l;
703 }
704}
705
bellard92a31b12005-02-10 22:00:52 +0000706#if TARGET_LONG_BITS == 64
707#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
708#else
709#define GET_TLONG(h, l) (l)
710#endif
711
ths5fafdf22007-09-16 21:08:06 +0000712static void do_memory_dump(int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000713 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000714{
bellard92a31b12005-02-10 22:00:52 +0000715 target_long addr = GET_TLONG(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000716 memory_dump(count, format, size, addr, 0);
717}
718
blueswir17743e582007-09-24 18:39:04 +0000719#if TARGET_PHYS_ADDR_BITS > 32
720#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
721#else
722#define GET_TPHYSADDR(h, l) (l)
723#endif
724
bellard92a31b12005-02-10 22:00:52 +0000725static void do_physical_memory_dump(int count, int format, int size,
726 uint32_t addrh, uint32_t addrl)
727
bellard9307c4c2004-04-04 12:57:25 +0000728{
blueswir17743e582007-09-24 18:39:04 +0000729 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000730 memory_dump(count, format, size, addr, 1);
731}
732
bellard92a31b12005-02-10 22:00:52 +0000733static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000734{
blueswir17743e582007-09-24 18:39:04 +0000735 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
736#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000737 switch(format) {
738 case 'o':
739 term_printf("%#o", val);
740 break;
741 case 'x':
742 term_printf("%#x", val);
743 break;
744 case 'u':
745 term_printf("%u", val);
746 break;
747 default:
748 case 'd':
749 term_printf("%d", val);
750 break;
751 case 'c':
752 term_printc(val);
753 break;
754 }
bellard92a31b12005-02-10 22:00:52 +0000755#else
756 switch(format) {
757 case 'o':
bellard26a76462006-06-25 18:15:32 +0000758 term_printf("%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000759 break;
760 case 'x':
bellard26a76462006-06-25 18:15:32 +0000761 term_printf("%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000762 break;
763 case 'u':
bellard26a76462006-06-25 18:15:32 +0000764 term_printf("%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000765 break;
766 default:
767 case 'd':
bellard26a76462006-06-25 18:15:32 +0000768 term_printf("%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000769 break;
770 case 'c':
771 term_printc(val);
772 break;
773 }
774#endif
bellard9307c4c2004-04-04 12:57:25 +0000775 term_printf("\n");
776}
777
ths5fafdf22007-09-16 21:08:06 +0000778static void do_memory_save(unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000779 uint32_t size, const char *filename)
780{
781 FILE *f;
782 target_long addr = GET_TLONG(valh, vall);
783 uint32_t l;
784 CPUState *env;
785 uint8_t buf[1024];
786
787 env = mon_get_cpu();
788 if (!env)
789 return;
790
791 f = fopen(filename, "wb");
792 if (!f) {
793 term_printf("could not open '%s'\n", filename);
794 return;
795 }
796 while (size != 0) {
797 l = sizeof(buf);
798 if (l > size)
799 l = size;
800 cpu_memory_rw_debug(env, addr, buf, l, 0);
801 fwrite(buf, 1, l, f);
802 addr += l;
803 size -= l;
804 }
805 fclose(f);
806}
807
aurel32a8bdf7a2008-04-11 21:36:14 +0000808static void do_physical_memory_save(unsigned int valh, unsigned int vall,
809 uint32_t size, const char *filename)
810{
811 FILE *f;
812 uint32_t l;
813 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000814 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000815
816 f = fopen(filename, "wb");
817 if (!f) {
818 term_printf("could not open '%s'\n", filename);
819 return;
820 }
821 while (size != 0) {
822 l = sizeof(buf);
823 if (l > size)
824 l = size;
825 cpu_physical_memory_rw(addr, buf, l, 0);
826 fwrite(buf, 1, l, f);
827 fflush(f);
828 addr += l;
829 size -= l;
830 }
831 fclose(f);
832}
833
bellarde4cf1ad2005-06-04 20:15:57 +0000834static void do_sum(uint32_t start, uint32_t size)
835{
836 uint32_t addr;
837 uint8_t buf[1];
838 uint16_t sum;
839
840 sum = 0;
841 for(addr = start; addr < (start + size); addr++) {
842 cpu_physical_memory_rw(addr, buf, 1, 0);
843 /* BSD sum algorithm ('sum' Unix command) */
844 sum = (sum >> 1) | (sum << 15);
845 sum += buf[0];
846 }
847 term_printf("%05d\n", sum);
848}
849
bellarda3a91a32004-06-04 11:06:21 +0000850typedef struct {
851 int keycode;
852 const char *name;
853} KeyDef;
854
855static const KeyDef key_defs[] = {
856 { 0x2a, "shift" },
857 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000858
bellarda3a91a32004-06-04 11:06:21 +0000859 { 0x38, "alt" },
860 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000861 { 0x64, "altgr" },
862 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000863 { 0x1d, "ctrl" },
864 { 0x9d, "ctrl_r" },
865
866 { 0xdd, "menu" },
867
868 { 0x01, "esc" },
869
870 { 0x02, "1" },
871 { 0x03, "2" },
872 { 0x04, "3" },
873 { 0x05, "4" },
874 { 0x06, "5" },
875 { 0x07, "6" },
876 { 0x08, "7" },
877 { 0x09, "8" },
878 { 0x0a, "9" },
879 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000880 { 0x0c, "minus" },
881 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000882 { 0x0e, "backspace" },
883
884 { 0x0f, "tab" },
885 { 0x10, "q" },
886 { 0x11, "w" },
887 { 0x12, "e" },
888 { 0x13, "r" },
889 { 0x14, "t" },
890 { 0x15, "y" },
891 { 0x16, "u" },
892 { 0x17, "i" },
893 { 0x18, "o" },
894 { 0x19, "p" },
895
896 { 0x1c, "ret" },
897
898 { 0x1e, "a" },
899 { 0x1f, "s" },
900 { 0x20, "d" },
901 { 0x21, "f" },
902 { 0x22, "g" },
903 { 0x23, "h" },
904 { 0x24, "j" },
905 { 0x25, "k" },
906 { 0x26, "l" },
907
908 { 0x2c, "z" },
909 { 0x2d, "x" },
910 { 0x2e, "c" },
911 { 0x2f, "v" },
912 { 0x30, "b" },
913 { 0x31, "n" },
914 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000915 { 0x33, "comma" },
916 { 0x34, "dot" },
917 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000918
balrog4d3b6f62008-02-10 16:33:14 +0000919 { 0x37, "asterisk" },
920
bellarda3a91a32004-06-04 11:06:21 +0000921 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000922 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000923 { 0x3b, "f1" },
924 { 0x3c, "f2" },
925 { 0x3d, "f3" },
926 { 0x3e, "f4" },
927 { 0x3f, "f5" },
928 { 0x40, "f6" },
929 { 0x41, "f7" },
930 { 0x42, "f8" },
931 { 0x43, "f9" },
932 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000933 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000934 { 0x46, "scroll_lock" },
935
bellard64866c32006-05-07 18:03:31 +0000936 { 0xb5, "kp_divide" },
937 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000938 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000939 { 0x4e, "kp_add" },
940 { 0x9c, "kp_enter" },
941 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000942 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000943
944 { 0x52, "kp_0" },
945 { 0x4f, "kp_1" },
946 { 0x50, "kp_2" },
947 { 0x51, "kp_3" },
948 { 0x4b, "kp_4" },
949 { 0x4c, "kp_5" },
950 { 0x4d, "kp_6" },
951 { 0x47, "kp_7" },
952 { 0x48, "kp_8" },
953 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000954
bellarda3a91a32004-06-04 11:06:21 +0000955 { 0x56, "<" },
956
957 { 0x57, "f11" },
958 { 0x58, "f12" },
959
960 { 0xb7, "print" },
961
962 { 0xc7, "home" },
963 { 0xc9, "pgup" },
964 { 0xd1, "pgdn" },
965 { 0xcf, "end" },
966
967 { 0xcb, "left" },
968 { 0xc8, "up" },
969 { 0xd0, "down" },
970 { 0xcd, "right" },
971
972 { 0xd2, "insert" },
973 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +0000974#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
975 { 0xf0, "stop" },
976 { 0xf1, "again" },
977 { 0xf2, "props" },
978 { 0xf3, "undo" },
979 { 0xf4, "front" },
980 { 0xf5, "copy" },
981 { 0xf6, "open" },
982 { 0xf7, "paste" },
983 { 0xf8, "find" },
984 { 0xf9, "cut" },
985 { 0xfa, "lf" },
986 { 0xfb, "help" },
987 { 0xfc, "meta_l" },
988 { 0xfd, "meta_r" },
989 { 0xfe, "compose" },
990#endif
bellarda3a91a32004-06-04 11:06:21 +0000991 { 0, NULL },
992};
993
994static int get_keycode(const char *key)
995{
996 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +0000997 char *endp;
998 int ret;
bellarda3a91a32004-06-04 11:06:21 +0000999
1000 for(p = key_defs; p->name != NULL; p++) {
1001 if (!strcmp(key, p->name))
1002 return p->keycode;
1003 }
bellard64866c32006-05-07 18:03:31 +00001004 if (strstart(key, "0x", NULL)) {
1005 ret = strtoul(key, &endp, 0);
1006 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1007 return ret;
1008 }
bellarda3a91a32004-06-04 11:06:21 +00001009 return -1;
1010}
1011
balrogc8256f92008-06-08 22:45:01 +00001012#define MAX_KEYCODES 16
1013static uint8_t keycodes[MAX_KEYCODES];
1014static int nb_pending_keycodes;
1015static QEMUTimer *key_timer;
1016
1017static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +00001018{
balrogc8256f92008-06-08 22:45:01 +00001019 int keycode;
1020
1021 while (nb_pending_keycodes > 0) {
1022 nb_pending_keycodes--;
1023 keycode = keycodes[nb_pending_keycodes];
1024 if (keycode & 0x80)
1025 kbd_put_keycode(0xe0);
1026 kbd_put_keycode(keycode | 0x80);
1027 }
1028}
1029
1030static void do_sendkey(const char *string, int has_hold_time, int hold_time)
1031{
balrog3401c0d2008-06-04 10:05:59 +00001032 char keyname_buf[16];
1033 char *separator;
1034 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +00001035
balrogc8256f92008-06-08 22:45:01 +00001036 if (nb_pending_keycodes > 0) {
1037 qemu_del_timer(key_timer);
1038 release_keys(NULL);
1039 }
1040 if (!has_hold_time)
1041 hold_time = 100;
1042 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001043 while (1) {
1044 separator = strchr(string, '-');
1045 keyname_len = separator ? separator - string : strlen(string);
1046 if (keyname_len > 0) {
1047 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1048 if (keyname_len > sizeof(keyname_buf) - 1) {
1049 term_printf("invalid key: '%s...'\n", keyname_buf);
1050 return;
bellarda3a91a32004-06-04 11:06:21 +00001051 }
balrogc8256f92008-06-08 22:45:01 +00001052 if (i == MAX_KEYCODES) {
balrog3401c0d2008-06-04 10:05:59 +00001053 term_printf("too many keys\n");
1054 return;
1055 }
1056 keyname_buf[keyname_len] = 0;
1057 keycode = get_keycode(keyname_buf);
1058 if (keycode < 0) {
1059 term_printf("unknown key: '%s'\n", keyname_buf);
1060 return;
1061 }
balrogc8256f92008-06-08 22:45:01 +00001062 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001063 }
balrog3401c0d2008-06-04 10:05:59 +00001064 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001065 break;
balrog3401c0d2008-06-04 10:05:59 +00001066 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001067 }
balrogc8256f92008-06-08 22:45:01 +00001068 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001069 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001070 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001071 keycode = keycodes[i];
1072 if (keycode & 0x80)
1073 kbd_put_keycode(0xe0);
1074 kbd_put_keycode(keycode & 0x7f);
1075 }
balrogc8256f92008-06-08 22:45:01 +00001076 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001077 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1078 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001079}
1080
bellard13224a82006-07-14 22:03:35 +00001081static int mouse_button_state;
1082
ths5fafdf22007-09-16 21:08:06 +00001083static void do_mouse_move(const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001084 const char *dz_str)
1085{
1086 int dx, dy, dz;
1087 dx = strtol(dx_str, NULL, 0);
1088 dy = strtol(dy_str, NULL, 0);
1089 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001090 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001091 dz = strtol(dz_str, NULL, 0);
1092 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1093}
1094
1095static void do_mouse_button(int button_state)
1096{
1097 mouse_button_state = button_state;
1098 kbd_mouse_event(0, 0, 0, mouse_button_state);
1099}
1100
bellard34405572004-06-08 00:55:58 +00001101static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1102{
1103 uint32_t val;
1104 int suffix;
1105
1106 if (has_index) {
1107 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1108 addr++;
1109 }
1110 addr &= 0xffff;
1111
1112 switch(size) {
1113 default:
1114 case 1:
1115 val = cpu_inb(NULL, addr);
1116 suffix = 'b';
1117 break;
1118 case 2:
1119 val = cpu_inw(NULL, addr);
1120 suffix = 'w';
1121 break;
1122 case 4:
1123 val = cpu_inl(NULL, addr);
1124 suffix = 'l';
1125 break;
1126 }
1127 term_printf("port%c[0x%04x] = %#0*x\n",
1128 suffix, addr, size * 2, val);
1129}
bellarda3a91a32004-06-04 11:06:21 +00001130
blueswir13b4366d2008-06-20 16:25:06 +00001131/* boot_set handler */
1132static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1133static void *boot_opaque;
1134
1135void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1136{
1137 qemu_boot_set_handler = func;
1138 boot_opaque = opaque;
1139}
1140
aurel320ecdffb2008-05-04 20:11:34 +00001141static void do_boot_set(const char *bootdevice)
1142{
1143 int res;
1144
1145 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001146 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001147 if (res == 0)
1148 term_printf("boot device list now set to %s\n", bootdevice);
1149 else
1150 term_printf("setting boot device list failed with error %i\n", res);
1151 } else {
1152 term_printf("no function defined to set boot device list for this architecture\n");
1153 }
1154}
1155
bellarde4f90822004-06-20 12:35:44 +00001156static void do_system_reset(void)
1157{
1158 qemu_system_reset_request();
1159}
1160
bellard34751872005-07-02 14:31:34 +00001161static void do_system_powerdown(void)
1162{
1163 qemu_system_powerdown_request();
1164}
1165
bellardb86bda52004-09-18 19:32:46 +00001166#if defined(TARGET_I386)
1167static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1168{
ths5fafdf22007-09-16 21:08:06 +00001169 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
bellardb86bda52004-09-18 19:32:46 +00001170 addr,
1171 pte & mask,
1172 pte & PG_GLOBAL_MASK ? 'G' : '-',
1173 pte & PG_PSE_MASK ? 'P' : '-',
1174 pte & PG_DIRTY_MASK ? 'D' : '-',
1175 pte & PG_ACCESSED_MASK ? 'A' : '-',
1176 pte & PG_PCD_MASK ? 'C' : '-',
1177 pte & PG_PWT_MASK ? 'T' : '-',
1178 pte & PG_USER_MASK ? 'U' : '-',
1179 pte & PG_RW_MASK ? 'W' : '-');
1180}
1181
1182static void tlb_info(void)
1183{
bellard6a00d602005-11-21 23:25:50 +00001184 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001185 int l1, l2;
1186 uint32_t pgd, pde, pte;
1187
bellard6a00d602005-11-21 23:25:50 +00001188 env = mon_get_cpu();
1189 if (!env)
1190 return;
1191
bellardb86bda52004-09-18 19:32:46 +00001192 if (!(env->cr[0] & CR0_PG_MASK)) {
1193 term_printf("PG disabled\n");
1194 return;
1195 }
1196 pgd = env->cr[3] & ~0xfff;
1197 for(l1 = 0; l1 < 1024; l1++) {
1198 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1199 pde = le32_to_cpu(pde);
1200 if (pde & PG_PRESENT_MASK) {
1201 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1202 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1203 } else {
1204 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001205 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001206 (uint8_t *)&pte, 4);
1207 pte = le32_to_cpu(pte);
1208 if (pte & PG_PRESENT_MASK) {
ths5fafdf22007-09-16 21:08:06 +00001209 print_pte((l1 << 22) + (l2 << 12),
1210 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001211 ~0xfff);
1212 }
1213 }
1214 }
1215 }
1216 }
1217}
1218
ths5fafdf22007-09-16 21:08:06 +00001219static void mem_print(uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001220 uint32_t end, int prot)
1221{
bellard9746b152004-11-11 18:30:24 +00001222 int prot1;
1223 prot1 = *plast_prot;
1224 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001225 if (*pstart != -1) {
1226 term_printf("%08x-%08x %08x %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001227 *pstart, end, end - *pstart,
bellard9746b152004-11-11 18:30:24 +00001228 prot1 & PG_USER_MASK ? 'u' : '-',
bellardb86bda52004-09-18 19:32:46 +00001229 'r',
bellard9746b152004-11-11 18:30:24 +00001230 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001231 }
1232 if (prot != 0)
1233 *pstart = end;
1234 else
1235 *pstart = -1;
1236 *plast_prot = prot;
1237 }
1238}
1239
1240static void mem_info(void)
1241{
bellard6a00d602005-11-21 23:25:50 +00001242 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001243 int l1, l2, prot, last_prot;
1244 uint32_t pgd, pde, pte, start, end;
1245
bellard6a00d602005-11-21 23:25:50 +00001246 env = mon_get_cpu();
1247 if (!env)
1248 return;
1249
bellardb86bda52004-09-18 19:32:46 +00001250 if (!(env->cr[0] & CR0_PG_MASK)) {
1251 term_printf("PG disabled\n");
1252 return;
1253 }
1254 pgd = env->cr[3] & ~0xfff;
1255 last_prot = 0;
1256 start = -1;
1257 for(l1 = 0; l1 < 1024; l1++) {
1258 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1259 pde = le32_to_cpu(pde);
1260 end = l1 << 22;
1261 if (pde & PG_PRESENT_MASK) {
1262 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1263 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1264 mem_print(&start, &last_prot, end, prot);
1265 } else {
1266 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001267 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001268 (uint8_t *)&pte, 4);
1269 pte = le32_to_cpu(pte);
1270 end = (l1 << 22) + (l2 << 12);
1271 if (pte & PG_PRESENT_MASK) {
1272 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1273 } else {
1274 prot = 0;
1275 }
1276 mem_print(&start, &last_prot, end, prot);
1277 }
1278 }
1279 } else {
1280 prot = 0;
1281 mem_print(&start, &last_prot, end, prot);
1282 }
1283 }
1284}
1285#endif
1286
aurel327c664e22009-03-03 06:12:22 +00001287#if defined(TARGET_SH4)
1288
1289static void print_tlb(int idx, tlb_t *tlb)
1290{
1291 term_printf(" tlb%i:\t"
1292 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1293 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1294 "dirty=%hhu writethrough=%hhu\n",
1295 idx,
1296 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1297 tlb->v, tlb->sh, tlb->c, tlb->pr,
1298 tlb->d, tlb->wt);
1299}
1300
1301static void tlb_info(void)
1302{
1303 CPUState *env = mon_get_cpu();
1304 int i;
1305
1306 term_printf ("ITLB:\n");
1307 for (i = 0 ; i < ITLB_SIZE ; i++)
1308 print_tlb (i, &env->itlb[i]);
1309 term_printf ("UTLB:\n");
1310 for (i = 0 ; i < UTLB_SIZE ; i++)
1311 print_tlb (i, &env->utlb[i]);
1312}
1313
1314#endif
1315
bellard0f4c6412005-07-24 18:10:56 +00001316static void do_info_kqemu(void)
1317{
1318#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001319 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001320 int val;
1321 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001322 env = mon_get_cpu();
1323 if (!env) {
1324 term_printf("No cpu initialized yet");
1325 return;
1326 }
1327 val = env->kqemu_enabled;
bellard5f1ce942006-02-08 22:40:15 +00001328 term_printf("kqemu support: ");
1329 switch(val) {
1330 default:
1331 case 0:
1332 term_printf("disabled\n");
1333 break;
1334 case 1:
1335 term_printf("enabled for user code\n");
1336 break;
1337 case 2:
1338 term_printf("enabled for user and kernel code\n");
1339 break;
1340 }
bellard0f4c6412005-07-24 18:10:56 +00001341#else
bellard5f1ce942006-02-08 22:40:15 +00001342 term_printf("kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001343#endif
ths5fafdf22007-09-16 21:08:06 +00001344}
bellard0f4c6412005-07-24 18:10:56 +00001345
aliguori7ba1e612008-11-05 16:04:33 +00001346static void do_info_kvm(void)
1347{
1348#ifdef CONFIG_KVM
1349 term_printf("kvm support: ");
1350 if (kvm_enabled())
1351 term_printf("enabled\n");
1352 else
1353 term_printf("disabled\n");
1354#else
1355 term_printf("kvm support: not compiled\n");
1356#endif
1357}
1358
bellard5f1ce942006-02-08 22:40:15 +00001359#ifdef CONFIG_PROFILER
1360
1361int64_t kqemu_time;
1362int64_t qemu_time;
1363int64_t kqemu_exec_count;
1364int64_t dev_time;
1365int64_t kqemu_ret_int_count;
1366int64_t kqemu_ret_excp_count;
1367int64_t kqemu_ret_intr_count;
1368
1369static void do_info_profile(void)
1370{
1371 int64_t total;
1372 total = qemu_time;
1373 if (total == 0)
1374 total = 1;
bellard26a76462006-06-25 18:15:32 +00001375 term_printf("async time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001376 dev_time, dev_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001377 term_printf("qemu time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001378 qemu_time, qemu_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001379 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
bellard5f1ce942006-02-08 22:40:15 +00001380 kqemu_time, kqemu_time / (double)ticks_per_sec,
1381 kqemu_time / (double)total * 100.0,
1382 kqemu_exec_count,
1383 kqemu_ret_int_count,
1384 kqemu_ret_excp_count,
1385 kqemu_ret_intr_count);
1386 qemu_time = 0;
1387 kqemu_time = 0;
1388 kqemu_exec_count = 0;
1389 dev_time = 0;
1390 kqemu_ret_int_count = 0;
1391 kqemu_ret_excp_count = 0;
1392 kqemu_ret_intr_count = 0;
1393#ifdef USE_KQEMU
1394 kqemu_record_dump();
1395#endif
1396}
1397#else
1398static void do_info_profile(void)
1399{
1400 term_printf("Internal profiler not compiled\n");
1401}
1402#endif
1403
bellardec36b692006-07-16 18:57:03 +00001404/* Capture support */
1405static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1406
1407static void do_info_capture (void)
1408{
1409 int i;
1410 CaptureState *s;
1411
1412 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1413 term_printf ("[%d]: ", i);
1414 s->ops.info (s->opaque);
1415 }
1416}
1417
1418static void do_stop_capture (int n)
1419{
1420 int i;
1421 CaptureState *s;
1422
1423 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1424 if (i == n) {
1425 s->ops.destroy (s->opaque);
1426 LIST_REMOVE (s, entries);
1427 qemu_free (s);
1428 return;
1429 }
1430 }
1431}
1432
1433#ifdef HAS_AUDIO
bellardec36b692006-07-16 18:57:03 +00001434static void do_wav_capture (const char *path,
1435 int has_freq, int freq,
1436 int has_bits, int bits,
1437 int has_channels, int nchannels)
1438{
1439 CaptureState *s;
1440
1441 s = qemu_mallocz (sizeof (*s));
bellardec36b692006-07-16 18:57:03 +00001442
1443 freq = has_freq ? freq : 44100;
1444 bits = has_bits ? bits : 16;
1445 nchannels = has_channels ? nchannels : 2;
1446
1447 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1448 term_printf ("Faied to add wave capture\n");
1449 qemu_free (s);
1450 }
1451 LIST_INSERT_HEAD (&capture_head, s, entries);
1452}
1453#endif
1454
aurel32dc1c0b72008-04-27 23:52:12 +00001455#if defined(TARGET_I386)
1456static void do_inject_nmi(int cpu_index)
1457{
1458 CPUState *env;
1459
1460 for (env = first_cpu; env != NULL; env = env->next_cpu)
1461 if (env->cpu_index == cpu_index) {
1462 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1463 break;
1464 }
1465}
1466#endif
1467
aurel326f9c5ee2008-12-18 22:43:56 +00001468static void do_info_status(void)
1469{
1470 if (vm_running)
1471 term_printf("VM status: running\n");
1472 else
1473 term_printf("VM status: paused\n");
1474}
1475
1476
aliguoridf751fa2008-12-04 20:19:35 +00001477static void do_balloon(int value)
1478{
1479 ram_addr_t target = value;
1480 qemu_balloon(target << 20);
1481}
1482
1483static void do_info_balloon(void)
1484{
1485 ram_addr_t actual;
1486
1487 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001488 if (kvm_enabled() && !kvm_has_sync_mmu())
1489 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1490 else if (actual == 0)
aliguoridf751fa2008-12-04 20:19:35 +00001491 term_printf("Ballooning not activated in VM\n");
1492 else
1493 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1494}
1495
blueswir1d2c639d2009-01-24 18:19:25 +00001496/* Please update qemu-doc.texi when adding or changing commands */
blueswir18662d652008-10-02 18:32:44 +00001497static const term_cmd_t term_cmds[] = {
ths5fafdf22007-09-16 21:08:06 +00001498 { "help|?", "s?", do_help,
bellard9dc39cb2004-03-14 21:38:27 +00001499 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001500 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001501 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001502 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001503 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001504 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001505 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001506 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001507 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001508 { "change", "BFs?", do_change,
1509 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001510 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001511 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001512 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001513 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001514 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001515 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001516 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001517 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001518 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001519 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001520 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001521 "tag|id", "delete a VM snapshot from its tag or id" },
1522 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001523 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001524 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001525 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001526#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001527 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001528 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001529#endif
ths5fafdf22007-09-16 21:08:06 +00001530 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001531 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001532 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001533 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001534 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001535 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001536 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001537 "/fmt addr", "I/O port read" },
1538
balrogc8256f92008-06-08 22:45:01 +00001539 { "sendkey", "si?", do_sendkey,
1540 "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 +00001541 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001542 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001543 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001544 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001545 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001546 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001547 { "usb_add", "s", do_usb_add,
1548 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1549 { "usb_del", "s", do_usb_del,
1550 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001551 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001552 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001553 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001554 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001555 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001556 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001557 { "mouse_set", "i", do_mouse_set,
1558 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001559#ifdef HAS_AUDIO
1560 { "wavcapture", "si?i?i?", do_wav_capture,
1561 "path [frequency bits channels]",
1562 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1563#endif
blueswir1d2c639d2009-01-24 18:19:25 +00001564 { "stopcapture", "i", do_stop_capture,
1565 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001566 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001567 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001568 { "pmemsave", "lis", do_physical_memory_save,
1569 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001570 { "boot_set", "s", do_boot_set,
1571 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001572#if defined(TARGET_I386)
1573 { "nmi", "i", do_inject_nmi,
1574 "cpu", "inject an NMI on the given CPU", },
1575#endif
aliguori5bb79102008-10-13 03:12:02 +00001576 { "migrate", "-ds", do_migrate,
1577 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1578 { "migrate_cancel", "", do_migrate_cancel,
1579 "", "cancel the current VM migration" },
1580 { "migrate_set_speed", "s", do_migrate_set_speed,
1581 "value", "set maximum speed (in bytes) for migrations" },
aliguori6f338c32009-02-11 15:21:54 +00001582#if defined(TARGET_I386)
1583 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1584 "[file=file][,if=type][,bus=n]\n"
1585 "[,unit=m][,media=d][index=i]\n"
1586 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1587 "[snapshot=on|off][,cache=on|off]",
1588 "add drive to PCI storage controller" },
1589 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1590 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1591 { "host_net_add", "ss", net_host_device_add,
1592 "[tap,user,socket,vde] options", "add host VLAN client" },
1593 { "host_net_remove", "is", net_host_device_remove,
1594 "vlan_id name", "remove host VLAN client" },
1595#endif
aliguoridf751fa2008-12-04 20:19:35 +00001596 { "balloon", "i", do_balloon,
1597 "target", "request VM to change it's memory allocation (in MB)" },
aliguorif029bd92009-02-11 15:19:16 +00001598 { "set_link", "ss", do_set_link,
1599 "name [up|down]", "change the link status of a network adapter" },
ths5fafdf22007-09-16 21:08:06 +00001600 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001601};
1602
blueswir1d2c639d2009-01-24 18:19:25 +00001603/* Please update qemu-doc.texi when adding or changing commands */
blueswir18662d652008-10-02 18:32:44 +00001604static const term_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001605 { "version", "", do_info_version,
blueswir1d2c639d2009-01-24 18:19:25 +00001606 "", "show the version of QEMU" },
bellard9307c4c2004-04-04 12:57:25 +00001607 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001608 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001609 { "chardev", "", qemu_chr_info,
1610 "", "show the character devices" },
bellard9307c4c2004-04-04 12:57:25 +00001611 { "block", "", do_info_block,
bellard9dc39cb2004-03-14 21:38:27 +00001612 "", "show the block devices" },
thsa36e69d2007-12-02 05:18:19 +00001613 { "blockstats", "", do_info_blockstats,
1614 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001615 { "registers", "", do_info_registers,
1616 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001617 { "cpus", "", do_info_cpus,
1618 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001619 { "history", "", do_info_history,
1620 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001621 { "irq", "", irq_info,
1622 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001623 { "pic", "", pic_info,
1624 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001625 { "pci", "", pci_info,
1626 "", "show PCI info", },
aurel327c664e22009-03-03 06:12:22 +00001627#if defined(TARGET_I386) || defined(TARGET_SH4)
bellardb86bda52004-09-18 19:32:46 +00001628 { "tlb", "", tlb_info,
1629 "", "show virtual to physical memory mappings", },
aurel327c664e22009-03-03 06:12:22 +00001630#endif
1631#if defined(TARGET_I386)
bellardb86bda52004-09-18 19:32:46 +00001632 { "mem", "", mem_info,
1633 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001634 { "hpet", "", do_info_hpet,
1635 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001636#endif
bellarde3db7222005-01-26 22:00:47 +00001637 { "jit", "", do_info_jit,
1638 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001639 { "kqemu", "", do_info_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00001640 "", "show KQEMU information", },
aliguori7ba1e612008-11-05 16:04:33 +00001641 { "kvm", "", do_info_kvm,
blueswir1d2c639d2009-01-24 18:19:25 +00001642 "", "show KVM information", },
bellarda594cfb2005-11-06 16:13:29 +00001643 { "usb", "", usb_info,
1644 "", "show guest USB devices", },
1645 { "usbhost", "", usb_host_info,
1646 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001647 { "profile", "", do_info_profile,
1648 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001649 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001650 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001651 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001652 "", "show the currently saved VM snapshots" },
aurel326f9c5ee2008-12-18 22:43:56 +00001653 { "status", "", do_info_status,
1654 "", "show the current VM status (running|paused)" },
balrog201a51f2007-04-30 00:51:09 +00001655 { "pcmcia", "", pcmcia_info,
1656 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001657 { "mice", "", do_info_mice,
1658 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001659 { "vnc", "", do_info_vnc,
1660 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001661 { "name", "", do_info_name,
1662 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001663 { "uuid", "", do_info_uuid,
1664 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001665#if defined(TARGET_PPC)
1666 { "cpustats", "", do_info_cpu_stats,
1667 "", "show CPU statistics", },
1668#endif
blueswir131a60e22007-10-26 18:42:59 +00001669#if defined(CONFIG_SLIRP)
1670 { "slirp", "", do_info_slirp,
1671 "", "show SLIRP statistics", },
1672#endif
aliguori5bb79102008-10-13 03:12:02 +00001673 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001674 { "balloon", "", do_info_balloon,
1675 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001676 { NULL, NULL, },
1677};
1678
bellard9307c4c2004-04-04 12:57:25 +00001679/*******************************************************************/
1680
1681static const char *pch;
1682static jmp_buf expr_env;
1683
bellard92a31b12005-02-10 22:00:52 +00001684#define MD_TLONG 0
1685#define MD_I32 1
1686
bellard9307c4c2004-04-04 12:57:25 +00001687typedef struct MonitorDef {
1688 const char *name;
1689 int offset;
blueswir18662d652008-10-02 18:32:44 +00001690 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001691 int type;
bellard9307c4c2004-04-04 12:57:25 +00001692} MonitorDef;
1693
bellard57206fd2004-04-25 18:54:52 +00001694#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001695static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001696{
bellard6a00d602005-11-21 23:25:50 +00001697 CPUState *env = mon_get_cpu();
1698 if (!env)
1699 return 0;
1700 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001701}
1702#endif
1703
bellarda541f292004-04-12 20:39:29 +00001704#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001705static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001706{
bellard6a00d602005-11-21 23:25:50 +00001707 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001708 unsigned int u;
1709 int i;
1710
bellard6a00d602005-11-21 23:25:50 +00001711 if (!env)
1712 return 0;
1713
bellarda541f292004-04-12 20:39:29 +00001714 u = 0;
1715 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001716 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001717
1718 return u;
1719}
1720
blueswir18662d652008-10-02 18:32:44 +00001721static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001722{
bellard6a00d602005-11-21 23:25:50 +00001723 CPUState *env = mon_get_cpu();
1724 if (!env)
1725 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001726 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001727}
1728
blueswir18662d652008-10-02 18:32:44 +00001729static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001730{
bellard6a00d602005-11-21 23:25:50 +00001731 CPUState *env = mon_get_cpu();
1732 if (!env)
1733 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001734 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001735}
bellard9fddaa02004-05-21 12:59:32 +00001736
blueswir18662d652008-10-02 18:32:44 +00001737static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001738{
bellard6a00d602005-11-21 23:25:50 +00001739 CPUState *env = mon_get_cpu();
1740 if (!env)
1741 return 0;
1742 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001743}
1744
blueswir18662d652008-10-02 18:32:44 +00001745static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001746{
bellard6a00d602005-11-21 23:25:50 +00001747 CPUState *env = mon_get_cpu();
1748 if (!env)
1749 return 0;
1750 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001751}
1752
blueswir18662d652008-10-02 18:32:44 +00001753static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001754{
bellard6a00d602005-11-21 23:25:50 +00001755 CPUState *env = mon_get_cpu();
1756 if (!env)
1757 return 0;
1758 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001759}
bellarda541f292004-04-12 20:39:29 +00001760#endif
1761
bellarde95c8d52004-09-30 22:22:08 +00001762#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001763#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001764static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001765{
bellard6a00d602005-11-21 23:25:50 +00001766 CPUState *env = mon_get_cpu();
1767 if (!env)
1768 return 0;
1769 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001770}
bellard7b936c02005-10-30 17:05:13 +00001771#endif
bellarde95c8d52004-09-30 22:22:08 +00001772
blueswir18662d652008-10-02 18:32:44 +00001773static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001774{
bellard6a00d602005-11-21 23:25:50 +00001775 CPUState *env = mon_get_cpu();
1776 if (!env)
1777 return 0;
1778 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001779}
1780#endif
1781
blueswir18662d652008-10-02 18:32:44 +00001782static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001783#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001784
1785#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001786 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001787 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001788 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001789
bellard9307c4c2004-04-04 12:57:25 +00001790 { "eax", offsetof(CPUState, regs[0]) },
1791 { "ecx", offsetof(CPUState, regs[1]) },
1792 { "edx", offsetof(CPUState, regs[2]) },
1793 { "ebx", offsetof(CPUState, regs[3]) },
1794 { "esp|sp", offsetof(CPUState, regs[4]) },
1795 { "ebp|fp", offsetof(CPUState, regs[5]) },
1796 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001797 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001798#ifdef TARGET_X86_64
1799 { "r8", offsetof(CPUState, regs[8]) },
1800 { "r9", offsetof(CPUState, regs[9]) },
1801 { "r10", offsetof(CPUState, regs[10]) },
1802 { "r11", offsetof(CPUState, regs[11]) },
1803 { "r12", offsetof(CPUState, regs[12]) },
1804 { "r13", offsetof(CPUState, regs[13]) },
1805 { "r14", offsetof(CPUState, regs[14]) },
1806 { "r15", offsetof(CPUState, regs[15]) },
1807#endif
bellard9307c4c2004-04-04 12:57:25 +00001808 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001809 { "eip", offsetof(CPUState, eip) },
1810 SEG("cs", R_CS)
1811 SEG("ds", R_DS)
1812 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001813 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001814 SEG("fs", R_FS)
1815 SEG("gs", R_GS)
1816 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001817#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001818 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001819 { "r0", offsetof(CPUState, gpr[0]) },
1820 { "r1", offsetof(CPUState, gpr[1]) },
1821 { "r2", offsetof(CPUState, gpr[2]) },
1822 { "r3", offsetof(CPUState, gpr[3]) },
1823 { "r4", offsetof(CPUState, gpr[4]) },
1824 { "r5", offsetof(CPUState, gpr[5]) },
1825 { "r6", offsetof(CPUState, gpr[6]) },
1826 { "r7", offsetof(CPUState, gpr[7]) },
1827 { "r8", offsetof(CPUState, gpr[8]) },
1828 { "r9", offsetof(CPUState, gpr[9]) },
1829 { "r10", offsetof(CPUState, gpr[10]) },
1830 { "r11", offsetof(CPUState, gpr[11]) },
1831 { "r12", offsetof(CPUState, gpr[12]) },
1832 { "r13", offsetof(CPUState, gpr[13]) },
1833 { "r14", offsetof(CPUState, gpr[14]) },
1834 { "r15", offsetof(CPUState, gpr[15]) },
1835 { "r16", offsetof(CPUState, gpr[16]) },
1836 { "r17", offsetof(CPUState, gpr[17]) },
1837 { "r18", offsetof(CPUState, gpr[18]) },
1838 { "r19", offsetof(CPUState, gpr[19]) },
1839 { "r20", offsetof(CPUState, gpr[20]) },
1840 { "r21", offsetof(CPUState, gpr[21]) },
1841 { "r22", offsetof(CPUState, gpr[22]) },
1842 { "r23", offsetof(CPUState, gpr[23]) },
1843 { "r24", offsetof(CPUState, gpr[24]) },
1844 { "r25", offsetof(CPUState, gpr[25]) },
1845 { "r26", offsetof(CPUState, gpr[26]) },
1846 { "r27", offsetof(CPUState, gpr[27]) },
1847 { "r28", offsetof(CPUState, gpr[28]) },
1848 { "r29", offsetof(CPUState, gpr[29]) },
1849 { "r30", offsetof(CPUState, gpr[30]) },
1850 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001851 /* Floating point registers */
1852 { "f0", offsetof(CPUState, fpr[0]) },
1853 { "f1", offsetof(CPUState, fpr[1]) },
1854 { "f2", offsetof(CPUState, fpr[2]) },
1855 { "f3", offsetof(CPUState, fpr[3]) },
1856 { "f4", offsetof(CPUState, fpr[4]) },
1857 { "f5", offsetof(CPUState, fpr[5]) },
1858 { "f6", offsetof(CPUState, fpr[6]) },
1859 { "f7", offsetof(CPUState, fpr[7]) },
1860 { "f8", offsetof(CPUState, fpr[8]) },
1861 { "f9", offsetof(CPUState, fpr[9]) },
1862 { "f10", offsetof(CPUState, fpr[10]) },
1863 { "f11", offsetof(CPUState, fpr[11]) },
1864 { "f12", offsetof(CPUState, fpr[12]) },
1865 { "f13", offsetof(CPUState, fpr[13]) },
1866 { "f14", offsetof(CPUState, fpr[14]) },
1867 { "f15", offsetof(CPUState, fpr[15]) },
1868 { "f16", offsetof(CPUState, fpr[16]) },
1869 { "f17", offsetof(CPUState, fpr[17]) },
1870 { "f18", offsetof(CPUState, fpr[18]) },
1871 { "f19", offsetof(CPUState, fpr[19]) },
1872 { "f20", offsetof(CPUState, fpr[20]) },
1873 { "f21", offsetof(CPUState, fpr[21]) },
1874 { "f22", offsetof(CPUState, fpr[22]) },
1875 { "f23", offsetof(CPUState, fpr[23]) },
1876 { "f24", offsetof(CPUState, fpr[24]) },
1877 { "f25", offsetof(CPUState, fpr[25]) },
1878 { "f26", offsetof(CPUState, fpr[26]) },
1879 { "f27", offsetof(CPUState, fpr[27]) },
1880 { "f28", offsetof(CPUState, fpr[28]) },
1881 { "f29", offsetof(CPUState, fpr[29]) },
1882 { "f30", offsetof(CPUState, fpr[30]) },
1883 { "f31", offsetof(CPUState, fpr[31]) },
1884 { "fpscr", offsetof(CPUState, fpscr) },
1885 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00001886 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00001887 { "lr", offsetof(CPUState, lr) },
1888 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00001889 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00001890 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00001891 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00001892 { "msr", 0, &monitor_get_msr, },
1893 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00001894 { "tbu", 0, &monitor_get_tbu, },
1895 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00001896#if defined(TARGET_PPC64)
1897 /* Address space register */
1898 { "asr", offsetof(CPUState, asr) },
1899#endif
1900 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00001901 { "sdr1", offsetof(CPUState, sdr1) },
1902 { "sr0", offsetof(CPUState, sr[0]) },
1903 { "sr1", offsetof(CPUState, sr[1]) },
1904 { "sr2", offsetof(CPUState, sr[2]) },
1905 { "sr3", offsetof(CPUState, sr[3]) },
1906 { "sr4", offsetof(CPUState, sr[4]) },
1907 { "sr5", offsetof(CPUState, sr[5]) },
1908 { "sr6", offsetof(CPUState, sr[6]) },
1909 { "sr7", offsetof(CPUState, sr[7]) },
1910 { "sr8", offsetof(CPUState, sr[8]) },
1911 { "sr9", offsetof(CPUState, sr[9]) },
1912 { "sr10", offsetof(CPUState, sr[10]) },
1913 { "sr11", offsetof(CPUState, sr[11]) },
1914 { "sr12", offsetof(CPUState, sr[12]) },
1915 { "sr13", offsetof(CPUState, sr[13]) },
1916 { "sr14", offsetof(CPUState, sr[14]) },
1917 { "sr15", offsetof(CPUState, sr[15]) },
1918 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00001919#elif defined(TARGET_SPARC)
1920 { "g0", offsetof(CPUState, gregs[0]) },
1921 { "g1", offsetof(CPUState, gregs[1]) },
1922 { "g2", offsetof(CPUState, gregs[2]) },
1923 { "g3", offsetof(CPUState, gregs[3]) },
1924 { "g4", offsetof(CPUState, gregs[4]) },
1925 { "g5", offsetof(CPUState, gregs[5]) },
1926 { "g6", offsetof(CPUState, gregs[6]) },
1927 { "g7", offsetof(CPUState, gregs[7]) },
1928 { "o0", 0, monitor_get_reg },
1929 { "o1", 1, monitor_get_reg },
1930 { "o2", 2, monitor_get_reg },
1931 { "o3", 3, monitor_get_reg },
1932 { "o4", 4, monitor_get_reg },
1933 { "o5", 5, monitor_get_reg },
1934 { "o6", 6, monitor_get_reg },
1935 { "o7", 7, monitor_get_reg },
1936 { "l0", 8, monitor_get_reg },
1937 { "l1", 9, monitor_get_reg },
1938 { "l2", 10, monitor_get_reg },
1939 { "l3", 11, monitor_get_reg },
1940 { "l4", 12, monitor_get_reg },
1941 { "l5", 13, monitor_get_reg },
1942 { "l6", 14, monitor_get_reg },
1943 { "l7", 15, monitor_get_reg },
1944 { "i0", 16, monitor_get_reg },
1945 { "i1", 17, monitor_get_reg },
1946 { "i2", 18, monitor_get_reg },
1947 { "i3", 19, monitor_get_reg },
1948 { "i4", 20, monitor_get_reg },
1949 { "i5", 21, monitor_get_reg },
1950 { "i6", 22, monitor_get_reg },
1951 { "i7", 23, monitor_get_reg },
1952 { "pc", offsetof(CPUState, pc) },
1953 { "npc", offsetof(CPUState, npc) },
1954 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00001955#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00001956 { "psr", 0, &monitor_get_psr, },
1957 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00001958#endif
bellarde95c8d52004-09-30 22:22:08 +00001959 { "tbr", offsetof(CPUState, tbr) },
1960 { "fsr", offsetof(CPUState, fsr) },
1961 { "f0", offsetof(CPUState, fpr[0]) },
1962 { "f1", offsetof(CPUState, fpr[1]) },
1963 { "f2", offsetof(CPUState, fpr[2]) },
1964 { "f3", offsetof(CPUState, fpr[3]) },
1965 { "f4", offsetof(CPUState, fpr[4]) },
1966 { "f5", offsetof(CPUState, fpr[5]) },
1967 { "f6", offsetof(CPUState, fpr[6]) },
1968 { "f7", offsetof(CPUState, fpr[7]) },
1969 { "f8", offsetof(CPUState, fpr[8]) },
1970 { "f9", offsetof(CPUState, fpr[9]) },
1971 { "f10", offsetof(CPUState, fpr[10]) },
1972 { "f11", offsetof(CPUState, fpr[11]) },
1973 { "f12", offsetof(CPUState, fpr[12]) },
1974 { "f13", offsetof(CPUState, fpr[13]) },
1975 { "f14", offsetof(CPUState, fpr[14]) },
1976 { "f15", offsetof(CPUState, fpr[15]) },
1977 { "f16", offsetof(CPUState, fpr[16]) },
1978 { "f17", offsetof(CPUState, fpr[17]) },
1979 { "f18", offsetof(CPUState, fpr[18]) },
1980 { "f19", offsetof(CPUState, fpr[19]) },
1981 { "f20", offsetof(CPUState, fpr[20]) },
1982 { "f21", offsetof(CPUState, fpr[21]) },
1983 { "f22", offsetof(CPUState, fpr[22]) },
1984 { "f23", offsetof(CPUState, fpr[23]) },
1985 { "f24", offsetof(CPUState, fpr[24]) },
1986 { "f25", offsetof(CPUState, fpr[25]) },
1987 { "f26", offsetof(CPUState, fpr[26]) },
1988 { "f27", offsetof(CPUState, fpr[27]) },
1989 { "f28", offsetof(CPUState, fpr[28]) },
1990 { "f29", offsetof(CPUState, fpr[29]) },
1991 { "f30", offsetof(CPUState, fpr[30]) },
1992 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00001993#ifdef TARGET_SPARC64
1994 { "f32", offsetof(CPUState, fpr[32]) },
1995 { "f34", offsetof(CPUState, fpr[34]) },
1996 { "f36", offsetof(CPUState, fpr[36]) },
1997 { "f38", offsetof(CPUState, fpr[38]) },
1998 { "f40", offsetof(CPUState, fpr[40]) },
1999 { "f42", offsetof(CPUState, fpr[42]) },
2000 { "f44", offsetof(CPUState, fpr[44]) },
2001 { "f46", offsetof(CPUState, fpr[46]) },
2002 { "f48", offsetof(CPUState, fpr[48]) },
2003 { "f50", offsetof(CPUState, fpr[50]) },
2004 { "f52", offsetof(CPUState, fpr[52]) },
2005 { "f54", offsetof(CPUState, fpr[54]) },
2006 { "f56", offsetof(CPUState, fpr[56]) },
2007 { "f58", offsetof(CPUState, fpr[58]) },
2008 { "f60", offsetof(CPUState, fpr[60]) },
2009 { "f62", offsetof(CPUState, fpr[62]) },
2010 { "asi", offsetof(CPUState, asi) },
2011 { "pstate", offsetof(CPUState, pstate) },
2012 { "cansave", offsetof(CPUState, cansave) },
2013 { "canrestore", offsetof(CPUState, canrestore) },
2014 { "otherwin", offsetof(CPUState, otherwin) },
2015 { "wstate", offsetof(CPUState, wstate) },
2016 { "cleanwin", offsetof(CPUState, cleanwin) },
2017 { "fprs", offsetof(CPUState, fprs) },
2018#endif
bellard9307c4c2004-04-04 12:57:25 +00002019#endif
2020 { NULL },
2021};
2022
blueswir160cbfb92008-12-28 13:14:48 +00002023static void expr_error(const char *msg)
bellard9dc39cb2004-03-14 21:38:27 +00002024{
blueswir160cbfb92008-12-28 13:14:48 +00002025 term_printf("%s\n", msg);
bellard9307c4c2004-04-04 12:57:25 +00002026 longjmp(expr_env, 1);
2027}
2028
bellard6a00d602005-11-21 23:25:50 +00002029/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00002030static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00002031{
blueswir18662d652008-10-02 18:32:44 +00002032 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00002033 void *ptr;
2034
bellard9307c4c2004-04-04 12:57:25 +00002035 for(md = monitor_defs; md->name != NULL; md++) {
2036 if (compare_cmd(name, md->name)) {
2037 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00002038 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00002039 } else {
bellard6a00d602005-11-21 23:25:50 +00002040 CPUState *env = mon_get_cpu();
2041 if (!env)
2042 return -2;
2043 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00002044 switch(md->type) {
2045 case MD_I32:
2046 *pval = *(int32_t *)ptr;
2047 break;
2048 case MD_TLONG:
2049 *pval = *(target_long *)ptr;
2050 break;
2051 default:
2052 *pval = 0;
2053 break;
2054 }
bellard9307c4c2004-04-04 12:57:25 +00002055 }
2056 return 0;
2057 }
2058 }
2059 return -1;
2060}
2061
2062static void next(void)
2063{
2064 if (pch != '\0') {
2065 pch++;
blueswir1cd390082008-11-16 13:53:32 +00002066 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002067 pch++;
2068 }
2069}
2070
blueswir1c2efc952007-09-25 17:28:42 +00002071static int64_t expr_sum(void);
bellard9307c4c2004-04-04 12:57:25 +00002072
blueswir1c2efc952007-09-25 17:28:42 +00002073static int64_t expr_unary(void)
bellard9307c4c2004-04-04 12:57:25 +00002074{
blueswir1c2efc952007-09-25 17:28:42 +00002075 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00002076 char *p;
bellard6a00d602005-11-21 23:25:50 +00002077 int ret;
bellard9307c4c2004-04-04 12:57:25 +00002078
2079 switch(*pch) {
2080 case '+':
2081 next();
2082 n = expr_unary();
2083 break;
2084 case '-':
2085 next();
2086 n = -expr_unary();
2087 break;
2088 case '~':
2089 next();
2090 n = ~expr_unary();
2091 break;
2092 case '(':
2093 next();
2094 n = expr_sum();
2095 if (*pch != ')') {
2096 expr_error("')' expected");
2097 }
2098 next();
2099 break;
bellard81d09122004-07-14 17:21:37 +00002100 case '\'':
2101 pch++;
2102 if (*pch == '\0')
2103 expr_error("character constant expected");
2104 n = *pch;
2105 pch++;
2106 if (*pch != '\'')
2107 expr_error("missing terminating \' character");
2108 next();
2109 break;
bellard9307c4c2004-04-04 12:57:25 +00002110 case '$':
2111 {
2112 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002113 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002114
bellard9307c4c2004-04-04 12:57:25 +00002115 pch++;
2116 q = buf;
2117 while ((*pch >= 'a' && *pch <= 'z') ||
2118 (*pch >= 'A' && *pch <= 'Z') ||
2119 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002120 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002121 if ((q - buf) < sizeof(buf) - 1)
2122 *q++ = *pch;
2123 pch++;
2124 }
blueswir1cd390082008-11-16 13:53:32 +00002125 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002126 pch++;
2127 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002128 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002129 if (ret == -1)
bellard9307c4c2004-04-04 12:57:25 +00002130 expr_error("unknown register");
ths5fafdf22007-09-16 21:08:06 +00002131 else if (ret == -2)
bellard6a00d602005-11-21 23:25:50 +00002132 expr_error("no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002133 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002134 }
2135 break;
2136 case '\0':
2137 expr_error("unexpected end of expression");
2138 n = 0;
2139 break;
2140 default:
blueswir17743e582007-09-24 18:39:04 +00002141#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002142 n = strtoull(pch, &p, 0);
2143#else
bellard9307c4c2004-04-04 12:57:25 +00002144 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002145#endif
bellard9307c4c2004-04-04 12:57:25 +00002146 if (pch == p) {
2147 expr_error("invalid char in expression");
2148 }
2149 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002150 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002151 pch++;
2152 break;
2153 }
2154 return n;
2155}
2156
2157
blueswir1c2efc952007-09-25 17:28:42 +00002158static int64_t expr_prod(void)
bellard9307c4c2004-04-04 12:57:25 +00002159{
blueswir1c2efc952007-09-25 17:28:42 +00002160 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002161 int op;
ths3b46e622007-09-17 08:09:54 +00002162
bellard9307c4c2004-04-04 12:57:25 +00002163 val = expr_unary();
2164 for(;;) {
2165 op = *pch;
2166 if (op != '*' && op != '/' && op != '%')
2167 break;
2168 next();
2169 val2 = expr_unary();
2170 switch(op) {
2171 default:
2172 case '*':
2173 val *= val2;
2174 break;
2175 case '/':
2176 case '%':
ths5fafdf22007-09-16 21:08:06 +00002177 if (val2 == 0)
bellard5b602122004-05-22 21:41:05 +00002178 expr_error("division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002179 if (op == '/')
2180 val /= val2;
2181 else
2182 val %= val2;
2183 break;
2184 }
2185 }
2186 return val;
2187}
2188
blueswir1c2efc952007-09-25 17:28:42 +00002189static int64_t expr_logic(void)
bellard9307c4c2004-04-04 12:57:25 +00002190{
blueswir1c2efc952007-09-25 17:28:42 +00002191 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002192 int op;
bellard9307c4c2004-04-04 12:57:25 +00002193
2194 val = expr_prod();
2195 for(;;) {
2196 op = *pch;
2197 if (op != '&' && op != '|' && op != '^')
2198 break;
2199 next();
2200 val2 = expr_prod();
2201 switch(op) {
2202 default:
2203 case '&':
2204 val &= val2;
2205 break;
2206 case '|':
2207 val |= val2;
2208 break;
2209 case '^':
2210 val ^= val2;
2211 break;
2212 }
2213 }
2214 return val;
2215}
2216
blueswir1c2efc952007-09-25 17:28:42 +00002217static int64_t expr_sum(void)
bellard9307c4c2004-04-04 12:57:25 +00002218{
blueswir1c2efc952007-09-25 17:28:42 +00002219 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002220 int op;
bellard9307c4c2004-04-04 12:57:25 +00002221
2222 val = expr_logic();
2223 for(;;) {
2224 op = *pch;
2225 if (op != '+' && op != '-')
2226 break;
2227 next();
2228 val2 = expr_logic();
2229 if (op == '+')
2230 val += val2;
2231 else
2232 val -= val2;
2233 }
2234 return val;
2235}
2236
blueswir1c2efc952007-09-25 17:28:42 +00002237static int get_expr(int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002238{
2239 pch = *pp;
2240 if (setjmp(expr_env)) {
2241 *pp = pch;
2242 return -1;
2243 }
blueswir1cd390082008-11-16 13:53:32 +00002244 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002245 pch++;
2246 *pval = expr_sum();
2247 *pp = pch;
2248 return 0;
2249}
2250
2251static int get_str(char *buf, int buf_size, const char **pp)
2252{
2253 const char *p;
2254 char *q;
2255 int c;
2256
bellard81d09122004-07-14 17:21:37 +00002257 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002258 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002259 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002260 p++;
2261 if (*p == '\0') {
2262 fail:
bellard81d09122004-07-14 17:21:37 +00002263 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002264 *pp = p;
2265 return -1;
2266 }
bellard9307c4c2004-04-04 12:57:25 +00002267 if (*p == '\"') {
2268 p++;
2269 while (*p != '\0' && *p != '\"') {
2270 if (*p == '\\') {
2271 p++;
2272 c = *p++;
2273 switch(c) {
2274 case 'n':
2275 c = '\n';
2276 break;
2277 case 'r':
2278 c = '\r';
2279 break;
2280 case '\\':
2281 case '\'':
2282 case '\"':
2283 break;
2284 default:
2285 qemu_printf("unsupported escape code: '\\%c'\n", c);
2286 goto fail;
2287 }
2288 if ((q - buf) < buf_size - 1) {
2289 *q++ = c;
2290 }
2291 } else {
2292 if ((q - buf) < buf_size - 1) {
2293 *q++ = *p;
2294 }
2295 p++;
2296 }
2297 }
2298 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002299 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002300 goto fail;
2301 }
2302 p++;
2303 } else {
blueswir1cd390082008-11-16 13:53:32 +00002304 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002305 if ((q - buf) < buf_size - 1) {
2306 *q++ = *p;
2307 }
2308 p++;
2309 }
bellard9307c4c2004-04-04 12:57:25 +00002310 }
bellard81d09122004-07-14 17:21:37 +00002311 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002312 *pp = p;
2313 return 0;
2314}
2315
2316static int default_fmt_format = 'x';
2317static int default_fmt_size = 4;
2318
2319#define MAX_ARGS 16
2320
bellard7e2515e2004-08-01 21:52:19 +00002321static void monitor_handle_command(const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002322{
2323 const char *p, *pstart, *typestr;
2324 char *q;
2325 int c, nb_args, len, i, has_arg;
blueswir18662d652008-10-02 18:32:44 +00002326 const term_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002327 char cmdname[256];
2328 char buf[1024];
2329 void *str_allocated[MAX_ARGS];
2330 void *args[MAX_ARGS];
blueswir1a5f1b962008-08-17 20:21:51 +00002331 void (*handler_0)(void);
2332 void (*handler_1)(void *arg0);
2333 void (*handler_2)(void *arg0, void *arg1);
2334 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2335 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2336 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2337 void *arg4);
2338 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2339 void *arg4, void *arg5);
2340 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2341 void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002342
2343#ifdef DEBUG
2344 term_printf("command='%s'\n", cmdline);
2345#endif
ths3b46e622007-09-17 08:09:54 +00002346
bellard9307c4c2004-04-04 12:57:25 +00002347 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002348 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002349 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002350 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002351 p++;
2352 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002353 return;
bellard9307c4c2004-04-04 12:57:25 +00002354 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002355 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002356 p++;
2357 len = p - pstart;
2358 if (len > sizeof(cmdname) - 1)
2359 len = sizeof(cmdname) - 1;
2360 memcpy(cmdname, pstart, len);
2361 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002362
bellard9307c4c2004-04-04 12:57:25 +00002363 /* find the command */
bellard9dc39cb2004-03-14 21:38:27 +00002364 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002365 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002366 goto found;
2367 }
bellard9307c4c2004-04-04 12:57:25 +00002368 term_printf("unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002369 return;
2370 found:
bellard9307c4c2004-04-04 12:57:25 +00002371
2372 for(i = 0; i < MAX_ARGS; i++)
2373 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002374
bellard9307c4c2004-04-04 12:57:25 +00002375 /* parse the parameters */
2376 typestr = cmd->args_type;
2377 nb_args = 0;
2378 for(;;) {
2379 c = *typestr;
2380 if (c == '\0')
2381 break;
2382 typestr++;
2383 switch(c) {
2384 case 'F':
bellard81d09122004-07-14 17:21:37 +00002385 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002386 case 's':
2387 {
2388 int ret;
2389 char *str;
ths3b46e622007-09-17 08:09:54 +00002390
blueswir1cd390082008-11-16 13:53:32 +00002391 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002392 p++;
2393 if (*typestr == '?') {
2394 typestr++;
2395 if (*p == '\0') {
2396 /* no optional string: NULL argument */
2397 str = NULL;
2398 goto add_str;
2399 }
2400 }
2401 ret = get_str(buf, sizeof(buf), &p);
2402 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002403 switch(c) {
2404 case 'F':
bellard9307c4c2004-04-04 12:57:25 +00002405 term_printf("%s: filename expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002406 break;
2407 case 'B':
2408 term_printf("%s: block device name expected\n", cmdname);
2409 break;
2410 default:
bellard9307c4c2004-04-04 12:57:25 +00002411 term_printf("%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002412 break;
2413 }
bellard9307c4c2004-04-04 12:57:25 +00002414 goto fail;
2415 }
2416 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002417 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002418 str_allocated[nb_args] = str;
2419 add_str:
2420 if (nb_args >= MAX_ARGS) {
2421 error_args:
2422 term_printf("%s: too many arguments\n", cmdname);
2423 goto fail;
2424 }
2425 args[nb_args++] = str;
2426 }
2427 break;
2428 case '/':
2429 {
2430 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002431
blueswir1cd390082008-11-16 13:53:32 +00002432 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002433 p++;
2434 if (*p == '/') {
2435 /* format found */
2436 p++;
2437 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002438 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002439 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002440 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002441 count = count * 10 + (*p - '0');
2442 p++;
2443 }
2444 }
2445 size = -1;
2446 format = -1;
2447 for(;;) {
2448 switch(*p) {
2449 case 'o':
2450 case 'd':
2451 case 'u':
2452 case 'x':
2453 case 'i':
2454 case 'c':
2455 format = *p++;
2456 break;
2457 case 'b':
2458 size = 1;
2459 p++;
2460 break;
2461 case 'h':
2462 size = 2;
2463 p++;
2464 break;
2465 case 'w':
2466 size = 4;
2467 p++;
2468 break;
2469 case 'g':
2470 case 'L':
2471 size = 8;
2472 p++;
2473 break;
2474 default:
2475 goto next;
2476 }
2477 }
2478 next:
blueswir1cd390082008-11-16 13:53:32 +00002479 if (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002480 term_printf("invalid char in format: '%c'\n", *p);
2481 goto fail;
2482 }
bellard9307c4c2004-04-04 12:57:25 +00002483 if (format < 0)
2484 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002485 if (format != 'i') {
2486 /* for 'i', not specifying a size gives -1 as size */
2487 if (size < 0)
2488 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002489 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002490 }
bellard9307c4c2004-04-04 12:57:25 +00002491 default_fmt_format = format;
2492 } else {
2493 count = 1;
2494 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002495 if (format != 'i') {
2496 size = default_fmt_size;
2497 } else {
2498 size = -1;
2499 }
bellard9307c4c2004-04-04 12:57:25 +00002500 }
2501 if (nb_args + 3 > MAX_ARGS)
2502 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002503 args[nb_args++] = (void*)(long)count;
2504 args[nb_args++] = (void*)(long)format;
2505 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002506 }
2507 break;
2508 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002509 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002510 {
blueswir1c2efc952007-09-25 17:28:42 +00002511 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002512
blueswir1cd390082008-11-16 13:53:32 +00002513 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002514 p++;
bellard34405572004-06-08 00:55:58 +00002515 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002516 if (*typestr == '?') {
2517 if (*p == '\0')
2518 has_arg = 0;
2519 else
2520 has_arg = 1;
2521 } else {
2522 if (*p == '.') {
2523 p++;
blueswir1cd390082008-11-16 13:53:32 +00002524 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002525 p++;
2526 has_arg = 1;
2527 } else {
2528 has_arg = 0;
2529 }
2530 }
bellard13224a82006-07-14 22:03:35 +00002531 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002532 if (nb_args >= MAX_ARGS)
2533 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002534 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002535 if (!has_arg) {
2536 if (nb_args >= MAX_ARGS)
2537 goto error_args;
2538 val = -1;
2539 goto add_num;
2540 }
2541 }
2542 if (get_expr(&val, &p))
2543 goto fail;
2544 add_num:
bellard92a31b12005-02-10 22:00:52 +00002545 if (c == 'i') {
2546 if (nb_args >= MAX_ARGS)
2547 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002548 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002549 } else {
2550 if ((nb_args + 1) >= MAX_ARGS)
2551 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002552#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002553 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002554#else
2555 args[nb_args++] = (void *)0;
2556#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002557 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002558 }
bellard9307c4c2004-04-04 12:57:25 +00002559 }
2560 break;
2561 case '-':
2562 {
2563 int has_option;
2564 /* option */
ths3b46e622007-09-17 08:09:54 +00002565
bellard9307c4c2004-04-04 12:57:25 +00002566 c = *typestr++;
2567 if (c == '\0')
2568 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002569 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002570 p++;
2571 has_option = 0;
2572 if (*p == '-') {
2573 p++;
2574 if (*p != c) {
ths5fafdf22007-09-16 21:08:06 +00002575 term_printf("%s: unsupported option -%c\n",
bellard9307c4c2004-04-04 12:57:25 +00002576 cmdname, *p);
2577 goto fail;
2578 }
2579 p++;
2580 has_option = 1;
2581 }
2582 if (nb_args >= MAX_ARGS)
2583 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002584 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002585 }
2586 break;
2587 default:
2588 bad_type:
2589 term_printf("%s: unknown type '%c'\n", cmdname, c);
2590 goto fail;
2591 }
2592 }
2593 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002594 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002595 p++;
2596 if (*p != '\0') {
ths5fafdf22007-09-16 21:08:06 +00002597 term_printf("%s: extraneous characters at the end of line\n",
bellard9307c4c2004-04-04 12:57:25 +00002598 cmdname);
2599 goto fail;
2600 }
2601
2602 switch(nb_args) {
2603 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002604 handler_0 = cmd->handler;
2605 handler_0();
bellard9307c4c2004-04-04 12:57:25 +00002606 break;
2607 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002608 handler_1 = cmd->handler;
2609 handler_1(args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002610 break;
2611 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002612 handler_2 = cmd->handler;
2613 handler_2(args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002614 break;
2615 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002616 handler_3 = cmd->handler;
2617 handler_3(args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002618 break;
2619 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002620 handler_4 = cmd->handler;
2621 handler_4(args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002622 break;
2623 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002624 handler_5 = cmd->handler;
2625 handler_5(args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002626 break;
bellard34405572004-06-08 00:55:58 +00002627 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002628 handler_6 = cmd->handler;
2629 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002630 break;
bellardec36b692006-07-16 18:57:03 +00002631 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002632 handler_7 = cmd->handler;
2633 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
bellardec36b692006-07-16 18:57:03 +00002634 break;
bellard9307c4c2004-04-04 12:57:25 +00002635 default:
2636 term_printf("unsupported number of arguments: %d\n", nb_args);
2637 goto fail;
2638 }
2639 fail:
2640 for(i = 0; i < MAX_ARGS; i++)
2641 qemu_free(str_allocated[i]);
2642 return;
bellard9dc39cb2004-03-14 21:38:27 +00002643}
2644
bellard81d09122004-07-14 17:21:37 +00002645static void cmd_completion(const char *name, const char *list)
2646{
2647 const char *p, *pstart;
2648 char cmd[128];
2649 int len;
2650
2651 p = list;
2652 for(;;) {
2653 pstart = p;
2654 p = strchr(p, '|');
2655 if (!p)
2656 p = pstart + strlen(pstart);
2657 len = p - pstart;
2658 if (len > sizeof(cmd) - 2)
2659 len = sizeof(cmd) - 2;
2660 memcpy(cmd, pstart, len);
2661 cmd[len] = '\0';
2662 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2663 add_completion(cmd);
2664 }
2665 if (*p == '\0')
2666 break;
2667 p++;
2668 }
2669}
2670
2671static void file_completion(const char *input)
2672{
2673 DIR *ffs;
2674 struct dirent *d;
2675 char path[1024];
2676 char file[1024], file_prefix[1024];
2677 int input_path_len;
2678 const char *p;
2679
ths5fafdf22007-09-16 21:08:06 +00002680 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002681 if (!p) {
2682 input_path_len = 0;
2683 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002684 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002685 } else {
2686 input_path_len = p - input + 1;
2687 memcpy(path, input, input_path_len);
2688 if (input_path_len > sizeof(path) - 1)
2689 input_path_len = sizeof(path) - 1;
2690 path[input_path_len] = '\0';
2691 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2692 }
2693#ifdef DEBUG_COMPLETION
2694 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2695#endif
2696 ffs = opendir(path);
2697 if (!ffs)
2698 return;
2699 for(;;) {
2700 struct stat sb;
2701 d = readdir(ffs);
2702 if (!d)
2703 break;
2704 if (strstart(d->d_name, file_prefix, NULL)) {
2705 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002706 if (input_path_len < sizeof(file))
2707 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2708 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002709 /* stat the file to find out if it's a directory.
2710 * In that case add a slash to speed up typing long paths
2711 */
2712 stat(file, &sb);
2713 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002714 pstrcat(file, sizeof(file), "/");
bellard81d09122004-07-14 17:21:37 +00002715 add_completion(file);
2716 }
2717 }
2718 closedir(ffs);
2719}
2720
aliguori51de9762009-03-05 23:00:43 +00002721static void block_completion_it(void *opaque, BlockDriverState *bs)
bellard81d09122004-07-14 17:21:37 +00002722{
aliguori51de9762009-03-05 23:00:43 +00002723 const char *name = bdrv_get_device_name(bs);
bellard81d09122004-07-14 17:21:37 +00002724 const char *input = opaque;
2725
2726 if (input[0] == '\0' ||
2727 !strncmp(name, (char *)input, strlen(input))) {
2728 add_completion(name);
2729 }
2730}
2731
2732/* NOTE: this parser is an approximate form of the real command parser */
2733static void parse_cmdline(const char *cmdline,
2734 int *pnb_args, char **args)
2735{
2736 const char *p;
2737 int nb_args, ret;
2738 char buf[1024];
2739
2740 p = cmdline;
2741 nb_args = 0;
2742 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002743 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002744 p++;
2745 if (*p == '\0')
2746 break;
2747 if (nb_args >= MAX_ARGS)
2748 break;
2749 ret = get_str(buf, sizeof(buf), &p);
2750 args[nb_args] = qemu_strdup(buf);
2751 nb_args++;
2752 if (ret < 0)
2753 break;
2754 }
2755 *pnb_args = nb_args;
2756}
2757
bellard7e2515e2004-08-01 21:52:19 +00002758void readline_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002759{
2760 const char *cmdname;
2761 char *args[MAX_ARGS];
2762 int nb_args, i, len;
2763 const char *ptype, *str;
blueswir18662d652008-10-02 18:32:44 +00002764 const term_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002765 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002766
2767 parse_cmdline(cmdline, &nb_args, args);
2768#ifdef DEBUG_COMPLETION
2769 for(i = 0; i < nb_args; i++) {
2770 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2771 }
2772#endif
2773
2774 /* if the line ends with a space, it means we want to complete the
2775 next arg */
2776 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002777 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002778 if (nb_args >= MAX_ARGS)
2779 return;
2780 args[nb_args++] = qemu_strdup("");
2781 }
2782 if (nb_args <= 1) {
2783 /* command completion */
2784 if (nb_args == 0)
2785 cmdname = "";
2786 else
2787 cmdname = args[0];
2788 completion_index = strlen(cmdname);
2789 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2790 cmd_completion(cmdname, cmd->name);
2791 }
2792 } else {
2793 /* find the command */
2794 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2795 if (compare_cmd(args[0], cmd->name))
2796 goto found;
2797 }
2798 return;
2799 found:
2800 ptype = cmd->args_type;
2801 for(i = 0; i < nb_args - 2; i++) {
2802 if (*ptype != '\0') {
2803 ptype++;
2804 while (*ptype == '?')
2805 ptype++;
2806 }
2807 }
2808 str = args[nb_args - 1];
2809 switch(*ptype) {
2810 case 'F':
2811 /* file completion */
2812 completion_index = strlen(str);
2813 file_completion(str);
2814 break;
2815 case 'B':
2816 /* block device name completion */
2817 completion_index = strlen(str);
2818 bdrv_iterate(block_completion_it, (void *)str);
2819 break;
bellard7fe48482004-10-09 18:08:01 +00002820 case 's':
2821 /* XXX: more generic ? */
2822 if (!strcmp(cmd->name, "info")) {
2823 completion_index = strlen(str);
2824 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2825 cmd_completion(str, cmd->name);
2826 }
bellard64866c32006-05-07 18:03:31 +00002827 } else if (!strcmp(cmd->name, "sendkey")) {
2828 completion_index = strlen(str);
2829 for(key = key_defs; key->name != NULL; key++) {
2830 cmd_completion(str, key->name);
2831 }
bellard7fe48482004-10-09 18:08:01 +00002832 }
2833 break;
bellard81d09122004-07-14 17:21:37 +00002834 default:
2835 break;
2836 }
2837 }
2838 for(i = 0; i < nb_args; i++)
2839 qemu_free(args[i]);
2840}
2841
bellard9dc39cb2004-03-14 21:38:27 +00002842static int term_can_read(void *opaque)
2843{
bellard81d09122004-07-14 17:21:37 +00002844 return 128;
bellard9dc39cb2004-03-14 21:38:27 +00002845}
2846
2847static void term_read(void *opaque, const uint8_t *buf, int size)
2848{
2849 int i;
2850 for(i = 0; i < size; i++)
bellard7e2515e2004-08-01 21:52:19 +00002851 readline_handle_byte(buf[i]);
2852}
2853
aliguorid8f44602008-10-06 13:52:44 +00002854static int monitor_suspended;
2855
bellard7e2515e2004-08-01 21:52:19 +00002856static void monitor_handle_command1(void *opaque, const char *cmdline)
2857{
2858 monitor_handle_command(cmdline);
aliguorid8f44602008-10-06 13:52:44 +00002859 if (!monitor_suspended)
aliguoribb5fc202009-03-05 23:01:15 +00002860 readline_show_prompt();
aliguorid8f44602008-10-06 13:52:44 +00002861 else
2862 monitor_suspended = 2;
2863}
2864
2865void monitor_suspend(void)
2866{
2867 monitor_suspended = 1;
2868}
2869
2870void monitor_resume(void)
2871{
2872 if (monitor_suspended == 2)
2873 monitor_start_input();
2874 monitor_suspended = 0;
bellard7e2515e2004-08-01 21:52:19 +00002875}
2876
aliguori83ab7952008-08-19 14:44:22 +00002877static void monitor_start_input(void)
bellard7e2515e2004-08-01 21:52:19 +00002878{
2879 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
aliguori9dd442b2009-03-05 23:01:10 +00002880 readline_show_prompt();
bellard9dc39cb2004-03-14 21:38:27 +00002881}
2882
ths86e94de2007-01-05 22:01:59 +00002883static void term_event(void *opaque, int event)
2884{
2885 if (event != CHR_EVENT_RESET)
2886 return;
2887
2888 if (!hide_banner)
2889 term_printf("QEMU %s monitor - type 'help' for more information\n",
2890 QEMU_VERSION);
2891 monitor_start_input();
2892}
2893
ths20d8a3e2007-02-18 17:04:49 +00002894static int is_first_init = 1;
2895
bellard81d09122004-07-14 17:21:37 +00002896void monitor_init(CharDriverState *hd, int show_banner)
bellard9dc39cb2004-03-14 21:38:27 +00002897{
ths20d8a3e2007-02-18 17:04:49 +00002898 int i;
2899
2900 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00002901 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2902 if (!key_timer)
2903 return;
ths20d8a3e2007-02-18 17:04:49 +00002904 for (i = 0; i < MAX_MON; i++) {
2905 monitor_hd[i] = NULL;
2906 }
2907 is_first_init = 0;
2908 }
2909 for (i = 0; i < MAX_MON; i++) {
2910 if (monitor_hd[i] == NULL) {
2911 monitor_hd[i] = hd;
2912 break;
2913 }
2914 }
2915
ths86e94de2007-01-05 22:01:59 +00002916 hide_banner = !show_banner;
2917
pbrooke5b0bc42007-01-27 23:46:43 +00002918 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
aliguori83ab7952008-08-19 14:44:22 +00002919
2920 readline_start("", 0, monitor_handle_command1, NULL);
bellard7e2515e2004-08-01 21:52:19 +00002921}
2922
aliguoribb5fc202009-03-05 23:01:15 +00002923static void bdrv_password_cb(void *opaque, const char *password)
bellard7e2515e2004-08-01 21:52:19 +00002924{
aliguoribb5fc202009-03-05 23:01:15 +00002925 BlockDriverState *bs = opaque;
2926 int ret = 0;
bellard7e2515e2004-08-01 21:52:19 +00002927
aliguoribb5fc202009-03-05 23:01:15 +00002928 if (bdrv_set_key(bs, password) != 0) {
2929 term_printf("invalid password\n");
2930 ret = -EPERM;
bellard7e2515e2004-08-01 21:52:19 +00002931 }
aliguoribb5fc202009-03-05 23:01:15 +00002932 if (password_completion_cb)
2933 password_completion_cb(password_opaque, ret);
2934
2935 monitor_start_input();
bellard9dc39cb2004-03-14 21:38:27 +00002936}
aliguoric0f4ce72009-03-05 23:01:01 +00002937
aliguoribb5fc202009-03-05 23:01:15 +00002938void monitor_read_bdrv_key_start(BlockDriverState *bs,
2939 BlockDriverCompletionFunc *completion_cb,
2940 void *opaque)
aliguoric0f4ce72009-03-05 23:01:01 +00002941{
aliguoribb5fc202009-03-05 23:01:15 +00002942 if (!bdrv_key_required(bs)) {
2943 if (completion_cb)
2944 completion_cb(opaque, 0);
2945 return;
2946 }
aliguoric0f4ce72009-03-05 23:01:01 +00002947
2948 term_printf("%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2949 bdrv_get_encrypted_filename(bs));
aliguoribb5fc202009-03-05 23:01:15 +00002950
2951 password_completion_cb = completion_cb;
2952 password_opaque = opaque;
2953
2954 monitor_read_password(bdrv_password_cb, bs);
aliguoric0f4ce72009-03-05 23:01:01 +00002955}