blob: 042d2b0bd7f432e3d8a9a74cb8049bc17c7b1e7d [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "hw/usb.h"
26#include "hw/pcmcia.h"
27#include "hw/pc.h"
28#include "hw/pci.h"
29#include "gdbstub.h"
30#include "net.h"
31#include "qemu-char.h"
32#include "sysemu.h"
33#include "console.h"
34#include "block.h"
35#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000036#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000037#include "balloon.h"
bellard81d09122004-07-14 17:21:37 +000038#include <dirent.h>
balrogc8256f92008-06-08 22:45:01 +000039#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000040#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000041#include "kvm.h"
ths6a5bd302007-12-03 17:05:38 +000042
bellard9dc39cb2004-03-14 21:38:27 +000043//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000044//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000045
bellard9307c4c2004-04-04 12:57:25 +000046/*
47 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000048 *
bellard9307c4c2004-04-04 12:57:25 +000049 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000050 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000051 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000052 * 'i' 32 bit integer
53 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000054 * '/' optional gdb-like print format (like "/10x")
55 *
56 * '?' optional type (for 'F', 's' and 'i')
57 *
58 */
59
bellard9dc39cb2004-03-14 21:38:27 +000060typedef struct term_cmd_t {
61 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000062 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000063 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000064 const char *params;
65 const char *help;
66} term_cmd_t;
67
ths20d8a3e2007-02-18 17:04:49 +000068#define MAX_MON 4
69static CharDriverState *monitor_hd[MAX_MON];
ths86e94de2007-01-05 22:01:59 +000070static int hide_banner;
bellard7e2515e2004-08-01 21:52:19 +000071
blueswir18662d652008-10-02 18:32:44 +000072static const term_cmd_t term_cmds[];
73static const term_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000074
ths60fe76f2007-12-16 03:02:09 +000075static uint8_t term_outbuf[1024];
bellard7e2515e2004-08-01 21:52:19 +000076static int term_outbuf_index;
bellard81d09122004-07-14 17:21:37 +000077
aliguori83ab7952008-08-19 14:44:22 +000078static void monitor_start_input(void);
79
blueswir1b1d8e522008-10-26 13:43:07 +000080static CPUState *mon_cpu = NULL;
bellard6a00d602005-11-21 23:25:50 +000081
bellard9dc39cb2004-03-14 21:38:27 +000082void term_flush(void)
83{
ths20d8a3e2007-02-18 17:04:49 +000084 int i;
bellard7e2515e2004-08-01 21:52:19 +000085 if (term_outbuf_index > 0) {
ths20d8a3e2007-02-18 17:04:49 +000086 for (i = 0; i < MAX_MON; i++)
87 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
88 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
bellard7e2515e2004-08-01 21:52:19 +000089 term_outbuf_index = 0;
90 }
91}
92
93/* flush at every end of line or if the buffer is full */
94void term_puts(const char *str)
95{
ths60fe76f2007-12-16 03:02:09 +000096 char c;
bellard7e2515e2004-08-01 21:52:19 +000097 for(;;) {
98 c = *str++;
99 if (c == '\0')
100 break;
bellard7ba12602006-07-14 20:26:42 +0000101 if (c == '\n')
102 term_outbuf[term_outbuf_index++] = '\r';
bellard7e2515e2004-08-01 21:52:19 +0000103 term_outbuf[term_outbuf_index++] = c;
bellard7ba12602006-07-14 20:26:42 +0000104 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
bellard7e2515e2004-08-01 21:52:19 +0000105 c == '\n')
106 term_flush();
107 }
108}
109
110void term_vprintf(const char *fmt, va_list ap)
111{
112 char buf[4096];
113 vsnprintf(buf, sizeof(buf), fmt, ap);
114 term_puts(buf);
115}
116
117void term_printf(const char *fmt, ...)
118{
119 va_list ap;
120 va_start(ap, fmt);
121 term_vprintf(fmt, ap);
122 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000123}
124
thsfef30742006-12-22 14:11:32 +0000125void term_print_filename(const char *filename)
126{
127 int i;
128
129 for (i = 0; filename[i]; i++) {
130 switch (filename[i]) {
131 case ' ':
132 case '"':
133 case '\\':
134 term_printf("\\%c", filename[i]);
135 break;
136 case '\t':
137 term_printf("\\t");
138 break;
139 case '\r':
140 term_printf("\\r");
141 break;
142 case '\n':
143 term_printf("\\n");
144 break;
145 default:
146 term_printf("%c", filename[i]);
147 break;
148 }
149 }
150}
151
bellard7fe48482004-10-09 18:08:01 +0000152static int monitor_fprintf(FILE *stream, const char *fmt, ...)
153{
154 va_list ap;
155 va_start(ap, fmt);
156 term_vprintf(fmt, ap);
157 va_end(ap);
158 return 0;
159}
160
bellard9dc39cb2004-03-14 21:38:27 +0000161static int compare_cmd(const char *name, const char *list)
162{
163 const char *p, *pstart;
164 int len;
165 len = strlen(name);
166 p = list;
167 for(;;) {
168 pstart = p;
169 p = strchr(p, '|');
170 if (!p)
171 p = pstart + strlen(pstart);
172 if ((p - pstart) == len && !memcmp(pstart, name, len))
173 return 1;
174 if (*p == '\0')
175 break;
176 p++;
177 }
178 return 0;
179}
180
blueswir18662d652008-10-02 18:32:44 +0000181static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000182{
blueswir18662d652008-10-02 18:32:44 +0000183 const term_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000184
185 for(cmd = cmds; cmd->name != NULL; cmd++) {
186 if (!name || !strcmp(name, cmd->name))
187 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
188 }
189}
190
191static void help_cmd(const char *name)
192{
193 if (name && !strcmp(name, "info")) {
194 help_cmd1(info_cmds, "info ", NULL);
195 } else {
196 help_cmd1(term_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000197 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000198 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +0000199 term_printf("Log items (comma separated):\n");
200 term_printf("%-10s %s\n", "none", "remove all logs");
201 for(item = cpu_log_items; item->mask != 0; item++) {
202 term_printf("%-10s %s\n", item->name, item->help);
203 }
204 }
bellard9dc39cb2004-03-14 21:38:27 +0000205 }
206}
207
bellard9307c4c2004-04-04 12:57:25 +0000208static void do_help(const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000209{
bellard9307c4c2004-04-04 12:57:25 +0000210 help_cmd(name);
bellard9dc39cb2004-03-14 21:38:27 +0000211}
212
bellard7954c732006-08-01 15:52:40 +0000213static void do_commit(const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000214{
bellard7954c732006-08-01 15:52:40 +0000215 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000216
bellard7954c732006-08-01 15:52:40 +0000217 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000218 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000219 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000220 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
221 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000222 }
223}
224
bellard9307c4c2004-04-04 12:57:25 +0000225static void do_info(const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000226{
blueswir18662d652008-10-02 18:32:44 +0000227 const term_cmd_t *cmd;
blueswir1a5f1b962008-08-17 20:21:51 +0000228 void (*handler)(void);
bellard9dc39cb2004-03-14 21:38:27 +0000229
bellard9307c4c2004-04-04 12:57:25 +0000230 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000231 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000232 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000233 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000234 goto found;
235 }
236 help:
bellard9307c4c2004-04-04 12:57:25 +0000237 help_cmd("info");
bellard9dc39cb2004-03-14 21:38:27 +0000238 return;
239 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000240 handler = cmd->handler;
241 handler();
bellard9dc39cb2004-03-14 21:38:27 +0000242}
243
bellard9bc9d1c2004-10-10 15:15:51 +0000244static void do_info_version(void)
245{
246 term_printf("%s\n", QEMU_VERSION);
247}
248
thsc35734b2007-03-19 15:17:08 +0000249static void do_info_name(void)
250{
251 if (qemu_name)
252 term_printf("%s\n", qemu_name);
253}
254
aliguori16b29ae2008-12-17 23:28:44 +0000255static void do_info_hpet(void)
256{
257 term_printf("HPET is %s by QEMU\n", (no_hpet) ? "disabled" : "enabled");
258}
259
blueswir1f1f23ad2008-09-18 18:30:20 +0000260static void do_info_uuid(void)
261{
262 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
263 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
264 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
265 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
266 qemu_uuid[15]);
267}
268
bellard9307c4c2004-04-04 12:57:25 +0000269static void do_info_block(void)
bellard9dc39cb2004-03-14 21:38:27 +0000270{
271 bdrv_info();
272}
273
thsa36e69d2007-12-02 05:18:19 +0000274static void do_info_blockstats(void)
275{
276 bdrv_info_stats();
277}
278
bellard6a00d602005-11-21 23:25:50 +0000279/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000280static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000281{
282 CPUState *env;
283
284 for(env = first_cpu; env != NULL; env = env->next_cpu) {
285 if (env->cpu_index == cpu_index) {
286 mon_cpu = env;
287 return 0;
288 }
289 }
290 return -1;
291}
292
pbrook9596ebb2007-11-18 01:44:38 +0000293static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000294{
295 if (!mon_cpu) {
296 mon_set_cpu(0);
297 }
298 return mon_cpu;
299}
300
bellard9307c4c2004-04-04 12:57:25 +0000301static void do_info_registers(void)
302{
bellard6a00d602005-11-21 23:25:50 +0000303 CPUState *env;
304 env = mon_get_cpu();
305 if (!env)
306 return;
bellard9307c4c2004-04-04 12:57:25 +0000307#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000308 cpu_dump_state(env, NULL, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000309 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000310#else
ths5fafdf22007-09-16 21:08:06 +0000311 cpu_dump_state(env, NULL, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000312 0);
bellard9307c4c2004-04-04 12:57:25 +0000313#endif
314}
315
bellard6a00d602005-11-21 23:25:50 +0000316static void do_info_cpus(void)
317{
318 CPUState *env;
319
320 /* just to set the default cpu if not already done */
321 mon_get_cpu();
322
323 for(env = first_cpu; env != NULL; env = env->next_cpu) {
ths5fafdf22007-09-16 21:08:06 +0000324 term_printf("%c CPU #%d:",
bellard6a00d602005-11-21 23:25:50 +0000325 (env == mon_cpu) ? '*' : ' ',
326 env->cpu_index);
327#if defined(TARGET_I386)
328 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000329#elif defined(TARGET_PPC)
330 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000331#elif defined(TARGET_SPARC)
332 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000333#elif defined(TARGET_MIPS)
thsb5dc7732008-06-27 10:02:35 +0000334 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000335#endif
thsead93602007-09-06 00:18:15 +0000336 if (env->halted)
337 term_printf(" (halted)");
bellard6a00d602005-11-21 23:25:50 +0000338 term_printf("\n");
339 }
340}
341
342static void do_cpu_set(int index)
343{
344 if (mon_set_cpu(index) < 0)
345 term_printf("Invalid CPU index\n");
346}
347
bellarde3db7222005-01-26 22:00:47 +0000348static void do_info_jit(void)
349{
350 dump_exec_info(NULL, monitor_fprintf);
351}
352
bellardaa455482004-04-04 13:07:25 +0000353static void do_info_history (void)
354{
355 int i;
bellard7e2515e2004-08-01 21:52:19 +0000356 const char *str;
ths3b46e622007-09-17 08:09:54 +0000357
bellard7e2515e2004-08-01 21:52:19 +0000358 i = 0;
359 for(;;) {
360 str = readline_get_history(i);
361 if (!str)
362 break;
363 term_printf("%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000364 i++;
bellardaa455482004-04-04 13:07:25 +0000365 }
366}
367
j_mayer76a66252007-03-07 08:32:30 +0000368#if defined(TARGET_PPC)
369/* XXX: not implemented in other targets */
370static void do_info_cpu_stats (void)
371{
372 CPUState *env;
373
374 env = mon_get_cpu();
375 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
376}
377#endif
378
bellard9307c4c2004-04-04 12:57:25 +0000379static void do_quit(void)
bellard9dc39cb2004-03-14 21:38:27 +0000380{
381 exit(0);
382}
383
384static int eject_device(BlockDriverState *bs, int force)
385{
386 if (bdrv_is_inserted(bs)) {
387 if (!force) {
388 if (!bdrv_is_removable(bs)) {
389 term_printf("device is not removable\n");
390 return -1;
391 }
392 if (bdrv_is_locked(bs)) {
393 term_printf("device is locked\n");
394 return -1;
395 }
396 }
397 bdrv_close(bs);
398 }
399 return 0;
400}
401
bellard9307c4c2004-04-04 12:57:25 +0000402static void do_eject(int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000403{
404 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000405
bellard9307c4c2004-04-04 12:57:25 +0000406 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000407 if (!bs) {
408 term_printf("device not found\n");
409 return;
410 }
411 eject_device(bs, force);
412}
413
aurel322ecea9b2008-06-18 22:10:01 +0000414static void do_change_block(const char *device, const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000415{
416 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000417 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000418
bellard9307c4c2004-04-04 12:57:25 +0000419 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000420 if (!bs) {
421 term_printf("device not found\n");
422 return;
423 }
aurel322ecea9b2008-06-18 22:10:01 +0000424 if (fmt) {
425 drv = bdrv_find_format(fmt);
426 if (!drv) {
427 term_printf("invalid format %s\n", fmt);
428 return;
429 }
430 }
bellard9dc39cb2004-03-14 21:38:27 +0000431 if (eject_device(bs, 0) < 0)
432 return;
aurel322ecea9b2008-06-18 22:10:01 +0000433 bdrv_open2(bs, filename, 0, drv);
balrog2bac6012007-04-30 01:34:31 +0000434 qemu_key_check(bs, filename);
bellard9dc39cb2004-03-14 21:38:27 +0000435}
436
aliguori2569da02008-12-10 15:14:13 +0000437static void do_change_vnc(const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000438{
ths70848512007-08-25 01:37:05 +0000439 if (strcmp(target, "passwd") == 0 ||
440 strcmp(target, "password") == 0) {
441 char password[9];
aliguori2569da02008-12-10 15:14:13 +0000442 if (arg) {
443 strncpy(password, arg, sizeof(password));
444 password[sizeof(password) - 1] = '\0';
445 } else
446 monitor_readline("Password: ", 1, password, sizeof(password));
ths70848512007-08-25 01:37:05 +0000447 if (vnc_display_password(NULL, password) < 0)
448 term_printf("could not set VNC server password\n");
449 } else {
450 if (vnc_display_open(NULL, target) < 0)
451 term_printf("could not start VNC server on %s\n", target);
452 }
thse25a5822007-08-25 01:36:20 +0000453}
454
aliguori2569da02008-12-10 15:14:13 +0000455static void do_change(const char *device, const char *target, const char *arg)
thse25a5822007-08-25 01:36:20 +0000456{
457 if (strcmp(device, "vnc") == 0) {
aliguori2569da02008-12-10 15:14:13 +0000458 do_change_vnc(target, arg);
thse25a5822007-08-25 01:36:20 +0000459 } else {
aliguori2569da02008-12-10 15:14:13 +0000460 do_change_block(device, target, arg);
thse25a5822007-08-25 01:36:20 +0000461 }
462}
463
bellard9307c4c2004-04-04 12:57:25 +0000464static void do_screen_dump(const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000465{
pbrook95219892006-04-09 01:06:34 +0000466 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000467}
468
pbrooke735b912007-06-30 13:53:24 +0000469static void do_logfile(const char *filename)
470{
471 cpu_set_log_filename(filename);
472}
473
bellard9307c4c2004-04-04 12:57:25 +0000474static void do_log(const char *items)
bellardf193c792004-03-21 17:06:25 +0000475{
476 int mask;
ths3b46e622007-09-17 08:09:54 +0000477
bellard9307c4c2004-04-04 12:57:25 +0000478 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000479 mask = 0;
480 } else {
bellard9307c4c2004-04-04 12:57:25 +0000481 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000482 if (!mask) {
bellard9307c4c2004-04-04 12:57:25 +0000483 help_cmd("log");
bellardf193c792004-03-21 17:06:25 +0000484 return;
485 }
486 }
487 cpu_set_log(mask);
488}
489
bellard9307c4c2004-04-04 12:57:25 +0000490static void do_stop(void)
bellard8a7ddc32004-03-31 19:00:16 +0000491{
492 vm_stop(EXCP_INTERRUPT);
493}
494
bellard9307c4c2004-04-04 12:57:25 +0000495static void do_cont(void)
bellard8a7ddc32004-03-31 19:00:16 +0000496{
497 vm_start();
498}
499
bellard67b915a2004-03-31 23:37:16 +0000500#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +0000501static void do_gdbserver(const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000502{
pbrookcfc34752007-02-22 01:48:01 +0000503 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000504 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000505 if (gdbserver_start(port) < 0) {
506 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000507 } else {
pbrookcfc34752007-02-22 01:48:01 +0000508 qemu_printf("Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000509 }
510}
bellard67b915a2004-03-31 23:37:16 +0000511#endif
bellard8a7ddc32004-03-31 19:00:16 +0000512
bellard9307c4c2004-04-04 12:57:25 +0000513static void term_printc(int c)
514{
515 term_printf("'");
516 switch(c) {
517 case '\'':
518 term_printf("\\'");
519 break;
520 case '\\':
521 term_printf("\\\\");
522 break;
523 case '\n':
524 term_printf("\\n");
525 break;
526 case '\r':
527 term_printf("\\r");
528 break;
529 default:
530 if (c >= 32 && c <= 126) {
531 term_printf("%c", c);
532 } else {
533 term_printf("\\x%02x", c);
534 }
535 break;
536 }
537 term_printf("'");
538}
539
ths5fafdf22007-09-16 21:08:06 +0000540static void memory_dump(int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000541 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000542{
bellard6a00d602005-11-21 23:25:50 +0000543 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000544 int nb_per_line, l, line_size, i, max_digits, len;
545 uint8_t buf[16];
546 uint64_t v;
547
548 if (format == 'i') {
549 int flags;
550 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000551 env = mon_get_cpu();
552 if (!env && !is_physical)
553 return;
bellard9307c4c2004-04-04 12:57:25 +0000554#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000555 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000556 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000557 } else if (wsize == 4) {
558 flags = 0;
559 } else {
bellard6a15fd12006-04-12 21:07:07 +0000560 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000561 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000562 if (env) {
563#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000564 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000565 (env->segs[R_CS].flags & DESC_L_MASK))
566 flags = 2;
567 else
568#endif
569 if (!(env->segs[R_CS].flags & DESC_B_MASK))
570 flags = 1;
571 }
bellard4c27ba22004-04-25 18:05:08 +0000572 }
573#endif
bellard6a00d602005-11-21 23:25:50 +0000574 monitor_disas(env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000575 return;
576 }
577
578 len = wsize * count;
579 if (wsize == 1)
580 line_size = 8;
581 else
582 line_size = 16;
583 nb_per_line = line_size / wsize;
584 max_digits = 0;
585
586 switch(format) {
587 case 'o':
588 max_digits = (wsize * 8 + 2) / 3;
589 break;
590 default:
591 case 'x':
592 max_digits = (wsize * 8) / 4;
593 break;
594 case 'u':
595 case 'd':
596 max_digits = (wsize * 8 * 10 + 32) / 33;
597 break;
598 case 'c':
599 wsize = 1;
600 break;
601 }
602
603 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000604 if (is_physical)
605 term_printf(TARGET_FMT_plx ":", addr);
606 else
607 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000608 l = len;
609 if (l > line_size)
610 l = line_size;
611 if (is_physical) {
612 cpu_physical_memory_rw(addr, buf, l, 0);
613 } else {
bellard6a00d602005-11-21 23:25:50 +0000614 env = mon_get_cpu();
615 if (!env)
616 break;
aliguoric8f79b62008-08-18 14:00:20 +0000617 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
618 term_printf(" Cannot access memory\n");
619 break;
620 }
bellard9307c4c2004-04-04 12:57:25 +0000621 }
ths5fafdf22007-09-16 21:08:06 +0000622 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000623 while (i < l) {
624 switch(wsize) {
625 default:
626 case 1:
627 v = ldub_raw(buf + i);
628 break;
629 case 2:
630 v = lduw_raw(buf + i);
631 break;
632 case 4:
bellard92a31b12005-02-10 22:00:52 +0000633 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000634 break;
635 case 8:
636 v = ldq_raw(buf + i);
637 break;
638 }
639 term_printf(" ");
640 switch(format) {
641 case 'o':
bellard26a76462006-06-25 18:15:32 +0000642 term_printf("%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000643 break;
644 case 'x':
bellard26a76462006-06-25 18:15:32 +0000645 term_printf("0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000646 break;
647 case 'u':
bellard26a76462006-06-25 18:15:32 +0000648 term_printf("%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000649 break;
650 case 'd':
bellard26a76462006-06-25 18:15:32 +0000651 term_printf("%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000652 break;
653 case 'c':
654 term_printc(v);
655 break;
656 }
657 i += wsize;
658 }
659 term_printf("\n");
660 addr += l;
661 len -= l;
662 }
663}
664
bellard92a31b12005-02-10 22:00:52 +0000665#if TARGET_LONG_BITS == 64
666#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
667#else
668#define GET_TLONG(h, l) (l)
669#endif
670
ths5fafdf22007-09-16 21:08:06 +0000671static void do_memory_dump(int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000672 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000673{
bellard92a31b12005-02-10 22:00:52 +0000674 target_long addr = GET_TLONG(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000675 memory_dump(count, format, size, addr, 0);
676}
677
blueswir17743e582007-09-24 18:39:04 +0000678#if TARGET_PHYS_ADDR_BITS > 32
679#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
680#else
681#define GET_TPHYSADDR(h, l) (l)
682#endif
683
bellard92a31b12005-02-10 22:00:52 +0000684static void do_physical_memory_dump(int count, int format, int size,
685 uint32_t addrh, uint32_t addrl)
686
bellard9307c4c2004-04-04 12:57:25 +0000687{
blueswir17743e582007-09-24 18:39:04 +0000688 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000689 memory_dump(count, format, size, addr, 1);
690}
691
bellard92a31b12005-02-10 22:00:52 +0000692static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000693{
blueswir17743e582007-09-24 18:39:04 +0000694 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
695#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000696 switch(format) {
697 case 'o':
698 term_printf("%#o", val);
699 break;
700 case 'x':
701 term_printf("%#x", val);
702 break;
703 case 'u':
704 term_printf("%u", val);
705 break;
706 default:
707 case 'd':
708 term_printf("%d", val);
709 break;
710 case 'c':
711 term_printc(val);
712 break;
713 }
bellard92a31b12005-02-10 22:00:52 +0000714#else
715 switch(format) {
716 case 'o':
bellard26a76462006-06-25 18:15:32 +0000717 term_printf("%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000718 break;
719 case 'x':
bellard26a76462006-06-25 18:15:32 +0000720 term_printf("%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000721 break;
722 case 'u':
bellard26a76462006-06-25 18:15:32 +0000723 term_printf("%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000724 break;
725 default:
726 case 'd':
bellard26a76462006-06-25 18:15:32 +0000727 term_printf("%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000728 break;
729 case 'c':
730 term_printc(val);
731 break;
732 }
733#endif
bellard9307c4c2004-04-04 12:57:25 +0000734 term_printf("\n");
735}
736
ths5fafdf22007-09-16 21:08:06 +0000737static void do_memory_save(unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000738 uint32_t size, const char *filename)
739{
740 FILE *f;
741 target_long addr = GET_TLONG(valh, vall);
742 uint32_t l;
743 CPUState *env;
744 uint8_t buf[1024];
745
746 env = mon_get_cpu();
747 if (!env)
748 return;
749
750 f = fopen(filename, "wb");
751 if (!f) {
752 term_printf("could not open '%s'\n", filename);
753 return;
754 }
755 while (size != 0) {
756 l = sizeof(buf);
757 if (l > size)
758 l = size;
759 cpu_memory_rw_debug(env, addr, buf, l, 0);
760 fwrite(buf, 1, l, f);
761 addr += l;
762 size -= l;
763 }
764 fclose(f);
765}
766
aurel32a8bdf7a2008-04-11 21:36:14 +0000767static void do_physical_memory_save(unsigned int valh, unsigned int vall,
768 uint32_t size, const char *filename)
769{
770 FILE *f;
771 uint32_t l;
772 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000773 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000774
775 f = fopen(filename, "wb");
776 if (!f) {
777 term_printf("could not open '%s'\n", filename);
778 return;
779 }
780 while (size != 0) {
781 l = sizeof(buf);
782 if (l > size)
783 l = size;
784 cpu_physical_memory_rw(addr, buf, l, 0);
785 fwrite(buf, 1, l, f);
786 fflush(f);
787 addr += l;
788 size -= l;
789 }
790 fclose(f);
791}
792
bellarde4cf1ad2005-06-04 20:15:57 +0000793static void do_sum(uint32_t start, uint32_t size)
794{
795 uint32_t addr;
796 uint8_t buf[1];
797 uint16_t sum;
798
799 sum = 0;
800 for(addr = start; addr < (start + size); addr++) {
801 cpu_physical_memory_rw(addr, buf, 1, 0);
802 /* BSD sum algorithm ('sum' Unix command) */
803 sum = (sum >> 1) | (sum << 15);
804 sum += buf[0];
805 }
806 term_printf("%05d\n", sum);
807}
808
bellarda3a91a32004-06-04 11:06:21 +0000809typedef struct {
810 int keycode;
811 const char *name;
812} KeyDef;
813
814static const KeyDef key_defs[] = {
815 { 0x2a, "shift" },
816 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000817
bellarda3a91a32004-06-04 11:06:21 +0000818 { 0x38, "alt" },
819 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000820 { 0x64, "altgr" },
821 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000822 { 0x1d, "ctrl" },
823 { 0x9d, "ctrl_r" },
824
825 { 0xdd, "menu" },
826
827 { 0x01, "esc" },
828
829 { 0x02, "1" },
830 { 0x03, "2" },
831 { 0x04, "3" },
832 { 0x05, "4" },
833 { 0x06, "5" },
834 { 0x07, "6" },
835 { 0x08, "7" },
836 { 0x09, "8" },
837 { 0x0a, "9" },
838 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000839 { 0x0c, "minus" },
840 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000841 { 0x0e, "backspace" },
842
843 { 0x0f, "tab" },
844 { 0x10, "q" },
845 { 0x11, "w" },
846 { 0x12, "e" },
847 { 0x13, "r" },
848 { 0x14, "t" },
849 { 0x15, "y" },
850 { 0x16, "u" },
851 { 0x17, "i" },
852 { 0x18, "o" },
853 { 0x19, "p" },
854
855 { 0x1c, "ret" },
856
857 { 0x1e, "a" },
858 { 0x1f, "s" },
859 { 0x20, "d" },
860 { 0x21, "f" },
861 { 0x22, "g" },
862 { 0x23, "h" },
863 { 0x24, "j" },
864 { 0x25, "k" },
865 { 0x26, "l" },
866
867 { 0x2c, "z" },
868 { 0x2d, "x" },
869 { 0x2e, "c" },
870 { 0x2f, "v" },
871 { 0x30, "b" },
872 { 0x31, "n" },
873 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000874 { 0x33, "comma" },
875 { 0x34, "dot" },
876 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000877
balrog4d3b6f62008-02-10 16:33:14 +0000878 { 0x37, "asterisk" },
879
bellarda3a91a32004-06-04 11:06:21 +0000880 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000881 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000882 { 0x3b, "f1" },
883 { 0x3c, "f2" },
884 { 0x3d, "f3" },
885 { 0x3e, "f4" },
886 { 0x3f, "f5" },
887 { 0x40, "f6" },
888 { 0x41, "f7" },
889 { 0x42, "f8" },
890 { 0x43, "f9" },
891 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000892 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000893 { 0x46, "scroll_lock" },
894
bellard64866c32006-05-07 18:03:31 +0000895 { 0xb5, "kp_divide" },
896 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000897 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000898 { 0x4e, "kp_add" },
899 { 0x9c, "kp_enter" },
900 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000901 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000902
903 { 0x52, "kp_0" },
904 { 0x4f, "kp_1" },
905 { 0x50, "kp_2" },
906 { 0x51, "kp_3" },
907 { 0x4b, "kp_4" },
908 { 0x4c, "kp_5" },
909 { 0x4d, "kp_6" },
910 { 0x47, "kp_7" },
911 { 0x48, "kp_8" },
912 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000913
bellarda3a91a32004-06-04 11:06:21 +0000914 { 0x56, "<" },
915
916 { 0x57, "f11" },
917 { 0x58, "f12" },
918
919 { 0xb7, "print" },
920
921 { 0xc7, "home" },
922 { 0xc9, "pgup" },
923 { 0xd1, "pgdn" },
924 { 0xcf, "end" },
925
926 { 0xcb, "left" },
927 { 0xc8, "up" },
928 { 0xd0, "down" },
929 { 0xcd, "right" },
930
931 { 0xd2, "insert" },
932 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +0000933#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
934 { 0xf0, "stop" },
935 { 0xf1, "again" },
936 { 0xf2, "props" },
937 { 0xf3, "undo" },
938 { 0xf4, "front" },
939 { 0xf5, "copy" },
940 { 0xf6, "open" },
941 { 0xf7, "paste" },
942 { 0xf8, "find" },
943 { 0xf9, "cut" },
944 { 0xfa, "lf" },
945 { 0xfb, "help" },
946 { 0xfc, "meta_l" },
947 { 0xfd, "meta_r" },
948 { 0xfe, "compose" },
949#endif
bellarda3a91a32004-06-04 11:06:21 +0000950 { 0, NULL },
951};
952
953static int get_keycode(const char *key)
954{
955 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +0000956 char *endp;
957 int ret;
bellarda3a91a32004-06-04 11:06:21 +0000958
959 for(p = key_defs; p->name != NULL; p++) {
960 if (!strcmp(key, p->name))
961 return p->keycode;
962 }
bellard64866c32006-05-07 18:03:31 +0000963 if (strstart(key, "0x", NULL)) {
964 ret = strtoul(key, &endp, 0);
965 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
966 return ret;
967 }
bellarda3a91a32004-06-04 11:06:21 +0000968 return -1;
969}
970
balrogc8256f92008-06-08 22:45:01 +0000971#define MAX_KEYCODES 16
972static uint8_t keycodes[MAX_KEYCODES];
973static int nb_pending_keycodes;
974static QEMUTimer *key_timer;
975
976static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +0000977{
balrogc8256f92008-06-08 22:45:01 +0000978 int keycode;
979
980 while (nb_pending_keycodes > 0) {
981 nb_pending_keycodes--;
982 keycode = keycodes[nb_pending_keycodes];
983 if (keycode & 0x80)
984 kbd_put_keycode(0xe0);
985 kbd_put_keycode(keycode | 0x80);
986 }
987}
988
989static void do_sendkey(const char *string, int has_hold_time, int hold_time)
990{
balrog3401c0d2008-06-04 10:05:59 +0000991 char keyname_buf[16];
992 char *separator;
993 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +0000994
balrogc8256f92008-06-08 22:45:01 +0000995 if (nb_pending_keycodes > 0) {
996 qemu_del_timer(key_timer);
997 release_keys(NULL);
998 }
999 if (!has_hold_time)
1000 hold_time = 100;
1001 i = 0;
balrog3401c0d2008-06-04 10:05:59 +00001002 while (1) {
1003 separator = strchr(string, '-');
1004 keyname_len = separator ? separator - string : strlen(string);
1005 if (keyname_len > 0) {
1006 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1007 if (keyname_len > sizeof(keyname_buf) - 1) {
1008 term_printf("invalid key: '%s...'\n", keyname_buf);
1009 return;
bellarda3a91a32004-06-04 11:06:21 +00001010 }
balrogc8256f92008-06-08 22:45:01 +00001011 if (i == MAX_KEYCODES) {
balrog3401c0d2008-06-04 10:05:59 +00001012 term_printf("too many keys\n");
1013 return;
1014 }
1015 keyname_buf[keyname_len] = 0;
1016 keycode = get_keycode(keyname_buf);
1017 if (keycode < 0) {
1018 term_printf("unknown key: '%s'\n", keyname_buf);
1019 return;
1020 }
balrogc8256f92008-06-08 22:45:01 +00001021 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001022 }
balrog3401c0d2008-06-04 10:05:59 +00001023 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001024 break;
balrog3401c0d2008-06-04 10:05:59 +00001025 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001026 }
balrogc8256f92008-06-08 22:45:01 +00001027 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001028 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001029 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001030 keycode = keycodes[i];
1031 if (keycode & 0x80)
1032 kbd_put_keycode(0xe0);
1033 kbd_put_keycode(keycode & 0x7f);
1034 }
balrogc8256f92008-06-08 22:45:01 +00001035 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001036 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1037 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001038}
1039
bellard13224a82006-07-14 22:03:35 +00001040static int mouse_button_state;
1041
ths5fafdf22007-09-16 21:08:06 +00001042static void do_mouse_move(const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001043 const char *dz_str)
1044{
1045 int dx, dy, dz;
1046 dx = strtol(dx_str, NULL, 0);
1047 dy = strtol(dy_str, NULL, 0);
1048 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001049 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001050 dz = strtol(dz_str, NULL, 0);
1051 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1052}
1053
1054static void do_mouse_button(int button_state)
1055{
1056 mouse_button_state = button_state;
1057 kbd_mouse_event(0, 0, 0, mouse_button_state);
1058}
1059
bellard34405572004-06-08 00:55:58 +00001060static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1061{
1062 uint32_t val;
1063 int suffix;
1064
1065 if (has_index) {
1066 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1067 addr++;
1068 }
1069 addr &= 0xffff;
1070
1071 switch(size) {
1072 default:
1073 case 1:
1074 val = cpu_inb(NULL, addr);
1075 suffix = 'b';
1076 break;
1077 case 2:
1078 val = cpu_inw(NULL, addr);
1079 suffix = 'w';
1080 break;
1081 case 4:
1082 val = cpu_inl(NULL, addr);
1083 suffix = 'l';
1084 break;
1085 }
1086 term_printf("port%c[0x%04x] = %#0*x\n",
1087 suffix, addr, size * 2, val);
1088}
bellarda3a91a32004-06-04 11:06:21 +00001089
blueswir13b4366d2008-06-20 16:25:06 +00001090/* boot_set handler */
1091static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1092static void *boot_opaque;
1093
1094void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1095{
1096 qemu_boot_set_handler = func;
1097 boot_opaque = opaque;
1098}
1099
aurel320ecdffb2008-05-04 20:11:34 +00001100static void do_boot_set(const char *bootdevice)
1101{
1102 int res;
1103
1104 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001105 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001106 if (res == 0)
1107 term_printf("boot device list now set to %s\n", bootdevice);
1108 else
1109 term_printf("setting boot device list failed with error %i\n", res);
1110 } else {
1111 term_printf("no function defined to set boot device list for this architecture\n");
1112 }
1113}
1114
bellarde4f90822004-06-20 12:35:44 +00001115static void do_system_reset(void)
1116{
1117 qemu_system_reset_request();
1118}
1119
bellard34751872005-07-02 14:31:34 +00001120static void do_system_powerdown(void)
1121{
1122 qemu_system_powerdown_request();
1123}
1124
bellardb86bda52004-09-18 19:32:46 +00001125#if defined(TARGET_I386)
1126static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1127{
ths5fafdf22007-09-16 21:08:06 +00001128 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
bellardb86bda52004-09-18 19:32:46 +00001129 addr,
1130 pte & mask,
1131 pte & PG_GLOBAL_MASK ? 'G' : '-',
1132 pte & PG_PSE_MASK ? 'P' : '-',
1133 pte & PG_DIRTY_MASK ? 'D' : '-',
1134 pte & PG_ACCESSED_MASK ? 'A' : '-',
1135 pte & PG_PCD_MASK ? 'C' : '-',
1136 pte & PG_PWT_MASK ? 'T' : '-',
1137 pte & PG_USER_MASK ? 'U' : '-',
1138 pte & PG_RW_MASK ? 'W' : '-');
1139}
1140
1141static void tlb_info(void)
1142{
bellard6a00d602005-11-21 23:25:50 +00001143 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001144 int l1, l2;
1145 uint32_t pgd, pde, pte;
1146
bellard6a00d602005-11-21 23:25:50 +00001147 env = mon_get_cpu();
1148 if (!env)
1149 return;
1150
bellardb86bda52004-09-18 19:32:46 +00001151 if (!(env->cr[0] & CR0_PG_MASK)) {
1152 term_printf("PG disabled\n");
1153 return;
1154 }
1155 pgd = env->cr[3] & ~0xfff;
1156 for(l1 = 0; l1 < 1024; l1++) {
1157 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1158 pde = le32_to_cpu(pde);
1159 if (pde & PG_PRESENT_MASK) {
1160 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1161 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1162 } else {
1163 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001164 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001165 (uint8_t *)&pte, 4);
1166 pte = le32_to_cpu(pte);
1167 if (pte & PG_PRESENT_MASK) {
ths5fafdf22007-09-16 21:08:06 +00001168 print_pte((l1 << 22) + (l2 << 12),
1169 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001170 ~0xfff);
1171 }
1172 }
1173 }
1174 }
1175 }
1176}
1177
ths5fafdf22007-09-16 21:08:06 +00001178static void mem_print(uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001179 uint32_t end, int prot)
1180{
bellard9746b152004-11-11 18:30:24 +00001181 int prot1;
1182 prot1 = *plast_prot;
1183 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001184 if (*pstart != -1) {
1185 term_printf("%08x-%08x %08x %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001186 *pstart, end, end - *pstart,
bellard9746b152004-11-11 18:30:24 +00001187 prot1 & PG_USER_MASK ? 'u' : '-',
bellardb86bda52004-09-18 19:32:46 +00001188 'r',
bellard9746b152004-11-11 18:30:24 +00001189 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001190 }
1191 if (prot != 0)
1192 *pstart = end;
1193 else
1194 *pstart = -1;
1195 *plast_prot = prot;
1196 }
1197}
1198
1199static void mem_info(void)
1200{
bellard6a00d602005-11-21 23:25:50 +00001201 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001202 int l1, l2, prot, last_prot;
1203 uint32_t pgd, pde, pte, start, end;
1204
bellard6a00d602005-11-21 23:25:50 +00001205 env = mon_get_cpu();
1206 if (!env)
1207 return;
1208
bellardb86bda52004-09-18 19:32:46 +00001209 if (!(env->cr[0] & CR0_PG_MASK)) {
1210 term_printf("PG disabled\n");
1211 return;
1212 }
1213 pgd = env->cr[3] & ~0xfff;
1214 last_prot = 0;
1215 start = -1;
1216 for(l1 = 0; l1 < 1024; l1++) {
1217 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1218 pde = le32_to_cpu(pde);
1219 end = l1 << 22;
1220 if (pde & PG_PRESENT_MASK) {
1221 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1222 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1223 mem_print(&start, &last_prot, end, prot);
1224 } else {
1225 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001226 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001227 (uint8_t *)&pte, 4);
1228 pte = le32_to_cpu(pte);
1229 end = (l1 << 22) + (l2 << 12);
1230 if (pte & PG_PRESENT_MASK) {
1231 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1232 } else {
1233 prot = 0;
1234 }
1235 mem_print(&start, &last_prot, end, prot);
1236 }
1237 }
1238 } else {
1239 prot = 0;
1240 mem_print(&start, &last_prot, end, prot);
1241 }
1242 }
1243}
1244#endif
1245
bellard0f4c6412005-07-24 18:10:56 +00001246static void do_info_kqemu(void)
1247{
1248#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001249 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001250 int val;
1251 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001252 env = mon_get_cpu();
1253 if (!env) {
1254 term_printf("No cpu initialized yet");
1255 return;
1256 }
1257 val = env->kqemu_enabled;
bellard5f1ce942006-02-08 22:40:15 +00001258 term_printf("kqemu support: ");
1259 switch(val) {
1260 default:
1261 case 0:
1262 term_printf("disabled\n");
1263 break;
1264 case 1:
1265 term_printf("enabled for user code\n");
1266 break;
1267 case 2:
1268 term_printf("enabled for user and kernel code\n");
1269 break;
1270 }
bellard0f4c6412005-07-24 18:10:56 +00001271#else
bellard5f1ce942006-02-08 22:40:15 +00001272 term_printf("kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001273#endif
ths5fafdf22007-09-16 21:08:06 +00001274}
bellard0f4c6412005-07-24 18:10:56 +00001275
aliguori7ba1e612008-11-05 16:04:33 +00001276static void do_info_kvm(void)
1277{
1278#ifdef CONFIG_KVM
1279 term_printf("kvm support: ");
1280 if (kvm_enabled())
1281 term_printf("enabled\n");
1282 else
1283 term_printf("disabled\n");
1284#else
1285 term_printf("kvm support: not compiled\n");
1286#endif
1287}
1288
bellard5f1ce942006-02-08 22:40:15 +00001289#ifdef CONFIG_PROFILER
1290
1291int64_t kqemu_time;
1292int64_t qemu_time;
1293int64_t kqemu_exec_count;
1294int64_t dev_time;
1295int64_t kqemu_ret_int_count;
1296int64_t kqemu_ret_excp_count;
1297int64_t kqemu_ret_intr_count;
1298
1299static void do_info_profile(void)
1300{
1301 int64_t total;
1302 total = qemu_time;
1303 if (total == 0)
1304 total = 1;
bellard26a76462006-06-25 18:15:32 +00001305 term_printf("async time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001306 dev_time, dev_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001307 term_printf("qemu time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001308 qemu_time, qemu_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001309 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
bellard5f1ce942006-02-08 22:40:15 +00001310 kqemu_time, kqemu_time / (double)ticks_per_sec,
1311 kqemu_time / (double)total * 100.0,
1312 kqemu_exec_count,
1313 kqemu_ret_int_count,
1314 kqemu_ret_excp_count,
1315 kqemu_ret_intr_count);
1316 qemu_time = 0;
1317 kqemu_time = 0;
1318 kqemu_exec_count = 0;
1319 dev_time = 0;
1320 kqemu_ret_int_count = 0;
1321 kqemu_ret_excp_count = 0;
1322 kqemu_ret_intr_count = 0;
1323#ifdef USE_KQEMU
1324 kqemu_record_dump();
1325#endif
1326}
1327#else
1328static void do_info_profile(void)
1329{
1330 term_printf("Internal profiler not compiled\n");
1331}
1332#endif
1333
bellardec36b692006-07-16 18:57:03 +00001334/* Capture support */
1335static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1336
1337static void do_info_capture (void)
1338{
1339 int i;
1340 CaptureState *s;
1341
1342 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1343 term_printf ("[%d]: ", i);
1344 s->ops.info (s->opaque);
1345 }
1346}
1347
1348static void do_stop_capture (int n)
1349{
1350 int i;
1351 CaptureState *s;
1352
1353 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1354 if (i == n) {
1355 s->ops.destroy (s->opaque);
1356 LIST_REMOVE (s, entries);
1357 qemu_free (s);
1358 return;
1359 }
1360 }
1361}
1362
1363#ifdef HAS_AUDIO
bellardec36b692006-07-16 18:57:03 +00001364static void do_wav_capture (const char *path,
1365 int has_freq, int freq,
1366 int has_bits, int bits,
1367 int has_channels, int nchannels)
1368{
1369 CaptureState *s;
1370
1371 s = qemu_mallocz (sizeof (*s));
1372 if (!s) {
1373 term_printf ("Not enough memory to add wave capture\n");
1374 return;
1375 }
1376
1377 freq = has_freq ? freq : 44100;
1378 bits = has_bits ? bits : 16;
1379 nchannels = has_channels ? nchannels : 2;
1380
1381 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1382 term_printf ("Faied to add wave capture\n");
1383 qemu_free (s);
1384 }
1385 LIST_INSERT_HEAD (&capture_head, s, entries);
1386}
1387#endif
1388
aurel32dc1c0b72008-04-27 23:52:12 +00001389#if defined(TARGET_I386)
1390static void do_inject_nmi(int cpu_index)
1391{
1392 CPUState *env;
1393
1394 for (env = first_cpu; env != NULL; env = env->next_cpu)
1395 if (env->cpu_index == cpu_index) {
1396 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1397 break;
1398 }
1399}
1400#endif
1401
aliguoridf751fa2008-12-04 20:19:35 +00001402static void do_balloon(int value)
1403{
1404 ram_addr_t target = value;
1405 qemu_balloon(target << 20);
1406}
1407
1408static void do_info_balloon(void)
1409{
1410 ram_addr_t actual;
1411
1412 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001413 if (kvm_enabled() && !kvm_has_sync_mmu())
1414 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1415 else if (actual == 0)
aliguoridf751fa2008-12-04 20:19:35 +00001416 term_printf("Ballooning not activated in VM\n");
1417 else
1418 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1419}
1420
blueswir18662d652008-10-02 18:32:44 +00001421static const term_cmd_t term_cmds[] = {
ths5fafdf22007-09-16 21:08:06 +00001422 { "help|?", "s?", do_help,
bellard9dc39cb2004-03-14 21:38:27 +00001423 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001424 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001425 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001426 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001427 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001428 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001429 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001430 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001431 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001432 { "change", "BFs?", do_change,
1433 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001434 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001435 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001436 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001437 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001438 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001439 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001440 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001441 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001442 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001443 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001444 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001445 "tag|id", "delete a VM snapshot from its tag or id" },
1446 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001447 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001448 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001449 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001450#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001451 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001452 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001453#endif
ths5fafdf22007-09-16 21:08:06 +00001454 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001455 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001456 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001457 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001458 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001459 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001460 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001461 "/fmt addr", "I/O port read" },
1462
balrogc8256f92008-06-08 22:45:01 +00001463 { "sendkey", "si?", do_sendkey,
1464 "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 +00001465 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001466 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001467 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001468 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001469 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001470 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001471 { "usb_add", "s", do_usb_add,
1472 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1473 { "usb_del", "s", do_usb_del,
1474 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001475 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001476 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001477 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001478 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001479 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001480 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001481 { "mouse_set", "i", do_mouse_set,
1482 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001483#ifdef HAS_AUDIO
1484 { "wavcapture", "si?i?i?", do_wav_capture,
1485 "path [frequency bits channels]",
1486 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1487#endif
1488 { "stopcapture", "i", do_stop_capture,
1489 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001490 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001491 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001492 { "pmemsave", "lis", do_physical_memory_save,
1493 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001494 { "boot_set", "s", do_boot_set,
1495 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001496#if defined(TARGET_I386)
1497 { "nmi", "i", do_inject_nmi,
1498 "cpu", "inject an NMI on the given CPU", },
1499#endif
aliguori5bb79102008-10-13 03:12:02 +00001500 { "migrate", "-ds", do_migrate,
1501 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1502 { "migrate_cancel", "", do_migrate_cancel,
1503 "", "cancel the current VM migration" },
1504 { "migrate_set_speed", "s", do_migrate_set_speed,
1505 "value", "set maximum speed (in bytes) for migrations" },
aliguoridf751fa2008-12-04 20:19:35 +00001506 { "balloon", "i", do_balloon,
1507 "target", "request VM to change it's memory allocation (in MB)" },
ths5fafdf22007-09-16 21:08:06 +00001508 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001509};
1510
blueswir18662d652008-10-02 18:32:44 +00001511static const term_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001512 { "version", "", do_info_version,
1513 "", "show the version of qemu" },
bellard9307c4c2004-04-04 12:57:25 +00001514 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001515 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001516 { "chardev", "", qemu_chr_info,
1517 "", "show the character devices" },
bellard9307c4c2004-04-04 12:57:25 +00001518 { "block", "", do_info_block,
bellard9dc39cb2004-03-14 21:38:27 +00001519 "", "show the block devices" },
thsa36e69d2007-12-02 05:18:19 +00001520 { "blockstats", "", do_info_blockstats,
1521 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001522 { "registers", "", do_info_registers,
1523 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001524 { "cpus", "", do_info_cpus,
1525 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001526 { "history", "", do_info_history,
1527 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001528 { "irq", "", irq_info,
1529 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001530 { "pic", "", pic_info,
1531 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001532 { "pci", "", pci_info,
1533 "", "show PCI info", },
bellardb86bda52004-09-18 19:32:46 +00001534#if defined(TARGET_I386)
1535 { "tlb", "", tlb_info,
1536 "", "show virtual to physical memory mappings", },
1537 { "mem", "", mem_info,
1538 "", "show the active virtual memory mappings", },
aliguori16b29ae2008-12-17 23:28:44 +00001539 { "hpet", "", do_info_hpet,
1540 "", "show state of HPET", },
bellardb86bda52004-09-18 19:32:46 +00001541#endif
bellarde3db7222005-01-26 22:00:47 +00001542 { "jit", "", do_info_jit,
1543 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001544 { "kqemu", "", do_info_kqemu,
1545 "", "show kqemu information", },
aliguori7ba1e612008-11-05 16:04:33 +00001546 { "kvm", "", do_info_kvm,
1547 "", "show kvm information", },
bellarda594cfb2005-11-06 16:13:29 +00001548 { "usb", "", usb_info,
1549 "", "show guest USB devices", },
1550 { "usbhost", "", usb_host_info,
1551 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001552 { "profile", "", do_info_profile,
1553 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001554 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001555 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001556 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001557 "", "show the currently saved VM snapshots" },
balrog201a51f2007-04-30 00:51:09 +00001558 { "pcmcia", "", pcmcia_info,
1559 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001560 { "mice", "", do_info_mice,
1561 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001562 { "vnc", "", do_info_vnc,
1563 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001564 { "name", "", do_info_name,
1565 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001566 { "uuid", "", do_info_uuid,
1567 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001568#if defined(TARGET_PPC)
1569 { "cpustats", "", do_info_cpu_stats,
1570 "", "show CPU statistics", },
1571#endif
blueswir131a60e22007-10-26 18:42:59 +00001572#if defined(CONFIG_SLIRP)
1573 { "slirp", "", do_info_slirp,
1574 "", "show SLIRP statistics", },
1575#endif
aliguori5bb79102008-10-13 03:12:02 +00001576 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001577 { "balloon", "", do_info_balloon,
1578 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001579 { NULL, NULL, },
1580};
1581
bellard9307c4c2004-04-04 12:57:25 +00001582/*******************************************************************/
1583
1584static const char *pch;
1585static jmp_buf expr_env;
1586
bellard92a31b12005-02-10 22:00:52 +00001587#define MD_TLONG 0
1588#define MD_I32 1
1589
bellard9307c4c2004-04-04 12:57:25 +00001590typedef struct MonitorDef {
1591 const char *name;
1592 int offset;
blueswir18662d652008-10-02 18:32:44 +00001593 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001594 int type;
bellard9307c4c2004-04-04 12:57:25 +00001595} MonitorDef;
1596
bellard57206fd2004-04-25 18:54:52 +00001597#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001598static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001599{
bellard6a00d602005-11-21 23:25:50 +00001600 CPUState *env = mon_get_cpu();
1601 if (!env)
1602 return 0;
1603 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001604}
1605#endif
1606
bellarda541f292004-04-12 20:39:29 +00001607#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001608static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001609{
bellard6a00d602005-11-21 23:25:50 +00001610 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001611 unsigned int u;
1612 int i;
1613
bellard6a00d602005-11-21 23:25:50 +00001614 if (!env)
1615 return 0;
1616
bellarda541f292004-04-12 20:39:29 +00001617 u = 0;
1618 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001619 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001620
1621 return u;
1622}
1623
blueswir18662d652008-10-02 18:32:44 +00001624static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001625{
bellard6a00d602005-11-21 23:25:50 +00001626 CPUState *env = mon_get_cpu();
1627 if (!env)
1628 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001629 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001630}
1631
blueswir18662d652008-10-02 18:32:44 +00001632static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001633{
bellard6a00d602005-11-21 23:25:50 +00001634 CPUState *env = mon_get_cpu();
1635 if (!env)
1636 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001637 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001638}
bellard9fddaa02004-05-21 12:59:32 +00001639
blueswir18662d652008-10-02 18:32:44 +00001640static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001641{
bellard6a00d602005-11-21 23:25:50 +00001642 CPUState *env = mon_get_cpu();
1643 if (!env)
1644 return 0;
1645 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001646}
1647
blueswir18662d652008-10-02 18:32:44 +00001648static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001649{
bellard6a00d602005-11-21 23:25:50 +00001650 CPUState *env = mon_get_cpu();
1651 if (!env)
1652 return 0;
1653 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001654}
1655
blueswir18662d652008-10-02 18:32:44 +00001656static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001657{
bellard6a00d602005-11-21 23:25:50 +00001658 CPUState *env = mon_get_cpu();
1659 if (!env)
1660 return 0;
1661 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001662}
bellarda541f292004-04-12 20:39:29 +00001663#endif
1664
bellarde95c8d52004-09-30 22:22:08 +00001665#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001666#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001667static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001668{
bellard6a00d602005-11-21 23:25:50 +00001669 CPUState *env = mon_get_cpu();
1670 if (!env)
1671 return 0;
1672 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001673}
bellard7b936c02005-10-30 17:05:13 +00001674#endif
bellarde95c8d52004-09-30 22:22:08 +00001675
blueswir18662d652008-10-02 18:32:44 +00001676static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001677{
bellard6a00d602005-11-21 23:25:50 +00001678 CPUState *env = mon_get_cpu();
1679 if (!env)
1680 return 0;
1681 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001682}
1683#endif
1684
blueswir18662d652008-10-02 18:32:44 +00001685static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001686#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001687
1688#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001689 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001690 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001691 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001692
bellard9307c4c2004-04-04 12:57:25 +00001693 { "eax", offsetof(CPUState, regs[0]) },
1694 { "ecx", offsetof(CPUState, regs[1]) },
1695 { "edx", offsetof(CPUState, regs[2]) },
1696 { "ebx", offsetof(CPUState, regs[3]) },
1697 { "esp|sp", offsetof(CPUState, regs[4]) },
1698 { "ebp|fp", offsetof(CPUState, regs[5]) },
1699 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001700 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001701#ifdef TARGET_X86_64
1702 { "r8", offsetof(CPUState, regs[8]) },
1703 { "r9", offsetof(CPUState, regs[9]) },
1704 { "r10", offsetof(CPUState, regs[10]) },
1705 { "r11", offsetof(CPUState, regs[11]) },
1706 { "r12", offsetof(CPUState, regs[12]) },
1707 { "r13", offsetof(CPUState, regs[13]) },
1708 { "r14", offsetof(CPUState, regs[14]) },
1709 { "r15", offsetof(CPUState, regs[15]) },
1710#endif
bellard9307c4c2004-04-04 12:57:25 +00001711 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001712 { "eip", offsetof(CPUState, eip) },
1713 SEG("cs", R_CS)
1714 SEG("ds", R_DS)
1715 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001716 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001717 SEG("fs", R_FS)
1718 SEG("gs", R_GS)
1719 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001720#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001721 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001722 { "r0", offsetof(CPUState, gpr[0]) },
1723 { "r1", offsetof(CPUState, gpr[1]) },
1724 { "r2", offsetof(CPUState, gpr[2]) },
1725 { "r3", offsetof(CPUState, gpr[3]) },
1726 { "r4", offsetof(CPUState, gpr[4]) },
1727 { "r5", offsetof(CPUState, gpr[5]) },
1728 { "r6", offsetof(CPUState, gpr[6]) },
1729 { "r7", offsetof(CPUState, gpr[7]) },
1730 { "r8", offsetof(CPUState, gpr[8]) },
1731 { "r9", offsetof(CPUState, gpr[9]) },
1732 { "r10", offsetof(CPUState, gpr[10]) },
1733 { "r11", offsetof(CPUState, gpr[11]) },
1734 { "r12", offsetof(CPUState, gpr[12]) },
1735 { "r13", offsetof(CPUState, gpr[13]) },
1736 { "r14", offsetof(CPUState, gpr[14]) },
1737 { "r15", offsetof(CPUState, gpr[15]) },
1738 { "r16", offsetof(CPUState, gpr[16]) },
1739 { "r17", offsetof(CPUState, gpr[17]) },
1740 { "r18", offsetof(CPUState, gpr[18]) },
1741 { "r19", offsetof(CPUState, gpr[19]) },
1742 { "r20", offsetof(CPUState, gpr[20]) },
1743 { "r21", offsetof(CPUState, gpr[21]) },
1744 { "r22", offsetof(CPUState, gpr[22]) },
1745 { "r23", offsetof(CPUState, gpr[23]) },
1746 { "r24", offsetof(CPUState, gpr[24]) },
1747 { "r25", offsetof(CPUState, gpr[25]) },
1748 { "r26", offsetof(CPUState, gpr[26]) },
1749 { "r27", offsetof(CPUState, gpr[27]) },
1750 { "r28", offsetof(CPUState, gpr[28]) },
1751 { "r29", offsetof(CPUState, gpr[29]) },
1752 { "r30", offsetof(CPUState, gpr[30]) },
1753 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001754 /* Floating point registers */
1755 { "f0", offsetof(CPUState, fpr[0]) },
1756 { "f1", offsetof(CPUState, fpr[1]) },
1757 { "f2", offsetof(CPUState, fpr[2]) },
1758 { "f3", offsetof(CPUState, fpr[3]) },
1759 { "f4", offsetof(CPUState, fpr[4]) },
1760 { "f5", offsetof(CPUState, fpr[5]) },
1761 { "f6", offsetof(CPUState, fpr[6]) },
1762 { "f7", offsetof(CPUState, fpr[7]) },
1763 { "f8", offsetof(CPUState, fpr[8]) },
1764 { "f9", offsetof(CPUState, fpr[9]) },
1765 { "f10", offsetof(CPUState, fpr[10]) },
1766 { "f11", offsetof(CPUState, fpr[11]) },
1767 { "f12", offsetof(CPUState, fpr[12]) },
1768 { "f13", offsetof(CPUState, fpr[13]) },
1769 { "f14", offsetof(CPUState, fpr[14]) },
1770 { "f15", offsetof(CPUState, fpr[15]) },
1771 { "f16", offsetof(CPUState, fpr[16]) },
1772 { "f17", offsetof(CPUState, fpr[17]) },
1773 { "f18", offsetof(CPUState, fpr[18]) },
1774 { "f19", offsetof(CPUState, fpr[19]) },
1775 { "f20", offsetof(CPUState, fpr[20]) },
1776 { "f21", offsetof(CPUState, fpr[21]) },
1777 { "f22", offsetof(CPUState, fpr[22]) },
1778 { "f23", offsetof(CPUState, fpr[23]) },
1779 { "f24", offsetof(CPUState, fpr[24]) },
1780 { "f25", offsetof(CPUState, fpr[25]) },
1781 { "f26", offsetof(CPUState, fpr[26]) },
1782 { "f27", offsetof(CPUState, fpr[27]) },
1783 { "f28", offsetof(CPUState, fpr[28]) },
1784 { "f29", offsetof(CPUState, fpr[29]) },
1785 { "f30", offsetof(CPUState, fpr[30]) },
1786 { "f31", offsetof(CPUState, fpr[31]) },
1787 { "fpscr", offsetof(CPUState, fpscr) },
1788 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00001789 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00001790 { "lr", offsetof(CPUState, lr) },
1791 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00001792 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00001793 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00001794 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00001795 { "msr", 0, &monitor_get_msr, },
1796 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00001797 { "tbu", 0, &monitor_get_tbu, },
1798 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00001799#if defined(TARGET_PPC64)
1800 /* Address space register */
1801 { "asr", offsetof(CPUState, asr) },
1802#endif
1803 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00001804 { "sdr1", offsetof(CPUState, sdr1) },
1805 { "sr0", offsetof(CPUState, sr[0]) },
1806 { "sr1", offsetof(CPUState, sr[1]) },
1807 { "sr2", offsetof(CPUState, sr[2]) },
1808 { "sr3", offsetof(CPUState, sr[3]) },
1809 { "sr4", offsetof(CPUState, sr[4]) },
1810 { "sr5", offsetof(CPUState, sr[5]) },
1811 { "sr6", offsetof(CPUState, sr[6]) },
1812 { "sr7", offsetof(CPUState, sr[7]) },
1813 { "sr8", offsetof(CPUState, sr[8]) },
1814 { "sr9", offsetof(CPUState, sr[9]) },
1815 { "sr10", offsetof(CPUState, sr[10]) },
1816 { "sr11", offsetof(CPUState, sr[11]) },
1817 { "sr12", offsetof(CPUState, sr[12]) },
1818 { "sr13", offsetof(CPUState, sr[13]) },
1819 { "sr14", offsetof(CPUState, sr[14]) },
1820 { "sr15", offsetof(CPUState, sr[15]) },
1821 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00001822#elif defined(TARGET_SPARC)
1823 { "g0", offsetof(CPUState, gregs[0]) },
1824 { "g1", offsetof(CPUState, gregs[1]) },
1825 { "g2", offsetof(CPUState, gregs[2]) },
1826 { "g3", offsetof(CPUState, gregs[3]) },
1827 { "g4", offsetof(CPUState, gregs[4]) },
1828 { "g5", offsetof(CPUState, gregs[5]) },
1829 { "g6", offsetof(CPUState, gregs[6]) },
1830 { "g7", offsetof(CPUState, gregs[7]) },
1831 { "o0", 0, monitor_get_reg },
1832 { "o1", 1, monitor_get_reg },
1833 { "o2", 2, monitor_get_reg },
1834 { "o3", 3, monitor_get_reg },
1835 { "o4", 4, monitor_get_reg },
1836 { "o5", 5, monitor_get_reg },
1837 { "o6", 6, monitor_get_reg },
1838 { "o7", 7, monitor_get_reg },
1839 { "l0", 8, monitor_get_reg },
1840 { "l1", 9, monitor_get_reg },
1841 { "l2", 10, monitor_get_reg },
1842 { "l3", 11, monitor_get_reg },
1843 { "l4", 12, monitor_get_reg },
1844 { "l5", 13, monitor_get_reg },
1845 { "l6", 14, monitor_get_reg },
1846 { "l7", 15, monitor_get_reg },
1847 { "i0", 16, monitor_get_reg },
1848 { "i1", 17, monitor_get_reg },
1849 { "i2", 18, monitor_get_reg },
1850 { "i3", 19, monitor_get_reg },
1851 { "i4", 20, monitor_get_reg },
1852 { "i5", 21, monitor_get_reg },
1853 { "i6", 22, monitor_get_reg },
1854 { "i7", 23, monitor_get_reg },
1855 { "pc", offsetof(CPUState, pc) },
1856 { "npc", offsetof(CPUState, npc) },
1857 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00001858#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00001859 { "psr", 0, &monitor_get_psr, },
1860 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00001861#endif
bellarde95c8d52004-09-30 22:22:08 +00001862 { "tbr", offsetof(CPUState, tbr) },
1863 { "fsr", offsetof(CPUState, fsr) },
1864 { "f0", offsetof(CPUState, fpr[0]) },
1865 { "f1", offsetof(CPUState, fpr[1]) },
1866 { "f2", offsetof(CPUState, fpr[2]) },
1867 { "f3", offsetof(CPUState, fpr[3]) },
1868 { "f4", offsetof(CPUState, fpr[4]) },
1869 { "f5", offsetof(CPUState, fpr[5]) },
1870 { "f6", offsetof(CPUState, fpr[6]) },
1871 { "f7", offsetof(CPUState, fpr[7]) },
1872 { "f8", offsetof(CPUState, fpr[8]) },
1873 { "f9", offsetof(CPUState, fpr[9]) },
1874 { "f10", offsetof(CPUState, fpr[10]) },
1875 { "f11", offsetof(CPUState, fpr[11]) },
1876 { "f12", offsetof(CPUState, fpr[12]) },
1877 { "f13", offsetof(CPUState, fpr[13]) },
1878 { "f14", offsetof(CPUState, fpr[14]) },
1879 { "f15", offsetof(CPUState, fpr[15]) },
1880 { "f16", offsetof(CPUState, fpr[16]) },
1881 { "f17", offsetof(CPUState, fpr[17]) },
1882 { "f18", offsetof(CPUState, fpr[18]) },
1883 { "f19", offsetof(CPUState, fpr[19]) },
1884 { "f20", offsetof(CPUState, fpr[20]) },
1885 { "f21", offsetof(CPUState, fpr[21]) },
1886 { "f22", offsetof(CPUState, fpr[22]) },
1887 { "f23", offsetof(CPUState, fpr[23]) },
1888 { "f24", offsetof(CPUState, fpr[24]) },
1889 { "f25", offsetof(CPUState, fpr[25]) },
1890 { "f26", offsetof(CPUState, fpr[26]) },
1891 { "f27", offsetof(CPUState, fpr[27]) },
1892 { "f28", offsetof(CPUState, fpr[28]) },
1893 { "f29", offsetof(CPUState, fpr[29]) },
1894 { "f30", offsetof(CPUState, fpr[30]) },
1895 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00001896#ifdef TARGET_SPARC64
1897 { "f32", offsetof(CPUState, fpr[32]) },
1898 { "f34", offsetof(CPUState, fpr[34]) },
1899 { "f36", offsetof(CPUState, fpr[36]) },
1900 { "f38", offsetof(CPUState, fpr[38]) },
1901 { "f40", offsetof(CPUState, fpr[40]) },
1902 { "f42", offsetof(CPUState, fpr[42]) },
1903 { "f44", offsetof(CPUState, fpr[44]) },
1904 { "f46", offsetof(CPUState, fpr[46]) },
1905 { "f48", offsetof(CPUState, fpr[48]) },
1906 { "f50", offsetof(CPUState, fpr[50]) },
1907 { "f52", offsetof(CPUState, fpr[52]) },
1908 { "f54", offsetof(CPUState, fpr[54]) },
1909 { "f56", offsetof(CPUState, fpr[56]) },
1910 { "f58", offsetof(CPUState, fpr[58]) },
1911 { "f60", offsetof(CPUState, fpr[60]) },
1912 { "f62", offsetof(CPUState, fpr[62]) },
1913 { "asi", offsetof(CPUState, asi) },
1914 { "pstate", offsetof(CPUState, pstate) },
1915 { "cansave", offsetof(CPUState, cansave) },
1916 { "canrestore", offsetof(CPUState, canrestore) },
1917 { "otherwin", offsetof(CPUState, otherwin) },
1918 { "wstate", offsetof(CPUState, wstate) },
1919 { "cleanwin", offsetof(CPUState, cleanwin) },
1920 { "fprs", offsetof(CPUState, fprs) },
1921#endif
bellard9307c4c2004-04-04 12:57:25 +00001922#endif
1923 { NULL },
1924};
1925
ths5fafdf22007-09-16 21:08:06 +00001926static void expr_error(const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001927{
bellard9307c4c2004-04-04 12:57:25 +00001928 term_printf(fmt);
1929 term_printf("\n");
1930 longjmp(expr_env, 1);
1931}
1932
bellard6a00d602005-11-21 23:25:50 +00001933/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00001934static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00001935{
blueswir18662d652008-10-02 18:32:44 +00001936 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00001937 void *ptr;
1938
bellard9307c4c2004-04-04 12:57:25 +00001939 for(md = monitor_defs; md->name != NULL; md++) {
1940 if (compare_cmd(name, md->name)) {
1941 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00001942 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00001943 } else {
bellard6a00d602005-11-21 23:25:50 +00001944 CPUState *env = mon_get_cpu();
1945 if (!env)
1946 return -2;
1947 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00001948 switch(md->type) {
1949 case MD_I32:
1950 *pval = *(int32_t *)ptr;
1951 break;
1952 case MD_TLONG:
1953 *pval = *(target_long *)ptr;
1954 break;
1955 default:
1956 *pval = 0;
1957 break;
1958 }
bellard9307c4c2004-04-04 12:57:25 +00001959 }
1960 return 0;
1961 }
1962 }
1963 return -1;
1964}
1965
1966static void next(void)
1967{
1968 if (pch != '\0') {
1969 pch++;
blueswir1cd390082008-11-16 13:53:32 +00001970 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00001971 pch++;
1972 }
1973}
1974
blueswir1c2efc952007-09-25 17:28:42 +00001975static int64_t expr_sum(void);
bellard9307c4c2004-04-04 12:57:25 +00001976
blueswir1c2efc952007-09-25 17:28:42 +00001977static int64_t expr_unary(void)
bellard9307c4c2004-04-04 12:57:25 +00001978{
blueswir1c2efc952007-09-25 17:28:42 +00001979 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00001980 char *p;
bellard6a00d602005-11-21 23:25:50 +00001981 int ret;
bellard9307c4c2004-04-04 12:57:25 +00001982
1983 switch(*pch) {
1984 case '+':
1985 next();
1986 n = expr_unary();
1987 break;
1988 case '-':
1989 next();
1990 n = -expr_unary();
1991 break;
1992 case '~':
1993 next();
1994 n = ~expr_unary();
1995 break;
1996 case '(':
1997 next();
1998 n = expr_sum();
1999 if (*pch != ')') {
2000 expr_error("')' expected");
2001 }
2002 next();
2003 break;
bellard81d09122004-07-14 17:21:37 +00002004 case '\'':
2005 pch++;
2006 if (*pch == '\0')
2007 expr_error("character constant expected");
2008 n = *pch;
2009 pch++;
2010 if (*pch != '\'')
2011 expr_error("missing terminating \' character");
2012 next();
2013 break;
bellard9307c4c2004-04-04 12:57:25 +00002014 case '$':
2015 {
2016 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002017 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002018
bellard9307c4c2004-04-04 12:57:25 +00002019 pch++;
2020 q = buf;
2021 while ((*pch >= 'a' && *pch <= 'z') ||
2022 (*pch >= 'A' && *pch <= 'Z') ||
2023 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002024 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002025 if ((q - buf) < sizeof(buf) - 1)
2026 *q++ = *pch;
2027 pch++;
2028 }
blueswir1cd390082008-11-16 13:53:32 +00002029 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002030 pch++;
2031 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002032 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002033 if (ret == -1)
bellard9307c4c2004-04-04 12:57:25 +00002034 expr_error("unknown register");
ths5fafdf22007-09-16 21:08:06 +00002035 else if (ret == -2)
bellard6a00d602005-11-21 23:25:50 +00002036 expr_error("no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002037 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002038 }
2039 break;
2040 case '\0':
2041 expr_error("unexpected end of expression");
2042 n = 0;
2043 break;
2044 default:
blueswir17743e582007-09-24 18:39:04 +00002045#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002046 n = strtoull(pch, &p, 0);
2047#else
bellard9307c4c2004-04-04 12:57:25 +00002048 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002049#endif
bellard9307c4c2004-04-04 12:57:25 +00002050 if (pch == p) {
2051 expr_error("invalid char in expression");
2052 }
2053 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002054 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002055 pch++;
2056 break;
2057 }
2058 return n;
2059}
2060
2061
blueswir1c2efc952007-09-25 17:28:42 +00002062static int64_t expr_prod(void)
bellard9307c4c2004-04-04 12:57:25 +00002063{
blueswir1c2efc952007-09-25 17:28:42 +00002064 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002065 int op;
ths3b46e622007-09-17 08:09:54 +00002066
bellard9307c4c2004-04-04 12:57:25 +00002067 val = expr_unary();
2068 for(;;) {
2069 op = *pch;
2070 if (op != '*' && op != '/' && op != '%')
2071 break;
2072 next();
2073 val2 = expr_unary();
2074 switch(op) {
2075 default:
2076 case '*':
2077 val *= val2;
2078 break;
2079 case '/':
2080 case '%':
ths5fafdf22007-09-16 21:08:06 +00002081 if (val2 == 0)
bellard5b602122004-05-22 21:41:05 +00002082 expr_error("division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002083 if (op == '/')
2084 val /= val2;
2085 else
2086 val %= val2;
2087 break;
2088 }
2089 }
2090 return val;
2091}
2092
blueswir1c2efc952007-09-25 17:28:42 +00002093static int64_t expr_logic(void)
bellard9307c4c2004-04-04 12:57:25 +00002094{
blueswir1c2efc952007-09-25 17:28:42 +00002095 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002096 int op;
bellard9307c4c2004-04-04 12:57:25 +00002097
2098 val = expr_prod();
2099 for(;;) {
2100 op = *pch;
2101 if (op != '&' && op != '|' && op != '^')
2102 break;
2103 next();
2104 val2 = expr_prod();
2105 switch(op) {
2106 default:
2107 case '&':
2108 val &= val2;
2109 break;
2110 case '|':
2111 val |= val2;
2112 break;
2113 case '^':
2114 val ^= val2;
2115 break;
2116 }
2117 }
2118 return val;
2119}
2120
blueswir1c2efc952007-09-25 17:28:42 +00002121static int64_t expr_sum(void)
bellard9307c4c2004-04-04 12:57:25 +00002122{
blueswir1c2efc952007-09-25 17:28:42 +00002123 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002124 int op;
bellard9307c4c2004-04-04 12:57:25 +00002125
2126 val = expr_logic();
2127 for(;;) {
2128 op = *pch;
2129 if (op != '+' && op != '-')
2130 break;
2131 next();
2132 val2 = expr_logic();
2133 if (op == '+')
2134 val += val2;
2135 else
2136 val -= val2;
2137 }
2138 return val;
2139}
2140
blueswir1c2efc952007-09-25 17:28:42 +00002141static int get_expr(int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002142{
2143 pch = *pp;
2144 if (setjmp(expr_env)) {
2145 *pp = pch;
2146 return -1;
2147 }
blueswir1cd390082008-11-16 13:53:32 +00002148 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002149 pch++;
2150 *pval = expr_sum();
2151 *pp = pch;
2152 return 0;
2153}
2154
2155static int get_str(char *buf, int buf_size, const char **pp)
2156{
2157 const char *p;
2158 char *q;
2159 int c;
2160
bellard81d09122004-07-14 17:21:37 +00002161 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002162 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002163 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002164 p++;
2165 if (*p == '\0') {
2166 fail:
bellard81d09122004-07-14 17:21:37 +00002167 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002168 *pp = p;
2169 return -1;
2170 }
bellard9307c4c2004-04-04 12:57:25 +00002171 if (*p == '\"') {
2172 p++;
2173 while (*p != '\0' && *p != '\"') {
2174 if (*p == '\\') {
2175 p++;
2176 c = *p++;
2177 switch(c) {
2178 case 'n':
2179 c = '\n';
2180 break;
2181 case 'r':
2182 c = '\r';
2183 break;
2184 case '\\':
2185 case '\'':
2186 case '\"':
2187 break;
2188 default:
2189 qemu_printf("unsupported escape code: '\\%c'\n", c);
2190 goto fail;
2191 }
2192 if ((q - buf) < buf_size - 1) {
2193 *q++ = c;
2194 }
2195 } else {
2196 if ((q - buf) < buf_size - 1) {
2197 *q++ = *p;
2198 }
2199 p++;
2200 }
2201 }
2202 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002203 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002204 goto fail;
2205 }
2206 p++;
2207 } else {
blueswir1cd390082008-11-16 13:53:32 +00002208 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002209 if ((q - buf) < buf_size - 1) {
2210 *q++ = *p;
2211 }
2212 p++;
2213 }
bellard9307c4c2004-04-04 12:57:25 +00002214 }
bellard81d09122004-07-14 17:21:37 +00002215 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002216 *pp = p;
2217 return 0;
2218}
2219
2220static int default_fmt_format = 'x';
2221static int default_fmt_size = 4;
2222
2223#define MAX_ARGS 16
2224
bellard7e2515e2004-08-01 21:52:19 +00002225static void monitor_handle_command(const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002226{
2227 const char *p, *pstart, *typestr;
2228 char *q;
2229 int c, nb_args, len, i, has_arg;
blueswir18662d652008-10-02 18:32:44 +00002230 const term_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002231 char cmdname[256];
2232 char buf[1024];
2233 void *str_allocated[MAX_ARGS];
2234 void *args[MAX_ARGS];
blueswir1a5f1b962008-08-17 20:21:51 +00002235 void (*handler_0)(void);
2236 void (*handler_1)(void *arg0);
2237 void (*handler_2)(void *arg0, void *arg1);
2238 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2239 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2240 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2241 void *arg4);
2242 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2243 void *arg4, void *arg5);
2244 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2245 void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002246
2247#ifdef DEBUG
2248 term_printf("command='%s'\n", cmdline);
2249#endif
ths3b46e622007-09-17 08:09:54 +00002250
bellard9307c4c2004-04-04 12:57:25 +00002251 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002252 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002253 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002254 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002255 p++;
2256 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002257 return;
bellard9307c4c2004-04-04 12:57:25 +00002258 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002259 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002260 p++;
2261 len = p - pstart;
2262 if (len > sizeof(cmdname) - 1)
2263 len = sizeof(cmdname) - 1;
2264 memcpy(cmdname, pstart, len);
2265 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002266
bellard9307c4c2004-04-04 12:57:25 +00002267 /* find the command */
bellard9dc39cb2004-03-14 21:38:27 +00002268 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002269 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002270 goto found;
2271 }
bellard9307c4c2004-04-04 12:57:25 +00002272 term_printf("unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002273 return;
2274 found:
bellard9307c4c2004-04-04 12:57:25 +00002275
2276 for(i = 0; i < MAX_ARGS; i++)
2277 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002278
bellard9307c4c2004-04-04 12:57:25 +00002279 /* parse the parameters */
2280 typestr = cmd->args_type;
2281 nb_args = 0;
2282 for(;;) {
2283 c = *typestr;
2284 if (c == '\0')
2285 break;
2286 typestr++;
2287 switch(c) {
2288 case 'F':
bellard81d09122004-07-14 17:21:37 +00002289 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002290 case 's':
2291 {
2292 int ret;
2293 char *str;
ths3b46e622007-09-17 08:09:54 +00002294
blueswir1cd390082008-11-16 13:53:32 +00002295 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002296 p++;
2297 if (*typestr == '?') {
2298 typestr++;
2299 if (*p == '\0') {
2300 /* no optional string: NULL argument */
2301 str = NULL;
2302 goto add_str;
2303 }
2304 }
2305 ret = get_str(buf, sizeof(buf), &p);
2306 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002307 switch(c) {
2308 case 'F':
bellard9307c4c2004-04-04 12:57:25 +00002309 term_printf("%s: filename expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002310 break;
2311 case 'B':
2312 term_printf("%s: block device name expected\n", cmdname);
2313 break;
2314 default:
bellard9307c4c2004-04-04 12:57:25 +00002315 term_printf("%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002316 break;
2317 }
bellard9307c4c2004-04-04 12:57:25 +00002318 goto fail;
2319 }
2320 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002321 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002322 str_allocated[nb_args] = str;
2323 add_str:
2324 if (nb_args >= MAX_ARGS) {
2325 error_args:
2326 term_printf("%s: too many arguments\n", cmdname);
2327 goto fail;
2328 }
2329 args[nb_args++] = str;
2330 }
2331 break;
2332 case '/':
2333 {
2334 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002335
blueswir1cd390082008-11-16 13:53:32 +00002336 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002337 p++;
2338 if (*p == '/') {
2339 /* format found */
2340 p++;
2341 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002342 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002343 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002344 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002345 count = count * 10 + (*p - '0');
2346 p++;
2347 }
2348 }
2349 size = -1;
2350 format = -1;
2351 for(;;) {
2352 switch(*p) {
2353 case 'o':
2354 case 'd':
2355 case 'u':
2356 case 'x':
2357 case 'i':
2358 case 'c':
2359 format = *p++;
2360 break;
2361 case 'b':
2362 size = 1;
2363 p++;
2364 break;
2365 case 'h':
2366 size = 2;
2367 p++;
2368 break;
2369 case 'w':
2370 size = 4;
2371 p++;
2372 break;
2373 case 'g':
2374 case 'L':
2375 size = 8;
2376 p++;
2377 break;
2378 default:
2379 goto next;
2380 }
2381 }
2382 next:
blueswir1cd390082008-11-16 13:53:32 +00002383 if (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002384 term_printf("invalid char in format: '%c'\n", *p);
2385 goto fail;
2386 }
bellard9307c4c2004-04-04 12:57:25 +00002387 if (format < 0)
2388 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002389 if (format != 'i') {
2390 /* for 'i', not specifying a size gives -1 as size */
2391 if (size < 0)
2392 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002393 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002394 }
bellard9307c4c2004-04-04 12:57:25 +00002395 default_fmt_format = format;
2396 } else {
2397 count = 1;
2398 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002399 if (format != 'i') {
2400 size = default_fmt_size;
2401 } else {
2402 size = -1;
2403 }
bellard9307c4c2004-04-04 12:57:25 +00002404 }
2405 if (nb_args + 3 > MAX_ARGS)
2406 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002407 args[nb_args++] = (void*)(long)count;
2408 args[nb_args++] = (void*)(long)format;
2409 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002410 }
2411 break;
2412 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002413 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002414 {
blueswir1c2efc952007-09-25 17:28:42 +00002415 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002416
blueswir1cd390082008-11-16 13:53:32 +00002417 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002418 p++;
bellard34405572004-06-08 00:55:58 +00002419 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002420 if (*typestr == '?') {
2421 if (*p == '\0')
2422 has_arg = 0;
2423 else
2424 has_arg = 1;
2425 } else {
2426 if (*p == '.') {
2427 p++;
blueswir1cd390082008-11-16 13:53:32 +00002428 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002429 p++;
2430 has_arg = 1;
2431 } else {
2432 has_arg = 0;
2433 }
2434 }
bellard13224a82006-07-14 22:03:35 +00002435 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002436 if (nb_args >= MAX_ARGS)
2437 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002438 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002439 if (!has_arg) {
2440 if (nb_args >= MAX_ARGS)
2441 goto error_args;
2442 val = -1;
2443 goto add_num;
2444 }
2445 }
2446 if (get_expr(&val, &p))
2447 goto fail;
2448 add_num:
bellard92a31b12005-02-10 22:00:52 +00002449 if (c == 'i') {
2450 if (nb_args >= MAX_ARGS)
2451 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002452 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002453 } else {
2454 if ((nb_args + 1) >= MAX_ARGS)
2455 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002456#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002457 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002458#else
2459 args[nb_args++] = (void *)0;
2460#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002461 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002462 }
bellard9307c4c2004-04-04 12:57:25 +00002463 }
2464 break;
2465 case '-':
2466 {
2467 int has_option;
2468 /* option */
ths3b46e622007-09-17 08:09:54 +00002469
bellard9307c4c2004-04-04 12:57:25 +00002470 c = *typestr++;
2471 if (c == '\0')
2472 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002473 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002474 p++;
2475 has_option = 0;
2476 if (*p == '-') {
2477 p++;
2478 if (*p != c) {
ths5fafdf22007-09-16 21:08:06 +00002479 term_printf("%s: unsupported option -%c\n",
bellard9307c4c2004-04-04 12:57:25 +00002480 cmdname, *p);
2481 goto fail;
2482 }
2483 p++;
2484 has_option = 1;
2485 }
2486 if (nb_args >= MAX_ARGS)
2487 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002488 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002489 }
2490 break;
2491 default:
2492 bad_type:
2493 term_printf("%s: unknown type '%c'\n", cmdname, c);
2494 goto fail;
2495 }
2496 }
2497 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002498 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002499 p++;
2500 if (*p != '\0') {
ths5fafdf22007-09-16 21:08:06 +00002501 term_printf("%s: extraneous characters at the end of line\n",
bellard9307c4c2004-04-04 12:57:25 +00002502 cmdname);
2503 goto fail;
2504 }
2505
2506 switch(nb_args) {
2507 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002508 handler_0 = cmd->handler;
2509 handler_0();
bellard9307c4c2004-04-04 12:57:25 +00002510 break;
2511 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002512 handler_1 = cmd->handler;
2513 handler_1(args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002514 break;
2515 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002516 handler_2 = cmd->handler;
2517 handler_2(args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002518 break;
2519 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002520 handler_3 = cmd->handler;
2521 handler_3(args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002522 break;
2523 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002524 handler_4 = cmd->handler;
2525 handler_4(args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002526 break;
2527 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002528 handler_5 = cmd->handler;
2529 handler_5(args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002530 break;
bellard34405572004-06-08 00:55:58 +00002531 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002532 handler_6 = cmd->handler;
2533 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002534 break;
bellardec36b692006-07-16 18:57:03 +00002535 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002536 handler_7 = cmd->handler;
2537 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
bellardec36b692006-07-16 18:57:03 +00002538 break;
bellard9307c4c2004-04-04 12:57:25 +00002539 default:
2540 term_printf("unsupported number of arguments: %d\n", nb_args);
2541 goto fail;
2542 }
2543 fail:
2544 for(i = 0; i < MAX_ARGS; i++)
2545 qemu_free(str_allocated[i]);
2546 return;
bellard9dc39cb2004-03-14 21:38:27 +00002547}
2548
bellard81d09122004-07-14 17:21:37 +00002549static void cmd_completion(const char *name, const char *list)
2550{
2551 const char *p, *pstart;
2552 char cmd[128];
2553 int len;
2554
2555 p = list;
2556 for(;;) {
2557 pstart = p;
2558 p = strchr(p, '|');
2559 if (!p)
2560 p = pstart + strlen(pstart);
2561 len = p - pstart;
2562 if (len > sizeof(cmd) - 2)
2563 len = sizeof(cmd) - 2;
2564 memcpy(cmd, pstart, len);
2565 cmd[len] = '\0';
2566 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2567 add_completion(cmd);
2568 }
2569 if (*p == '\0')
2570 break;
2571 p++;
2572 }
2573}
2574
2575static void file_completion(const char *input)
2576{
2577 DIR *ffs;
2578 struct dirent *d;
2579 char path[1024];
2580 char file[1024], file_prefix[1024];
2581 int input_path_len;
2582 const char *p;
2583
ths5fafdf22007-09-16 21:08:06 +00002584 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002585 if (!p) {
2586 input_path_len = 0;
2587 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002588 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002589 } else {
2590 input_path_len = p - input + 1;
2591 memcpy(path, input, input_path_len);
2592 if (input_path_len > sizeof(path) - 1)
2593 input_path_len = sizeof(path) - 1;
2594 path[input_path_len] = '\0';
2595 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2596 }
2597#ifdef DEBUG_COMPLETION
2598 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2599#endif
2600 ffs = opendir(path);
2601 if (!ffs)
2602 return;
2603 for(;;) {
2604 struct stat sb;
2605 d = readdir(ffs);
2606 if (!d)
2607 break;
2608 if (strstart(d->d_name, file_prefix, NULL)) {
2609 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002610 if (input_path_len < sizeof(file))
2611 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2612 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002613 /* stat the file to find out if it's a directory.
2614 * In that case add a slash to speed up typing long paths
2615 */
2616 stat(file, &sb);
2617 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002618 pstrcat(file, sizeof(file), "/");
bellard81d09122004-07-14 17:21:37 +00002619 add_completion(file);
2620 }
2621 }
2622 closedir(ffs);
2623}
2624
2625static void block_completion_it(void *opaque, const char *name)
2626{
2627 const char *input = opaque;
2628
2629 if (input[0] == '\0' ||
2630 !strncmp(name, (char *)input, strlen(input))) {
2631 add_completion(name);
2632 }
2633}
2634
2635/* NOTE: this parser is an approximate form of the real command parser */
2636static void parse_cmdline(const char *cmdline,
2637 int *pnb_args, char **args)
2638{
2639 const char *p;
2640 int nb_args, ret;
2641 char buf[1024];
2642
2643 p = cmdline;
2644 nb_args = 0;
2645 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002646 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002647 p++;
2648 if (*p == '\0')
2649 break;
2650 if (nb_args >= MAX_ARGS)
2651 break;
2652 ret = get_str(buf, sizeof(buf), &p);
2653 args[nb_args] = qemu_strdup(buf);
2654 nb_args++;
2655 if (ret < 0)
2656 break;
2657 }
2658 *pnb_args = nb_args;
2659}
2660
bellard7e2515e2004-08-01 21:52:19 +00002661void readline_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002662{
2663 const char *cmdname;
2664 char *args[MAX_ARGS];
2665 int nb_args, i, len;
2666 const char *ptype, *str;
blueswir18662d652008-10-02 18:32:44 +00002667 const term_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002668 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002669
2670 parse_cmdline(cmdline, &nb_args, args);
2671#ifdef DEBUG_COMPLETION
2672 for(i = 0; i < nb_args; i++) {
2673 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2674 }
2675#endif
2676
2677 /* if the line ends with a space, it means we want to complete the
2678 next arg */
2679 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002680 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002681 if (nb_args >= MAX_ARGS)
2682 return;
2683 args[nb_args++] = qemu_strdup("");
2684 }
2685 if (nb_args <= 1) {
2686 /* command completion */
2687 if (nb_args == 0)
2688 cmdname = "";
2689 else
2690 cmdname = args[0];
2691 completion_index = strlen(cmdname);
2692 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2693 cmd_completion(cmdname, cmd->name);
2694 }
2695 } else {
2696 /* find the command */
2697 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2698 if (compare_cmd(args[0], cmd->name))
2699 goto found;
2700 }
2701 return;
2702 found:
2703 ptype = cmd->args_type;
2704 for(i = 0; i < nb_args - 2; i++) {
2705 if (*ptype != '\0') {
2706 ptype++;
2707 while (*ptype == '?')
2708 ptype++;
2709 }
2710 }
2711 str = args[nb_args - 1];
2712 switch(*ptype) {
2713 case 'F':
2714 /* file completion */
2715 completion_index = strlen(str);
2716 file_completion(str);
2717 break;
2718 case 'B':
2719 /* block device name completion */
2720 completion_index = strlen(str);
2721 bdrv_iterate(block_completion_it, (void *)str);
2722 break;
bellard7fe48482004-10-09 18:08:01 +00002723 case 's':
2724 /* XXX: more generic ? */
2725 if (!strcmp(cmd->name, "info")) {
2726 completion_index = strlen(str);
2727 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2728 cmd_completion(str, cmd->name);
2729 }
bellard64866c32006-05-07 18:03:31 +00002730 } else if (!strcmp(cmd->name, "sendkey")) {
2731 completion_index = strlen(str);
2732 for(key = key_defs; key->name != NULL; key++) {
2733 cmd_completion(str, key->name);
2734 }
bellard7fe48482004-10-09 18:08:01 +00002735 }
2736 break;
bellard81d09122004-07-14 17:21:37 +00002737 default:
2738 break;
2739 }
2740 }
2741 for(i = 0; i < nb_args; i++)
2742 qemu_free(args[i]);
2743}
2744
bellard9dc39cb2004-03-14 21:38:27 +00002745static int term_can_read(void *opaque)
2746{
bellard81d09122004-07-14 17:21:37 +00002747 return 128;
bellard9dc39cb2004-03-14 21:38:27 +00002748}
2749
2750static void term_read(void *opaque, const uint8_t *buf, int size)
2751{
2752 int i;
2753 for(i = 0; i < size; i++)
bellard7e2515e2004-08-01 21:52:19 +00002754 readline_handle_byte(buf[i]);
2755}
2756
aliguorid8f44602008-10-06 13:52:44 +00002757static int monitor_suspended;
2758
bellard7e2515e2004-08-01 21:52:19 +00002759static void monitor_handle_command1(void *opaque, const char *cmdline)
2760{
2761 monitor_handle_command(cmdline);
aliguorid8f44602008-10-06 13:52:44 +00002762 if (!monitor_suspended)
2763 monitor_start_input();
2764 else
2765 monitor_suspended = 2;
2766}
2767
2768void monitor_suspend(void)
2769{
2770 monitor_suspended = 1;
2771}
2772
2773void monitor_resume(void)
2774{
2775 if (monitor_suspended == 2)
2776 monitor_start_input();
2777 monitor_suspended = 0;
bellard7e2515e2004-08-01 21:52:19 +00002778}
2779
aliguori83ab7952008-08-19 14:44:22 +00002780static void monitor_start_input(void)
bellard7e2515e2004-08-01 21:52:19 +00002781{
2782 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
bellard9dc39cb2004-03-14 21:38:27 +00002783}
2784
ths86e94de2007-01-05 22:01:59 +00002785static void term_event(void *opaque, int event)
2786{
2787 if (event != CHR_EVENT_RESET)
2788 return;
2789
2790 if (!hide_banner)
2791 term_printf("QEMU %s monitor - type 'help' for more information\n",
2792 QEMU_VERSION);
2793 monitor_start_input();
2794}
2795
ths20d8a3e2007-02-18 17:04:49 +00002796static int is_first_init = 1;
2797
bellard81d09122004-07-14 17:21:37 +00002798void monitor_init(CharDriverState *hd, int show_banner)
bellard9dc39cb2004-03-14 21:38:27 +00002799{
ths20d8a3e2007-02-18 17:04:49 +00002800 int i;
2801
2802 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00002803 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2804 if (!key_timer)
2805 return;
ths20d8a3e2007-02-18 17:04:49 +00002806 for (i = 0; i < MAX_MON; i++) {
2807 monitor_hd[i] = NULL;
2808 }
2809 is_first_init = 0;
2810 }
2811 for (i = 0; i < MAX_MON; i++) {
2812 if (monitor_hd[i] == NULL) {
2813 monitor_hd[i] = hd;
2814 break;
2815 }
2816 }
2817
ths86e94de2007-01-05 22:01:59 +00002818 hide_banner = !show_banner;
2819
pbrooke5b0bc42007-01-27 23:46:43 +00002820 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
aliguori83ab7952008-08-19 14:44:22 +00002821
2822 readline_start("", 0, monitor_handle_command1, NULL);
bellard7e2515e2004-08-01 21:52:19 +00002823}
2824
2825/* XXX: use threads ? */
2826/* modal monitor readline */
2827static int monitor_readline_started;
2828static char *monitor_readline_buf;
2829static int monitor_readline_buf_size;
2830
2831static void monitor_readline_cb(void *opaque, const char *input)
2832{
2833 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2834 monitor_readline_started = 0;
2835}
2836
2837void monitor_readline(const char *prompt, int is_password,
2838 char *buf, int buf_size)
2839{
ths20d8a3e2007-02-18 17:04:49 +00002840 int i;
aliguoribc0129d2008-08-01 15:12:34 +00002841 int old_focus[MAX_MON];
ths20d8a3e2007-02-18 17:04:49 +00002842
bellard7e2515e2004-08-01 21:52:19 +00002843 if (is_password) {
aliguoribc0129d2008-08-01 15:12:34 +00002844 for (i = 0; i < MAX_MON; i++) {
2845 old_focus[i] = 0;
2846 if (monitor_hd[i]) {
2847 old_focus[i] = monitor_hd[i]->focus;
2848 monitor_hd[i]->focus = 0;
ths20d8a3e2007-02-18 17:04:49 +00002849 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
aliguoribc0129d2008-08-01 15:12:34 +00002850 }
2851 }
bellard7e2515e2004-08-01 21:52:19 +00002852 }
aliguoribc0129d2008-08-01 15:12:34 +00002853
bellard7e2515e2004-08-01 21:52:19 +00002854 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2855 monitor_readline_buf = buf;
2856 monitor_readline_buf_size = buf_size;
2857 monitor_readline_started = 1;
2858 while (monitor_readline_started) {
2859 main_loop_wait(10);
2860 }
aliguoribc0129d2008-08-01 15:12:34 +00002861 /* restore original focus */
2862 if (is_password) {
2863 for (i = 0; i < MAX_MON; i++)
2864 if (old_focus[i])
2865 monitor_hd[i]->focus = old_focus[i];
2866 }
bellard9dc39cb2004-03-14 21:38:27 +00002867}