blob: 01a177ade8da322e21ef94fdfb51eea88390b573 [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"
bellard81d09122004-07-14 17:21:37 +000037#include <dirent.h>
balrogc8256f92008-06-08 22:45:01 +000038#include "qemu-timer.h"
aliguori5bb79102008-10-13 03:12:02 +000039#include "migration.h"
ths6a5bd302007-12-03 17:05:38 +000040
bellard9dc39cb2004-03-14 21:38:27 +000041//#define DEBUG
bellard81d09122004-07-14 17:21:37 +000042//#define DEBUG_COMPLETION
bellard9dc39cb2004-03-14 21:38:27 +000043
bellard9307c4c2004-04-04 12:57:25 +000044/*
45 * Supported types:
ths5fafdf22007-09-16 21:08:06 +000046 *
bellard9307c4c2004-04-04 12:57:25 +000047 * 'F' filename
bellard81d09122004-07-14 17:21:37 +000048 * 'B' block device name
bellard9307c4c2004-04-04 12:57:25 +000049 * 's' string (accept optional quote)
bellard92a31b12005-02-10 22:00:52 +000050 * 'i' 32 bit integer
51 * 'l' target long (32 or 64 bit)
bellard9307c4c2004-04-04 12:57:25 +000052 * '/' optional gdb-like print format (like "/10x")
53 *
54 * '?' optional type (for 'F', 's' and 'i')
55 *
56 */
57
bellard9dc39cb2004-03-14 21:38:27 +000058typedef struct term_cmd_t {
59 const char *name;
bellard9307c4c2004-04-04 12:57:25 +000060 const char *args_type;
blueswir1a5f1b962008-08-17 20:21:51 +000061 void *handler;
bellard9dc39cb2004-03-14 21:38:27 +000062 const char *params;
63 const char *help;
64} term_cmd_t;
65
ths20d8a3e2007-02-18 17:04:49 +000066#define MAX_MON 4
67static CharDriverState *monitor_hd[MAX_MON];
ths86e94de2007-01-05 22:01:59 +000068static int hide_banner;
bellard7e2515e2004-08-01 21:52:19 +000069
blueswir18662d652008-10-02 18:32:44 +000070static const term_cmd_t term_cmds[];
71static const term_cmd_t info_cmds[];
bellard9dc39cb2004-03-14 21:38:27 +000072
ths60fe76f2007-12-16 03:02:09 +000073static uint8_t term_outbuf[1024];
bellard7e2515e2004-08-01 21:52:19 +000074static int term_outbuf_index;
bellard81d09122004-07-14 17:21:37 +000075
aliguori83ab7952008-08-19 14:44:22 +000076static void monitor_start_input(void);
77
blueswir1b1d8e522008-10-26 13:43:07 +000078static CPUState *mon_cpu = NULL;
bellard6a00d602005-11-21 23:25:50 +000079
bellard9dc39cb2004-03-14 21:38:27 +000080void term_flush(void)
81{
ths20d8a3e2007-02-18 17:04:49 +000082 int i;
bellard7e2515e2004-08-01 21:52:19 +000083 if (term_outbuf_index > 0) {
ths20d8a3e2007-02-18 17:04:49 +000084 for (i = 0; i < MAX_MON; i++)
85 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
86 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
bellard7e2515e2004-08-01 21:52:19 +000087 term_outbuf_index = 0;
88 }
89}
90
91/* flush at every end of line or if the buffer is full */
92void term_puts(const char *str)
93{
ths60fe76f2007-12-16 03:02:09 +000094 char c;
bellard7e2515e2004-08-01 21:52:19 +000095 for(;;) {
96 c = *str++;
97 if (c == '\0')
98 break;
bellard7ba12602006-07-14 20:26:42 +000099 if (c == '\n')
100 term_outbuf[term_outbuf_index++] = '\r';
bellard7e2515e2004-08-01 21:52:19 +0000101 term_outbuf[term_outbuf_index++] = c;
bellard7ba12602006-07-14 20:26:42 +0000102 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
bellard7e2515e2004-08-01 21:52:19 +0000103 c == '\n')
104 term_flush();
105 }
106}
107
108void term_vprintf(const char *fmt, va_list ap)
109{
110 char buf[4096];
111 vsnprintf(buf, sizeof(buf), fmt, ap);
112 term_puts(buf);
113}
114
115void term_printf(const char *fmt, ...)
116{
117 va_list ap;
118 va_start(ap, fmt);
119 term_vprintf(fmt, ap);
120 va_end(ap);
bellard9dc39cb2004-03-14 21:38:27 +0000121}
122
thsfef30742006-12-22 14:11:32 +0000123void term_print_filename(const char *filename)
124{
125 int i;
126
127 for (i = 0; filename[i]; i++) {
128 switch (filename[i]) {
129 case ' ':
130 case '"':
131 case '\\':
132 term_printf("\\%c", filename[i]);
133 break;
134 case '\t':
135 term_printf("\\t");
136 break;
137 case '\r':
138 term_printf("\\r");
139 break;
140 case '\n':
141 term_printf("\\n");
142 break;
143 default:
144 term_printf("%c", filename[i]);
145 break;
146 }
147 }
148}
149
bellard7fe48482004-10-09 18:08:01 +0000150static int monitor_fprintf(FILE *stream, const char *fmt, ...)
151{
152 va_list ap;
153 va_start(ap, fmt);
154 term_vprintf(fmt, ap);
155 va_end(ap);
156 return 0;
157}
158
bellard9dc39cb2004-03-14 21:38:27 +0000159static int compare_cmd(const char *name, const char *list)
160{
161 const char *p, *pstart;
162 int len;
163 len = strlen(name);
164 p = list;
165 for(;;) {
166 pstart = p;
167 p = strchr(p, '|');
168 if (!p)
169 p = pstart + strlen(pstart);
170 if ((p - pstart) == len && !memcmp(pstart, name, len))
171 return 1;
172 if (*p == '\0')
173 break;
174 p++;
175 }
176 return 0;
177}
178
blueswir18662d652008-10-02 18:32:44 +0000179static void help_cmd1(const term_cmd_t *cmds, const char *prefix, const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000180{
blueswir18662d652008-10-02 18:32:44 +0000181 const term_cmd_t *cmd;
bellard9dc39cb2004-03-14 21:38:27 +0000182
183 for(cmd = cmds; cmd->name != NULL; cmd++) {
184 if (!name || !strcmp(name, cmd->name))
185 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
186 }
187}
188
189static void help_cmd(const char *name)
190{
191 if (name && !strcmp(name, "info")) {
192 help_cmd1(info_cmds, "info ", NULL);
193 } else {
194 help_cmd1(term_cmds, "", name);
bellardf193c792004-03-21 17:06:25 +0000195 if (name && !strcmp(name, "log")) {
blueswir18662d652008-10-02 18:32:44 +0000196 const CPULogItem *item;
bellardf193c792004-03-21 17:06:25 +0000197 term_printf("Log items (comma separated):\n");
198 term_printf("%-10s %s\n", "none", "remove all logs");
199 for(item = cpu_log_items; item->mask != 0; item++) {
200 term_printf("%-10s %s\n", item->name, item->help);
201 }
202 }
bellard9dc39cb2004-03-14 21:38:27 +0000203 }
204}
205
bellard9307c4c2004-04-04 12:57:25 +0000206static void do_help(const char *name)
bellard9dc39cb2004-03-14 21:38:27 +0000207{
bellard9307c4c2004-04-04 12:57:25 +0000208 help_cmd(name);
bellard9dc39cb2004-03-14 21:38:27 +0000209}
210
bellard7954c732006-08-01 15:52:40 +0000211static void do_commit(const char *device)
bellard9dc39cb2004-03-14 21:38:27 +0000212{
bellard7954c732006-08-01 15:52:40 +0000213 int i, all_devices;
balrog2dc7b602007-05-24 18:53:22 +0000214
bellard7954c732006-08-01 15:52:40 +0000215 all_devices = !strcmp(device, "all");
thse4bcb142007-12-02 04:51:10 +0000216 for (i = 0; i < nb_drives; i++) {
ths5fafdf22007-09-16 21:08:06 +0000217 if (all_devices ||
thse4bcb142007-12-02 04:51:10 +0000218 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
219 bdrv_commit(drives_table[i].bdrv);
bellard9dc39cb2004-03-14 21:38:27 +0000220 }
221}
222
bellard9307c4c2004-04-04 12:57:25 +0000223static void do_info(const char *item)
bellard9dc39cb2004-03-14 21:38:27 +0000224{
blueswir18662d652008-10-02 18:32:44 +0000225 const term_cmd_t *cmd;
blueswir1a5f1b962008-08-17 20:21:51 +0000226 void (*handler)(void);
bellard9dc39cb2004-03-14 21:38:27 +0000227
bellard9307c4c2004-04-04 12:57:25 +0000228 if (!item)
bellard9dc39cb2004-03-14 21:38:27 +0000229 goto help;
bellard9dc39cb2004-03-14 21:38:27 +0000230 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +0000231 if (compare_cmd(item, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +0000232 goto found;
233 }
234 help:
bellard9307c4c2004-04-04 12:57:25 +0000235 help_cmd("info");
bellard9dc39cb2004-03-14 21:38:27 +0000236 return;
237 found:
blueswir1a5f1b962008-08-17 20:21:51 +0000238 handler = cmd->handler;
239 handler();
bellard9dc39cb2004-03-14 21:38:27 +0000240}
241
bellard9bc9d1c2004-10-10 15:15:51 +0000242static void do_info_version(void)
243{
244 term_printf("%s\n", QEMU_VERSION);
245}
246
thsc35734b2007-03-19 15:17:08 +0000247static void do_info_name(void)
248{
249 if (qemu_name)
250 term_printf("%s\n", qemu_name);
251}
252
blueswir1f1f23ad2008-09-18 18:30:20 +0000253static void do_info_uuid(void)
254{
255 term_printf(UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1], qemu_uuid[2],
256 qemu_uuid[3], qemu_uuid[4], qemu_uuid[5], qemu_uuid[6],
257 qemu_uuid[7], qemu_uuid[8], qemu_uuid[9], qemu_uuid[10],
258 qemu_uuid[11], qemu_uuid[12], qemu_uuid[13], qemu_uuid[14],
259 qemu_uuid[15]);
260}
261
bellard9307c4c2004-04-04 12:57:25 +0000262static void do_info_block(void)
bellard9dc39cb2004-03-14 21:38:27 +0000263{
264 bdrv_info();
265}
266
thsa36e69d2007-12-02 05:18:19 +0000267static void do_info_blockstats(void)
268{
269 bdrv_info_stats();
270}
271
bellard6a00d602005-11-21 23:25:50 +0000272/* get the current CPU defined by the user */
pbrook9596ebb2007-11-18 01:44:38 +0000273static int mon_set_cpu(int cpu_index)
bellard6a00d602005-11-21 23:25:50 +0000274{
275 CPUState *env;
276
277 for(env = first_cpu; env != NULL; env = env->next_cpu) {
278 if (env->cpu_index == cpu_index) {
279 mon_cpu = env;
280 return 0;
281 }
282 }
283 return -1;
284}
285
pbrook9596ebb2007-11-18 01:44:38 +0000286static CPUState *mon_get_cpu(void)
bellard6a00d602005-11-21 23:25:50 +0000287{
288 if (!mon_cpu) {
289 mon_set_cpu(0);
290 }
291 return mon_cpu;
292}
293
bellard9307c4c2004-04-04 12:57:25 +0000294static void do_info_registers(void)
295{
bellard6a00d602005-11-21 23:25:50 +0000296 CPUState *env;
297 env = mon_get_cpu();
298 if (!env)
299 return;
bellard9307c4c2004-04-04 12:57:25 +0000300#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000301 cpu_dump_state(env, NULL, monitor_fprintf,
bellardd24b15a2005-07-03 21:28:00 +0000302 X86_DUMP_FPU);
bellard9307c4c2004-04-04 12:57:25 +0000303#else
ths5fafdf22007-09-16 21:08:06 +0000304 cpu_dump_state(env, NULL, monitor_fprintf,
bellard7fe48482004-10-09 18:08:01 +0000305 0);
bellard9307c4c2004-04-04 12:57:25 +0000306#endif
307}
308
bellard6a00d602005-11-21 23:25:50 +0000309static void do_info_cpus(void)
310{
311 CPUState *env;
312
313 /* just to set the default cpu if not already done */
314 mon_get_cpu();
315
316 for(env = first_cpu; env != NULL; env = env->next_cpu) {
ths5fafdf22007-09-16 21:08:06 +0000317 term_printf("%c CPU #%d:",
bellard6a00d602005-11-21 23:25:50 +0000318 (env == mon_cpu) ? '*' : ' ',
319 env->cpu_index);
320#if defined(TARGET_I386)
321 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
bellarde80e1cc2005-11-23 22:05:28 +0000322#elif defined(TARGET_PPC)
323 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
bellardba3c64f2005-12-05 20:31:52 +0000324#elif defined(TARGET_SPARC)
325 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
thsead93602007-09-06 00:18:15 +0000326#elif defined(TARGET_MIPS)
thsb5dc7732008-06-27 10:02:35 +0000327 term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
bellardce5232c2008-05-28 17:14:10 +0000328#endif
thsead93602007-09-06 00:18:15 +0000329 if (env->halted)
330 term_printf(" (halted)");
bellard6a00d602005-11-21 23:25:50 +0000331 term_printf("\n");
332 }
333}
334
335static void do_cpu_set(int index)
336{
337 if (mon_set_cpu(index) < 0)
338 term_printf("Invalid CPU index\n");
339}
340
bellarde3db7222005-01-26 22:00:47 +0000341static void do_info_jit(void)
342{
343 dump_exec_info(NULL, monitor_fprintf);
344}
345
bellardaa455482004-04-04 13:07:25 +0000346static void do_info_history (void)
347{
348 int i;
bellard7e2515e2004-08-01 21:52:19 +0000349 const char *str;
ths3b46e622007-09-17 08:09:54 +0000350
bellard7e2515e2004-08-01 21:52:19 +0000351 i = 0;
352 for(;;) {
353 str = readline_get_history(i);
354 if (!str)
355 break;
356 term_printf("%d: '%s'\n", i, str);
bellard8e3a9fd2004-10-09 17:32:58 +0000357 i++;
bellardaa455482004-04-04 13:07:25 +0000358 }
359}
360
j_mayer76a66252007-03-07 08:32:30 +0000361#if defined(TARGET_PPC)
362/* XXX: not implemented in other targets */
363static void do_info_cpu_stats (void)
364{
365 CPUState *env;
366
367 env = mon_get_cpu();
368 cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
369}
370#endif
371
bellard9307c4c2004-04-04 12:57:25 +0000372static void do_quit(void)
bellard9dc39cb2004-03-14 21:38:27 +0000373{
374 exit(0);
375}
376
377static int eject_device(BlockDriverState *bs, int force)
378{
379 if (bdrv_is_inserted(bs)) {
380 if (!force) {
381 if (!bdrv_is_removable(bs)) {
382 term_printf("device is not removable\n");
383 return -1;
384 }
385 if (bdrv_is_locked(bs)) {
386 term_printf("device is locked\n");
387 return -1;
388 }
389 }
390 bdrv_close(bs);
391 }
392 return 0;
393}
394
bellard9307c4c2004-04-04 12:57:25 +0000395static void do_eject(int force, const char *filename)
bellard9dc39cb2004-03-14 21:38:27 +0000396{
397 BlockDriverState *bs;
bellard9dc39cb2004-03-14 21:38:27 +0000398
bellard9307c4c2004-04-04 12:57:25 +0000399 bs = bdrv_find(filename);
bellard9dc39cb2004-03-14 21:38:27 +0000400 if (!bs) {
401 term_printf("device not found\n");
402 return;
403 }
404 eject_device(bs, force);
405}
406
aurel322ecea9b2008-06-18 22:10:01 +0000407static void do_change_block(const char *device, const char *filename, const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +0000408{
409 BlockDriverState *bs;
aurel322ecea9b2008-06-18 22:10:01 +0000410 BlockDriver *drv = NULL;
bellard9dc39cb2004-03-14 21:38:27 +0000411
bellard9307c4c2004-04-04 12:57:25 +0000412 bs = bdrv_find(device);
bellard9dc39cb2004-03-14 21:38:27 +0000413 if (!bs) {
414 term_printf("device not found\n");
415 return;
416 }
aurel322ecea9b2008-06-18 22:10:01 +0000417 if (fmt) {
418 drv = bdrv_find_format(fmt);
419 if (!drv) {
420 term_printf("invalid format %s\n", fmt);
421 return;
422 }
423 }
bellard9dc39cb2004-03-14 21:38:27 +0000424 if (eject_device(bs, 0) < 0)
425 return;
aurel322ecea9b2008-06-18 22:10:01 +0000426 bdrv_open2(bs, filename, 0, drv);
balrog2bac6012007-04-30 01:34:31 +0000427 qemu_key_check(bs, filename);
bellard9dc39cb2004-03-14 21:38:27 +0000428}
429
thse25a5822007-08-25 01:36:20 +0000430static void do_change_vnc(const char *target)
431{
ths70848512007-08-25 01:37:05 +0000432 if (strcmp(target, "passwd") == 0 ||
433 strcmp(target, "password") == 0) {
434 char password[9];
435 monitor_readline("Password: ", 1, password, sizeof(password)-1);
436 password[sizeof(password)-1] = '\0';
437 if (vnc_display_password(NULL, password) < 0)
438 term_printf("could not set VNC server password\n");
439 } else {
440 if (vnc_display_open(NULL, target) < 0)
441 term_printf("could not start VNC server on %s\n", target);
442 }
thse25a5822007-08-25 01:36:20 +0000443}
444
aurel322ecea9b2008-06-18 22:10:01 +0000445static void do_change(const char *device, const char *target, const char *fmt)
thse25a5822007-08-25 01:36:20 +0000446{
447 if (strcmp(device, "vnc") == 0) {
448 do_change_vnc(target);
449 } else {
aurel322ecea9b2008-06-18 22:10:01 +0000450 do_change_block(device, target, fmt);
thse25a5822007-08-25 01:36:20 +0000451 }
452}
453
bellard9307c4c2004-04-04 12:57:25 +0000454static void do_screen_dump(const char *filename)
bellard59a983b2004-03-17 23:17:16 +0000455{
pbrook95219892006-04-09 01:06:34 +0000456 vga_hw_screen_dump(filename);
bellard59a983b2004-03-17 23:17:16 +0000457}
458
pbrooke735b912007-06-30 13:53:24 +0000459static void do_logfile(const char *filename)
460{
461 cpu_set_log_filename(filename);
462}
463
bellard9307c4c2004-04-04 12:57:25 +0000464static void do_log(const char *items)
bellardf193c792004-03-21 17:06:25 +0000465{
466 int mask;
ths3b46e622007-09-17 08:09:54 +0000467
bellard9307c4c2004-04-04 12:57:25 +0000468 if (!strcmp(items, "none")) {
bellardf193c792004-03-21 17:06:25 +0000469 mask = 0;
470 } else {
bellard9307c4c2004-04-04 12:57:25 +0000471 mask = cpu_str_to_log_mask(items);
bellardf193c792004-03-21 17:06:25 +0000472 if (!mask) {
bellard9307c4c2004-04-04 12:57:25 +0000473 help_cmd("log");
bellardf193c792004-03-21 17:06:25 +0000474 return;
475 }
476 }
477 cpu_set_log(mask);
478}
479
bellard9307c4c2004-04-04 12:57:25 +0000480static void do_stop(void)
bellard8a7ddc32004-03-31 19:00:16 +0000481{
482 vm_stop(EXCP_INTERRUPT);
483}
484
bellard9307c4c2004-04-04 12:57:25 +0000485static void do_cont(void)
bellard8a7ddc32004-03-31 19:00:16 +0000486{
487 vm_start();
488}
489
bellard67b915a2004-03-31 23:37:16 +0000490#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +0000491static void do_gdbserver(const char *port)
bellard8a7ddc32004-03-31 19:00:16 +0000492{
pbrookcfc34752007-02-22 01:48:01 +0000493 if (!port)
bellard9307c4c2004-04-04 12:57:25 +0000494 port = DEFAULT_GDBSTUB_PORT;
pbrookcfc34752007-02-22 01:48:01 +0000495 if (gdbserver_start(port) < 0) {
496 qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000497 } else {
pbrookcfc34752007-02-22 01:48:01 +0000498 qemu_printf("Waiting gdb connection on port '%s'\n", port);
bellard8a7ddc32004-03-31 19:00:16 +0000499 }
500}
bellard67b915a2004-03-31 23:37:16 +0000501#endif
bellard8a7ddc32004-03-31 19:00:16 +0000502
bellard9307c4c2004-04-04 12:57:25 +0000503static void term_printc(int c)
504{
505 term_printf("'");
506 switch(c) {
507 case '\'':
508 term_printf("\\'");
509 break;
510 case '\\':
511 term_printf("\\\\");
512 break;
513 case '\n':
514 term_printf("\\n");
515 break;
516 case '\r':
517 term_printf("\\r");
518 break;
519 default:
520 if (c >= 32 && c <= 126) {
521 term_printf("%c", c);
522 } else {
523 term_printf("\\x%02x", c);
524 }
525 break;
526 }
527 term_printf("'");
528}
529
ths5fafdf22007-09-16 21:08:06 +0000530static void memory_dump(int count, int format, int wsize,
blueswir17743e582007-09-24 18:39:04 +0000531 target_phys_addr_t addr, int is_physical)
bellard9307c4c2004-04-04 12:57:25 +0000532{
bellard6a00d602005-11-21 23:25:50 +0000533 CPUState *env;
bellard9307c4c2004-04-04 12:57:25 +0000534 int nb_per_line, l, line_size, i, max_digits, len;
535 uint8_t buf[16];
536 uint64_t v;
537
538 if (format == 'i') {
539 int flags;
540 flags = 0;
bellard6a00d602005-11-21 23:25:50 +0000541 env = mon_get_cpu();
542 if (!env && !is_physical)
543 return;
bellard9307c4c2004-04-04 12:57:25 +0000544#ifdef TARGET_I386
bellard4c27ba22004-04-25 18:05:08 +0000545 if (wsize == 2) {
bellard9307c4c2004-04-04 12:57:25 +0000546 flags = 1;
bellard4c27ba22004-04-25 18:05:08 +0000547 } else if (wsize == 4) {
548 flags = 0;
549 } else {
bellard6a15fd12006-04-12 21:07:07 +0000550 /* as default we use the current CS size */
bellard4c27ba22004-04-25 18:05:08 +0000551 flags = 0;
bellard6a15fd12006-04-12 21:07:07 +0000552 if (env) {
553#ifdef TARGET_X86_64
ths5fafdf22007-09-16 21:08:06 +0000554 if ((env->efer & MSR_EFER_LMA) &&
bellard6a15fd12006-04-12 21:07:07 +0000555 (env->segs[R_CS].flags & DESC_L_MASK))
556 flags = 2;
557 else
558#endif
559 if (!(env->segs[R_CS].flags & DESC_B_MASK))
560 flags = 1;
561 }
bellard4c27ba22004-04-25 18:05:08 +0000562 }
563#endif
bellard6a00d602005-11-21 23:25:50 +0000564 monitor_disas(env, addr, count, is_physical, flags);
bellard9307c4c2004-04-04 12:57:25 +0000565 return;
566 }
567
568 len = wsize * count;
569 if (wsize == 1)
570 line_size = 8;
571 else
572 line_size = 16;
573 nb_per_line = line_size / wsize;
574 max_digits = 0;
575
576 switch(format) {
577 case 'o':
578 max_digits = (wsize * 8 + 2) / 3;
579 break;
580 default:
581 case 'x':
582 max_digits = (wsize * 8) / 4;
583 break;
584 case 'u':
585 case 'd':
586 max_digits = (wsize * 8 * 10 + 32) / 33;
587 break;
588 case 'c':
589 wsize = 1;
590 break;
591 }
592
593 while (len > 0) {
blueswir17743e582007-09-24 18:39:04 +0000594 if (is_physical)
595 term_printf(TARGET_FMT_plx ":", addr);
596 else
597 term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
bellard9307c4c2004-04-04 12:57:25 +0000598 l = len;
599 if (l > line_size)
600 l = line_size;
601 if (is_physical) {
602 cpu_physical_memory_rw(addr, buf, l, 0);
603 } else {
bellard6a00d602005-11-21 23:25:50 +0000604 env = mon_get_cpu();
605 if (!env)
606 break;
aliguoric8f79b62008-08-18 14:00:20 +0000607 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
608 term_printf(" Cannot access memory\n");
609 break;
610 }
bellard9307c4c2004-04-04 12:57:25 +0000611 }
ths5fafdf22007-09-16 21:08:06 +0000612 i = 0;
bellard9307c4c2004-04-04 12:57:25 +0000613 while (i < l) {
614 switch(wsize) {
615 default:
616 case 1:
617 v = ldub_raw(buf + i);
618 break;
619 case 2:
620 v = lduw_raw(buf + i);
621 break;
622 case 4:
bellard92a31b12005-02-10 22:00:52 +0000623 v = (uint32_t)ldl_raw(buf + i);
bellard9307c4c2004-04-04 12:57:25 +0000624 break;
625 case 8:
626 v = ldq_raw(buf + i);
627 break;
628 }
629 term_printf(" ");
630 switch(format) {
631 case 'o':
bellard26a76462006-06-25 18:15:32 +0000632 term_printf("%#*" PRIo64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000633 break;
634 case 'x':
bellard26a76462006-06-25 18:15:32 +0000635 term_printf("0x%0*" PRIx64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000636 break;
637 case 'u':
bellard26a76462006-06-25 18:15:32 +0000638 term_printf("%*" PRIu64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000639 break;
640 case 'd':
bellard26a76462006-06-25 18:15:32 +0000641 term_printf("%*" PRId64, max_digits, v);
bellard9307c4c2004-04-04 12:57:25 +0000642 break;
643 case 'c':
644 term_printc(v);
645 break;
646 }
647 i += wsize;
648 }
649 term_printf("\n");
650 addr += l;
651 len -= l;
652 }
653}
654
bellard92a31b12005-02-10 22:00:52 +0000655#if TARGET_LONG_BITS == 64
656#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
657#else
658#define GET_TLONG(h, l) (l)
659#endif
660
ths5fafdf22007-09-16 21:08:06 +0000661static void do_memory_dump(int count, int format, int size,
bellard92a31b12005-02-10 22:00:52 +0000662 uint32_t addrh, uint32_t addrl)
bellard9307c4c2004-04-04 12:57:25 +0000663{
bellard92a31b12005-02-10 22:00:52 +0000664 target_long addr = GET_TLONG(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000665 memory_dump(count, format, size, addr, 0);
666}
667
blueswir17743e582007-09-24 18:39:04 +0000668#if TARGET_PHYS_ADDR_BITS > 32
669#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
670#else
671#define GET_TPHYSADDR(h, l) (l)
672#endif
673
bellard92a31b12005-02-10 22:00:52 +0000674static void do_physical_memory_dump(int count, int format, int size,
675 uint32_t addrh, uint32_t addrl)
676
bellard9307c4c2004-04-04 12:57:25 +0000677{
blueswir17743e582007-09-24 18:39:04 +0000678 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
bellard9307c4c2004-04-04 12:57:25 +0000679 memory_dump(count, format, size, addr, 1);
680}
681
bellard92a31b12005-02-10 22:00:52 +0000682static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
bellard9307c4c2004-04-04 12:57:25 +0000683{
blueswir17743e582007-09-24 18:39:04 +0000684 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
685#if TARGET_PHYS_ADDR_BITS == 32
bellard9307c4c2004-04-04 12:57:25 +0000686 switch(format) {
687 case 'o':
688 term_printf("%#o", val);
689 break;
690 case 'x':
691 term_printf("%#x", val);
692 break;
693 case 'u':
694 term_printf("%u", val);
695 break;
696 default:
697 case 'd':
698 term_printf("%d", val);
699 break;
700 case 'c':
701 term_printc(val);
702 break;
703 }
bellard92a31b12005-02-10 22:00:52 +0000704#else
705 switch(format) {
706 case 'o':
bellard26a76462006-06-25 18:15:32 +0000707 term_printf("%#" PRIo64, val);
bellard92a31b12005-02-10 22:00:52 +0000708 break;
709 case 'x':
bellard26a76462006-06-25 18:15:32 +0000710 term_printf("%#" PRIx64, val);
bellard92a31b12005-02-10 22:00:52 +0000711 break;
712 case 'u':
bellard26a76462006-06-25 18:15:32 +0000713 term_printf("%" PRIu64, val);
bellard92a31b12005-02-10 22:00:52 +0000714 break;
715 default:
716 case 'd':
bellard26a76462006-06-25 18:15:32 +0000717 term_printf("%" PRId64, val);
bellard92a31b12005-02-10 22:00:52 +0000718 break;
719 case 'c':
720 term_printc(val);
721 break;
722 }
723#endif
bellard9307c4c2004-04-04 12:57:25 +0000724 term_printf("\n");
725}
726
ths5fafdf22007-09-16 21:08:06 +0000727static void do_memory_save(unsigned int valh, unsigned int vall,
bellardb371dc52007-01-03 15:20:39 +0000728 uint32_t size, const char *filename)
729{
730 FILE *f;
731 target_long addr = GET_TLONG(valh, vall);
732 uint32_t l;
733 CPUState *env;
734 uint8_t buf[1024];
735
736 env = mon_get_cpu();
737 if (!env)
738 return;
739
740 f = fopen(filename, "wb");
741 if (!f) {
742 term_printf("could not open '%s'\n", filename);
743 return;
744 }
745 while (size != 0) {
746 l = sizeof(buf);
747 if (l > size)
748 l = size;
749 cpu_memory_rw_debug(env, addr, buf, l, 0);
750 fwrite(buf, 1, l, f);
751 addr += l;
752 size -= l;
753 }
754 fclose(f);
755}
756
aurel32a8bdf7a2008-04-11 21:36:14 +0000757static void do_physical_memory_save(unsigned int valh, unsigned int vall,
758 uint32_t size, const char *filename)
759{
760 FILE *f;
761 uint32_t l;
762 uint8_t buf[1024];
aurel32339dea22008-04-12 20:14:43 +0000763 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
aurel32a8bdf7a2008-04-11 21:36:14 +0000764
765 f = fopen(filename, "wb");
766 if (!f) {
767 term_printf("could not open '%s'\n", filename);
768 return;
769 }
770 while (size != 0) {
771 l = sizeof(buf);
772 if (l > size)
773 l = size;
774 cpu_physical_memory_rw(addr, buf, l, 0);
775 fwrite(buf, 1, l, f);
776 fflush(f);
777 addr += l;
778 size -= l;
779 }
780 fclose(f);
781}
782
bellarde4cf1ad2005-06-04 20:15:57 +0000783static void do_sum(uint32_t start, uint32_t size)
784{
785 uint32_t addr;
786 uint8_t buf[1];
787 uint16_t sum;
788
789 sum = 0;
790 for(addr = start; addr < (start + size); addr++) {
791 cpu_physical_memory_rw(addr, buf, 1, 0);
792 /* BSD sum algorithm ('sum' Unix command) */
793 sum = (sum >> 1) | (sum << 15);
794 sum += buf[0];
795 }
796 term_printf("%05d\n", sum);
797}
798
bellarda3a91a32004-06-04 11:06:21 +0000799typedef struct {
800 int keycode;
801 const char *name;
802} KeyDef;
803
804static const KeyDef key_defs[] = {
805 { 0x2a, "shift" },
806 { 0x36, "shift_r" },
ths3b46e622007-09-17 08:09:54 +0000807
bellarda3a91a32004-06-04 11:06:21 +0000808 { 0x38, "alt" },
809 { 0xb8, "alt_r" },
ths2ba27c72008-08-13 12:54:23 +0000810 { 0x64, "altgr" },
811 { 0xe4, "altgr_r" },
bellarda3a91a32004-06-04 11:06:21 +0000812 { 0x1d, "ctrl" },
813 { 0x9d, "ctrl_r" },
814
815 { 0xdd, "menu" },
816
817 { 0x01, "esc" },
818
819 { 0x02, "1" },
820 { 0x03, "2" },
821 { 0x04, "3" },
822 { 0x05, "4" },
823 { 0x06, "5" },
824 { 0x07, "6" },
825 { 0x08, "7" },
826 { 0x09, "8" },
827 { 0x0a, "9" },
828 { 0x0b, "0" },
bellard64866c32006-05-07 18:03:31 +0000829 { 0x0c, "minus" },
830 { 0x0d, "equal" },
bellarda3a91a32004-06-04 11:06:21 +0000831 { 0x0e, "backspace" },
832
833 { 0x0f, "tab" },
834 { 0x10, "q" },
835 { 0x11, "w" },
836 { 0x12, "e" },
837 { 0x13, "r" },
838 { 0x14, "t" },
839 { 0x15, "y" },
840 { 0x16, "u" },
841 { 0x17, "i" },
842 { 0x18, "o" },
843 { 0x19, "p" },
844
845 { 0x1c, "ret" },
846
847 { 0x1e, "a" },
848 { 0x1f, "s" },
849 { 0x20, "d" },
850 { 0x21, "f" },
851 { 0x22, "g" },
852 { 0x23, "h" },
853 { 0x24, "j" },
854 { 0x25, "k" },
855 { 0x26, "l" },
856
857 { 0x2c, "z" },
858 { 0x2d, "x" },
859 { 0x2e, "c" },
860 { 0x2f, "v" },
861 { 0x30, "b" },
862 { 0x31, "n" },
863 { 0x32, "m" },
aurel329155fc42008-10-01 21:46:15 +0000864 { 0x33, "comma" },
865 { 0x34, "dot" },
866 { 0x35, "slash" },
ths3b46e622007-09-17 08:09:54 +0000867
balrog4d3b6f62008-02-10 16:33:14 +0000868 { 0x37, "asterisk" },
869
bellarda3a91a32004-06-04 11:06:21 +0000870 { 0x39, "spc" },
bellard00ffa622004-06-04 13:25:15 +0000871 { 0x3a, "caps_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000872 { 0x3b, "f1" },
873 { 0x3c, "f2" },
874 { 0x3d, "f3" },
875 { 0x3e, "f4" },
876 { 0x3f, "f5" },
877 { 0x40, "f6" },
878 { 0x41, "f7" },
879 { 0x42, "f8" },
880 { 0x43, "f9" },
881 { 0x44, "f10" },
bellard00ffa622004-06-04 13:25:15 +0000882 { 0x45, "num_lock" },
bellarda3a91a32004-06-04 11:06:21 +0000883 { 0x46, "scroll_lock" },
884
bellard64866c32006-05-07 18:03:31 +0000885 { 0xb5, "kp_divide" },
886 { 0x37, "kp_multiply" },
ths0cfec832007-06-23 16:02:43 +0000887 { 0x4a, "kp_subtract" },
bellard64866c32006-05-07 18:03:31 +0000888 { 0x4e, "kp_add" },
889 { 0x9c, "kp_enter" },
890 { 0x53, "kp_decimal" },
balrogf2289cb2008-06-04 10:14:16 +0000891 { 0x54, "sysrq" },
bellard64866c32006-05-07 18:03:31 +0000892
893 { 0x52, "kp_0" },
894 { 0x4f, "kp_1" },
895 { 0x50, "kp_2" },
896 { 0x51, "kp_3" },
897 { 0x4b, "kp_4" },
898 { 0x4c, "kp_5" },
899 { 0x4d, "kp_6" },
900 { 0x47, "kp_7" },
901 { 0x48, "kp_8" },
902 { 0x49, "kp_9" },
ths3b46e622007-09-17 08:09:54 +0000903
bellarda3a91a32004-06-04 11:06:21 +0000904 { 0x56, "<" },
905
906 { 0x57, "f11" },
907 { 0x58, "f12" },
908
909 { 0xb7, "print" },
910
911 { 0xc7, "home" },
912 { 0xc9, "pgup" },
913 { 0xd1, "pgdn" },
914 { 0xcf, "end" },
915
916 { 0xcb, "left" },
917 { 0xc8, "up" },
918 { 0xd0, "down" },
919 { 0xcd, "right" },
920
921 { 0xd2, "insert" },
922 { 0xd3, "delete" },
blueswir1c0b5b102008-06-22 07:45:42 +0000923#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
924 { 0xf0, "stop" },
925 { 0xf1, "again" },
926 { 0xf2, "props" },
927 { 0xf3, "undo" },
928 { 0xf4, "front" },
929 { 0xf5, "copy" },
930 { 0xf6, "open" },
931 { 0xf7, "paste" },
932 { 0xf8, "find" },
933 { 0xf9, "cut" },
934 { 0xfa, "lf" },
935 { 0xfb, "help" },
936 { 0xfc, "meta_l" },
937 { 0xfd, "meta_r" },
938 { 0xfe, "compose" },
939#endif
bellarda3a91a32004-06-04 11:06:21 +0000940 { 0, NULL },
941};
942
943static int get_keycode(const char *key)
944{
945 const KeyDef *p;
bellard64866c32006-05-07 18:03:31 +0000946 char *endp;
947 int ret;
bellarda3a91a32004-06-04 11:06:21 +0000948
949 for(p = key_defs; p->name != NULL; p++) {
950 if (!strcmp(key, p->name))
951 return p->keycode;
952 }
bellard64866c32006-05-07 18:03:31 +0000953 if (strstart(key, "0x", NULL)) {
954 ret = strtoul(key, &endp, 0);
955 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
956 return ret;
957 }
bellarda3a91a32004-06-04 11:06:21 +0000958 return -1;
959}
960
balrogc8256f92008-06-08 22:45:01 +0000961#define MAX_KEYCODES 16
962static uint8_t keycodes[MAX_KEYCODES];
963static int nb_pending_keycodes;
964static QEMUTimer *key_timer;
965
966static void release_keys(void *opaque)
bellarda3a91a32004-06-04 11:06:21 +0000967{
balrogc8256f92008-06-08 22:45:01 +0000968 int keycode;
969
970 while (nb_pending_keycodes > 0) {
971 nb_pending_keycodes--;
972 keycode = keycodes[nb_pending_keycodes];
973 if (keycode & 0x80)
974 kbd_put_keycode(0xe0);
975 kbd_put_keycode(keycode | 0x80);
976 }
977}
978
979static void do_sendkey(const char *string, int has_hold_time, int hold_time)
980{
balrog3401c0d2008-06-04 10:05:59 +0000981 char keyname_buf[16];
982 char *separator;
983 int keyname_len, keycode, i;
ths3b46e622007-09-17 08:09:54 +0000984
balrogc8256f92008-06-08 22:45:01 +0000985 if (nb_pending_keycodes > 0) {
986 qemu_del_timer(key_timer);
987 release_keys(NULL);
988 }
989 if (!has_hold_time)
990 hold_time = 100;
991 i = 0;
balrog3401c0d2008-06-04 10:05:59 +0000992 while (1) {
993 separator = strchr(string, '-');
994 keyname_len = separator ? separator - string : strlen(string);
995 if (keyname_len > 0) {
996 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
997 if (keyname_len > sizeof(keyname_buf) - 1) {
998 term_printf("invalid key: '%s...'\n", keyname_buf);
999 return;
bellarda3a91a32004-06-04 11:06:21 +00001000 }
balrogc8256f92008-06-08 22:45:01 +00001001 if (i == MAX_KEYCODES) {
balrog3401c0d2008-06-04 10:05:59 +00001002 term_printf("too many keys\n");
1003 return;
1004 }
1005 keyname_buf[keyname_len] = 0;
1006 keycode = get_keycode(keyname_buf);
1007 if (keycode < 0) {
1008 term_printf("unknown key: '%s'\n", keyname_buf);
1009 return;
1010 }
balrogc8256f92008-06-08 22:45:01 +00001011 keycodes[i++] = keycode;
bellarda3a91a32004-06-04 11:06:21 +00001012 }
balrog3401c0d2008-06-04 10:05:59 +00001013 if (!separator)
bellarda3a91a32004-06-04 11:06:21 +00001014 break;
balrog3401c0d2008-06-04 10:05:59 +00001015 string = separator + 1;
bellarda3a91a32004-06-04 11:06:21 +00001016 }
balrogc8256f92008-06-08 22:45:01 +00001017 nb_pending_keycodes = i;
bellarda3a91a32004-06-04 11:06:21 +00001018 /* key down events */
balrogc8256f92008-06-08 22:45:01 +00001019 for (i = 0; i < nb_pending_keycodes; i++) {
bellarda3a91a32004-06-04 11:06:21 +00001020 keycode = keycodes[i];
1021 if (keycode & 0x80)
1022 kbd_put_keycode(0xe0);
1023 kbd_put_keycode(keycode & 0x7f);
1024 }
balrogc8256f92008-06-08 22:45:01 +00001025 /* delayed key up events */
balrogf227f172008-06-09 00:03:47 +00001026 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1027 muldiv64(ticks_per_sec, hold_time, 1000));
bellarda3a91a32004-06-04 11:06:21 +00001028}
1029
bellard13224a82006-07-14 22:03:35 +00001030static int mouse_button_state;
1031
ths5fafdf22007-09-16 21:08:06 +00001032static void do_mouse_move(const char *dx_str, const char *dy_str,
bellard13224a82006-07-14 22:03:35 +00001033 const char *dz_str)
1034{
1035 int dx, dy, dz;
1036 dx = strtol(dx_str, NULL, 0);
1037 dy = strtol(dy_str, NULL, 0);
1038 dz = 0;
ths5fafdf22007-09-16 21:08:06 +00001039 if (dz_str)
bellard13224a82006-07-14 22:03:35 +00001040 dz = strtol(dz_str, NULL, 0);
1041 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1042}
1043
1044static void do_mouse_button(int button_state)
1045{
1046 mouse_button_state = button_state;
1047 kbd_mouse_event(0, 0, 0, mouse_button_state);
1048}
1049
bellard34405572004-06-08 00:55:58 +00001050static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1051{
1052 uint32_t val;
1053 int suffix;
1054
1055 if (has_index) {
1056 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1057 addr++;
1058 }
1059 addr &= 0xffff;
1060
1061 switch(size) {
1062 default:
1063 case 1:
1064 val = cpu_inb(NULL, addr);
1065 suffix = 'b';
1066 break;
1067 case 2:
1068 val = cpu_inw(NULL, addr);
1069 suffix = 'w';
1070 break;
1071 case 4:
1072 val = cpu_inl(NULL, addr);
1073 suffix = 'l';
1074 break;
1075 }
1076 term_printf("port%c[0x%04x] = %#0*x\n",
1077 suffix, addr, size * 2, val);
1078}
bellarda3a91a32004-06-04 11:06:21 +00001079
blueswir13b4366d2008-06-20 16:25:06 +00001080/* boot_set handler */
1081static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1082static void *boot_opaque;
1083
1084void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1085{
1086 qemu_boot_set_handler = func;
1087 boot_opaque = opaque;
1088}
1089
aurel320ecdffb2008-05-04 20:11:34 +00001090static void do_boot_set(const char *bootdevice)
1091{
1092 int res;
1093
1094 if (qemu_boot_set_handler) {
blueswir13b4366d2008-06-20 16:25:06 +00001095 res = qemu_boot_set_handler(boot_opaque, bootdevice);
aurel320ecdffb2008-05-04 20:11:34 +00001096 if (res == 0)
1097 term_printf("boot device list now set to %s\n", bootdevice);
1098 else
1099 term_printf("setting boot device list failed with error %i\n", res);
1100 } else {
1101 term_printf("no function defined to set boot device list for this architecture\n");
1102 }
1103}
1104
bellarde4f90822004-06-20 12:35:44 +00001105static void do_system_reset(void)
1106{
1107 qemu_system_reset_request();
1108}
1109
bellard34751872005-07-02 14:31:34 +00001110static void do_system_powerdown(void)
1111{
1112 qemu_system_powerdown_request();
1113}
1114
bellardb86bda52004-09-18 19:32:46 +00001115#if defined(TARGET_I386)
1116static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1117{
ths5fafdf22007-09-16 21:08:06 +00001118 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
bellardb86bda52004-09-18 19:32:46 +00001119 addr,
1120 pte & mask,
1121 pte & PG_GLOBAL_MASK ? 'G' : '-',
1122 pte & PG_PSE_MASK ? 'P' : '-',
1123 pte & PG_DIRTY_MASK ? 'D' : '-',
1124 pte & PG_ACCESSED_MASK ? 'A' : '-',
1125 pte & PG_PCD_MASK ? 'C' : '-',
1126 pte & PG_PWT_MASK ? 'T' : '-',
1127 pte & PG_USER_MASK ? 'U' : '-',
1128 pte & PG_RW_MASK ? 'W' : '-');
1129}
1130
1131static void tlb_info(void)
1132{
bellard6a00d602005-11-21 23:25:50 +00001133 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001134 int l1, l2;
1135 uint32_t pgd, pde, pte;
1136
bellard6a00d602005-11-21 23:25:50 +00001137 env = mon_get_cpu();
1138 if (!env)
1139 return;
1140
bellardb86bda52004-09-18 19:32:46 +00001141 if (!(env->cr[0] & CR0_PG_MASK)) {
1142 term_printf("PG disabled\n");
1143 return;
1144 }
1145 pgd = env->cr[3] & ~0xfff;
1146 for(l1 = 0; l1 < 1024; l1++) {
1147 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1148 pde = le32_to_cpu(pde);
1149 if (pde & PG_PRESENT_MASK) {
1150 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1151 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1152 } else {
1153 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001154 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001155 (uint8_t *)&pte, 4);
1156 pte = le32_to_cpu(pte);
1157 if (pte & PG_PRESENT_MASK) {
ths5fafdf22007-09-16 21:08:06 +00001158 print_pte((l1 << 22) + (l2 << 12),
1159 pte & ~PG_PSE_MASK,
bellardb86bda52004-09-18 19:32:46 +00001160 ~0xfff);
1161 }
1162 }
1163 }
1164 }
1165 }
1166}
1167
ths5fafdf22007-09-16 21:08:06 +00001168static void mem_print(uint32_t *pstart, int *plast_prot,
bellardb86bda52004-09-18 19:32:46 +00001169 uint32_t end, int prot)
1170{
bellard9746b152004-11-11 18:30:24 +00001171 int prot1;
1172 prot1 = *plast_prot;
1173 if (prot != prot1) {
bellardb86bda52004-09-18 19:32:46 +00001174 if (*pstart != -1) {
1175 term_printf("%08x-%08x %08x %c%c%c\n",
ths5fafdf22007-09-16 21:08:06 +00001176 *pstart, end, end - *pstart,
bellard9746b152004-11-11 18:30:24 +00001177 prot1 & PG_USER_MASK ? 'u' : '-',
bellardb86bda52004-09-18 19:32:46 +00001178 'r',
bellard9746b152004-11-11 18:30:24 +00001179 prot1 & PG_RW_MASK ? 'w' : '-');
bellardb86bda52004-09-18 19:32:46 +00001180 }
1181 if (prot != 0)
1182 *pstart = end;
1183 else
1184 *pstart = -1;
1185 *plast_prot = prot;
1186 }
1187}
1188
1189static void mem_info(void)
1190{
bellard6a00d602005-11-21 23:25:50 +00001191 CPUState *env;
bellardb86bda52004-09-18 19:32:46 +00001192 int l1, l2, prot, last_prot;
1193 uint32_t pgd, pde, pte, start, end;
1194
bellard6a00d602005-11-21 23:25:50 +00001195 env = mon_get_cpu();
1196 if (!env)
1197 return;
1198
bellardb86bda52004-09-18 19:32:46 +00001199 if (!(env->cr[0] & CR0_PG_MASK)) {
1200 term_printf("PG disabled\n");
1201 return;
1202 }
1203 pgd = env->cr[3] & ~0xfff;
1204 last_prot = 0;
1205 start = -1;
1206 for(l1 = 0; l1 < 1024; l1++) {
1207 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1208 pde = le32_to_cpu(pde);
1209 end = l1 << 22;
1210 if (pde & PG_PRESENT_MASK) {
1211 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1212 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1213 mem_print(&start, &last_prot, end, prot);
1214 } else {
1215 for(l2 = 0; l2 < 1024; l2++) {
ths5fafdf22007-09-16 21:08:06 +00001216 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
bellardb86bda52004-09-18 19:32:46 +00001217 (uint8_t *)&pte, 4);
1218 pte = le32_to_cpu(pte);
1219 end = (l1 << 22) + (l2 << 12);
1220 if (pte & PG_PRESENT_MASK) {
1221 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1222 } else {
1223 prot = 0;
1224 }
1225 mem_print(&start, &last_prot, end, prot);
1226 }
1227 }
1228 } else {
1229 prot = 0;
1230 mem_print(&start, &last_prot, end, prot);
1231 }
1232 }
1233}
1234#endif
1235
bellard0f4c6412005-07-24 18:10:56 +00001236static void do_info_kqemu(void)
1237{
1238#ifdef USE_KQEMU
bellard6a00d602005-11-21 23:25:50 +00001239 CPUState *env;
bellard0f4c6412005-07-24 18:10:56 +00001240 int val;
1241 val = 0;
bellard6a00d602005-11-21 23:25:50 +00001242 env = mon_get_cpu();
1243 if (!env) {
1244 term_printf("No cpu initialized yet");
1245 return;
1246 }
1247 val = env->kqemu_enabled;
bellard5f1ce942006-02-08 22:40:15 +00001248 term_printf("kqemu support: ");
1249 switch(val) {
1250 default:
1251 case 0:
1252 term_printf("disabled\n");
1253 break;
1254 case 1:
1255 term_printf("enabled for user code\n");
1256 break;
1257 case 2:
1258 term_printf("enabled for user and kernel code\n");
1259 break;
1260 }
bellard0f4c6412005-07-24 18:10:56 +00001261#else
bellard5f1ce942006-02-08 22:40:15 +00001262 term_printf("kqemu support: not compiled\n");
bellard0f4c6412005-07-24 18:10:56 +00001263#endif
ths5fafdf22007-09-16 21:08:06 +00001264}
bellard0f4c6412005-07-24 18:10:56 +00001265
bellard5f1ce942006-02-08 22:40:15 +00001266#ifdef CONFIG_PROFILER
1267
1268int64_t kqemu_time;
1269int64_t qemu_time;
1270int64_t kqemu_exec_count;
1271int64_t dev_time;
1272int64_t kqemu_ret_int_count;
1273int64_t kqemu_ret_excp_count;
1274int64_t kqemu_ret_intr_count;
1275
1276static void do_info_profile(void)
1277{
1278 int64_t total;
1279 total = qemu_time;
1280 if (total == 0)
1281 total = 1;
bellard26a76462006-06-25 18:15:32 +00001282 term_printf("async time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001283 dev_time, dev_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001284 term_printf("qemu time %" PRId64 " (%0.3f)\n",
bellard5f1ce942006-02-08 22:40:15 +00001285 qemu_time, qemu_time / (double)ticks_per_sec);
bellard26a76462006-06-25 18:15:32 +00001286 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
bellard5f1ce942006-02-08 22:40:15 +00001287 kqemu_time, kqemu_time / (double)ticks_per_sec,
1288 kqemu_time / (double)total * 100.0,
1289 kqemu_exec_count,
1290 kqemu_ret_int_count,
1291 kqemu_ret_excp_count,
1292 kqemu_ret_intr_count);
1293 qemu_time = 0;
1294 kqemu_time = 0;
1295 kqemu_exec_count = 0;
1296 dev_time = 0;
1297 kqemu_ret_int_count = 0;
1298 kqemu_ret_excp_count = 0;
1299 kqemu_ret_intr_count = 0;
1300#ifdef USE_KQEMU
1301 kqemu_record_dump();
1302#endif
1303}
1304#else
1305static void do_info_profile(void)
1306{
1307 term_printf("Internal profiler not compiled\n");
1308}
1309#endif
1310
bellardec36b692006-07-16 18:57:03 +00001311/* Capture support */
1312static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1313
1314static void do_info_capture (void)
1315{
1316 int i;
1317 CaptureState *s;
1318
1319 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1320 term_printf ("[%d]: ", i);
1321 s->ops.info (s->opaque);
1322 }
1323}
1324
1325static void do_stop_capture (int n)
1326{
1327 int i;
1328 CaptureState *s;
1329
1330 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1331 if (i == n) {
1332 s->ops.destroy (s->opaque);
1333 LIST_REMOVE (s, entries);
1334 qemu_free (s);
1335 return;
1336 }
1337 }
1338}
1339
1340#ifdef HAS_AUDIO
bellardec36b692006-07-16 18:57:03 +00001341static void do_wav_capture (const char *path,
1342 int has_freq, int freq,
1343 int has_bits, int bits,
1344 int has_channels, int nchannels)
1345{
1346 CaptureState *s;
1347
1348 s = qemu_mallocz (sizeof (*s));
1349 if (!s) {
1350 term_printf ("Not enough memory to add wave capture\n");
1351 return;
1352 }
1353
1354 freq = has_freq ? freq : 44100;
1355 bits = has_bits ? bits : 16;
1356 nchannels = has_channels ? nchannels : 2;
1357
1358 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1359 term_printf ("Faied to add wave capture\n");
1360 qemu_free (s);
1361 }
1362 LIST_INSERT_HEAD (&capture_head, s, entries);
1363}
1364#endif
1365
aurel32dc1c0b72008-04-27 23:52:12 +00001366#if defined(TARGET_I386)
1367static void do_inject_nmi(int cpu_index)
1368{
1369 CPUState *env;
1370
1371 for (env = first_cpu; env != NULL; env = env->next_cpu)
1372 if (env->cpu_index == cpu_index) {
1373 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1374 break;
1375 }
1376}
1377#endif
1378
blueswir18662d652008-10-02 18:32:44 +00001379static const term_cmd_t term_cmds[] = {
ths5fafdf22007-09-16 21:08:06 +00001380 { "help|?", "s?", do_help,
bellard9dc39cb2004-03-14 21:38:27 +00001381 "[cmd]", "show the help" },
ths5fafdf22007-09-16 21:08:06 +00001382 { "commit", "s", do_commit,
bellard7954c732006-08-01 15:52:40 +00001383 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
bellard9307c4c2004-04-04 12:57:25 +00001384 { "info", "s?", do_info,
bellard9dc39cb2004-03-14 21:38:27 +00001385 "subcommand", "show various information about the system state" },
bellard9307c4c2004-04-04 12:57:25 +00001386 { "q|quit", "", do_quit,
bellard9dc39cb2004-03-14 21:38:27 +00001387 "", "quit the emulator" },
bellard81d09122004-07-14 17:21:37 +00001388 { "eject", "-fB", do_eject,
thse5987522007-03-30 18:58:01 +00001389 "[-f] device", "eject a removable medium (use -f to force it)" },
aurel322ecea9b2008-06-18 22:10:01 +00001390 { "change", "BFs?", do_change,
1391 "device filename [format]", "change a removable medium, optional format" },
ths5fafdf22007-09-16 21:08:06 +00001392 { "screendump", "F", do_screen_dump,
bellard59a983b2004-03-17 23:17:16 +00001393 "filename", "save screen into PPM image 'filename'" },
balrog788228c2008-05-24 23:15:46 +00001394 { "logfile", "F", do_logfile,
pbrooke735b912007-06-30 13:53:24 +00001395 "filename", "output logs to 'filename'" },
bellard9307c4c2004-04-04 12:57:25 +00001396 { "log", "s", do_log,
ths5fafdf22007-09-16 21:08:06 +00001397 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
bellardfaea38e2006-08-05 21:31:00 +00001398 { "savevm", "s?", do_savevm,
ths5fafdf22007-09-16 21:08:06 +00001399 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
bellardfaea38e2006-08-05 21:31:00 +00001400 { "loadvm", "s", do_loadvm,
ths5fafdf22007-09-16 21:08:06 +00001401 "tag|id", "restore a VM snapshot from its tag or id" },
bellardfaea38e2006-08-05 21:31:00 +00001402 { "delvm", "s", do_delvm,
ths5fafdf22007-09-16 21:08:06 +00001403 "tag|id", "delete a VM snapshot from its tag or id" },
1404 { "stop", "", do_stop,
bellard9307c4c2004-04-04 12:57:25 +00001405 "", "stop emulation", },
ths5fafdf22007-09-16 21:08:06 +00001406 { "c|cont", "", do_cont,
bellard9307c4c2004-04-04 12:57:25 +00001407 "", "resume emulation", },
bellard67b915a2004-03-31 23:37:16 +00001408#ifdef CONFIG_GDBSTUB
ths5fafdf22007-09-16 21:08:06 +00001409 { "gdbserver", "s?", do_gdbserver,
bellard9307c4c2004-04-04 12:57:25 +00001410 "[port]", "start gdbserver session (default port=1234)", },
bellard67b915a2004-03-31 23:37:16 +00001411#endif
ths5fafdf22007-09-16 21:08:06 +00001412 { "x", "/l", do_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001413 "/fmt addr", "virtual memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001414 { "xp", "/l", do_physical_memory_dump,
bellard9307c4c2004-04-04 12:57:25 +00001415 "/fmt addr", "physical memory dump starting at 'addr'", },
ths5fafdf22007-09-16 21:08:06 +00001416 { "p|print", "/l", do_print,
bellard9307c4c2004-04-04 12:57:25 +00001417 "/fmt expr", "print expression value (use $reg for CPU register access)", },
ths5fafdf22007-09-16 21:08:06 +00001418 { "i", "/ii.", do_ioport_read,
bellard34405572004-06-08 00:55:58 +00001419 "/fmt addr", "I/O port read" },
1420
balrogc8256f92008-06-08 22:45:01 +00001421 { "sendkey", "si?", do_sendkey,
1422 "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 +00001423 { "system_reset", "", do_system_reset,
bellarde4f90822004-06-20 12:35:44 +00001424 "", "reset the system" },
ths5fafdf22007-09-16 21:08:06 +00001425 { "system_powerdown", "", do_system_powerdown,
bellard34751872005-07-02 14:31:34 +00001426 "", "send system power down event" },
ths5fafdf22007-09-16 21:08:06 +00001427 { "sum", "ii", do_sum,
bellarde4cf1ad2005-06-04 20:15:57 +00001428 "addr size", "compute the checksum of a memory region" },
bellarda594cfb2005-11-06 16:13:29 +00001429 { "usb_add", "s", do_usb_add,
1430 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1431 { "usb_del", "s", do_usb_del,
1432 "device", "remove USB device 'bus.addr'" },
ths5fafdf22007-09-16 21:08:06 +00001433 { "cpu", "i", do_cpu_set,
bellard6a00d602005-11-21 23:25:50 +00001434 "index", "set the default CPU" },
ths5fafdf22007-09-16 21:08:06 +00001435 { "mouse_move", "sss?", do_mouse_move,
bellard13224a82006-07-14 22:03:35 +00001436 "dx dy [dz]", "send mouse move events" },
ths5fafdf22007-09-16 21:08:06 +00001437 { "mouse_button", "i", do_mouse_button,
bellard13224a82006-07-14 22:03:35 +00001438 "state", "change mouse button state (1=L, 2=M, 4=R)" },
ths455204e2007-01-05 16:42:13 +00001439 { "mouse_set", "i", do_mouse_set,
1440 "index", "set which mouse device receives events" },
bellardec36b692006-07-16 18:57:03 +00001441#ifdef HAS_AUDIO
1442 { "wavcapture", "si?i?i?", do_wav_capture,
1443 "path [frequency bits channels]",
1444 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1445#endif
1446 { "stopcapture", "i", do_stop_capture,
1447 "capture index", "stop capture" },
ths5fafdf22007-09-16 21:08:06 +00001448 { "memsave", "lis", do_memory_save,
bellardb371dc52007-01-03 15:20:39 +00001449 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
aurel32a8bdf7a2008-04-11 21:36:14 +00001450 { "pmemsave", "lis", do_physical_memory_save,
1451 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
aurel320ecdffb2008-05-04 20:11:34 +00001452 { "boot_set", "s", do_boot_set,
1453 "bootdevice", "define new values for the boot device list" },
aurel32dc1c0b72008-04-27 23:52:12 +00001454#if defined(TARGET_I386)
1455 { "nmi", "i", do_inject_nmi,
1456 "cpu", "inject an NMI on the given CPU", },
1457#endif
aliguori5bb79102008-10-13 03:12:02 +00001458 { "migrate", "-ds", do_migrate,
1459 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1460 { "migrate_cancel", "", do_migrate_cancel,
1461 "", "cancel the current VM migration" },
1462 { "migrate_set_speed", "s", do_migrate_set_speed,
1463 "value", "set maximum speed (in bytes) for migrations" },
ths5fafdf22007-09-16 21:08:06 +00001464 { NULL, NULL, },
bellard9dc39cb2004-03-14 21:38:27 +00001465};
1466
blueswir18662d652008-10-02 18:32:44 +00001467static const term_cmd_t info_cmds[] = {
bellard9bc9d1c2004-10-10 15:15:51 +00001468 { "version", "", do_info_version,
1469 "", "show the version of qemu" },
bellard9307c4c2004-04-04 12:57:25 +00001470 { "network", "", do_info_network,
bellard9dc39cb2004-03-14 21:38:27 +00001471 "", "show the network state" },
bellard9307c4c2004-04-04 12:57:25 +00001472 { "block", "", do_info_block,
bellard9dc39cb2004-03-14 21:38:27 +00001473 "", "show the block devices" },
thsa36e69d2007-12-02 05:18:19 +00001474 { "blockstats", "", do_info_blockstats,
1475 "", "show block device statistics" },
bellard9307c4c2004-04-04 12:57:25 +00001476 { "registers", "", do_info_registers,
1477 "", "show the cpu registers" },
bellard6a00d602005-11-21 23:25:50 +00001478 { "cpus", "", do_info_cpus,
1479 "", "show infos for each CPU" },
bellardaa455482004-04-04 13:07:25 +00001480 { "history", "", do_info_history,
1481 "", "show the command line history", },
bellard4a0fb71e2004-05-21 11:39:07 +00001482 { "irq", "", irq_info,
1483 "", "show the interrupts statistics (if available)", },
bellard4c27ba22004-04-25 18:05:08 +00001484 { "pic", "", pic_info,
1485 "", "show i8259 (PIC) state", },
bellard86e0c042004-05-20 12:49:21 +00001486 { "pci", "", pci_info,
1487 "", "show PCI info", },
bellardb86bda52004-09-18 19:32:46 +00001488#if defined(TARGET_I386)
1489 { "tlb", "", tlb_info,
1490 "", "show virtual to physical memory mappings", },
1491 { "mem", "", mem_info,
1492 "", "show the active virtual memory mappings", },
1493#endif
bellarde3db7222005-01-26 22:00:47 +00001494 { "jit", "", do_info_jit,
1495 "", "show dynamic compiler info", },
bellard0f4c6412005-07-24 18:10:56 +00001496 { "kqemu", "", do_info_kqemu,
1497 "", "show kqemu information", },
bellarda594cfb2005-11-06 16:13:29 +00001498 { "usb", "", usb_info,
1499 "", "show guest USB devices", },
1500 { "usbhost", "", usb_host_info,
1501 "", "show host USB devices", },
bellard5f1ce942006-02-08 22:40:15 +00001502 { "profile", "", do_info_profile,
1503 "", "show profiling information", },
bellardec36b692006-07-16 18:57:03 +00001504 { "capture", "", do_info_capture,
bellard17100152006-09-25 21:33:49 +00001505 "", "show capture information" },
bellardfaea38e2006-08-05 21:31:00 +00001506 { "snapshots", "", do_info_snapshots,
bellard17100152006-09-25 21:33:49 +00001507 "", "show the currently saved VM snapshots" },
balrog201a51f2007-04-30 00:51:09 +00001508 { "pcmcia", "", pcmcia_info,
1509 "", "show guest PCMCIA status" },
ths455204e2007-01-05 16:42:13 +00001510 { "mice", "", do_info_mice,
1511 "", "show which guest mouse is receiving events" },
bellarda9ce8592007-02-05 20:20:30 +00001512 { "vnc", "", do_info_vnc,
1513 "", "show the vnc server status"},
thsc35734b2007-03-19 15:17:08 +00001514 { "name", "", do_info_name,
1515 "", "show the current VM name" },
blueswir1f1f23ad2008-09-18 18:30:20 +00001516 { "uuid", "", do_info_uuid,
1517 "", "show the current VM UUID" },
j_mayer76a66252007-03-07 08:32:30 +00001518#if defined(TARGET_PPC)
1519 { "cpustats", "", do_info_cpu_stats,
1520 "", "show CPU statistics", },
1521#endif
blueswir131a60e22007-10-26 18:42:59 +00001522#if defined(CONFIG_SLIRP)
1523 { "slirp", "", do_info_slirp,
1524 "", "show SLIRP statistics", },
1525#endif
aliguori5bb79102008-10-13 03:12:02 +00001526 { "migrate", "", do_info_migrate, "", "show migration status" },
bellard9dc39cb2004-03-14 21:38:27 +00001527 { NULL, NULL, },
1528};
1529
bellard9307c4c2004-04-04 12:57:25 +00001530/*******************************************************************/
1531
1532static const char *pch;
1533static jmp_buf expr_env;
1534
bellard92a31b12005-02-10 22:00:52 +00001535#define MD_TLONG 0
1536#define MD_I32 1
1537
bellard9307c4c2004-04-04 12:57:25 +00001538typedef struct MonitorDef {
1539 const char *name;
1540 int offset;
blueswir18662d652008-10-02 18:32:44 +00001541 target_long (*get_value)(const struct MonitorDef *md, int val);
bellard92a31b12005-02-10 22:00:52 +00001542 int type;
bellard9307c4c2004-04-04 12:57:25 +00001543} MonitorDef;
1544
bellard57206fd2004-04-25 18:54:52 +00001545#if defined(TARGET_I386)
blueswir18662d652008-10-02 18:32:44 +00001546static target_long monitor_get_pc (const struct MonitorDef *md, int val)
bellard57206fd2004-04-25 18:54:52 +00001547{
bellard6a00d602005-11-21 23:25:50 +00001548 CPUState *env = mon_get_cpu();
1549 if (!env)
1550 return 0;
1551 return env->eip + env->segs[R_CS].base;
bellard57206fd2004-04-25 18:54:52 +00001552}
1553#endif
1554
bellarda541f292004-04-12 20:39:29 +00001555#if defined(TARGET_PPC)
blueswir18662d652008-10-02 18:32:44 +00001556static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001557{
bellard6a00d602005-11-21 23:25:50 +00001558 CPUState *env = mon_get_cpu();
bellarda541f292004-04-12 20:39:29 +00001559 unsigned int u;
1560 int i;
1561
bellard6a00d602005-11-21 23:25:50 +00001562 if (!env)
1563 return 0;
1564
bellarda541f292004-04-12 20:39:29 +00001565 u = 0;
1566 for (i = 0; i < 8; i++)
bellard6a00d602005-11-21 23:25:50 +00001567 u |= env->crf[i] << (32 - (4 * i));
bellarda541f292004-04-12 20:39:29 +00001568
1569 return u;
1570}
1571
blueswir18662d652008-10-02 18:32:44 +00001572static target_long monitor_get_msr (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001573{
bellard6a00d602005-11-21 23:25:50 +00001574 CPUState *env = mon_get_cpu();
1575 if (!env)
1576 return 0;
j_mayer0411a972007-10-25 21:35:50 +00001577 return env->msr;
bellarda541f292004-04-12 20:39:29 +00001578}
1579
blueswir18662d652008-10-02 18:32:44 +00001580static target_long monitor_get_xer (const struct MonitorDef *md, int val)
bellarda541f292004-04-12 20:39:29 +00001581{
bellard6a00d602005-11-21 23:25:50 +00001582 CPUState *env = mon_get_cpu();
1583 if (!env)
1584 return 0;
aurel323d7b4172008-10-21 11:28:46 +00001585 return env->xer;
bellarda541f292004-04-12 20:39:29 +00001586}
bellard9fddaa02004-05-21 12:59:32 +00001587
blueswir18662d652008-10-02 18:32:44 +00001588static target_long monitor_get_decr (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001589{
bellard6a00d602005-11-21 23:25:50 +00001590 CPUState *env = mon_get_cpu();
1591 if (!env)
1592 return 0;
1593 return cpu_ppc_load_decr(env);
bellard9fddaa02004-05-21 12:59:32 +00001594}
1595
blueswir18662d652008-10-02 18:32:44 +00001596static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001597{
bellard6a00d602005-11-21 23:25:50 +00001598 CPUState *env = mon_get_cpu();
1599 if (!env)
1600 return 0;
1601 return cpu_ppc_load_tbu(env);
bellard9fddaa02004-05-21 12:59:32 +00001602}
1603
blueswir18662d652008-10-02 18:32:44 +00001604static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
bellard9fddaa02004-05-21 12:59:32 +00001605{
bellard6a00d602005-11-21 23:25:50 +00001606 CPUState *env = mon_get_cpu();
1607 if (!env)
1608 return 0;
1609 return cpu_ppc_load_tbl(env);
bellard9fddaa02004-05-21 12:59:32 +00001610}
bellarda541f292004-04-12 20:39:29 +00001611#endif
1612
bellarde95c8d52004-09-30 22:22:08 +00001613#if defined(TARGET_SPARC)
bellard7b936c02005-10-30 17:05:13 +00001614#ifndef TARGET_SPARC64
blueswir18662d652008-10-02 18:32:44 +00001615static target_long monitor_get_psr (const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001616{
bellard6a00d602005-11-21 23:25:50 +00001617 CPUState *env = mon_get_cpu();
1618 if (!env)
1619 return 0;
1620 return GET_PSR(env);
bellarde95c8d52004-09-30 22:22:08 +00001621}
bellard7b936c02005-10-30 17:05:13 +00001622#endif
bellarde95c8d52004-09-30 22:22:08 +00001623
blueswir18662d652008-10-02 18:32:44 +00001624static target_long monitor_get_reg(const struct MonitorDef *md, int val)
bellarde95c8d52004-09-30 22:22:08 +00001625{
bellard6a00d602005-11-21 23:25:50 +00001626 CPUState *env = mon_get_cpu();
1627 if (!env)
1628 return 0;
1629 return env->regwptr[val];
bellarde95c8d52004-09-30 22:22:08 +00001630}
1631#endif
1632
blueswir18662d652008-10-02 18:32:44 +00001633static const MonitorDef monitor_defs[] = {
bellard9307c4c2004-04-04 12:57:25 +00001634#ifdef TARGET_I386
bellard57206fd2004-04-25 18:54:52 +00001635
1636#define SEG(name, seg) \
bellard92a31b12005-02-10 22:00:52 +00001637 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
bellard57206fd2004-04-25 18:54:52 +00001638 { name ".base", offsetof(CPUState, segs[seg].base) },\
bellard92a31b12005-02-10 22:00:52 +00001639 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
bellard57206fd2004-04-25 18:54:52 +00001640
bellard9307c4c2004-04-04 12:57:25 +00001641 { "eax", offsetof(CPUState, regs[0]) },
1642 { "ecx", offsetof(CPUState, regs[1]) },
1643 { "edx", offsetof(CPUState, regs[2]) },
1644 { "ebx", offsetof(CPUState, regs[3]) },
1645 { "esp|sp", offsetof(CPUState, regs[4]) },
1646 { "ebp|fp", offsetof(CPUState, regs[5]) },
1647 { "esi", offsetof(CPUState, regs[6]) },
bellard01038d22004-09-13 21:36:46 +00001648 { "edi", offsetof(CPUState, regs[7]) },
bellard92a31b12005-02-10 22:00:52 +00001649#ifdef TARGET_X86_64
1650 { "r8", offsetof(CPUState, regs[8]) },
1651 { "r9", offsetof(CPUState, regs[9]) },
1652 { "r10", offsetof(CPUState, regs[10]) },
1653 { "r11", offsetof(CPUState, regs[11]) },
1654 { "r12", offsetof(CPUState, regs[12]) },
1655 { "r13", offsetof(CPUState, regs[13]) },
1656 { "r14", offsetof(CPUState, regs[14]) },
1657 { "r15", offsetof(CPUState, regs[15]) },
1658#endif
bellard9307c4c2004-04-04 12:57:25 +00001659 { "eflags", offsetof(CPUState, eflags) },
bellard57206fd2004-04-25 18:54:52 +00001660 { "eip", offsetof(CPUState, eip) },
1661 SEG("cs", R_CS)
1662 SEG("ds", R_DS)
1663 SEG("es", R_ES)
bellard01038d22004-09-13 21:36:46 +00001664 SEG("ss", R_SS)
bellard57206fd2004-04-25 18:54:52 +00001665 SEG("fs", R_FS)
1666 SEG("gs", R_GS)
1667 { "pc", 0, monitor_get_pc, },
bellarda541f292004-04-12 20:39:29 +00001668#elif defined(TARGET_PPC)
j_mayerff937db2007-09-19 05:49:13 +00001669 /* General purpose registers */
bellarda541f292004-04-12 20:39:29 +00001670 { "r0", offsetof(CPUState, gpr[0]) },
1671 { "r1", offsetof(CPUState, gpr[1]) },
1672 { "r2", offsetof(CPUState, gpr[2]) },
1673 { "r3", offsetof(CPUState, gpr[3]) },
1674 { "r4", offsetof(CPUState, gpr[4]) },
1675 { "r5", offsetof(CPUState, gpr[5]) },
1676 { "r6", offsetof(CPUState, gpr[6]) },
1677 { "r7", offsetof(CPUState, gpr[7]) },
1678 { "r8", offsetof(CPUState, gpr[8]) },
1679 { "r9", offsetof(CPUState, gpr[9]) },
1680 { "r10", offsetof(CPUState, gpr[10]) },
1681 { "r11", offsetof(CPUState, gpr[11]) },
1682 { "r12", offsetof(CPUState, gpr[12]) },
1683 { "r13", offsetof(CPUState, gpr[13]) },
1684 { "r14", offsetof(CPUState, gpr[14]) },
1685 { "r15", offsetof(CPUState, gpr[15]) },
1686 { "r16", offsetof(CPUState, gpr[16]) },
1687 { "r17", offsetof(CPUState, gpr[17]) },
1688 { "r18", offsetof(CPUState, gpr[18]) },
1689 { "r19", offsetof(CPUState, gpr[19]) },
1690 { "r20", offsetof(CPUState, gpr[20]) },
1691 { "r21", offsetof(CPUState, gpr[21]) },
1692 { "r22", offsetof(CPUState, gpr[22]) },
1693 { "r23", offsetof(CPUState, gpr[23]) },
1694 { "r24", offsetof(CPUState, gpr[24]) },
1695 { "r25", offsetof(CPUState, gpr[25]) },
1696 { "r26", offsetof(CPUState, gpr[26]) },
1697 { "r27", offsetof(CPUState, gpr[27]) },
1698 { "r28", offsetof(CPUState, gpr[28]) },
1699 { "r29", offsetof(CPUState, gpr[29]) },
1700 { "r30", offsetof(CPUState, gpr[30]) },
1701 { "r31", offsetof(CPUState, gpr[31]) },
j_mayerff937db2007-09-19 05:49:13 +00001702 /* Floating point registers */
1703 { "f0", offsetof(CPUState, fpr[0]) },
1704 { "f1", offsetof(CPUState, fpr[1]) },
1705 { "f2", offsetof(CPUState, fpr[2]) },
1706 { "f3", offsetof(CPUState, fpr[3]) },
1707 { "f4", offsetof(CPUState, fpr[4]) },
1708 { "f5", offsetof(CPUState, fpr[5]) },
1709 { "f6", offsetof(CPUState, fpr[6]) },
1710 { "f7", offsetof(CPUState, fpr[7]) },
1711 { "f8", offsetof(CPUState, fpr[8]) },
1712 { "f9", offsetof(CPUState, fpr[9]) },
1713 { "f10", offsetof(CPUState, fpr[10]) },
1714 { "f11", offsetof(CPUState, fpr[11]) },
1715 { "f12", offsetof(CPUState, fpr[12]) },
1716 { "f13", offsetof(CPUState, fpr[13]) },
1717 { "f14", offsetof(CPUState, fpr[14]) },
1718 { "f15", offsetof(CPUState, fpr[15]) },
1719 { "f16", offsetof(CPUState, fpr[16]) },
1720 { "f17", offsetof(CPUState, fpr[17]) },
1721 { "f18", offsetof(CPUState, fpr[18]) },
1722 { "f19", offsetof(CPUState, fpr[19]) },
1723 { "f20", offsetof(CPUState, fpr[20]) },
1724 { "f21", offsetof(CPUState, fpr[21]) },
1725 { "f22", offsetof(CPUState, fpr[22]) },
1726 { "f23", offsetof(CPUState, fpr[23]) },
1727 { "f24", offsetof(CPUState, fpr[24]) },
1728 { "f25", offsetof(CPUState, fpr[25]) },
1729 { "f26", offsetof(CPUState, fpr[26]) },
1730 { "f27", offsetof(CPUState, fpr[27]) },
1731 { "f28", offsetof(CPUState, fpr[28]) },
1732 { "f29", offsetof(CPUState, fpr[29]) },
1733 { "f30", offsetof(CPUState, fpr[30]) },
1734 { "f31", offsetof(CPUState, fpr[31]) },
1735 { "fpscr", offsetof(CPUState, fpscr) },
1736 /* Next instruction pointer */
bellard57206fd2004-04-25 18:54:52 +00001737 { "nip|pc", offsetof(CPUState, nip) },
bellarda541f292004-04-12 20:39:29 +00001738 { "lr", offsetof(CPUState, lr) },
1739 { "ctr", offsetof(CPUState, ctr) },
bellard9fddaa02004-05-21 12:59:32 +00001740 { "decr", 0, &monitor_get_decr, },
bellarda541f292004-04-12 20:39:29 +00001741 { "ccr", 0, &monitor_get_ccr, },
j_mayerff937db2007-09-19 05:49:13 +00001742 /* Machine state register */
bellarda541f292004-04-12 20:39:29 +00001743 { "msr", 0, &monitor_get_msr, },
1744 { "xer", 0, &monitor_get_xer, },
bellard9fddaa02004-05-21 12:59:32 +00001745 { "tbu", 0, &monitor_get_tbu, },
1746 { "tbl", 0, &monitor_get_tbl, },
j_mayerff937db2007-09-19 05:49:13 +00001747#if defined(TARGET_PPC64)
1748 /* Address space register */
1749 { "asr", offsetof(CPUState, asr) },
1750#endif
1751 /* Segment registers */
bellarda541f292004-04-12 20:39:29 +00001752 { "sdr1", offsetof(CPUState, sdr1) },
1753 { "sr0", offsetof(CPUState, sr[0]) },
1754 { "sr1", offsetof(CPUState, sr[1]) },
1755 { "sr2", offsetof(CPUState, sr[2]) },
1756 { "sr3", offsetof(CPUState, sr[3]) },
1757 { "sr4", offsetof(CPUState, sr[4]) },
1758 { "sr5", offsetof(CPUState, sr[5]) },
1759 { "sr6", offsetof(CPUState, sr[6]) },
1760 { "sr7", offsetof(CPUState, sr[7]) },
1761 { "sr8", offsetof(CPUState, sr[8]) },
1762 { "sr9", offsetof(CPUState, sr[9]) },
1763 { "sr10", offsetof(CPUState, sr[10]) },
1764 { "sr11", offsetof(CPUState, sr[11]) },
1765 { "sr12", offsetof(CPUState, sr[12]) },
1766 { "sr13", offsetof(CPUState, sr[13]) },
1767 { "sr14", offsetof(CPUState, sr[14]) },
1768 { "sr15", offsetof(CPUState, sr[15]) },
1769 /* Too lazy to put BATs and SPRs ... */
bellarde95c8d52004-09-30 22:22:08 +00001770#elif defined(TARGET_SPARC)
1771 { "g0", offsetof(CPUState, gregs[0]) },
1772 { "g1", offsetof(CPUState, gregs[1]) },
1773 { "g2", offsetof(CPUState, gregs[2]) },
1774 { "g3", offsetof(CPUState, gregs[3]) },
1775 { "g4", offsetof(CPUState, gregs[4]) },
1776 { "g5", offsetof(CPUState, gregs[5]) },
1777 { "g6", offsetof(CPUState, gregs[6]) },
1778 { "g7", offsetof(CPUState, gregs[7]) },
1779 { "o0", 0, monitor_get_reg },
1780 { "o1", 1, monitor_get_reg },
1781 { "o2", 2, monitor_get_reg },
1782 { "o3", 3, monitor_get_reg },
1783 { "o4", 4, monitor_get_reg },
1784 { "o5", 5, monitor_get_reg },
1785 { "o6", 6, monitor_get_reg },
1786 { "o7", 7, monitor_get_reg },
1787 { "l0", 8, monitor_get_reg },
1788 { "l1", 9, monitor_get_reg },
1789 { "l2", 10, monitor_get_reg },
1790 { "l3", 11, monitor_get_reg },
1791 { "l4", 12, monitor_get_reg },
1792 { "l5", 13, monitor_get_reg },
1793 { "l6", 14, monitor_get_reg },
1794 { "l7", 15, monitor_get_reg },
1795 { "i0", 16, monitor_get_reg },
1796 { "i1", 17, monitor_get_reg },
1797 { "i2", 18, monitor_get_reg },
1798 { "i3", 19, monitor_get_reg },
1799 { "i4", 20, monitor_get_reg },
1800 { "i5", 21, monitor_get_reg },
1801 { "i6", 22, monitor_get_reg },
1802 { "i7", 23, monitor_get_reg },
1803 { "pc", offsetof(CPUState, pc) },
1804 { "npc", offsetof(CPUState, npc) },
1805 { "y", offsetof(CPUState, y) },
bellard7b936c02005-10-30 17:05:13 +00001806#ifndef TARGET_SPARC64
bellarde95c8d52004-09-30 22:22:08 +00001807 { "psr", 0, &monitor_get_psr, },
1808 { "wim", offsetof(CPUState, wim) },
bellard7b936c02005-10-30 17:05:13 +00001809#endif
bellarde95c8d52004-09-30 22:22:08 +00001810 { "tbr", offsetof(CPUState, tbr) },
1811 { "fsr", offsetof(CPUState, fsr) },
1812 { "f0", offsetof(CPUState, fpr[0]) },
1813 { "f1", offsetof(CPUState, fpr[1]) },
1814 { "f2", offsetof(CPUState, fpr[2]) },
1815 { "f3", offsetof(CPUState, fpr[3]) },
1816 { "f4", offsetof(CPUState, fpr[4]) },
1817 { "f5", offsetof(CPUState, fpr[5]) },
1818 { "f6", offsetof(CPUState, fpr[6]) },
1819 { "f7", offsetof(CPUState, fpr[7]) },
1820 { "f8", offsetof(CPUState, fpr[8]) },
1821 { "f9", offsetof(CPUState, fpr[9]) },
1822 { "f10", offsetof(CPUState, fpr[10]) },
1823 { "f11", offsetof(CPUState, fpr[11]) },
1824 { "f12", offsetof(CPUState, fpr[12]) },
1825 { "f13", offsetof(CPUState, fpr[13]) },
1826 { "f14", offsetof(CPUState, fpr[14]) },
1827 { "f15", offsetof(CPUState, fpr[15]) },
1828 { "f16", offsetof(CPUState, fpr[16]) },
1829 { "f17", offsetof(CPUState, fpr[17]) },
1830 { "f18", offsetof(CPUState, fpr[18]) },
1831 { "f19", offsetof(CPUState, fpr[19]) },
1832 { "f20", offsetof(CPUState, fpr[20]) },
1833 { "f21", offsetof(CPUState, fpr[21]) },
1834 { "f22", offsetof(CPUState, fpr[22]) },
1835 { "f23", offsetof(CPUState, fpr[23]) },
1836 { "f24", offsetof(CPUState, fpr[24]) },
1837 { "f25", offsetof(CPUState, fpr[25]) },
1838 { "f26", offsetof(CPUState, fpr[26]) },
1839 { "f27", offsetof(CPUState, fpr[27]) },
1840 { "f28", offsetof(CPUState, fpr[28]) },
1841 { "f29", offsetof(CPUState, fpr[29]) },
1842 { "f30", offsetof(CPUState, fpr[30]) },
1843 { "f31", offsetof(CPUState, fpr[31]) },
bellard7b936c02005-10-30 17:05:13 +00001844#ifdef TARGET_SPARC64
1845 { "f32", offsetof(CPUState, fpr[32]) },
1846 { "f34", offsetof(CPUState, fpr[34]) },
1847 { "f36", offsetof(CPUState, fpr[36]) },
1848 { "f38", offsetof(CPUState, fpr[38]) },
1849 { "f40", offsetof(CPUState, fpr[40]) },
1850 { "f42", offsetof(CPUState, fpr[42]) },
1851 { "f44", offsetof(CPUState, fpr[44]) },
1852 { "f46", offsetof(CPUState, fpr[46]) },
1853 { "f48", offsetof(CPUState, fpr[48]) },
1854 { "f50", offsetof(CPUState, fpr[50]) },
1855 { "f52", offsetof(CPUState, fpr[52]) },
1856 { "f54", offsetof(CPUState, fpr[54]) },
1857 { "f56", offsetof(CPUState, fpr[56]) },
1858 { "f58", offsetof(CPUState, fpr[58]) },
1859 { "f60", offsetof(CPUState, fpr[60]) },
1860 { "f62", offsetof(CPUState, fpr[62]) },
1861 { "asi", offsetof(CPUState, asi) },
1862 { "pstate", offsetof(CPUState, pstate) },
1863 { "cansave", offsetof(CPUState, cansave) },
1864 { "canrestore", offsetof(CPUState, canrestore) },
1865 { "otherwin", offsetof(CPUState, otherwin) },
1866 { "wstate", offsetof(CPUState, wstate) },
1867 { "cleanwin", offsetof(CPUState, cleanwin) },
1868 { "fprs", offsetof(CPUState, fprs) },
1869#endif
bellard9307c4c2004-04-04 12:57:25 +00001870#endif
1871 { NULL },
1872};
1873
ths5fafdf22007-09-16 21:08:06 +00001874static void expr_error(const char *fmt)
bellard9dc39cb2004-03-14 21:38:27 +00001875{
bellard9307c4c2004-04-04 12:57:25 +00001876 term_printf(fmt);
1877 term_printf("\n");
1878 longjmp(expr_env, 1);
1879}
1880
bellard6a00d602005-11-21 23:25:50 +00001881/* return 0 if OK, -1 if not found, -2 if no CPU defined */
bellard92a31b12005-02-10 22:00:52 +00001882static int get_monitor_def(target_long *pval, const char *name)
bellard9307c4c2004-04-04 12:57:25 +00001883{
blueswir18662d652008-10-02 18:32:44 +00001884 const MonitorDef *md;
bellard92a31b12005-02-10 22:00:52 +00001885 void *ptr;
1886
bellard9307c4c2004-04-04 12:57:25 +00001887 for(md = monitor_defs; md->name != NULL; md++) {
1888 if (compare_cmd(name, md->name)) {
1889 if (md->get_value) {
bellarde95c8d52004-09-30 22:22:08 +00001890 *pval = md->get_value(md, md->offset);
bellard9307c4c2004-04-04 12:57:25 +00001891 } else {
bellard6a00d602005-11-21 23:25:50 +00001892 CPUState *env = mon_get_cpu();
1893 if (!env)
1894 return -2;
1895 ptr = (uint8_t *)env + md->offset;
bellard92a31b12005-02-10 22:00:52 +00001896 switch(md->type) {
1897 case MD_I32:
1898 *pval = *(int32_t *)ptr;
1899 break;
1900 case MD_TLONG:
1901 *pval = *(target_long *)ptr;
1902 break;
1903 default:
1904 *pval = 0;
1905 break;
1906 }
bellard9307c4c2004-04-04 12:57:25 +00001907 }
1908 return 0;
1909 }
1910 }
1911 return -1;
1912}
1913
1914static void next(void)
1915{
1916 if (pch != '\0') {
1917 pch++;
1918 while (isspace(*pch))
1919 pch++;
1920 }
1921}
1922
blueswir1c2efc952007-09-25 17:28:42 +00001923static int64_t expr_sum(void);
bellard9307c4c2004-04-04 12:57:25 +00001924
blueswir1c2efc952007-09-25 17:28:42 +00001925static int64_t expr_unary(void)
bellard9307c4c2004-04-04 12:57:25 +00001926{
blueswir1c2efc952007-09-25 17:28:42 +00001927 int64_t n;
bellard9307c4c2004-04-04 12:57:25 +00001928 char *p;
bellard6a00d602005-11-21 23:25:50 +00001929 int ret;
bellard9307c4c2004-04-04 12:57:25 +00001930
1931 switch(*pch) {
1932 case '+':
1933 next();
1934 n = expr_unary();
1935 break;
1936 case '-':
1937 next();
1938 n = -expr_unary();
1939 break;
1940 case '~':
1941 next();
1942 n = ~expr_unary();
1943 break;
1944 case '(':
1945 next();
1946 n = expr_sum();
1947 if (*pch != ')') {
1948 expr_error("')' expected");
1949 }
1950 next();
1951 break;
bellard81d09122004-07-14 17:21:37 +00001952 case '\'':
1953 pch++;
1954 if (*pch == '\0')
1955 expr_error("character constant expected");
1956 n = *pch;
1957 pch++;
1958 if (*pch != '\'')
1959 expr_error("missing terminating \' character");
1960 next();
1961 break;
bellard9307c4c2004-04-04 12:57:25 +00001962 case '$':
1963 {
1964 char buf[128], *q;
ths69b34972007-12-17 03:15:52 +00001965 target_long reg=0;
ths3b46e622007-09-17 08:09:54 +00001966
bellard9307c4c2004-04-04 12:57:25 +00001967 pch++;
1968 q = buf;
1969 while ((*pch >= 'a' && *pch <= 'z') ||
1970 (*pch >= 'A' && *pch <= 'Z') ||
1971 (*pch >= '0' && *pch <= '9') ||
bellard57206fd2004-04-25 18:54:52 +00001972 *pch == '_' || *pch == '.') {
bellard9307c4c2004-04-04 12:57:25 +00001973 if ((q - buf) < sizeof(buf) - 1)
1974 *q++ = *pch;
1975 pch++;
1976 }
1977 while (isspace(*pch))
1978 pch++;
1979 *q = 0;
blueswir17743e582007-09-24 18:39:04 +00001980 ret = get_monitor_def(&reg, buf);
bellard6a00d602005-11-21 23:25:50 +00001981 if (ret == -1)
bellard9307c4c2004-04-04 12:57:25 +00001982 expr_error("unknown register");
ths5fafdf22007-09-16 21:08:06 +00001983 else if (ret == -2)
bellard6a00d602005-11-21 23:25:50 +00001984 expr_error("no cpu defined");
blueswir17743e582007-09-24 18:39:04 +00001985 n = reg;
bellard9307c4c2004-04-04 12:57:25 +00001986 }
1987 break;
1988 case '\0':
1989 expr_error("unexpected end of expression");
1990 n = 0;
1991 break;
1992 default:
blueswir17743e582007-09-24 18:39:04 +00001993#if TARGET_PHYS_ADDR_BITS > 32
bellard4f4fbf72006-06-25 18:28:12 +00001994 n = strtoull(pch, &p, 0);
1995#else
bellard9307c4c2004-04-04 12:57:25 +00001996 n = strtoul(pch, &p, 0);
bellard4f4fbf72006-06-25 18:28:12 +00001997#endif
bellard9307c4c2004-04-04 12:57:25 +00001998 if (pch == p) {
1999 expr_error("invalid char in expression");
2000 }
2001 pch = p;
2002 while (isspace(*pch))
2003 pch++;
2004 break;
2005 }
2006 return n;
2007}
2008
2009
blueswir1c2efc952007-09-25 17:28:42 +00002010static int64_t expr_prod(void)
bellard9307c4c2004-04-04 12:57:25 +00002011{
blueswir1c2efc952007-09-25 17:28:42 +00002012 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002013 int op;
ths3b46e622007-09-17 08:09:54 +00002014
bellard9307c4c2004-04-04 12:57:25 +00002015 val = expr_unary();
2016 for(;;) {
2017 op = *pch;
2018 if (op != '*' && op != '/' && op != '%')
2019 break;
2020 next();
2021 val2 = expr_unary();
2022 switch(op) {
2023 default:
2024 case '*':
2025 val *= val2;
2026 break;
2027 case '/':
2028 case '%':
ths5fafdf22007-09-16 21:08:06 +00002029 if (val2 == 0)
bellard5b602122004-05-22 21:41:05 +00002030 expr_error("division by zero");
bellard9307c4c2004-04-04 12:57:25 +00002031 if (op == '/')
2032 val /= val2;
2033 else
2034 val %= val2;
2035 break;
2036 }
2037 }
2038 return val;
2039}
2040
blueswir1c2efc952007-09-25 17:28:42 +00002041static int64_t expr_logic(void)
bellard9307c4c2004-04-04 12:57:25 +00002042{
blueswir1c2efc952007-09-25 17:28:42 +00002043 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002044 int op;
bellard9307c4c2004-04-04 12:57:25 +00002045
2046 val = expr_prod();
2047 for(;;) {
2048 op = *pch;
2049 if (op != '&' && op != '|' && op != '^')
2050 break;
2051 next();
2052 val2 = expr_prod();
2053 switch(op) {
2054 default:
2055 case '&':
2056 val &= val2;
2057 break;
2058 case '|':
2059 val |= val2;
2060 break;
2061 case '^':
2062 val ^= val2;
2063 break;
2064 }
2065 }
2066 return val;
2067}
2068
blueswir1c2efc952007-09-25 17:28:42 +00002069static int64_t expr_sum(void)
bellard9307c4c2004-04-04 12:57:25 +00002070{
blueswir1c2efc952007-09-25 17:28:42 +00002071 int64_t val, val2;
bellard92a31b12005-02-10 22:00:52 +00002072 int op;
bellard9307c4c2004-04-04 12:57:25 +00002073
2074 val = expr_logic();
2075 for(;;) {
2076 op = *pch;
2077 if (op != '+' && op != '-')
2078 break;
2079 next();
2080 val2 = expr_logic();
2081 if (op == '+')
2082 val += val2;
2083 else
2084 val -= val2;
2085 }
2086 return val;
2087}
2088
blueswir1c2efc952007-09-25 17:28:42 +00002089static int get_expr(int64_t *pval, const char **pp)
bellard9307c4c2004-04-04 12:57:25 +00002090{
2091 pch = *pp;
2092 if (setjmp(expr_env)) {
2093 *pp = pch;
2094 return -1;
2095 }
2096 while (isspace(*pch))
2097 pch++;
2098 *pval = expr_sum();
2099 *pp = pch;
2100 return 0;
2101}
2102
2103static int get_str(char *buf, int buf_size, const char **pp)
2104{
2105 const char *p;
2106 char *q;
2107 int c;
2108
bellard81d09122004-07-14 17:21:37 +00002109 q = buf;
bellard9307c4c2004-04-04 12:57:25 +00002110 p = *pp;
2111 while (isspace(*p))
2112 p++;
2113 if (*p == '\0') {
2114 fail:
bellard81d09122004-07-14 17:21:37 +00002115 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002116 *pp = p;
2117 return -1;
2118 }
bellard9307c4c2004-04-04 12:57:25 +00002119 if (*p == '\"') {
2120 p++;
2121 while (*p != '\0' && *p != '\"') {
2122 if (*p == '\\') {
2123 p++;
2124 c = *p++;
2125 switch(c) {
2126 case 'n':
2127 c = '\n';
2128 break;
2129 case 'r':
2130 c = '\r';
2131 break;
2132 case '\\':
2133 case '\'':
2134 case '\"':
2135 break;
2136 default:
2137 qemu_printf("unsupported escape code: '\\%c'\n", c);
2138 goto fail;
2139 }
2140 if ((q - buf) < buf_size - 1) {
2141 *q++ = c;
2142 }
2143 } else {
2144 if ((q - buf) < buf_size - 1) {
2145 *q++ = *p;
2146 }
2147 p++;
2148 }
2149 }
2150 if (*p != '\"') {
bellard5b602122004-05-22 21:41:05 +00002151 qemu_printf("unterminated string\n");
bellard9307c4c2004-04-04 12:57:25 +00002152 goto fail;
2153 }
2154 p++;
2155 } else {
2156 while (*p != '\0' && !isspace(*p)) {
2157 if ((q - buf) < buf_size - 1) {
2158 *q++ = *p;
2159 }
2160 p++;
2161 }
bellard9307c4c2004-04-04 12:57:25 +00002162 }
bellard81d09122004-07-14 17:21:37 +00002163 *q = '\0';
bellard9307c4c2004-04-04 12:57:25 +00002164 *pp = p;
2165 return 0;
2166}
2167
2168static int default_fmt_format = 'x';
2169static int default_fmt_size = 4;
2170
2171#define MAX_ARGS 16
2172
bellard7e2515e2004-08-01 21:52:19 +00002173static void monitor_handle_command(const char *cmdline)
bellard9307c4c2004-04-04 12:57:25 +00002174{
2175 const char *p, *pstart, *typestr;
2176 char *q;
2177 int c, nb_args, len, i, has_arg;
blueswir18662d652008-10-02 18:32:44 +00002178 const term_cmd_t *cmd;
bellard9307c4c2004-04-04 12:57:25 +00002179 char cmdname[256];
2180 char buf[1024];
2181 void *str_allocated[MAX_ARGS];
2182 void *args[MAX_ARGS];
blueswir1a5f1b962008-08-17 20:21:51 +00002183 void (*handler_0)(void);
2184 void (*handler_1)(void *arg0);
2185 void (*handler_2)(void *arg0, void *arg1);
2186 void (*handler_3)(void *arg0, void *arg1, void *arg2);
2187 void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2188 void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2189 void *arg4);
2190 void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2191 void *arg4, void *arg5);
2192 void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2193 void *arg4, void *arg5, void *arg6);
bellard9dc39cb2004-03-14 21:38:27 +00002194
2195#ifdef DEBUG
2196 term_printf("command='%s'\n", cmdline);
2197#endif
ths3b46e622007-09-17 08:09:54 +00002198
bellard9307c4c2004-04-04 12:57:25 +00002199 /* extract the command name */
bellard9dc39cb2004-03-14 21:38:27 +00002200 p = cmdline;
bellard9307c4c2004-04-04 12:57:25 +00002201 q = cmdname;
2202 while (isspace(*p))
2203 p++;
2204 if (*p == '\0')
bellard9dc39cb2004-03-14 21:38:27 +00002205 return;
bellard9307c4c2004-04-04 12:57:25 +00002206 pstart = p;
2207 while (*p != '\0' && *p != '/' && !isspace(*p))
2208 p++;
2209 len = p - pstart;
2210 if (len > sizeof(cmdname) - 1)
2211 len = sizeof(cmdname) - 1;
2212 memcpy(cmdname, pstart, len);
2213 cmdname[len] = '\0';
ths3b46e622007-09-17 08:09:54 +00002214
bellard9307c4c2004-04-04 12:57:25 +00002215 /* find the command */
bellard9dc39cb2004-03-14 21:38:27 +00002216 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
ths5fafdf22007-09-16 21:08:06 +00002217 if (compare_cmd(cmdname, cmd->name))
bellard9dc39cb2004-03-14 21:38:27 +00002218 goto found;
2219 }
bellard9307c4c2004-04-04 12:57:25 +00002220 term_printf("unknown command: '%s'\n", cmdname);
bellard9dc39cb2004-03-14 21:38:27 +00002221 return;
2222 found:
bellard9307c4c2004-04-04 12:57:25 +00002223
2224 for(i = 0; i < MAX_ARGS; i++)
2225 str_allocated[i] = NULL;
ths3b46e622007-09-17 08:09:54 +00002226
bellard9307c4c2004-04-04 12:57:25 +00002227 /* parse the parameters */
2228 typestr = cmd->args_type;
2229 nb_args = 0;
2230 for(;;) {
2231 c = *typestr;
2232 if (c == '\0')
2233 break;
2234 typestr++;
2235 switch(c) {
2236 case 'F':
bellard81d09122004-07-14 17:21:37 +00002237 case 'B':
bellard9307c4c2004-04-04 12:57:25 +00002238 case 's':
2239 {
2240 int ret;
2241 char *str;
ths3b46e622007-09-17 08:09:54 +00002242
ths5fafdf22007-09-16 21:08:06 +00002243 while (isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002244 p++;
2245 if (*typestr == '?') {
2246 typestr++;
2247 if (*p == '\0') {
2248 /* no optional string: NULL argument */
2249 str = NULL;
2250 goto add_str;
2251 }
2252 }
2253 ret = get_str(buf, sizeof(buf), &p);
2254 if (ret < 0) {
bellard81d09122004-07-14 17:21:37 +00002255 switch(c) {
2256 case 'F':
bellard9307c4c2004-04-04 12:57:25 +00002257 term_printf("%s: filename expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002258 break;
2259 case 'B':
2260 term_printf("%s: block device name expected\n", cmdname);
2261 break;
2262 default:
bellard9307c4c2004-04-04 12:57:25 +00002263 term_printf("%s: string expected\n", cmdname);
bellard81d09122004-07-14 17:21:37 +00002264 break;
2265 }
bellard9307c4c2004-04-04 12:57:25 +00002266 goto fail;
2267 }
2268 str = qemu_malloc(strlen(buf) + 1);
blueswir1363a37d2008-08-21 17:58:08 +00002269 pstrcpy(str, sizeof(buf), buf);
bellard9307c4c2004-04-04 12:57:25 +00002270 str_allocated[nb_args] = str;
2271 add_str:
2272 if (nb_args >= MAX_ARGS) {
2273 error_args:
2274 term_printf("%s: too many arguments\n", cmdname);
2275 goto fail;
2276 }
2277 args[nb_args++] = str;
2278 }
2279 break;
2280 case '/':
2281 {
2282 int count, format, size;
ths3b46e622007-09-17 08:09:54 +00002283
bellard9307c4c2004-04-04 12:57:25 +00002284 while (isspace(*p))
2285 p++;
2286 if (*p == '/') {
2287 /* format found */
2288 p++;
2289 count = 1;
2290 if (isdigit(*p)) {
2291 count = 0;
2292 while (isdigit(*p)) {
2293 count = count * 10 + (*p - '0');
2294 p++;
2295 }
2296 }
2297 size = -1;
2298 format = -1;
2299 for(;;) {
2300 switch(*p) {
2301 case 'o':
2302 case 'd':
2303 case 'u':
2304 case 'x':
2305 case 'i':
2306 case 'c':
2307 format = *p++;
2308 break;
2309 case 'b':
2310 size = 1;
2311 p++;
2312 break;
2313 case 'h':
2314 size = 2;
2315 p++;
2316 break;
2317 case 'w':
2318 size = 4;
2319 p++;
2320 break;
2321 case 'g':
2322 case 'L':
2323 size = 8;
2324 p++;
2325 break;
2326 default:
2327 goto next;
2328 }
2329 }
2330 next:
2331 if (*p != '\0' && !isspace(*p)) {
2332 term_printf("invalid char in format: '%c'\n", *p);
2333 goto fail;
2334 }
bellard9307c4c2004-04-04 12:57:25 +00002335 if (format < 0)
2336 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002337 if (format != 'i') {
2338 /* for 'i', not specifying a size gives -1 as size */
2339 if (size < 0)
2340 size = default_fmt_size;
aurel32e90f0092008-10-01 21:45:51 +00002341 default_fmt_size = size;
bellard4c27ba22004-04-25 18:05:08 +00002342 }
bellard9307c4c2004-04-04 12:57:25 +00002343 default_fmt_format = format;
2344 } else {
2345 count = 1;
2346 format = default_fmt_format;
bellard4c27ba22004-04-25 18:05:08 +00002347 if (format != 'i') {
2348 size = default_fmt_size;
2349 } else {
2350 size = -1;
2351 }
bellard9307c4c2004-04-04 12:57:25 +00002352 }
2353 if (nb_args + 3 > MAX_ARGS)
2354 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002355 args[nb_args++] = (void*)(long)count;
2356 args[nb_args++] = (void*)(long)format;
2357 args[nb_args++] = (void*)(long)size;
bellard9307c4c2004-04-04 12:57:25 +00002358 }
2359 break;
2360 case 'i':
bellard92a31b12005-02-10 22:00:52 +00002361 case 'l':
bellard9307c4c2004-04-04 12:57:25 +00002362 {
blueswir1c2efc952007-09-25 17:28:42 +00002363 int64_t val;
blueswir17743e582007-09-24 18:39:04 +00002364
ths5fafdf22007-09-16 21:08:06 +00002365 while (isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002366 p++;
bellard34405572004-06-08 00:55:58 +00002367 if (*typestr == '?' || *typestr == '.') {
bellard34405572004-06-08 00:55:58 +00002368 if (*typestr == '?') {
2369 if (*p == '\0')
2370 has_arg = 0;
2371 else
2372 has_arg = 1;
2373 } else {
2374 if (*p == '.') {
2375 p++;
ths5fafdf22007-09-16 21:08:06 +00002376 while (isspace(*p))
bellard34405572004-06-08 00:55:58 +00002377 p++;
2378 has_arg = 1;
2379 } else {
2380 has_arg = 0;
2381 }
2382 }
bellard13224a82006-07-14 22:03:35 +00002383 typestr++;
bellard9307c4c2004-04-04 12:57:25 +00002384 if (nb_args >= MAX_ARGS)
2385 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002386 args[nb_args++] = (void *)(long)has_arg;
bellard9307c4c2004-04-04 12:57:25 +00002387 if (!has_arg) {
2388 if (nb_args >= MAX_ARGS)
2389 goto error_args;
2390 val = -1;
2391 goto add_num;
2392 }
2393 }
2394 if (get_expr(&val, &p))
2395 goto fail;
2396 add_num:
bellard92a31b12005-02-10 22:00:52 +00002397 if (c == 'i') {
2398 if (nb_args >= MAX_ARGS)
2399 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002400 args[nb_args++] = (void *)(long)val;
bellard92a31b12005-02-10 22:00:52 +00002401 } else {
2402 if ((nb_args + 1) >= MAX_ARGS)
2403 goto error_args;
blueswir17743e582007-09-24 18:39:04 +00002404#if TARGET_PHYS_ADDR_BITS > 32
j_mayer1c5bf3b2007-04-14 12:17:59 +00002405 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002406#else
2407 args[nb_args++] = (void *)0;
2408#endif
j_mayer1c5bf3b2007-04-14 12:17:59 +00002409 args[nb_args++] = (void *)(long)(val & 0xffffffff);
bellard92a31b12005-02-10 22:00:52 +00002410 }
bellard9307c4c2004-04-04 12:57:25 +00002411 }
2412 break;
2413 case '-':
2414 {
2415 int has_option;
2416 /* option */
ths3b46e622007-09-17 08:09:54 +00002417
bellard9307c4c2004-04-04 12:57:25 +00002418 c = *typestr++;
2419 if (c == '\0')
2420 goto bad_type;
ths5fafdf22007-09-16 21:08:06 +00002421 while (isspace(*p))
bellard9307c4c2004-04-04 12:57:25 +00002422 p++;
2423 has_option = 0;
2424 if (*p == '-') {
2425 p++;
2426 if (*p != c) {
ths5fafdf22007-09-16 21:08:06 +00002427 term_printf("%s: unsupported option -%c\n",
bellard9307c4c2004-04-04 12:57:25 +00002428 cmdname, *p);
2429 goto fail;
2430 }
2431 p++;
2432 has_option = 1;
2433 }
2434 if (nb_args >= MAX_ARGS)
2435 goto error_args;
j_mayer1c5bf3b2007-04-14 12:17:59 +00002436 args[nb_args++] = (void *)(long)has_option;
bellard9307c4c2004-04-04 12:57:25 +00002437 }
2438 break;
2439 default:
2440 bad_type:
2441 term_printf("%s: unknown type '%c'\n", cmdname, c);
2442 goto fail;
2443 }
2444 }
2445 /* check that all arguments were parsed */
2446 while (isspace(*p))
2447 p++;
2448 if (*p != '\0') {
ths5fafdf22007-09-16 21:08:06 +00002449 term_printf("%s: extraneous characters at the end of line\n",
bellard9307c4c2004-04-04 12:57:25 +00002450 cmdname);
2451 goto fail;
2452 }
2453
2454 switch(nb_args) {
2455 case 0:
blueswir1a5f1b962008-08-17 20:21:51 +00002456 handler_0 = cmd->handler;
2457 handler_0();
bellard9307c4c2004-04-04 12:57:25 +00002458 break;
2459 case 1:
blueswir1a5f1b962008-08-17 20:21:51 +00002460 handler_1 = cmd->handler;
2461 handler_1(args[0]);
bellard9307c4c2004-04-04 12:57:25 +00002462 break;
2463 case 2:
blueswir1a5f1b962008-08-17 20:21:51 +00002464 handler_2 = cmd->handler;
2465 handler_2(args[0], args[1]);
bellard9307c4c2004-04-04 12:57:25 +00002466 break;
2467 case 3:
blueswir1a5f1b962008-08-17 20:21:51 +00002468 handler_3 = cmd->handler;
2469 handler_3(args[0], args[1], args[2]);
bellard9307c4c2004-04-04 12:57:25 +00002470 break;
2471 case 4:
blueswir1a5f1b962008-08-17 20:21:51 +00002472 handler_4 = cmd->handler;
2473 handler_4(args[0], args[1], args[2], args[3]);
bellard9307c4c2004-04-04 12:57:25 +00002474 break;
2475 case 5:
blueswir1a5f1b962008-08-17 20:21:51 +00002476 handler_5 = cmd->handler;
2477 handler_5(args[0], args[1], args[2], args[3], args[4]);
bellard9307c4c2004-04-04 12:57:25 +00002478 break;
bellard34405572004-06-08 00:55:58 +00002479 case 6:
blueswir1a5f1b962008-08-17 20:21:51 +00002480 handler_6 = cmd->handler;
2481 handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
bellard34405572004-06-08 00:55:58 +00002482 break;
bellardec36b692006-07-16 18:57:03 +00002483 case 7:
blueswir1a5f1b962008-08-17 20:21:51 +00002484 handler_7 = cmd->handler;
2485 handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
bellardec36b692006-07-16 18:57:03 +00002486 break;
bellard9307c4c2004-04-04 12:57:25 +00002487 default:
2488 term_printf("unsupported number of arguments: %d\n", nb_args);
2489 goto fail;
2490 }
2491 fail:
2492 for(i = 0; i < MAX_ARGS; i++)
2493 qemu_free(str_allocated[i]);
2494 return;
bellard9dc39cb2004-03-14 21:38:27 +00002495}
2496
bellard81d09122004-07-14 17:21:37 +00002497static void cmd_completion(const char *name, const char *list)
2498{
2499 const char *p, *pstart;
2500 char cmd[128];
2501 int len;
2502
2503 p = list;
2504 for(;;) {
2505 pstart = p;
2506 p = strchr(p, '|');
2507 if (!p)
2508 p = pstart + strlen(pstart);
2509 len = p - pstart;
2510 if (len > sizeof(cmd) - 2)
2511 len = sizeof(cmd) - 2;
2512 memcpy(cmd, pstart, len);
2513 cmd[len] = '\0';
2514 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2515 add_completion(cmd);
2516 }
2517 if (*p == '\0')
2518 break;
2519 p++;
2520 }
2521}
2522
2523static void file_completion(const char *input)
2524{
2525 DIR *ffs;
2526 struct dirent *d;
2527 char path[1024];
2528 char file[1024], file_prefix[1024];
2529 int input_path_len;
2530 const char *p;
2531
ths5fafdf22007-09-16 21:08:06 +00002532 p = strrchr(input, '/');
bellard81d09122004-07-14 17:21:37 +00002533 if (!p) {
2534 input_path_len = 0;
2535 pstrcpy(file_prefix, sizeof(file_prefix), input);
blueswir1363a37d2008-08-21 17:58:08 +00002536 pstrcpy(path, sizeof(path), ".");
bellard81d09122004-07-14 17:21:37 +00002537 } else {
2538 input_path_len = p - input + 1;
2539 memcpy(path, input, input_path_len);
2540 if (input_path_len > sizeof(path) - 1)
2541 input_path_len = sizeof(path) - 1;
2542 path[input_path_len] = '\0';
2543 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2544 }
2545#ifdef DEBUG_COMPLETION
2546 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2547#endif
2548 ffs = opendir(path);
2549 if (!ffs)
2550 return;
2551 for(;;) {
2552 struct stat sb;
2553 d = readdir(ffs);
2554 if (!d)
2555 break;
2556 if (strstart(d->d_name, file_prefix, NULL)) {
2557 memcpy(file, input, input_path_len);
blueswir1363a37d2008-08-21 17:58:08 +00002558 if (input_path_len < sizeof(file))
2559 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2560 d->d_name);
bellard81d09122004-07-14 17:21:37 +00002561 /* stat the file to find out if it's a directory.
2562 * In that case add a slash to speed up typing long paths
2563 */
2564 stat(file, &sb);
2565 if(S_ISDIR(sb.st_mode))
blueswir1363a37d2008-08-21 17:58:08 +00002566 pstrcat(file, sizeof(file), "/");
bellard81d09122004-07-14 17:21:37 +00002567 add_completion(file);
2568 }
2569 }
2570 closedir(ffs);
2571}
2572
2573static void block_completion_it(void *opaque, const char *name)
2574{
2575 const char *input = opaque;
2576
2577 if (input[0] == '\0' ||
2578 !strncmp(name, (char *)input, strlen(input))) {
2579 add_completion(name);
2580 }
2581}
2582
2583/* NOTE: this parser is an approximate form of the real command parser */
2584static void parse_cmdline(const char *cmdline,
2585 int *pnb_args, char **args)
2586{
2587 const char *p;
2588 int nb_args, ret;
2589 char buf[1024];
2590
2591 p = cmdline;
2592 nb_args = 0;
2593 for(;;) {
2594 while (isspace(*p))
2595 p++;
2596 if (*p == '\0')
2597 break;
2598 if (nb_args >= MAX_ARGS)
2599 break;
2600 ret = get_str(buf, sizeof(buf), &p);
2601 args[nb_args] = qemu_strdup(buf);
2602 nb_args++;
2603 if (ret < 0)
2604 break;
2605 }
2606 *pnb_args = nb_args;
2607}
2608
bellard7e2515e2004-08-01 21:52:19 +00002609void readline_find_completion(const char *cmdline)
bellard81d09122004-07-14 17:21:37 +00002610{
2611 const char *cmdname;
2612 char *args[MAX_ARGS];
2613 int nb_args, i, len;
2614 const char *ptype, *str;
blueswir18662d652008-10-02 18:32:44 +00002615 const term_cmd_t *cmd;
bellard64866c32006-05-07 18:03:31 +00002616 const KeyDef *key;
bellard81d09122004-07-14 17:21:37 +00002617
2618 parse_cmdline(cmdline, &nb_args, args);
2619#ifdef DEBUG_COMPLETION
2620 for(i = 0; i < nb_args; i++) {
2621 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2622 }
2623#endif
2624
2625 /* if the line ends with a space, it means we want to complete the
2626 next arg */
2627 len = strlen(cmdline);
2628 if (len > 0 && isspace(cmdline[len - 1])) {
2629 if (nb_args >= MAX_ARGS)
2630 return;
2631 args[nb_args++] = qemu_strdup("");
2632 }
2633 if (nb_args <= 1) {
2634 /* command completion */
2635 if (nb_args == 0)
2636 cmdname = "";
2637 else
2638 cmdname = args[0];
2639 completion_index = strlen(cmdname);
2640 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2641 cmd_completion(cmdname, cmd->name);
2642 }
2643 } else {
2644 /* find the command */
2645 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2646 if (compare_cmd(args[0], cmd->name))
2647 goto found;
2648 }
2649 return;
2650 found:
2651 ptype = cmd->args_type;
2652 for(i = 0; i < nb_args - 2; i++) {
2653 if (*ptype != '\0') {
2654 ptype++;
2655 while (*ptype == '?')
2656 ptype++;
2657 }
2658 }
2659 str = args[nb_args - 1];
2660 switch(*ptype) {
2661 case 'F':
2662 /* file completion */
2663 completion_index = strlen(str);
2664 file_completion(str);
2665 break;
2666 case 'B':
2667 /* block device name completion */
2668 completion_index = strlen(str);
2669 bdrv_iterate(block_completion_it, (void *)str);
2670 break;
bellard7fe48482004-10-09 18:08:01 +00002671 case 's':
2672 /* XXX: more generic ? */
2673 if (!strcmp(cmd->name, "info")) {
2674 completion_index = strlen(str);
2675 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2676 cmd_completion(str, cmd->name);
2677 }
bellard64866c32006-05-07 18:03:31 +00002678 } else if (!strcmp(cmd->name, "sendkey")) {
2679 completion_index = strlen(str);
2680 for(key = key_defs; key->name != NULL; key++) {
2681 cmd_completion(str, key->name);
2682 }
bellard7fe48482004-10-09 18:08:01 +00002683 }
2684 break;
bellard81d09122004-07-14 17:21:37 +00002685 default:
2686 break;
2687 }
2688 }
2689 for(i = 0; i < nb_args; i++)
2690 qemu_free(args[i]);
2691}
2692
bellard9dc39cb2004-03-14 21:38:27 +00002693static int term_can_read(void *opaque)
2694{
bellard81d09122004-07-14 17:21:37 +00002695 return 128;
bellard9dc39cb2004-03-14 21:38:27 +00002696}
2697
2698static void term_read(void *opaque, const uint8_t *buf, int size)
2699{
2700 int i;
2701 for(i = 0; i < size; i++)
bellard7e2515e2004-08-01 21:52:19 +00002702 readline_handle_byte(buf[i]);
2703}
2704
aliguorid8f44602008-10-06 13:52:44 +00002705static int monitor_suspended;
2706
bellard7e2515e2004-08-01 21:52:19 +00002707static void monitor_handle_command1(void *opaque, const char *cmdline)
2708{
2709 monitor_handle_command(cmdline);
aliguorid8f44602008-10-06 13:52:44 +00002710 if (!monitor_suspended)
2711 monitor_start_input();
2712 else
2713 monitor_suspended = 2;
2714}
2715
2716void monitor_suspend(void)
2717{
2718 monitor_suspended = 1;
2719}
2720
2721void monitor_resume(void)
2722{
2723 if (monitor_suspended == 2)
2724 monitor_start_input();
2725 monitor_suspended = 0;
bellard7e2515e2004-08-01 21:52:19 +00002726}
2727
aliguori83ab7952008-08-19 14:44:22 +00002728static void monitor_start_input(void)
bellard7e2515e2004-08-01 21:52:19 +00002729{
2730 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
bellard9dc39cb2004-03-14 21:38:27 +00002731}
2732
ths86e94de2007-01-05 22:01:59 +00002733static void term_event(void *opaque, int event)
2734{
2735 if (event != CHR_EVENT_RESET)
2736 return;
2737
2738 if (!hide_banner)
2739 term_printf("QEMU %s monitor - type 'help' for more information\n",
2740 QEMU_VERSION);
2741 monitor_start_input();
2742}
2743
ths20d8a3e2007-02-18 17:04:49 +00002744static int is_first_init = 1;
2745
bellard81d09122004-07-14 17:21:37 +00002746void monitor_init(CharDriverState *hd, int show_banner)
bellard9dc39cb2004-03-14 21:38:27 +00002747{
ths20d8a3e2007-02-18 17:04:49 +00002748 int i;
2749
2750 if (is_first_init) {
balrogc8256f92008-06-08 22:45:01 +00002751 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2752 if (!key_timer)
2753 return;
ths20d8a3e2007-02-18 17:04:49 +00002754 for (i = 0; i < MAX_MON; i++) {
2755 monitor_hd[i] = NULL;
2756 }
2757 is_first_init = 0;
2758 }
2759 for (i = 0; i < MAX_MON; i++) {
2760 if (monitor_hd[i] == NULL) {
2761 monitor_hd[i] = hd;
2762 break;
2763 }
2764 }
2765
ths86e94de2007-01-05 22:01:59 +00002766 hide_banner = !show_banner;
2767
pbrooke5b0bc42007-01-27 23:46:43 +00002768 qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
aliguori83ab7952008-08-19 14:44:22 +00002769
2770 readline_start("", 0, monitor_handle_command1, NULL);
bellard7e2515e2004-08-01 21:52:19 +00002771}
2772
2773/* XXX: use threads ? */
2774/* modal monitor readline */
2775static int monitor_readline_started;
2776static char *monitor_readline_buf;
2777static int monitor_readline_buf_size;
2778
2779static void monitor_readline_cb(void *opaque, const char *input)
2780{
2781 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2782 monitor_readline_started = 0;
2783}
2784
2785void monitor_readline(const char *prompt, int is_password,
2786 char *buf, int buf_size)
2787{
ths20d8a3e2007-02-18 17:04:49 +00002788 int i;
aliguoribc0129d2008-08-01 15:12:34 +00002789 int old_focus[MAX_MON];
ths20d8a3e2007-02-18 17:04:49 +00002790
bellard7e2515e2004-08-01 21:52:19 +00002791 if (is_password) {
aliguoribc0129d2008-08-01 15:12:34 +00002792 for (i = 0; i < MAX_MON; i++) {
2793 old_focus[i] = 0;
2794 if (monitor_hd[i]) {
2795 old_focus[i] = monitor_hd[i]->focus;
2796 monitor_hd[i]->focus = 0;
ths20d8a3e2007-02-18 17:04:49 +00002797 qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
aliguoribc0129d2008-08-01 15:12:34 +00002798 }
2799 }
bellard7e2515e2004-08-01 21:52:19 +00002800 }
aliguoribc0129d2008-08-01 15:12:34 +00002801
bellard7e2515e2004-08-01 21:52:19 +00002802 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2803 monitor_readline_buf = buf;
2804 monitor_readline_buf_size = buf_size;
2805 monitor_readline_started = 1;
2806 while (monitor_readline_started) {
2807 main_loop_wait(10);
2808 }
aliguoribc0129d2008-08-01 15:12:34 +00002809 /* restore original focus */
2810 if (is_password) {
2811 for (i = 0; i < MAX_MON; i++)
2812 if (old_focus[i])
2813 monitor_hd[i]->focus = old_focus[i];
2814 }
bellard9dc39cb2004-03-14 21:38:27 +00002815}