blob: f142a879f47100c38c9d45c108559dd8cbe0e483 [file] [log] [blame]
bellard9dc39cb2004-03-14 21:38:27 +00001/*
2 * QEMU monitor
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard9dc39cb2004-03-14 21:38:27 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard9dc39cb2004-03-14 21:38:27 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "hw/usb.h"
26#include "hw/pcmcia.h"
27#include "hw/pc.h"
28#include "hw/pci.h"
29#include "gdbstub.h"
30#include "net.h"
31#include "qemu-char.h"
32#include "sysemu.h"
33#include "console.h"
34#include "block.h"
35#include "audio/audio.h"
bellard9307c4c2004-04-04 12:57:25 +000036#include "disas.h"
aliguoridf751fa2008-12-04 20:19:35 +000037#include "balloon.h"
bellard81d09122004-07-14 17:21:37 +000038#include <dirent.h>
balrogc8256f92008-06-08 22:45:01 +000039#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000040#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000041#include "kvm.h"
ths6a5bd302007-12-03 17:05:38 +000042
bellard9dc39cb2004-03-14 21:38:27 +000043//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000044//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000045
bellard9307c4c2004-04-04 12:57:25 +000046/*
47 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000048 *
bellard9307c4c2004-04-04 12:57:25 +000049 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000050 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000051 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000052 * 'i' 32 bit integer
53 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000054 * '/' optional gdb-like print format (like "/10x")
55 *
56 * '?' optional type (for 'F', 's' and 'i')
57 *
58 */
59
bellard9dc39cb2004-03-14 21:38:27 +000060typedef struct term_cmd_t {
61 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000062 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000063 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000064 const char *params;
65 const char *help;
66} term_cmd_t;
67
ths20d8a3e2007-02-18 17:04:49 +000068#define MAX_MON 4
69static CharDriverState *monitor_hd[MAX_MON];
ths86e94de2007-01-05 22:01:59 +000070static int hide_banner;
bellard7e2515e2004-08-01 21:52:19 +000071
blueswir18662d652008-10-02 18:32:44 +000072static const term_cmd_t term_cmds[];
73static const term_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000074
ths60fe76f2007-12-16 03:02:09 +000075static uint8_t term_outbuf[1024];
bellard7e2515e2004-08-01 21:52:19 +000076static int term_outbuf_index;
bellard81d09122004-07-14 17:21:37 +000077
aliguori83ab7952008-08-19 14:44:22 +000078static void monitor_start_input(void);
79
blueswir1b1d8e522008-10-26 13:43:07 +000080static CPUState *mon_cpu = NULL;
bellard6a00d602005-11-21 23:25:50 +000081
bellard9dc39cb2004-03-14 21:38:27 +000082void term_flush(void)
83{
ths20d8a3e2007-02-18 17:04:49 +000084 int i;
bellard7e2515e2004-08-01 21:52:19 +000085 if (term_outbuf_index > 0) {
ths20d8a3e2007-02-18 17:04:49 +000086 for (i = 0; i < MAX_MON; i++)
87 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
88 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
bellard7e2515e2004-08-01 21:52:19 +000089 term_outbuf_index = 0;
90 }
91}
92
93/* flush at every end of line or if the buffer is full */
94void term_puts(const char *str)
95{
ths60fe76f2007-12-16 03:02:09 +000096 char c;
bellard7e2515e2004-08-01 21:52:19 +000097 for(;;) {
98 c = *str++;
99 if (c == '\0')
100 break;
bellard7ba12602006-07-14 20:26:42 +0000101 if (c == '\n')
102 term_outbuf[term_outbuf_index++] = '\r';
bellard7e2515e2004-08-01 21:52:19 +0000103 term_outbuf[term_outbuf_index++] = c;
bellard7ba12602006-07-14 20:26:42 +0000104 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
bellard7e2515e2004-08-01 21:52:19 +0000105 c == '\n')
106 term_flush();
107 }
108}
109
110void term_vprintf(const char *fmt, va_list ap)
111{
112 char buf[4096];
113 vsnprintf(buf, sizeof(buf), fmt, ap);
114 term_puts(buf);
115}
116
117void term_printf(const char *fmt, ...)
118{
119 va_list ap;
120 va_start(ap, fmt);
121 term_vprintf(fmt, ap);
122 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000123}
124
thsfef30742006-12-22 14:11:32 +0000125void term_print_filename(const char *filename)
126{
127 int i;
128
129 for (i = 0; filename[i]; i++) {
130 switch (filename[i]) {
131 case ' ':
132 case '"':
133 case '\\':
134 term_printf("\\%c", filename[i]);
135 break;
136 case '\t':
137 term_printf("\\t");
138 break;
139 case '\r':
140 term_printf("\\r");
141 break;
142 case '\n':
143 term_printf("\\n");
144 break;
145 default:
146 term_printf("%c", filename[i]);
147 break;
148 }
149 }
150}
151
bellard7fe48482004-10-09 18:08:01 +0000152static int monitor_fprintf(FILE *stream, const char *fmt, ...)
153{
154 va_list ap;
155 va_start(ap, fmt);
156 term_vprintf(fmt, ap);
157 va_end(ap);
158 return 0;
159}
160
bellard9dc39cb2004-03-14 21:38:27 +0000161static int compare_cmd(const char *name, const char *list)
162{
163 const char *p, *pstart;
164 int len;
165 len = strlen(name);
166 p = list;
167 for(;;) {
168 pstart = p;
169 p = strchr(p, '|');
170 if (!p)
171 p = pstart + strlen(pstart);
172 if ((p - pstart) == len && !memcmp(pstart, name, len))
173 return 1;
174 if (*p == '\0')
175 break;
176 p++;
177 }
178 return 0;
179}
180
blueswir18662d652008-10-02 18:32:44 +0000181static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000182{
blueswir18662d652008-10-02 18:32:44 +0000183 const term_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000184
185 for(cmd = cmds; cmd->name != NULL; cmd++) {
186 if (!name || !strcmp(name, cmd->name))
187 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
188 }
189}
190
191static void help_cmd(const char *name)
192{
193 if (name && !strcmp(name, "info")) {
194 help_cmd1(info_cmds, "info ", NULL);
195 } else {
196 help_cmd1(term_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000197 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000198 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +0000199 term_printf("Log items (comma separated):\n");
200 term_printf("%-10s %s\n", "none", "remove all logs");
201 for(item = cpu_log_items; item->mask != 0; item++) {
202 term_printf("%-10s %s\n", item->name, item->help);
203 }
204 }
bellard9dc39cb2004-03-14 21:38:27 +0000205 }
206}
207
bellard9307c4c2004-04-04 12:57:25 +0000208static void do_help(const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000209{
bellard9307c4c2004-04-04 12:57:25 +0000210 help_cmd(name);
bellard9dc39cb2004-03-14 21:38:27 +0000211}
212
bellard7954c732006-08-01 15:52:40 +0000213static void do_commit(const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000214{
bellard7954c732006-08-01 15:52:40 +0000215 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000216
bellard7954c732006-08-01 15:52:40 +0000217 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000218 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000219 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000220 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
221 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000222 }
223}
224
bellard9307c4c2004-04-04 12:57:25 +0000225static void do_info(const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000226{
blueswir18662d652008-10-02 18:32:44 +0000227 const term_cmd_t *cmd;
blueswir1a5f1b962008-08-17 20:21:51 +0000228 void (*handler)(void);
bellard9dc39cb2004-03-14 21:38:27 +0000229
bellard9307c4c2004-04-04 12:57:25 +0000230 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000231 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000232 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000233 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000234 goto found;
235 }
236 help:
bellard9307c4c2004-04-04 12:57:25 +0000237 help_cmd("info");
bellard9dc39cb2004-03-14 21:38:27 +0000238 return;
239 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000240 handler = cmd->handler;
241 handler();
bellard9dc39cb2004-03-14 21:38:27 +0000242}
243
bellard9bc9d1c2004-10-10 15:15:51 +0000244static void do_info_version(void)
245{
246 term_printf("%s\n", QEMU_VERSION);
247}
248
thsc35734b2007-03-19 15:17:08 +0000249static void do_info_name(void)
250{
251 if (qemu_name)
252 term_printf("%s\n", qemu_name);
253}
254
blueswir1f1f23ad2008-09-18 18:30:20 +0000255static void do_info_uuid(void)
256{
257 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
258 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
259 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
260 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
261 qemu_uuid[15]);
262}
263
bellard9307c4c2004-04-04 12:57:25 +0000264static void do_info_block(void)
bellard9dc39cb2004-03-14 21:38:27 +0000265{
266 bdrv_info();
267}
268
thsa36e69d2007-12-02 05:18:19 +0000269static void do_info_blockstats(void)
270{
271 bdrv_info_stats();
272}
273
bellard6a00d602005-11-21 23:25:50 +0000274/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000275static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000276{
277 CPUState *env;
278
279 for(env = first_cpu; env != NULL; env = env->next_cpu) {
280 if (env->cpu_index == cpu_index) {
281 mon_cpu = env;
282 return 0;
283 }
284 }
285 return -1;
286}
287
pbrook9596ebb2007-11-18 01:44:38 +0000288static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000289{
290 if (!mon_cpu) {
291 mon_set_cpu(0);
292 }
293 return mon_cpu;
294}
295
bellard9307c4c2004-04-04 12:57:25 +0000296static void do_info_registers(void)
297{
bellard6a00d602005-11-21 23:25:50 +0000298 CPUState *env;
299 env = mon_get_cpu();
300 if (!env)
301 return;
bellard9307c4c2004-04-04 12:57:25 +0000302#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000303 cpu_dump_state(env, NULL, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000304 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000305#else
ths5fafdf22007-09-16 21:08:06 +0000306 cpu_dump_state(env, NULL, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000307 0);
bellard9307c4c2004-04-04 12:57:25 +0000308#endif
309}
310
bellard6a00d602005-11-21 23:25:50 +0000311static void do_info_cpus(void)
312{
313 CPUState *env;
314
315 /* just to set the default cpu if not already done */
316 mon_get_cpu();
317
318 for(env = first_cpu; env != NULL; env = env->next_cpu) {
ths5fafdf22007-09-16 21:08:06 +0000319 term_printf("%c CPU #%d:",
bellard6a00d602005-11-21 23:25:50 +0000320 (env == mon_cpu) ? '*' : ' ',
321 env->cpu_index);
322#if defined(TARGET_I386)
323 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000324#elif defined(TARGET_PPC)
325 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000326#elif defined(TARGET_SPARC)
327 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000328#elif defined(TARGET_MIPS)
thsb5dc7732008-06-27 10:02:35 +0000329 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000330#endif
thsead93602007-09-06 00:18:15 +0000331 if (env->halted)
332 term_printf(" (halted)");
bellard6a00d602005-11-21 23:25:50 +0000333 term_printf("\n");
334 }
335}
336
337static void do_cpu_set(int index)
338{
339 if (mon_set_cpu(index) < 0)
340 term_printf("Invalid CPU index\n");
341}
342
bellarde3db7222005-01-26 22:00:47 +0000343static void do_info_jit(void)
344{
345 dump_exec_info(NULL, monitor_fprintf);
346}
347
bellardaa455482004-04-04 13:07:25 +0000348static void do_info_history (void)
349{
350 int i;
bellard7e2515e2004-08-01 21:52:19 +0000351 const char *str;
ths3b46e622007-09-17 08:09:54 +0000352
bellard7e2515e2004-08-01 21:52:19 +0000353 i = 0;
354 for(;;) {
355 str = readline_get_history(i);
356 if (!str)
357 break;
358 term_printf("%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000359 i++;
bellardaa455482004-04-04 13:07:25 +0000360 }
361}
362
j_mayer76a66252007-03-07 08:32:30 +0000363#if defined(TARGET_PPC)
364/* XXX: not implemented in other targets */
365static void do_info_cpu_stats (void)
366{
367 CPUState *env;
368
369 env = mon_get_cpu();
370 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
371}
372#endif
373
bellard9307c4c2004-04-04 12:57:25 +0000374static void do_quit(void)
bellard9dc39cb2004-03-14 21:38:27 +0000375{
376 exit(0);
377}
378
379static int eject_device(BlockDriverState *bs, int force)
380{
381 if (bdrv_is_inserted(bs)) {
382 if (!force) {
383 if (!bdrv_is_removable(bs)) {
384 term_printf("device is not removable\n");
385 return -1;
386 }
387 if (bdrv_is_locked(bs)) {
388 term_printf("device is locked\n");
389 return -1;
390 }
391 }
392 bdrv_close(bs);
393 }
394 return 0;
395}
396
bellard9307c4c2004-04-04 12:57:25 +0000397static void do_eject(int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000398{
399 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000400
bellard9307c4c2004-04-04 12:57:25 +0000401 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000402 if (!bs) {
403 term_printf("device not found\n");
404 return;
405 }
406 eject_device(bs, force);
407}
408
aurel322ecea9b2008-06-18 22:10:01 +0000409static void do_change_block(const char *device, const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000410{
411 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000412 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000413
bellard9307c4c2004-04-04 12:57:25 +0000414 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000415 if (!bs) {
416 term_printf("device not found\n");
417 return;
418 }
aurel322ecea9b2008-06-18 22:10:01 +0000419 if (fmt) {
420 drv = bdrv_find_format(fmt);
421 if (!drv) {
422 term_printf("invalid format %s\n", fmt);
423 return;
424 }
425 }
bellard9dc39cb2004-03-14 21:38:27 +0000426 if (eject_device(bs, 0) < 0)
427 return;
aurel322ecea9b2008-06-18 22:10:01 +0000428 bdrv_open2(bs, filename, 0, drv);
balrog2bac6012007-04-30 01:34:31 +0000429 qemu_key_check(bs, filename);
bellard9dc39cb2004-03-14 21:38:27 +0000430}
431
thse25a5822007-08-25 01:36:20 +0000432static void do_change_vnc(const char *target)
433{
ths70848512007-08-25 01:37:05 +0000434 if (strcmp(target, "passwd") == 0 ||
435 strcmp(target, "password") == 0) {
436 char password[9];
437 monitor_readline("Password: ", 1, password, sizeof(password)-1);
438 password[sizeof(password)-1] = '\0';
439 if (vnc_display_password(NULL, password) < 0)
440 term_printf("could not set VNC server password\n");
441 } else {
442 if (vnc_display_open(NULL, target) < 0)
443 term_printf("could not start VNC server on %s\n", target);
444 }
thse25a5822007-08-25 01:36:20 +0000445}
446
aurel322ecea9b2008-06-18 22:10:01 +0000447static void do_change(const char *device, const char *target, const char *fmt)
thse25a5822007-08-25 01:36:20 +0000448{
449 if (strcmp(device, "vnc") == 0) {
450 do_change_vnc(target);
451 } else {
aurel322ecea9b2008-06-18 22:10:01 +0000452 do_change_block(device, target, fmt);
thse25a5822007-08-25 01:36:20 +0000453 }
454}
455
bellard9307c4c2004-04-04 12:57:25 +0000456static void do_screen_dump(const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000457{
pbrook95219892006-04-09 01:06:34 +0000458 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000459}
460
pbrooke735b912007-06-30 13:53:24 +0000461static void do_logfile(const char *filename)
462{
463 cpu_set_log_filename(filename);
464}
465
bellard9307c4c2004-04-04 12:57:25 +0000466static void do_log(const char *items)
bellardf193c792004-03-21 17:06:25 +0000467{
468 int mask;
ths3b46e622007-09-17 08:09:54 +0000469
bellard9307c4c2004-04-04 12:57:25 +0000470 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000471 mask = 0;
472 } else {
bellard9307c4c2004-04-04 12:57:25 +0000473 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000474 if (!mask) {
bellard9307c4c2004-04-04 12:57:25 +0000475 help_cmd("log");
bellardf193c792004-03-21 17:06:25 +0000476 return;
477 }
478 }
479 cpu_set_log(mask);
480}
481
bellard9307c4c2004-04-04 12:57:25 +0000482static void do_stop(void)
bellard8a7ddc32004-03-31 19:00:16 +0000483{
484 vm_stop(EXCP_INTERRUPT);
485}
486
bellard9307c4c2004-04-04 12:57:25 +0000487static void do_cont(void)
bellard8a7ddc32004-03-31 19:00:16 +0000488{
489 vm_start();
490}
491
bellard67b915a2004-03-31 23:37:16 +0000492#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +0000493static void do_gdbserver(const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000494{
pbrookcfc34752007-02-22 01:48:01 +0000495 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000496 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000497 if (gdbserver_start(port) < 0) {
498 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000499 } else {
pbrookcfc34752007-02-22 01:48:01 +0000500 qemu_printf("Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000501 }
502}
bellard67b915a2004-03-31 23:37:16 +0000503#endif
bellard8a7ddc32004-03-31 19:00:16 +0000504
bellard9307c4c2004-04-04 12:57:25 +0000505static void term_printc(int c)
506{
507 term_printf("'");
508 switch(c) {
509 case '\'':
510 term_printf("\\'");
511 break;
512 case '\\':
513 term_printf("\\\\");
514 break;
515 case '\n':
516 term_printf("\\n");
517 break;
518 case '\r':
519 term_printf("\\r");
520 break;
521 default:
522 if (c >= 32 && c <= 126) {
523 term_printf("%c", c);
524 } else {
525 term_printf("\\x%02x", c);
526 }
527 break;
528 }
529 term_printf("'");
530}
531
ths5fafdf22007-09-16 21:08:06 +0000532static void memory_dump(int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000533 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000534{
bellard6a00d602005-11-21 23:25:50 +0000535 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000536 int nb_per_line, l, line_size, i, max_digits, len;
537 uint8_t buf[16];
538 uint64_t v;
539
540 if (format == 'i') {
541 int flags;
542 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000543 env = mon_get_cpu();
544 if (!env && !is_physical)
545 return;
bellard9307c4c2004-04-04 12:57:25 +0000546#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000547 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000548 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000549 } else if (wsize == 4) {
550 flags = 0;
551 } else {
bellard6a15fd12006-04-12 21:07:07 +0000552 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000553 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000554 if (env) {
555#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000556 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000557 (env->segs[R_CS].flags & DESC_L_MASK))
558 flags = 2;
559 else
560#endif
561 if (!(env->segs[R_CS].flags & DESC_B_MASK))
562 flags = 1;
563 }
bellard4c27ba22004-04-25 18:05:08 +0000564 }
565#endif
bellard6a00d602005-11-21 23:25:50 +0000566 monitor_disas(env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000567 return;
568 }
569
570 len = wsize * count;
571 if (wsize == 1)
572 line_size = 8;
573 else
574 line_size = 16;
575 nb_per_line = line_size / wsize;
576 max_digits = 0;
577
578 switch(format) {
579 case 'o':
580 max_digits = (wsize * 8 + 2) / 3;
581 break;
582 default:
583 case 'x':
584 max_digits = (wsize * 8) / 4;
585 break;
586 case 'u':
587 case 'd':
588 max_digits = (wsize * 8 * 10 + 32) / 33;
589 break;
590 case 'c':
591 wsize = 1;
592 break;
593 }
594
595 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000596 if (is_physical)
597 term_printf(TARGET_FMT_plx ":", addr);
598 else
599 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000600 l = len;
601 if (l > line_size)
602 l = line_size;
603 if (is_physical) {
604 cpu_physical_memory_rw(addr, buf, l, 0);
605 } else {
bellard6a00d602005-11-21 23:25:50 +0000606 env = mon_get_cpu();
607 if (!env)
608 break;
aliguoric8f79b62008-08-18 14:00:20 +0000609 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
610 term_printf(" Cannot access memory\n");
611 break;
612 }
bellard9307c4c2004-04-04 12:57:25 +0000613 }
ths5fafdf22007-09-16 21:08:06 +0000614 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000615 while (i < l) {
616 switch(wsize) {
617 default:
618 case 1:
619 v = ldub_raw(buf + i);
620 break;
621 case 2:
622 v = lduw_raw(buf + i);
623 break;
624 case 4:
bellard92a31b12005-02-10 22:00:52 +0000625 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000626 break;
627 case 8:
628 v = ldq_raw(buf + i);
629 break;
630 }
631 term_printf(" ");
632 switch(format) {
633 case 'o':
bellard26a76462006-06-25 18:15:32 +0000634 term_printf("%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000635 break;
636 case 'x':
bellard26a76462006-06-25 18:15:32 +0000637 term_printf("0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000638 break;
639 case 'u':
bellard26a76462006-06-25 18:15:32 +0000640 term_printf("%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000641 break;
642 case 'd':
bellard26a76462006-06-25 18:15:32 +0000643 term_printf("%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000644 break;
645 case 'c':
646 term_printc(v);
647 break;
648 }
649 i += wsize;
650 }
651 term_printf("\n");
652 addr += l;
653 len -= l;
654 }
655}
656
bellard92a31b12005-02-10 22:00:52 +0000657#if TARGET_LONG_BITS == 64
658#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
659#else
660#define GET_TLONG(h, l) (l)
661#endif
662
ths5fafdf22007-09-16 21:08:06 +0000663static void do_memory_dump(int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000664 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000665{
bellard92a31b12005-02-10 22:00:52 +0000666 target_long addr = GET_TLONG(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000667 memory_dump(count, format, size, addr, 0);
668}
669
blueswir17743e582007-09-24 18:39:04 +0000670#if TARGET_PHYS_ADDR_BITS > 32
671#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
672#else
673#define GET_TPHYSADDR(h, l) (l)
674#endif
675
bellard92a31b12005-02-10 22:00:52 +0000676static void do_physical_memory_dump(int count, int format, int size,
677 uint32_t addrh, uint32_t addrl)
678
bellard9307c4c2004-04-04 12:57:25 +0000679{
blueswir17743e582007-09-24 18:39:04 +0000680 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000681 memory_dump(count, format, size, addr, 1);
682}
683
bellard92a31b12005-02-10 22:00:52 +0000684static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000685{
blueswir17743e582007-09-24 18:39:04 +0000686 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
687#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000688 switch(format) {
689 case 'o':
690 term_printf("%#o", val);
691 break;
692 case 'x':
693 term_printf("%#x", val);
694 break;
695 case 'u':
696 term_printf("%u", val);
697 break;
698 default:
699 case 'd':
700 term_printf("%d", val);
701 break;
702 case 'c':
703 term_printc(val);
704 break;
705 }
bellard92a31b12005-02-10 22:00:52 +0000706#else
707 switch(format) {
708 case 'o':
bellard26a76462006-06-25 18:15:32 +0000709 term_printf("%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000710 break;
711 case 'x':
bellard26a76462006-06-25 18:15:32 +0000712 term_printf("%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000713 break;
714 case 'u':
bellard26a76462006-06-25 18:15:32 +0000715 term_printf("%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000716 break;
717 default:
718 case 'd':
bellard26a76462006-06-25 18:15:32 +0000719 term_printf("%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000720 break;
721 case 'c':
722 term_printc(val);
723 break;
724 }
725#endif
bellard9307c4c2004-04-04 12:57:25 +0000726 term_printf("\n");
727}
728
ths5fafdf22007-09-16 21:08:06 +0000729static void do_memory_save(unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000730 uint32_t size, const char *filename)
731{
732 FILE *f;
733 target_long addr = GET_TLONG(valh, vall);
734 uint32_t l;
735 CPUState *env;
736 uint8_t buf[1024];
737
738 env = mon_get_cpu();
739 if (!env)
740 return;
741
742 f = fopen(filename, "wb");
743 if (!f) {
744 term_printf("could not open '%s'\n", filename);
745 return;
746 }
747 while (size != 0) {
748 l = sizeof(buf);
749 if (l > size)
750 l = size;
751 cpu_memory_rw_debug(env, addr, buf, l, 0);
752 fwrite(buf, 1, l, f);
753 addr += l;
754 size -= l;
755 }
756 fclose(f);
757}
758
aurel32a8bdf7a2008-04-11 21:36:14 +0000759static void do_physical_memory_save(unsigned int valh, unsigned int vall,
760 uint32_t size, const char *filename)
761{
762 FILE *f;
763 uint32_t l;
764 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000765 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000766
767 f = fopen(filename, "wb");
768 if (!f) {
769 term_printf("could not open '%s'\n", filename);
770 return;
771 }
772 while (size != 0) {
773 l = sizeof(buf);
774 if (l > size)
775 l = size;
776 cpu_physical_memory_rw(addr, buf, l, 0);
777 fwrite(buf, 1, l, f);
778 fflush(f);
779 addr += l;
780 size -= l;
781 }
782 fclose(f);
783}
784
bellarde4cf1ad2005-06-04 20:15:57 +0000785static void do_sum(uint32_t start, uint32_t size)
786{
787 uint32_t addr;
788 uint8_t buf[1];
789 uint16_t sum;
790
791 sum = 0;
792 for(addr = start; addr < (start + size); addr++) {
793 cpu_physical_memory_rw(addr, buf, 1, 0);
794 /* BSD sum algorithm ('sum' Unix command) */
795 sum = (sum >> 1) | (sum << 15);
796 sum += buf[0];
797 }
798 term_printf("%05d\n", sum);
799}
800
bellarda3a91a32004-06-04 11:06:21 +0000801typedef struct {
802 int keycode;
803 const char *name;
804} KeyDef;
805
806static const KeyDef key_defs[] = {
807 { 0x2a, "shift" },
808 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000809
bellarda3a91a32004-06-04 11:06:21 +0000810 { 0x38, "alt" },
811 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000812 { 0x64, "altgr" },
813 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000814 { 0x1d, "ctrl" },
815 { 0x9d, "ctrl_r" },
816
817 { 0xdd, "menu" },
818
819 { 0x01, "esc" },
820
821 { 0x02, "1" },
822 { 0x03, "2" },
823 { 0x04, "3" },
824 { 0x05, "4" },
825 { 0x06, "5" },
826 { 0x07, "6" },
827 { 0x08, "7" },
828 { 0x09, "8" },
829 { 0x0a, "9" },
830 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000831 { 0x0c, "minus" },
832 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000833 { 0x0e, "backspace" },
834
835 { 0x0f, "tab" },
836 { 0x10, "q" },
837 { 0x11, "w" },
838 { 0x12, "e" },
839 { 0x13, "r" },
840 { 0x14, "t" },
841 { 0x15, "y" },
842 { 0x16, "u" },
843 { 0x17, "i" },
844 { 0x18, "o" },
845 { 0x19, "p" },
846
847 { 0x1c, "ret" },
848
849 { 0x1e, "a" },
850 { 0x1f, "s" },
851 { 0x20, "d" },
852 { 0x21, "f" },
853 { 0x22, "g" },
854 { 0x23, "h" },
855 { 0x24, "j" },
856 { 0x25, "k" },
857 { 0x26, "l" },
858
859 { 0x2c, "z" },
860 { 0x2d, "x" },
861 { 0x2e, "c" },
862 { 0x2f, "v" },
863 { 0x30, "b" },
864 { 0x31, "n" },
865 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000866 { 0x33, "comma" },
867 { 0x34, "dot" },
868 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000869
balrog4d3b6f62008-02-10 16:33:14 +0000870 { 0x37, "asterisk" },
871
bellarda3a91a32004-06-04 11:06:21 +0000872 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000873 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000874 { 0x3b, "f1" },
875 { 0x3c, "f2" },
876 { 0x3d, "f3" },
877 { 0x3e, "f4" },
878 { 0x3f, "f5" },
879 { 0x40, "f6" },
880 { 0x41, "f7" },
881 { 0x42, "f8" },
882 { 0x43, "f9" },
883 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000884 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000885 { 0x46, "scroll_lock" },
886
bellard64866c32006-05-07 18:03:31 +0000887 { 0xb5, "kp_divide" },
888 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000889 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000890 { 0x4e, "kp_add" },
891 { 0x9c, "kp_enter" },
892 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000893 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000894
895 { 0x52, "kp_0" },
896 { 0x4f, "kp_1" },
897 { 0x50, "kp_2" },
898 { 0x51, "kp_3" },
899 { 0x4b, "kp_4" },
900 { 0x4c, "kp_5" },
901 { 0x4d, "kp_6" },
902 { 0x47, "kp_7" },
903 { 0x48, "kp_8" },
904 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000905
bellarda3a91a32004-06-04 11:06:21 +0000906 { 0x56, "<" },
907
908 { 0x57, "f11" },
909 { 0x58, "f12" },
910
911 { 0xb7, "print" },
912
913 { 0xc7, "home" },
914 { 0xc9, "pgup" },
915 { 0xd1, "pgdn" },
916 { 0xcf, "end" },
917
918 { 0xcb, "left" },
919 { 0xc8, "up" },
920 { 0xd0, "down" },
921 { 0xcd, "right" },
922
923 { 0xd2, "insert" },
924 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +0000925#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
926 { 0xf0, "stop" },
927 { 0xf1, "again" },
928 { 0xf2, "props" },
929 { 0xf3, "undo" },
930 { 0xf4, "front" },
931 { 0xf5, "copy" },
932 { 0xf6, "open" },
933 { 0xf7, "paste" },
934 { 0xf8, "find" },
935 { 0xf9, "cut" },
936 { 0xfa, "lf" },
937 { 0xfb, "help" },
938 { 0xfc, "meta_l" },
939 { 0xfd, "meta_r" },
940 { 0xfe, "compose" },
941#endif
bellarda3a91a32004-06-04 11:06:21 +0000942 { 0, NULL },
943};
944
945static int get_keycode(const char *key)
946{
947 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +0000948 char *endp;
949 int ret;
bellarda3a91a32004-06-04 11:06:21 +0000950
951 for(p = key_defs; p->name != NULL; p++) {
952 if (!strcmp(key, p->name))
953 return p->keycode;
954 }
bellard64866c32006-05-07 18:03:31 +0000955 if (strstart(key, "0x", NULL)) {
956 ret = strtoul(key, &endp, 0);
957 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
958 return ret;
959 }
bellarda3a91a32004-06-04 11:06:21 +0000960 return -1;
961}
962
balrogc8256f92008-06-08 22:45:01 +0000963#define MAX_KEYCODES 16
964static uint8_t keycodes[MAX_KEYCODES];
965static int nb_pending_keycodes;
966static QEMUTimer *key_timer;
967
968static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +0000969{
balrogc8256f92008-06-08 22:45:01 +0000970 int keycode;
971
972 while (nb_pending_keycodes > 0) {
973 nb_pending_keycodes--;
974 keycode = keycodes[nb_pending_keycodes];
975 if (keycode & 0x80)
976 kbd_put_keycode(0xe0);
977 kbd_put_keycode(keycode | 0x80);
978 }
979}
980
981static void do_sendkey(const char *string, int has_hold_time, int hold_time)
982{
balrog3401c0d2008-06-04 10:05:59 +0000983 char keyname_buf[16];
984 char *separator;
985 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +0000986
balrogc8256f92008-06-08 22:45:01 +0000987 if (nb_pending_keycodes > 0) {
988 qemu_del_timer(key_timer);
989 release_keys(NULL);
990 }
991 if (!has_hold_time)
992 hold_time = 100;
993 i = 0;
balrog3401c0d2008-06-04 10:05:59 +0000994 while (1) {
995 separator = strchr(string, '-');
996 keyname_len = separator ? separator - string : strlen(string);
997 if (keyname_len > 0) {
998 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
999 if (keyname_len > sizeof(keyname_buf) - 1) {
1000 term_printf("invalid key: '%s...'\n", keyname_buf);
1001 return;
bellarda3a91a32004-06-04 11:06:21 +00001002 }
balrogc8256f92008-06-08 22:45:01 +00001003 if (i == MAX_KEYCODES) {
balrog3401c0d2008-06-04 10:05:59 +00001004 term_printf("too many keys\n");
1005 return;
1006 }
1007 keyname_buf[keyname_len] = 0;
1008 keycode = get_keycode(keyname_buf);
1009 if (keycode < 0) {
1010 term_printf("unknown key: '%s'\n", keyname_buf);
1011 return;
1012 }
balrogc8256f92008-06-08 22:45:01 +00001013 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001014 }
balrog3401c0d2008-06-04 10:05:59 +00001015 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001016 break;
balrog3401c0d2008-06-04 10:05:59 +00001017 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001018 }
balrogc8256f92008-06-08 22:45:01 +00001019 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001020 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001021 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001022 keycode = keycodes[i];
1023 if (keycode & 0x80)
1024 kbd_put_keycode(0xe0);
1025 kbd_put_keycode(keycode & 0x7f);
1026 }
balrogc8256f92008-06-08 22:45:01 +00001027 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001028 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1029 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001030}
1031
bellard13224a82006-07-14 22:03:35 +00001032static int mouse_button_state;
1033
ths5fafdf22007-09-16 21:08:06 +00001034static void do_mouse_move(const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001035 const char *dz_str)
1036{
1037 int dx, dy, dz;
1038 dx = strtol(dx_str, NULL, 0);
1039 dy = strtol(dy_str, NULL, 0);
1040 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001041 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001042 dz = strtol(dz_str, NULL, 0);
1043 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1044}
1045
1046static void do_mouse_button(int button_state)
1047{
1048 mouse_button_state = button_state;
1049 kbd_mouse_event(0, 0, 0, mouse_button_state);
1050}
1051
bellard34405572004-06-08 00:55:58 +00001052static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1053{
1054 uint32_t val;
1055 int suffix;
1056
1057 if (has_index) {
1058 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1059 addr++;
1060 }
1061 addr &= 0xffff;
1062
1063 switch(size) {
1064 default:
1065 case 1:
1066 val = cpu_inb(NULL, addr);
1067 suffix = 'b';
1068 break;
1069 case 2:
1070 val = cpu_inw(NULL, addr);
1071 suffix = 'w';
1072 break;
1073 case 4:
1074 val = cpu_inl(NULL, addr);
1075 suffix = 'l';
1076 break;
1077 }
1078 term_printf("port%c[0x%04x] = %#0*x\n",
1079 suffix, addr, size * 2, val);
1080}
bellarda3a91a32004-06-04 11:06:21 +00001081
blueswir13b4366d2008-06-20 16:25:06 +00001082/* boot_set handler */
1083static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1084static void *boot_opaque;
1085
1086void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1087{
1088 qemu_boot_set_handler = func;
1089 boot_opaque = opaque;
1090}
1091
aurel320ecdffb2008-05-04 20:11:34 +00001092static void do_boot_set(const char *bootdevice)
1093{
1094 int res;
1095
1096 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001097 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001098 if (res == 0)
1099 term_printf("boot device list now set to %s\n", bootdevice);
1100 else
1101 term_printf("setting boot device list failed with error %i\n", res);
1102 } else {
1103 term_printf("no function defined to set boot device list for this architecture\n");
1104 }
1105}
1106
bellarde4f90822004-06-20 12:35:44 +00001107static void do_system_reset(void)
1108{
1109 qemu_system_reset_request();
1110}
1111
bellard34751872005-07-02 14:31:34 +00001112static void do_system_powerdown(void)
1113{
1114 qemu_system_powerdown_request();
1115}
1116
bellardb86bda52004-09-18 19:32:46 +00001117#if defined(TARGET_I386)
1118static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1119{
ths5fafdf22007-09-16 21:08:06 +00001120 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
bellardb86bda52004-09-18 19:32:46 +00001121 addr,
1122 pte & mask,
1123 pte & PG_GLOBAL_MASK ? 'G' : '-',
1124 pte & PG_PSE_MASK ? 'P' : '-',
1125 pte & PG_DIRTY_MASK ? 'D' : '-',
1126 pte & PG_ACCESSED_MASK ? 'A' : '-',
1127 pte & PG_PCD_MASK ? 'C' : '-',
1128 pte & PG_PWT_MASK ? 'T' : '-',
1129 pte & PG_USER_MASK ? 'U' : '-',
1130 pte & PG_RW_MASK ? 'W' : '-');
1131}
1132
1133static void tlb_info(void)
1134{
bellard6a00d602005-11-21 23:25:50 +00001135 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001136 int l1, l2;
1137 uint32_t pgd, pde, pte;
1138
bellard6a00d602005-11-21 23:25:50 +00001139 env = mon_get_cpu();
1140 if (!env)
1141 return;
1142
bellardb86bda52004-09-18 19:32:46 +00001143 if (!(env->cr[0] & CR0_PG_MASK)) {
1144 term_printf("PG disabled\n");
1145 return;
1146 }
1147 pgd = env->cr[3] & ~0xfff;
1148 for(l1 = 0; l1 < 1024; l1++) {
1149 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1150 pde = le32_to_cpu(pde);
1151 if (pde & PG_PRESENT_MASK) {
1152 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1153 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1154 } else {
1155 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001156 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001157 (uint8_t *)&pte, 4);
1158 pte = le32_to_cpu(pte);
1159 if (pte & PG_PRESENT_MASK) {
ths5fafdf22007-09-16 21:08:06 +00001160 print_pte((l1 << 22) + (l2 << 12),
1161 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001162 ~0xfff);
1163 }
1164 }
1165 }
1166 }
1167 }
1168}
1169
ths5fafdf22007-09-16 21:08:06 +00001170static void mem_print(uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001171 uint32_t end, int prot)
1172{
bellard9746b152004-11-11 18:30:24 +00001173 int prot1;
1174 prot1 = *plast_prot;
1175 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001176 if (*pstart != -1) {
1177 term_printf("%08x-%08x %08x %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001178 *pstart, end, end - *pstart,
bellard9746b152004-11-11 18:30:24 +00001179 prot1 & PG_USER_MASK ? 'u' : '-',
bellardb86bda52004-09-18 19:32:46 +00001180 'r',
bellard9746b152004-11-11 18:30:24 +00001181 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001182 }
1183 if (prot != 0)
1184 *pstart = end;
1185 else
1186 *pstart = -1;
1187 *plast_prot = prot;
1188 }
1189}
1190
1191static void mem_info(void)
1192{
bellard6a00d602005-11-21 23:25:50 +00001193 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001194 int l1, l2, prot, last_prot;
1195 uint32_t pgd, pde, pte, start, end;
1196
bellard6a00d602005-11-21 23:25:50 +00001197 env = mon_get_cpu();
1198 if (!env)
1199 return;
1200
bellardb86bda52004-09-18 19:32:46 +00001201 if (!(env->cr[0] & CR0_PG_MASK)) {
1202 term_printf("PG disabled\n");
1203 return;
1204 }
1205 pgd = env->cr[3] & ~0xfff;
1206 last_prot = 0;
1207 start = -1;
1208 for(l1 = 0; l1 < 1024; l1++) {
1209 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1210 pde = le32_to_cpu(pde);
1211 end = l1 << 22;
1212 if (pde & PG_PRESENT_MASK) {
1213 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1214 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1215 mem_print(&start, &last_prot, end, prot);
1216 } else {
1217 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001218 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001219 (uint8_t *)&pte, 4);
1220 pte = le32_to_cpu(pte);
1221 end = (l1 << 22) + (l2 << 12);
1222 if (pte & PG_PRESENT_MASK) {
1223 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1224 } else {
1225 prot = 0;
1226 }
1227 mem_print(&start, &last_prot, end, prot);
1228 }
1229 }
1230 } else {
1231 prot = 0;
1232 mem_print(&start, &last_prot, end, prot);
1233 }
1234 }
1235}
1236#endif
1237
bellard0f4c6412005-07-24 18:10:56 +00001238static void do_info_kqemu(void)
1239{
1240#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001241 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001242 int val;
1243 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001244 env = mon_get_cpu();
1245 if (!env) {
1246 term_printf("No cpu initialized yet");
1247 return;
1248 }
1249 val = env->kqemu_enabled;
bellard5f1ce942006-02-08 22:40:15 +00001250 term_printf("kqemu support: ");
1251 switch(val) {
1252 default:
1253 case 0:
1254 term_printf("disabled\n");
1255 break;
1256 case 1:
1257 term_printf("enabled for user code\n");
1258 break;
1259 case 2:
1260 term_printf("enabled for user and kernel code\n");
1261 break;
1262 }
bellard0f4c6412005-07-24 18:10:56 +00001263#else
bellard5f1ce942006-02-08 22:40:15 +00001264 term_printf("kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001265#endif
ths5fafdf22007-09-16 21:08:06 +00001266}
bellard0f4c6412005-07-24 18:10:56 +00001267
aliguori7ba1e612008-11-05 16:04:33 +00001268static void do_info_kvm(void)
1269{
1270#ifdef CONFIG_KVM
1271 term_printf("kvm support: ");
1272 if (kvm_enabled())
1273 term_printf("enabled\n");
1274 else
1275 term_printf("disabled\n");
1276#else
1277 term_printf("kvm support: not compiled\n");
1278#endif
1279}
1280
bellard5f1ce942006-02-08 22:40:15 +00001281#ifdef CONFIG_PROFILER
1282
1283int64_t kqemu_time;
1284int64_t qemu_time;
1285int64_t kqemu_exec_count;
1286int64_t dev_time;
1287int64_t kqemu_ret_int_count;
1288int64_t kqemu_ret_excp_count;
1289int64_t kqemu_ret_intr_count;
1290
1291static void do_info_profile(void)
1292{
1293 int64_t total;
1294 total = qemu_time;
1295 if (total == 0)
1296 total = 1;
bellard26a76462006-06-25 18:15:32 +00001297 term_printf("async time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001298 dev_time, dev_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001299 term_printf("qemu time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001300 qemu_time, qemu_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001301 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
bellard5f1ce942006-02-08 22:40:15 +00001302 kqemu_time, kqemu_time / (double)ticks_per_sec,
1303 kqemu_time / (double)total * 100.0,
1304 kqemu_exec_count,
1305 kqemu_ret_int_count,
1306 kqemu_ret_excp_count,
1307 kqemu_ret_intr_count);
1308 qemu_time = 0;
1309 kqemu_time = 0;
1310 kqemu_exec_count = 0;
1311 dev_time = 0;
1312 kqemu_ret_int_count = 0;
1313 kqemu_ret_excp_count = 0;
1314 kqemu_ret_intr_count = 0;
1315#ifdef USE_KQEMU
1316 kqemu_record_dump();
1317#endif
1318}
1319#else
1320static void do_info_profile(void)
1321{
1322 term_printf("Internal profiler not compiled\n");
1323}
1324#endif
1325
bellardec36b692006-07-16 18:57:03 +00001326/* Capture support */
1327static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1328
1329static void do_info_capture (void)
1330{
1331 int i;
1332 CaptureState *s;
1333
1334 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1335 term_printf ("[%d]: ", i);
1336 s->ops.info (s->opaque);
1337 }
1338}
1339
1340static void do_stop_capture (int n)
1341{
1342 int i;
1343 CaptureState *s;
1344
1345 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1346 if (i == n) {
1347 s->ops.destroy (s->opaque);
1348 LIST_REMOVE (s, entries);
1349 qemu_free (s);
1350 return;
1351 }
1352 }
1353}
1354
1355#ifdef HAS_AUDIO
bellardec36b692006-07-16 18:57:03 +00001356static void do_wav_capture (const char *path,
1357 int has_freq, int freq,
1358 int has_bits, int bits,
1359 int has_channels, int nchannels)
1360{
1361 CaptureState *s;
1362
1363 s = qemu_mallocz (sizeof (*s));
1364 if (!s) {
1365 term_printf ("Not enough memory to add wave capture\n");
1366 return;
1367 }
1368
1369 freq = has_freq ? freq : 44100;
1370 bits = has_bits ? bits : 16;
1371 nchannels = has_channels ? nchannels : 2;
1372
1373 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1374 term_printf ("Faied to add wave capture\n");
1375 qemu_free (s);
1376 }
1377 LIST_INSERT_HEAD (&capture_head, s, entries);
1378}
1379#endif
1380
aurel32dc1c0b72008-04-27 23:52:12 +00001381#if defined(TARGET_I386)
1382static void do_inject_nmi(int cpu_index)
1383{
1384 CPUState *env;
1385
1386 for (env = first_cpu; env != NULL; env = env->next_cpu)
1387 if (env->cpu_index == cpu_index) {
1388 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1389 break;
1390 }
1391}
1392#endif
1393
aliguoridf751fa2008-12-04 20:19:35 +00001394static void do_balloon(int value)
1395{
1396 ram_addr_t target = value;
1397 qemu_balloon(target << 20);
1398}
1399
1400static void do_info_balloon(void)
1401{
1402 ram_addr_t actual;
1403
1404 actual = qemu_balloon_status();
aliguoribd322082008-12-04 20:33:06 +00001405 if (kvm_enabled() && !kvm_has_sync_mmu())
1406 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1407 else if (actual == 0)
aliguoridf751fa2008-12-04 20:19:35 +00001408 term_printf("Ballooning not activated in VM\n");
1409 else
1410 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
1411}
1412
blueswir18662d652008-10-02 18:32:44 +00001413static const term_cmd_t term_cmds[] = {
ths5fafdf22007-09-16 21:08:06 +00001414 { "help|?", "s?", do_help,
bellard9dc39cb2004-03-14 21:38:27 +00001415 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001416 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001417 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001418 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001419 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001420 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001421 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001422 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001423 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001424 { "change", "BFs?", do_change,
1425 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001426 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001427 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001428 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001429 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001430 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001431 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001432 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001433 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001434 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001435 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001436 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001437 "tag|id", "delete a VM snapshot from its tag or id" },
1438 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001439 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001440 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001441 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001442#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001443 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001444 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001445#endif
ths5fafdf22007-09-16 21:08:06 +00001446 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001447 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001448 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001449 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001450 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001451 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001452 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001453 "/fmt addr", "I/O port read" },
1454
balrogc8256f92008-06-08 22:45:01 +00001455 { "sendkey", "si?", do_sendkey,
1456 "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 +00001457 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001458 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001459 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001460 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001461 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001462 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001463 { "usb_add", "s", do_usb_add,
1464 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1465 { "usb_del", "s", do_usb_del,
1466 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001467 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001468 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001469 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001470 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001471 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001472 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001473 { "mouse_set", "i", do_mouse_set,
1474 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001475#ifdef HAS_AUDIO
1476 { "wavcapture", "si?i?i?", do_wav_capture,
1477 "path [frequency bits channels]",
1478 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1479#endif
1480 { "stopcapture", "i", do_stop_capture,
1481 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001482 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001483 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001484 { "pmemsave", "lis", do_physical_memory_save,
1485 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001486 { "boot_set", "s", do_boot_set,
1487 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001488#if defined(TARGET_I386)
1489 { "nmi", "i", do_inject_nmi,
1490 "cpu", "inject an NMI on the given CPU", },
1491#endif
aliguori5bb79102008-10-13 03:12:02 +00001492 { "migrate", "-ds", do_migrate,
1493 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1494 { "migrate_cancel", "", do_migrate_cancel,
1495 "", "cancel the current VM migration" },
1496 { "migrate_set_speed", "s", do_migrate_set_speed,
1497 "value", "set maximum speed (in bytes) for migrations" },
aliguoridf751fa2008-12-04 20:19:35 +00001498 { "balloon", "i", do_balloon,
1499 "target", "request VM to change it's memory allocation (in MB)" },
ths5fafdf22007-09-16 21:08:06 +00001500 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001501};
1502
blueswir18662d652008-10-02 18:32:44 +00001503static const term_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001504 { "version", "", do_info_version,
1505 "", "show the version of qemu" },
bellard9307c4c2004-04-04 12:57:25 +00001506 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001507 "", "show the network state" },
aliguori5ccfae12008-10-31 17:31:29 +00001508 { "chardev", "", qemu_chr_info,
1509 "", "show the character devices" },
bellard9307c4c2004-04-04 12:57:25 +00001510 { "block", "", do_info_block,
bellard9dc39cb2004-03-14 21:38:27 +00001511 "", "show the block devices" },
thsa36e69d2007-12-02 05:18:19 +00001512 { "blockstats", "", do_info_blockstats,
1513 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001514 { "registers", "", do_info_registers,
1515 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001516 { "cpus", "", do_info_cpus,
1517 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001518 { "history", "", do_info_history,
1519 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001520 { "irq", "", irq_info,
1521 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001522 { "pic", "", pic_info,
1523 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001524 { "pci", "", pci_info,
1525 "", "show PCI info", },
bellardb86bda52004-09-18 19:32:46 +00001526#if defined(TARGET_I386)
1527 { "tlb", "", tlb_info,
1528 "", "show virtual to physical memory mappings", },
1529 { "mem", "", mem_info,
1530 "", "show the active virtual memory mappings", },
1531#endif
bellarde3db7222005-01-26 22:00:47 +00001532 { "jit", "", do_info_jit,
1533 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001534 { "kqemu", "", do_info_kqemu,
1535 "", "show kqemu information", },
aliguori7ba1e612008-11-05 16:04:33 +00001536 { "kvm", "", do_info_kvm,
1537 "", "show kvm information", },
bellarda594cfb2005-11-06 16:13:29 +00001538 { "usb", "", usb_info,
1539 "", "show guest USB devices", },
1540 { "usbhost", "", usb_host_info,
1541 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001542 { "profile", "", do_info_profile,
1543 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001544 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001545 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001546 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001547 "", "show the currently saved VM snapshots" },
balrog201a51f2007-04-30 00:51:09 +00001548 { "pcmcia", "", pcmcia_info,
1549 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001550 { "mice", "", do_info_mice,
1551 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001552 { "vnc", "", do_info_vnc,
1553 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001554 { "name", "", do_info_name,
1555 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001556 { "uuid", "", do_info_uuid,
1557 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001558#if defined(TARGET_PPC)
1559 { "cpustats", "", do_info_cpu_stats,
1560 "", "show CPU statistics", },
1561#endif
blueswir131a60e22007-10-26 18:42:59 +00001562#if defined(CONFIG_SLIRP)
1563 { "slirp", "", do_info_slirp,
1564 "", "show SLIRP statistics", },
1565#endif
aliguori5bb79102008-10-13 03:12:02 +00001566 { "migrate", "", do_info_migrate, "", "show migration status" },
aliguoridf751fa2008-12-04 20:19:35 +00001567 { "balloon", "", do_info_balloon,
1568 "", "show balloon information" },
bellard9dc39cb2004-03-14 21:38:27 +00001569 { NULL, NULL, },
1570};
1571
bellard9307c4c2004-04-04 12:57:25 +00001572/*******************************************************************/
1573
1574static const char *pch;
1575static jmp_buf expr_env;
1576
bellard92a31b12005-02-10 22:00:52 +00001577#define MD_TLONG 0
1578#define MD_I32 1
1579
bellard9307c4c2004-04-04 12:57:25 +00001580typedef struct MonitorDef {
1581 const char *name;
1582 int offset;
blueswir18662d652008-10-02 18:32:44 +00001583 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001584 int type;
bellard9307c4c2004-04-04 12:57:25 +00001585} MonitorDef;
1586
bellard57206fd2004-04-25 18:54:52 +00001587#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001588static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001589{
bellard6a00d602005-11-21 23:25:50 +00001590 CPUState *env = mon_get_cpu();
1591 if (!env)
1592 return 0;
1593 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001594}
1595#endif
1596
bellarda541f292004-04-12 20:39:29 +00001597#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001598static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001599{
bellard6a00d602005-11-21 23:25:50 +00001600 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001601 unsigned int u;
1602 int i;
1603
bellard6a00d602005-11-21 23:25:50 +00001604 if (!env)
1605 return 0;
1606
bellarda541f292004-04-12 20:39:29 +00001607 u = 0;
1608 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001609 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001610
1611 return u;
1612}
1613
blueswir18662d652008-10-02 18:32:44 +00001614static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001615{
bellard6a00d602005-11-21 23:25:50 +00001616 CPUState *env = mon_get_cpu();
1617 if (!env)
1618 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001619 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001620}
1621
blueswir18662d652008-10-02 18:32:44 +00001622static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001623{
bellard6a00d602005-11-21 23:25:50 +00001624 CPUState *env = mon_get_cpu();
1625 if (!env)
1626 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001627 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001628}
bellard9fddaa02004-05-21 12:59:32 +00001629
blueswir18662d652008-10-02 18:32:44 +00001630static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001631{
bellard6a00d602005-11-21 23:25:50 +00001632 CPUState *env = mon_get_cpu();
1633 if (!env)
1634 return 0;
1635 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001636}
1637
blueswir18662d652008-10-02 18:32:44 +00001638static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001639{
bellard6a00d602005-11-21 23:25:50 +00001640 CPUState *env = mon_get_cpu();
1641 if (!env)
1642 return 0;
1643 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001644}
1645
blueswir18662d652008-10-02 18:32:44 +00001646static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001647{
bellard6a00d602005-11-21 23:25:50 +00001648 CPUState *env = mon_get_cpu();
1649 if (!env)
1650 return 0;
1651 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001652}
bellarda541f292004-04-12 20:39:29 +00001653#endif
1654
bellarde95c8d52004-09-30 22:22:08 +00001655#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001656#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001657static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001658{
bellard6a00d602005-11-21 23:25:50 +00001659 CPUState *env = mon_get_cpu();
1660 if (!env)
1661 return 0;
1662 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001663}
bellard7b936c02005-10-30 17:05:13 +00001664#endif
bellarde95c8d52004-09-30 22:22:08 +00001665
blueswir18662d652008-10-02 18:32:44 +00001666static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001667{
bellard6a00d602005-11-21 23:25:50 +00001668 CPUState *env = mon_get_cpu();
1669 if (!env)
1670 return 0;
1671 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001672}
1673#endif
1674
blueswir18662d652008-10-02 18:32:44 +00001675static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001676#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001677
1678#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001679 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001680 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001681 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001682
bellard9307c4c2004-04-04 12:57:25 +00001683 { "eax", offsetof(CPUState, regs[0]) },
1684 { "ecx", offsetof(CPUState, regs[1]) },
1685 { "edx", offsetof(CPUState, regs[2]) },
1686 { "ebx", offsetof(CPUState, regs[3]) },
1687 { "esp|sp", offsetof(CPUState, regs[4]) },
1688 { "ebp|fp", offsetof(CPUState, regs[5]) },
1689 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001690 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001691#ifdef TARGET_X86_64
1692 { "r8", offsetof(CPUState, regs[8]) },
1693 { "r9", offsetof(CPUState, regs[9]) },
1694 { "r10", offsetof(CPUState, regs[10]) },
1695 { "r11", offsetof(CPUState, regs[11]) },
1696 { "r12", offsetof(CPUState, regs[12]) },
1697 { "r13", offsetof(CPUState, regs[13]) },
1698 { "r14", offsetof(CPUState, regs[14]) },
1699 { "r15", offsetof(CPUState, regs[15]) },
1700#endif
bellard9307c4c2004-04-04 12:57:25 +00001701 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001702 { "eip", offsetof(CPUState, eip) },
1703 SEG("cs", R_CS)
1704 SEG("ds", R_DS)
1705 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001706 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001707 SEG("fs", R_FS)
1708 SEG("gs", R_GS)
1709 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001710#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001711 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001712 { "r0", offsetof(CPUState, gpr[0]) },
1713 { "r1", offsetof(CPUState, gpr[1]) },
1714 { "r2", offsetof(CPUState, gpr[2]) },
1715 { "r3", offsetof(CPUState, gpr[3]) },
1716 { "r4", offsetof(CPUState, gpr[4]) },
1717 { "r5", offsetof(CPUState, gpr[5]) },
1718 { "r6", offsetof(CPUState, gpr[6]) },
1719 { "r7", offsetof(CPUState, gpr[7]) },
1720 { "r8", offsetof(CPUState, gpr[8]) },
1721 { "r9", offsetof(CPUState, gpr[9]) },
1722 { "r10", offsetof(CPUState, gpr[10]) },
1723 { "r11", offsetof(CPUState, gpr[11]) },
1724 { "r12", offsetof(CPUState, gpr[12]) },
1725 { "r13", offsetof(CPUState, gpr[13]) },
1726 { "r14", offsetof(CPUState, gpr[14]) },
1727 { "r15", offsetof(CPUState, gpr[15]) },
1728 { "r16", offsetof(CPUState, gpr[16]) },
1729 { "r17", offsetof(CPUState, gpr[17]) },
1730 { "r18", offsetof(CPUState, gpr[18]) },
1731 { "r19", offsetof(CPUState, gpr[19]) },
1732 { "r20", offsetof(CPUState, gpr[20]) },
1733 { "r21", offsetof(CPUState, gpr[21]) },
1734 { "r22", offsetof(CPUState, gpr[22]) },
1735 { "r23", offsetof(CPUState, gpr[23]) },
1736 { "r24", offsetof(CPUState, gpr[24]) },
1737 { "r25", offsetof(CPUState, gpr[25]) },
1738 { "r26", offsetof(CPUState, gpr[26]) },
1739 { "r27", offsetof(CPUState, gpr[27]) },
1740 { "r28", offsetof(CPUState, gpr[28]) },
1741 { "r29", offsetof(CPUState, gpr[29]) },
1742 { "r30", offsetof(CPUState, gpr[30]) },
1743 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001744 /* Floating point registers */
1745 { "f0", offsetof(CPUState, fpr[0]) },
1746 { "f1", offsetof(CPUState, fpr[1]) },
1747 { "f2", offsetof(CPUState, fpr[2]) },
1748 { "f3", offsetof(CPUState, fpr[3]) },
1749 { "f4", offsetof(CPUState, fpr[4]) },
1750 { "f5", offsetof(CPUState, fpr[5]) },
1751 { "f6", offsetof(CPUState, fpr[6]) },
1752 { "f7", offsetof(CPUState, fpr[7]) },
1753 { "f8", offsetof(CPUState, fpr[8]) },
1754 { "f9", offsetof(CPUState, fpr[9]) },
1755 { "f10", offsetof(CPUState, fpr[10]) },
1756 { "f11", offsetof(CPUState, fpr[11]) },
1757 { "f12", offsetof(CPUState, fpr[12]) },
1758 { "f13", offsetof(CPUState, fpr[13]) },
1759 { "f14", offsetof(CPUState, fpr[14]) },
1760 { "f15", offsetof(CPUState, fpr[15]) },
1761 { "f16", offsetof(CPUState, fpr[16]) },
1762 { "f17", offsetof(CPUState, fpr[17]) },
1763 { "f18", offsetof(CPUState, fpr[18]) },
1764 { "f19", offsetof(CPUState, fpr[19]) },
1765 { "f20", offsetof(CPUState, fpr[20]) },
1766 { "f21", offsetof(CPUState, fpr[21]) },
1767 { "f22", offsetof(CPUState, fpr[22]) },
1768 { "f23", offsetof(CPUState, fpr[23]) },
1769 { "f24", offsetof(CPUState, fpr[24]) },
1770 { "f25", offsetof(CPUState, fpr[25]) },
1771 { "f26", offsetof(CPUState, fpr[26]) },
1772 { "f27", offsetof(CPUState, fpr[27]) },
1773 { "f28", offsetof(CPUState, fpr[28]) },
1774 { "f29", offsetof(CPUState, fpr[29]) },
1775 { "f30", offsetof(CPUState, fpr[30]) },
1776 { "f31", offsetof(CPUState, fpr[31]) },
1777 { "fpscr", offsetof(CPUState, fpscr) },
1778 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00001779 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00001780 { "lr", offsetof(CPUState, lr) },
1781 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00001782 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00001783 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00001784 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00001785 { "msr", 0, &monitor_get_msr, },
1786 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00001787 { "tbu", 0, &monitor_get_tbu, },
1788 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00001789#if defined(TARGET_PPC64)
1790 /* Address space register */
1791 { "asr", offsetof(CPUState, asr) },
1792#endif
1793 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00001794 { "sdr1", offsetof(CPUState, sdr1) },
1795 { "sr0", offsetof(CPUState, sr[0]) },
1796 { "sr1", offsetof(CPUState, sr[1]) },
1797 { "sr2", offsetof(CPUState, sr[2]) },
1798 { "sr3", offsetof(CPUState, sr[3]) },
1799 { "sr4", offsetof(CPUState, sr[4]) },
1800 { "sr5", offsetof(CPUState, sr[5]) },
1801 { "sr6", offsetof(CPUState, sr[6]) },
1802 { "sr7", offsetof(CPUState, sr[7]) },
1803 { "sr8", offsetof(CPUState, sr[8]) },
1804 { "sr9", offsetof(CPUState, sr[9]) },
1805 { "sr10", offsetof(CPUState, sr[10]) },
1806 { "sr11", offsetof(CPUState, sr[11]) },
1807 { "sr12", offsetof(CPUState, sr[12]) },
1808 { "sr13", offsetof(CPUState, sr[13]) },
1809 { "sr14", offsetof(CPUState, sr[14]) },
1810 { "sr15", offsetof(CPUState, sr[15]) },
1811 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00001812#elif defined(TARGET_SPARC)
1813 { "g0", offsetof(CPUState, gregs[0]) },
1814 { "g1", offsetof(CPUState, gregs[1]) },
1815 { "g2", offsetof(CPUState, gregs[2]) },
1816 { "g3", offsetof(CPUState, gregs[3]) },
1817 { "g4", offsetof(CPUState, gregs[4]) },
1818 { "g5", offsetof(CPUState, gregs[5]) },
1819 { "g6", offsetof(CPUState, gregs[6]) },
1820 { "g7", offsetof(CPUState, gregs[7]) },
1821 { "o0", 0, monitor_get_reg },
1822 { "o1", 1, monitor_get_reg },
1823 { "o2", 2, monitor_get_reg },
1824 { "o3", 3, monitor_get_reg },
1825 { "o4", 4, monitor_get_reg },
1826 { "o5", 5, monitor_get_reg },
1827 { "o6", 6, monitor_get_reg },
1828 { "o7", 7, monitor_get_reg },
1829 { "l0", 8, monitor_get_reg },
1830 { "l1", 9, monitor_get_reg },
1831 { "l2", 10, monitor_get_reg },
1832 { "l3", 11, monitor_get_reg },
1833 { "l4", 12, monitor_get_reg },
1834 { "l5", 13, monitor_get_reg },
1835 { "l6", 14, monitor_get_reg },
1836 { "l7", 15, monitor_get_reg },
1837 { "i0", 16, monitor_get_reg },
1838 { "i1", 17, monitor_get_reg },
1839 { "i2", 18, monitor_get_reg },
1840 { "i3", 19, monitor_get_reg },
1841 { "i4", 20, monitor_get_reg },
1842 { "i5", 21, monitor_get_reg },
1843 { "i6", 22, monitor_get_reg },
1844 { "i7", 23, monitor_get_reg },
1845 { "pc", offsetof(CPUState, pc) },
1846 { "npc", offsetof(CPUState, npc) },
1847 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00001848#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00001849 { "psr", 0, &monitor_get_psr, },
1850 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00001851#endif
bellarde95c8d52004-09-30 22:22:08 +00001852 { "tbr", offsetof(CPUState, tbr) },
1853 { "fsr", offsetof(CPUState, fsr) },
1854 { "f0", offsetof(CPUState, fpr[0]) },
1855 { "f1", offsetof(CPUState, fpr[1]) },
1856 { "f2", offsetof(CPUState, fpr[2]) },
1857 { "f3", offsetof(CPUState, fpr[3]) },
1858 { "f4", offsetof(CPUState, fpr[4]) },
1859 { "f5", offsetof(CPUState, fpr[5]) },
1860 { "f6", offsetof(CPUState, fpr[6]) },
1861 { "f7", offsetof(CPUState, fpr[7]) },
1862 { "f8", offsetof(CPUState, fpr[8]) },
1863 { "f9", offsetof(CPUState, fpr[9]) },
1864 { "f10", offsetof(CPUState, fpr[10]) },
1865 { "f11", offsetof(CPUState, fpr[11]) },
1866 { "f12", offsetof(CPUState, fpr[12]) },
1867 { "f13", offsetof(CPUState, fpr[13]) },
1868 { "f14", offsetof(CPUState, fpr[14]) },
1869 { "f15", offsetof(CPUState, fpr[15]) },
1870 { "f16", offsetof(CPUState, fpr[16]) },
1871 { "f17", offsetof(CPUState, fpr[17]) },
1872 { "f18", offsetof(CPUState, fpr[18]) },
1873 { "f19", offsetof(CPUState, fpr[19]) },
1874 { "f20", offsetof(CPUState, fpr[20]) },
1875 { "f21", offsetof(CPUState, fpr[21]) },
1876 { "f22", offsetof(CPUState, fpr[22]) },
1877 { "f23", offsetof(CPUState, fpr[23]) },
1878 { "f24", offsetof(CPUState, fpr[24]) },
1879 { "f25", offsetof(CPUState, fpr[25]) },
1880 { "f26", offsetof(CPUState, fpr[26]) },
1881 { "f27", offsetof(CPUState, fpr[27]) },
1882 { "f28", offsetof(CPUState, fpr[28]) },
1883 { "f29", offsetof(CPUState, fpr[29]) },
1884 { "f30", offsetof(CPUState, fpr[30]) },
1885 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00001886#ifdef TARGET_SPARC64
1887 { "f32", offsetof(CPUState, fpr[32]) },
1888 { "f34", offsetof(CPUState, fpr[34]) },
1889 { "f36", offsetof(CPUState, fpr[36]) },
1890 { "f38", offsetof(CPUState, fpr[38]) },
1891 { "f40", offsetof(CPUState, fpr[40]) },
1892 { "f42", offsetof(CPUState, fpr[42]) },
1893 { "f44", offsetof(CPUState, fpr[44]) },
1894 { "f46", offsetof(CPUState, fpr[46]) },
1895 { "f48", offsetof(CPUState, fpr[48]) },
1896 { "f50", offsetof(CPUState, fpr[50]) },
1897 { "f52", offsetof(CPUState, fpr[52]) },
1898 { "f54", offsetof(CPUState, fpr[54]) },
1899 { "f56", offsetof(CPUState, fpr[56]) },
1900 { "f58", offsetof(CPUState, fpr[58]) },
1901 { "f60", offsetof(CPUState, fpr[60]) },
1902 { "f62", offsetof(CPUState, fpr[62]) },
1903 { "asi", offsetof(CPUState, asi) },
1904 { "pstate", offsetof(CPUState, pstate) },
1905 { "cansave", offsetof(CPUState, cansave) },
1906 { "canrestore", offsetof(CPUState, canrestore) },
1907 { "otherwin", offsetof(CPUState, otherwin) },
1908 { "wstate", offsetof(CPUState, wstate) },
1909 { "cleanwin", offsetof(CPUState, cleanwin) },
1910 { "fprs", offsetof(CPUState, fprs) },
1911#endif
bellard9307c4c2004-04-04 12:57:25 +00001912#endif
1913 { NULL },
1914};
1915
ths5fafdf22007-09-16 21:08:06 +00001916static void expr_error(const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001917{
bellard9307c4c2004-04-04 12:57:25 +00001918 term_printf(fmt);
1919 term_printf("\n");
1920 longjmp(expr_env, 1);
1921}
1922
bellard6a00d602005-11-21 23:25:50 +00001923/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00001924static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00001925{
blueswir18662d652008-10-02 18:32:44 +00001926 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00001927 void *ptr;
1928
bellard9307c4c2004-04-04 12:57:25 +00001929 for(md = monitor_defs; md->name != NULL; md++) {
1930 if (compare_cmd(name, md->name)) {
1931 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00001932 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00001933 } else {
bellard6a00d602005-11-21 23:25:50 +00001934 CPUState *env = mon_get_cpu();
1935 if (!env)
1936 return -2;
1937 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00001938 switch(md->type) {
1939 case MD_I32:
1940 *pval = *(int32_t *)ptr;
1941 break;
1942 case MD_TLONG:
1943 *pval = *(target_long *)ptr;
1944 break;
1945 default:
1946 *pval = 0;
1947 break;
1948 }
bellard9307c4c2004-04-04 12:57:25 +00001949 }
1950 return 0;
1951 }
1952 }
1953 return -1;
1954}
1955
1956static void next(void)
1957{
1958 if (pch != '\0') {
1959 pch++;
blueswir1cd390082008-11-16 13:53:32 +00001960 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00001961 pch++;
1962 }
1963}
1964
blueswir1c2efc952007-09-25 17:28:42 +00001965static int64_t expr_sum(void);
bellard9307c4c2004-04-04 12:57:25 +00001966
blueswir1c2efc952007-09-25 17:28:42 +00001967static int64_t expr_unary(void)
bellard9307c4c2004-04-04 12:57:25 +00001968{
blueswir1c2efc952007-09-25 17:28:42 +00001969 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00001970 char *p;
bellard6a00d602005-11-21 23:25:50 +00001971 int ret;
bellard9307c4c2004-04-04 12:57:25 +00001972
1973 switch(*pch) {
1974 case '+':
1975 next();
1976 n = expr_unary();
1977 break;
1978 case '-':
1979 next();
1980 n = -expr_unary();
1981 break;
1982 case '~':
1983 next();
1984 n = ~expr_unary();
1985 break;
1986 case '(':
1987 next();
1988 n = expr_sum();
1989 if (*pch != ')') {
1990 expr_error("')' expected");
1991 }
1992 next();
1993 break;
bellard81d09122004-07-14 17:21:37 +00001994 case '\'':
1995 pch++;
1996 if (*pch == '\0')
1997 expr_error("character constant expected");
1998 n = *pch;
1999 pch++;
2000 if (*pch != '\'')
2001 expr_error("missing terminating \' character");
2002 next();
2003 break;
bellard9307c4c2004-04-04 12:57:25 +00002004 case '$':
2005 {
2006 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00002007 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00002008
bellard9307c4c2004-04-04 12:57:25 +00002009 pch++;
2010 q = buf;
2011 while ((*pch >= 'a' && *pch <= 'z') ||
2012 (*pch >= 'A' && *pch <= 'Z') ||
2013 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00002014 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00002015 if ((q - buf) < sizeof(buf) - 1)
2016 *q++ = *pch;
2017 pch++;
2018 }
blueswir1cd390082008-11-16 13:53:32 +00002019 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002020 pch++;
2021 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00002022 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00002023 if (ret == -1)
bellard9307c4c2004-04-04 12:57:25 +00002024 expr_error("unknown register");
ths5fafdf22007-09-16 21:08:06 +00002025 else if (ret == -2)
bellard6a00d602005-11-21 23:25:50 +00002026 expr_error("no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00002027 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00002028 }
2029 break;
2030 case '\0':
2031 expr_error("unexpected end of expression");
2032 n = 0;
2033 break;
2034 default:
blueswir17743e582007-09-24 18:39:04 +00002035#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00002036 n = strtoull(pch, &p, 0);
2037#else
bellard9307c4c2004-04-04 12:57:25 +00002038 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00002039#endif
bellard9307c4c2004-04-04 12:57:25 +00002040 if (pch == p) {
2041 expr_error("invalid char in expression");
2042 }
2043 pch = p;
blueswir1cd390082008-11-16 13:53:32 +00002044 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002045 pch++;
2046 break;
2047 }
2048 return n;
2049}
2050
2051
blueswir1c2efc952007-09-25 17:28:42 +00002052static int64_t expr_prod(void)
bellard9307c4c2004-04-04 12:57:25 +00002053{
blueswir1c2efc952007-09-25 17:28:42 +00002054 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002055 int op;
ths3b46e622007-09-17 08:09:54 +00002056
bellard9307c4c2004-04-04 12:57:25 +00002057 val = expr_unary();
2058 for(;;) {
2059 op = *pch;
2060 if (op != '*' && op != '/' && op != '%')
2061 break;
2062 next();
2063 val2 = expr_unary();
2064 switch(op) {
2065 default:
2066 case '*':
2067 val *= val2;
2068 break;
2069 case '/':
2070 case '%':
ths5fafdf22007-09-16 21:08:06 +00002071 if (val2 == 0)
bellard5b602122004-05-22 21:41:05 +00002072 expr_error("division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002073 if (op == '/')
2074 val /= val2;
2075 else
2076 val %= val2;
2077 break;
2078 }
2079 }
2080 return val;
2081}
2082
blueswir1c2efc952007-09-25 17:28:42 +00002083static int64_t expr_logic(void)
bellard9307c4c2004-04-04 12:57:25 +00002084{
blueswir1c2efc952007-09-25 17:28:42 +00002085 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002086 int op;
bellard9307c4c2004-04-04 12:57:25 +00002087
2088 val = expr_prod();
2089 for(;;) {
2090 op = *pch;
2091 if (op != '&' && op != '|' && op != '^')
2092 break;
2093 next();
2094 val2 = expr_prod();
2095 switch(op) {
2096 default:
2097 case '&':
2098 val &= val2;
2099 break;
2100 case '|':
2101 val |= val2;
2102 break;
2103 case '^':
2104 val ^= val2;
2105 break;
2106 }
2107 }
2108 return val;
2109}
2110
blueswir1c2efc952007-09-25 17:28:42 +00002111static int64_t expr_sum(void)
bellard9307c4c2004-04-04 12:57:25 +00002112{
blueswir1c2efc952007-09-25 17:28:42 +00002113 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002114 int op;
bellard9307c4c2004-04-04 12:57:25 +00002115
2116 val = expr_logic();
2117 for(;;) {
2118 op = *pch;
2119 if (op != '+' && op != '-')
2120 break;
2121 next();
2122 val2 = expr_logic();
2123 if (op == '+')
2124 val += val2;
2125 else
2126 val -= val2;
2127 }
2128 return val;
2129}
2130
blueswir1c2efc952007-09-25 17:28:42 +00002131static int get_expr(int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002132{
2133 pch = *pp;
2134 if (setjmp(expr_env)) {
2135 *pp = pch;
2136 return -1;
2137 }
blueswir1cd390082008-11-16 13:53:32 +00002138 while (qemu_isspace(*pch))
bellard9307c4c2004-04-04 12:57:25 +00002139 pch++;
2140 *pval = expr_sum();
2141 *pp = pch;
2142 return 0;
2143}
2144
2145static int get_str(char *buf, int buf_size, const char **pp)
2146{
2147 const char *p;
2148 char *q;
2149 int c;
2150
bellard81d09122004-07-14 17:21:37 +00002151 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002152 p = *pp;
blueswir1cd390082008-11-16 13:53:32 +00002153 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002154 p++;
2155 if (*p == '\0') {
2156 fail:
bellard81d09122004-07-14 17:21:37 +00002157 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002158 *pp = p;
2159 return -1;
2160 }
bellard9307c4c2004-04-04 12:57:25 +00002161 if (*p == '\"') {
2162 p++;
2163 while (*p != '\0' && *p != '\"') {
2164 if (*p == '\\') {
2165 p++;
2166 c = *p++;
2167 switch(c) {
2168 case 'n':
2169 c = '\n';
2170 break;
2171 case 'r':
2172 c = '\r';
2173 break;
2174 case '\\':
2175 case '\'':
2176 case '\"':
2177 break;
2178 default:
2179 qemu_printf("unsupported escape code: '\\%c'\n", c);
2180 goto fail;
2181 }
2182 if ((q - buf) < buf_size - 1) {
2183 *q++ = c;
2184 }
2185 } else {
2186 if ((q - buf) < buf_size - 1) {
2187 *q++ = *p;
2188 }
2189 p++;
2190 }
2191 }
2192 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002193 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002194 goto fail;
2195 }
2196 p++;
2197 } else {
blueswir1cd390082008-11-16 13:53:32 +00002198 while (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002199 if ((q - buf) < buf_size - 1) {
2200 *q++ = *p;
2201 }
2202 p++;
2203 }
bellard9307c4c2004-04-04 12:57:25 +00002204 }
bellard81d09122004-07-14 17:21:37 +00002205 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002206 *pp = p;
2207 return 0;
2208}
2209
2210static int default_fmt_format = 'x';
2211static int default_fmt_size = 4;
2212
2213#define MAX_ARGS 16
2214
bellard7e2515e2004-08-01 21:52:19 +00002215static void monitor_handle_command(const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002216{
2217 const char *p, *pstart, *typestr;
2218 char *q;
2219 int c, nb_args, len, i, has_arg;
blueswir18662d652008-10-02 18:32:44 +00002220 const term_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002221 char cmdname[256];
2222 char buf[1024];
2223 void *str_allocated[MAX_ARGS];
2224 void *args[MAX_ARGS];
blueswir1a5f1b962008-08-17 20:21:51 +00002225 void (*handler_0)(void);
2226 void (*handler_1)(void *arg0);
2227 void (*handler_2)(void *arg0, void *arg1);
2228 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2229 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2230 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2231 void *arg4);
2232 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2233 void *arg4, void *arg5);
2234 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2235 void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002236
2237#ifdef DEBUG
2238 term_printf("command='%s'\n", cmdline);
2239#endif
ths3b46e622007-09-17 08:09:54 +00002240
bellard9307c4c2004-04-04 12:57:25 +00002241 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002242 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002243 q = cmdname;
blueswir1cd390082008-11-16 13:53:32 +00002244 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002245 p++;
2246 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002247 return;
bellard9307c4c2004-04-04 12:57:25 +00002248 pstart = p;
blueswir1cd390082008-11-16 13:53:32 +00002249 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002250 p++;
2251 len = p - pstart;
2252 if (len > sizeof(cmdname) - 1)
2253 len = sizeof(cmdname) - 1;
2254 memcpy(cmdname, pstart, len);
2255 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002256
bellard9307c4c2004-04-04 12:57:25 +00002257 /* find the command */
bellard9dc39cb2004-03-14 21:38:27 +00002258 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002259 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002260 goto found;
2261 }
bellard9307c4c2004-04-04 12:57:25 +00002262 term_printf("unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002263 return;
2264 found:
bellard9307c4c2004-04-04 12:57:25 +00002265
2266 for(i = 0; i < MAX_ARGS; i++)
2267 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002268
bellard9307c4c2004-04-04 12:57:25 +00002269 /* parse the parameters */
2270 typestr = cmd->args_type;
2271 nb_args = 0;
2272 for(;;) {
2273 c = *typestr;
2274 if (c == '\0')
2275 break;
2276 typestr++;
2277 switch(c) {
2278 case 'F':
bellard81d09122004-07-14 17:21:37 +00002279 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002280 case 's':
2281 {
2282 int ret;
2283 char *str;
ths3b46e622007-09-17 08:09:54 +00002284
blueswir1cd390082008-11-16 13:53:32 +00002285 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002286 p++;
2287 if (*typestr == '?') {
2288 typestr++;
2289 if (*p == '\0') {
2290 /* no optional string: NULL argument */
2291 str = NULL;
2292 goto add_str;
2293 }
2294 }
2295 ret = get_str(buf, sizeof(buf), &p);
2296 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002297 switch(c) {
2298 case 'F':
bellard9307c4c2004-04-04 12:57:25 +00002299 term_printf("%s: filename expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002300 break;
2301 case 'B':
2302 term_printf("%s: block device name expected\n", cmdname);
2303 break;
2304 default:
bellard9307c4c2004-04-04 12:57:25 +00002305 term_printf("%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002306 break;
2307 }
bellard9307c4c2004-04-04 12:57:25 +00002308 goto fail;
2309 }
2310 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002311 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002312 str_allocated[nb_args] = str;
2313 add_str:
2314 if (nb_args >= MAX_ARGS) {
2315 error_args:
2316 term_printf("%s: too many arguments\n", cmdname);
2317 goto fail;
2318 }
2319 args[nb_args++] = str;
2320 }
2321 break;
2322 case '/':
2323 {
2324 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002325
blueswir1cd390082008-11-16 13:53:32 +00002326 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002327 p++;
2328 if (*p == '/') {
2329 /* format found */
2330 p++;
2331 count = 1;
blueswir1cd390082008-11-16 13:53:32 +00002332 if (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002333 count = 0;
blueswir1cd390082008-11-16 13:53:32 +00002334 while (qemu_isdigit(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002335 count = count * 10 + (*p - '0');
2336 p++;
2337 }
2338 }
2339 size = -1;
2340 format = -1;
2341 for(;;) {
2342 switch(*p) {
2343 case 'o':
2344 case 'd':
2345 case 'u':
2346 case 'x':
2347 case 'i':
2348 case 'c':
2349 format = *p++;
2350 break;
2351 case 'b':
2352 size = 1;
2353 p++;
2354 break;
2355 case 'h':
2356 size = 2;
2357 p++;
2358 break;
2359 case 'w':
2360 size = 4;
2361 p++;
2362 break;
2363 case 'g':
2364 case 'L':
2365 size = 8;
2366 p++;
2367 break;
2368 default:
2369 goto next;
2370 }
2371 }
2372 next:
blueswir1cd390082008-11-16 13:53:32 +00002373 if (*p != '\0' && !qemu_isspace(*p)) {
bellard9307c4c2004-04-04 12:57:25 +00002374 term_printf("invalid char in format: '%c'\n", *p);
2375 goto fail;
2376 }
bellard9307c4c2004-04-04 12:57:25 +00002377 if (format < 0)
2378 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002379 if (format != 'i') {
2380 /* for 'i', not specifying a size gives -1 as size */
2381 if (size < 0)
2382 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002383 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002384 }
bellard9307c4c2004-04-04 12:57:25 +00002385 default_fmt_format = format;
2386 } else {
2387 count = 1;
2388 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002389 if (format != 'i') {
2390 size = default_fmt_size;
2391 } else {
2392 size = -1;
2393 }
bellard9307c4c2004-04-04 12:57:25 +00002394 }
2395 if (nb_args + 3 > MAX_ARGS)
2396 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002397 args[nb_args++] = (void*)(long)count;
2398 args[nb_args++] = (void*)(long)format;
2399 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002400 }
2401 break;
2402 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002403 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002404 {
blueswir1c2efc952007-09-25 17:28:42 +00002405 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002406
blueswir1cd390082008-11-16 13:53:32 +00002407 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002408 p++;
bellard34405572004-06-08 00:55:58 +00002409 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002410 if (*typestr == '?') {
2411 if (*p == '\0')
2412 has_arg = 0;
2413 else
2414 has_arg = 1;
2415 } else {
2416 if (*p == '.') {
2417 p++;
blueswir1cd390082008-11-16 13:53:32 +00002418 while (qemu_isspace(*p))
bellard34405572004-06-08 00:55:58 +00002419 p++;
2420 has_arg = 1;
2421 } else {
2422 has_arg = 0;
2423 }
2424 }
bellard13224a82006-07-14 22:03:35 +00002425 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002426 if (nb_args >= MAX_ARGS)
2427 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002428 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002429 if (!has_arg) {
2430 if (nb_args >= MAX_ARGS)
2431 goto error_args;
2432 val = -1;
2433 goto add_num;
2434 }
2435 }
2436 if (get_expr(&val, &p))
2437 goto fail;
2438 add_num:
bellard92a31b12005-02-10 22:00:52 +00002439 if (c == 'i') {
2440 if (nb_args >= MAX_ARGS)
2441 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002442 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002443 } else {
2444 if ((nb_args + 1) >= MAX_ARGS)
2445 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002446#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002447 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002448#else
2449 args[nb_args++] = (void *)0;
2450#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002451 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002452 }
bellard9307c4c2004-04-04 12:57:25 +00002453 }
2454 break;
2455 case '-':
2456 {
2457 int has_option;
2458 /* option */
ths3b46e622007-09-17 08:09:54 +00002459
bellard9307c4c2004-04-04 12:57:25 +00002460 c = *typestr++;
2461 if (c == '\0')
2462 goto bad_type;
blueswir1cd390082008-11-16 13:53:32 +00002463 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002464 p++;
2465 has_option = 0;
2466 if (*p == '-') {
2467 p++;
2468 if (*p != c) {
ths5fafdf22007-09-16 21:08:06 +00002469 term_printf("%s: unsupported option -%c\n",
bellard9307c4c2004-04-04 12:57:25 +00002470 cmdname, *p);
2471 goto fail;
2472 }
2473 p++;
2474 has_option = 1;
2475 }
2476 if (nb_args >= MAX_ARGS)
2477 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002478 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002479 }
2480 break;
2481 default:
2482 bad_type:
2483 term_printf("%s: unknown type '%c'\n", cmdname, c);
2484 goto fail;
2485 }
2486 }
2487 /* check that all arguments were parsed */
blueswir1cd390082008-11-16 13:53:32 +00002488 while (qemu_isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002489 p++;
2490 if (*p != '\0') {
ths5fafdf22007-09-16 21:08:06 +00002491 term_printf("%s: extraneous characters at the end of line\n",
bellard9307c4c2004-04-04 12:57:25 +00002492 cmdname);
2493 goto fail;
2494 }
2495
2496 switch(nb_args) {
2497 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002498 handler_0 = cmd->handler;
2499 handler_0();
bellard9307c4c2004-04-04 12:57:25 +00002500 break;
2501 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002502 handler_1 = cmd->handler;
2503 handler_1(args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002504 break;
2505 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002506 handler_2 = cmd->handler;
2507 handler_2(args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002508 break;
2509 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002510 handler_3 = cmd->handler;
2511 handler_3(args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002512 break;
2513 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002514 handler_4 = cmd->handler;
2515 handler_4(args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002516 break;
2517 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002518 handler_5 = cmd->handler;
2519 handler_5(args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002520 break;
bellard34405572004-06-08 00:55:58 +00002521 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002522 handler_6 = cmd->handler;
2523 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002524 break;
bellardec36b692006-07-16 18:57:03 +00002525 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002526 handler_7 = cmd->handler;
2527 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
bellardec36b692006-07-16 18:57:03 +00002528 break;
bellard9307c4c2004-04-04 12:57:25 +00002529 default:
2530 term_printf("unsupported number of arguments: %d\n", nb_args);
2531 goto fail;
2532 }
2533 fail:
2534 for(i = 0; i < MAX_ARGS; i++)
2535 qemu_free(str_allocated[i]);
2536 return;
bellard9dc39cb2004-03-14 21:38:27 +00002537}
2538
bellard81d09122004-07-14 17:21:37 +00002539static void cmd_completion(const char *name, const char *list)
2540{
2541 const char *p, *pstart;
2542 char cmd[128];
2543 int len;
2544
2545 p = list;
2546 for(;;) {
2547 pstart = p;
2548 p = strchr(p, '|');
2549 if (!p)
2550 p = pstart + strlen(pstart);
2551 len = p - pstart;
2552 if (len > sizeof(cmd) - 2)
2553 len = sizeof(cmd) - 2;
2554 memcpy(cmd, pstart, len);
2555 cmd[len] = '\0';
2556 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2557 add_completion(cmd);
2558 }
2559 if (*p == '\0')
2560 break;
2561 p++;
2562 }
2563}
2564
2565static void file_completion(const char *input)
2566{
2567 DIR *ffs;
2568 struct dirent *d;
2569 char path[1024];
2570 char file[1024], file_prefix[1024];
2571 int input_path_len;
2572 const char *p;
2573
ths5fafdf22007-09-16 21:08:06 +00002574 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002575 if (!p) {
2576 input_path_len = 0;
2577 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002578 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002579 } else {
2580 input_path_len = p - input + 1;
2581 memcpy(path, input, input_path_len);
2582 if (input_path_len > sizeof(path) - 1)
2583 input_path_len = sizeof(path) - 1;
2584 path[input_path_len] = '\0';
2585 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2586 }
2587#ifdef DEBUG_COMPLETION
2588 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2589#endif
2590 ffs = opendir(path);
2591 if (!ffs)
2592 return;
2593 for(;;) {
2594 struct stat sb;
2595 d = readdir(ffs);
2596 if (!d)
2597 break;
2598 if (strstart(d->d_name, file_prefix, NULL)) {
2599 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002600 if (input_path_len < sizeof(file))
2601 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2602 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002603 /* stat the file to find out if it's a directory.
2604 * In that case add a slash to speed up typing long paths
2605 */
2606 stat(file, &sb);
2607 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002608 pstrcat(file, sizeof(file), "/");
bellard81d09122004-07-14 17:21:37 +00002609 add_completion(file);
2610 }
2611 }
2612 closedir(ffs);
2613}
2614
2615static void block_completion_it(void *opaque, const char *name)
2616{
2617 const char *input = opaque;
2618
2619 if (input[0] == '\0' ||
2620 !strncmp(name, (char *)input, strlen(input))) {
2621 add_completion(name);
2622 }
2623}
2624
2625/* NOTE: this parser is an approximate form of the real command parser */
2626static void parse_cmdline(const char *cmdline,
2627 int *pnb_args, char **args)
2628{
2629 const char *p;
2630 int nb_args, ret;
2631 char buf[1024];
2632
2633 p = cmdline;
2634 nb_args = 0;
2635 for(;;) {
blueswir1cd390082008-11-16 13:53:32 +00002636 while (qemu_isspace(*p))
bellard81d09122004-07-14 17:21:37 +00002637 p++;
2638 if (*p == '\0')
2639 break;
2640 if (nb_args >= MAX_ARGS)
2641 break;
2642 ret = get_str(buf, sizeof(buf), &p);
2643 args[nb_args] = qemu_strdup(buf);
2644 nb_args++;
2645 if (ret < 0)
2646 break;
2647 }
2648 *pnb_args = nb_args;
2649}
2650
bellard7e2515e2004-08-01 21:52:19 +00002651void readline_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002652{
2653 const char *cmdname;
2654 char *args[MAX_ARGS];
2655 int nb_args, i, len;
2656 const char *ptype, *str;
blueswir18662d652008-10-02 18:32:44 +00002657 const term_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002658 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002659
2660 parse_cmdline(cmdline, &nb_args, args);
2661#ifdef DEBUG_COMPLETION
2662 for(i = 0; i < nb_args; i++) {
2663 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2664 }
2665#endif
2666
2667 /* if the line ends with a space, it means we want to complete the
2668 next arg */
2669 len = strlen(cmdline);
blueswir1cd390082008-11-16 13:53:32 +00002670 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
bellard81d09122004-07-14 17:21:37 +00002671 if (nb_args >= MAX_ARGS)
2672 return;
2673 args[nb_args++] = qemu_strdup("");
2674 }
2675 if (nb_args <= 1) {
2676 /* command completion */
2677 if (nb_args == 0)
2678 cmdname = "";
2679 else
2680 cmdname = args[0];
2681 completion_index = strlen(cmdname);
2682 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2683 cmd_completion(cmdname, cmd->name);
2684 }
2685 } else {
2686 /* find the command */
2687 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2688 if (compare_cmd(args[0], cmd->name))
2689 goto found;
2690 }
2691 return;
2692 found:
2693 ptype = cmd->args_type;
2694 for(i = 0; i < nb_args - 2; i++) {
2695 if (*ptype != '\0') {
2696 ptype++;
2697 while (*ptype == '?')
2698 ptype++;
2699 }
2700 }
2701 str = args[nb_args - 1];
2702 switch(*ptype) {
2703 case 'F':
2704 /* file completion */
2705 completion_index = strlen(str);
2706 file_completion(str);
2707 break;
2708 case 'B':
2709 /* block device name completion */
2710 completion_index = strlen(str);
2711 bdrv_iterate(block_completion_it, (void *)str);
2712 break;
bellard7fe48482004-10-09 18:08:01 +00002713 case 's':
2714 /* XXX: more generic ? */
2715 if (!strcmp(cmd->name, "info")) {
2716 completion_index = strlen(str);
2717 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2718 cmd_completion(str, cmd->name);
2719 }
bellard64866c32006-05-07 18:03:31 +00002720 } else if (!strcmp(cmd->name, "sendkey")) {
2721 completion_index = strlen(str);
2722 for(key = key_defs; key->name != NULL; key++) {
2723 cmd_completion(str, key->name);
2724 }
bellard7fe48482004-10-09 18:08:01 +00002725 }
2726 break;
bellard81d09122004-07-14 17:21:37 +00002727 default:
2728 break;
2729 }
2730 }
2731 for(i = 0; i < nb_args; i++)
2732 qemu_free(args[i]);
2733}
2734
bellard9dc39cb2004-03-14 21:38:27 +00002735static int term_can_read(void *opaque)
2736{
bellard81d09122004-07-14 17:21:37 +00002737 return 128;
bellard9dc39cb2004-03-14 21:38:27 +00002738}
2739
2740static void term_read(void *opaque, const uint8_t *buf, int size)
2741{
2742 int i;
2743 for(i = 0; i < size; i++)
bellard7e2515e2004-08-01 21:52:19 +00002744 readline_handle_byte(buf[i]);
2745}
2746
aliguorid8f44602008-10-06 13:52:44 +00002747static int monitor_suspended;
2748
bellard7e2515e2004-08-01 21:52:19 +00002749static void monitor_handle_command1(void *opaque, const char *cmdline)
2750{
2751 monitor_handle_command(cmdline);
aliguorid8f44602008-10-06 13:52:44 +00002752 if (!monitor_suspended)
2753 monitor_start_input();
2754 else
2755 monitor_suspended = 2;
2756}
2757
2758void monitor_suspend(void)
2759{
2760 monitor_suspended = 1;
2761}
2762
2763void monitor_resume(void)
2764{
2765 if (monitor_suspended == 2)
2766 monitor_start_input();
2767 monitor_suspended = 0;
bellard7e2515e2004-08-01 21:52:19 +00002768}
2769
aliguori83ab7952008-08-19 14:44:22 +00002770static void monitor_start_input(void)
bellard7e2515e2004-08-01 21:52:19 +00002771{
2772 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
bellard9dc39cb2004-03-14 21:38:27 +00002773}
2774
ths86e94de2007-01-05 22:01:59 +00002775static void term_event(void *opaque, int event)
2776{
2777 if (event != CHR_EVENT_RESET)
2778 return;
2779
2780 if (!hide_banner)
2781 term_printf("QEMU %s monitor - type 'help' for more information\n",
2782 QEMU_VERSION);
2783 monitor_start_input();
2784}
2785
ths20d8a3e2007-02-18 17:04:49 +00002786static int is_first_init = 1;
2787
bellard81d09122004-07-14 17:21:37 +00002788void monitor_init(CharDriverState *hd, int show_banner)
bellard9dc39cb2004-03-14 21:38:27 +00002789{
ths20d8a3e2007-02-18 17:04:49 +00002790 int i;
2791
2792 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00002793 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2794 if (!key_timer)
2795 return;
ths20d8a3e2007-02-18 17:04:49 +00002796 for (i = 0; i < MAX_MON; i++) {
2797 monitor_hd[i] = NULL;
2798 }
2799 is_first_init = 0;
2800 }
2801 for (i = 0; i < MAX_MON; i++) {
2802 if (monitor_hd[i] == NULL) {
2803 monitor_hd[i] = hd;
2804 break;
2805 }
2806 }
2807
ths86e94de2007-01-05 22:01:59 +00002808 hide_banner = !show_banner;
2809
pbrooke5b0bc42007-01-27 23:46:43 +00002810 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
aliguori83ab7952008-08-19 14:44:22 +00002811
2812 readline_start("", 0, monitor_handle_command1, NULL);
bellard7e2515e2004-08-01 21:52:19 +00002813}
2814
2815/* XXX: use threads ? */
2816/* modal monitor readline */
2817static int monitor_readline_started;
2818static char *monitor_readline_buf;
2819static int monitor_readline_buf_size;
2820
2821static void monitor_readline_cb(void *opaque, const char *input)
2822{
2823 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2824 monitor_readline_started = 0;
2825}
2826
2827void monitor_readline(const char *prompt, int is_password,
2828 char *buf, int buf_size)
2829{
ths20d8a3e2007-02-18 17:04:49 +00002830 int i;
aliguoribc0129d2008-08-01 15:12:34 +00002831 int old_focus[MAX_MON];
ths20d8a3e2007-02-18 17:04:49 +00002832
bellard7e2515e2004-08-01 21:52:19 +00002833 if (is_password) {
aliguoribc0129d2008-08-01 15:12:34 +00002834 for (i = 0; i < MAX_MON; i++) {
2835 old_focus[i] = 0;
2836 if (monitor_hd[i]) {
2837 old_focus[i] = monitor_hd[i]->focus;
2838 monitor_hd[i]->focus = 0;
ths20d8a3e2007-02-18 17:04:49 +00002839 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
aliguoribc0129d2008-08-01 15:12:34 +00002840 }
2841 }
bellard7e2515e2004-08-01 21:52:19 +00002842 }
aliguoribc0129d2008-08-01 15:12:34 +00002843
bellard7e2515e2004-08-01 21:52:19 +00002844 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2845 monitor_readline_buf = buf;
2846 monitor_readline_buf_size = buf_size;
2847 monitor_readline_started = 1;
2848 while (monitor_readline_started) {
2849 main_loop_wait(10);
2850 }
aliguoribc0129d2008-08-01 15:12:34 +00002851 /* restore original focus */
2852 if (is_password) {
2853 for (i = 0; i < MAX_MON; i++)
2854 if (old_focus[i])
2855 monitor_hd[i]->focus = old_focus[i];
2856 }
bellard9dc39cb2004-03-14 21:38:27 +00002857}