Statistics
| Branch: | Revision:

root / monitor.c @ 46dc3881

History | View | Annotate | Download (69.4 kB)

1 9dc39cba bellard
/*
2 9dc39cba bellard
 * QEMU monitor
3 5fafdf24 ths
 *
4 9dc39cba bellard
 * Copyright (c) 2003-2004 Fabrice Bellard
5 5fafdf24 ths
 *
6 9dc39cba bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 9dc39cba bellard
 * of this software and associated documentation files (the "Software"), to deal
8 9dc39cba bellard
 * in the Software without restriction, including without limitation the rights
9 9dc39cba bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 9dc39cba bellard
 * copies of the Software, and to permit persons to whom the Software is
11 9dc39cba bellard
 * furnished to do so, subject to the following conditions:
12 9dc39cba bellard
 *
13 9dc39cba bellard
 * The above copyright notice and this permission notice shall be included in
14 9dc39cba bellard
 * all copies or substantial portions of the Software.
15 9dc39cba bellard
 *
16 9dc39cba bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 9dc39cba bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 9dc39cba bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 9dc39cba bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 9dc39cba bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 9dc39cba bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 9dc39cba bellard
 * THE SOFTWARE.
23 9dc39cba bellard
 */
24 87ecb68b pbrook
#include "hw/hw.h"
25 87ecb68b pbrook
#include "hw/usb.h"
26 87ecb68b pbrook
#include "hw/pcmcia.h"
27 87ecb68b pbrook
#include "hw/pc.h"
28 87ecb68b pbrook
#include "hw/pci.h"
29 87ecb68b pbrook
#include "gdbstub.h"
30 87ecb68b pbrook
#include "net.h"
31 87ecb68b pbrook
#include "qemu-char.h"
32 87ecb68b pbrook
#include "sysemu.h"
33 87ecb68b pbrook
#include "console.h"
34 87ecb68b pbrook
#include "block.h"
35 87ecb68b pbrook
#include "audio/audio.h"
36 9307c4c1 bellard
#include "disas.h"
37 81d0912d bellard
#include <dirent.h>
38 9dc39cba bellard
39 6a5bd307 ths
#ifdef CONFIG_PROFILER
40 6a5bd307 ths
#include "qemu-timer.h" /* for ticks_per_sec */
41 6a5bd307 ths
#endif
42 6a5bd307 ths
43 9dc39cba bellard
//#define DEBUG
44 81d0912d bellard
//#define DEBUG_COMPLETION
45 9dc39cba bellard
46 9307c4c1 bellard
#ifndef offsetof
47 9307c4c1 bellard
#define offsetof(type, field) ((size_t) &((type *)0)->field)
48 9307c4c1 bellard
#endif
49 9307c4c1 bellard
50 9307c4c1 bellard
/*
51 9307c4c1 bellard
 * Supported types:
52 5fafdf24 ths
 *
53 9307c4c1 bellard
 * 'F'          filename
54 81d0912d bellard
 * 'B'          block device name
55 9307c4c1 bellard
 * 's'          string (accept optional quote)
56 92a31b1f bellard
 * 'i'          32 bit integer
57 92a31b1f bellard
 * 'l'          target long (32 or 64 bit)
58 9307c4c1 bellard
 * '/'          optional gdb-like print format (like "/10x")
59 9307c4c1 bellard
 *
60 9307c4c1 bellard
 * '?'          optional type (for 'F', 's' and 'i')
61 9307c4c1 bellard
 *
62 9307c4c1 bellard
 */
63 9307c4c1 bellard
64 9dc39cba bellard
typedef struct term_cmd_t {
65 9dc39cba bellard
    const char *name;
66 9307c4c1 bellard
    const char *args_type;
67 9307c4c1 bellard
    void (*handler)();
68 9dc39cba bellard
    const char *params;
69 9dc39cba bellard
    const char *help;
70 9dc39cba bellard
} term_cmd_t;
71 9dc39cba bellard
72 20d8a3ed ths
#define MAX_MON 4
73 20d8a3ed ths
static CharDriverState *monitor_hd[MAX_MON];
74 86e94dea ths
static int hide_banner;
75 7e2515e8 bellard
76 9dc39cba bellard
static term_cmd_t term_cmds[];
77 9dc39cba bellard
static term_cmd_t info_cmds[];
78 9dc39cba bellard
79 60fe76f3 ths
static uint8_t term_outbuf[1024];
80 7e2515e8 bellard
static int term_outbuf_index;
81 81d0912d bellard
82 7e2515e8 bellard
static void monitor_start_input(void);
83 7e2515e8 bellard
84 6a00d601 bellard
CPUState *mon_cpu = NULL;
85 6a00d601 bellard
86 7e2515e8 bellard
void term_flush(void)
87 7e2515e8 bellard
{
88 20d8a3ed ths
    int i;
89 7e2515e8 bellard
    if (term_outbuf_index > 0) {
90 20d8a3ed ths
        for (i = 0; i < MAX_MON; i++)
91 20d8a3ed ths
            if (monitor_hd[i] && monitor_hd[i]->focus == 0)
92 20d8a3ed ths
                qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
93 7e2515e8 bellard
        term_outbuf_index = 0;
94 7e2515e8 bellard
    }
95 7e2515e8 bellard
}
96 7e2515e8 bellard
97 7e2515e8 bellard
/* flush at every end of line or if the buffer is full */
98 7e2515e8 bellard
void term_puts(const char *str)
99 7e2515e8 bellard
{
100 60fe76f3 ths
    char c;
101 7e2515e8 bellard
    for(;;) {
102 7e2515e8 bellard
        c = *str++;
103 7e2515e8 bellard
        if (c == '\0')
104 7e2515e8 bellard
            break;
105 7ba1260a bellard
        if (c == '\n')
106 7ba1260a bellard
            term_outbuf[term_outbuf_index++] = '\r';
107 7e2515e8 bellard
        term_outbuf[term_outbuf_index++] = c;
108 7ba1260a bellard
        if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
109 7e2515e8 bellard
            c == '\n')
110 7e2515e8 bellard
            term_flush();
111 7e2515e8 bellard
    }
112 7e2515e8 bellard
}
113 7e2515e8 bellard
114 7e2515e8 bellard
void term_vprintf(const char *fmt, va_list ap)
115 9dc39cba bellard
{
116 81d0912d bellard
    char buf[4096];
117 81d0912d bellard
    vsnprintf(buf, sizeof(buf), fmt, ap);
118 7e2515e8 bellard
    term_puts(buf);
119 9dc39cba bellard
}
120 9dc39cba bellard
121 7e2515e8 bellard
void term_printf(const char *fmt, ...)
122 9dc39cba bellard
{
123 7e2515e8 bellard
    va_list ap;
124 7e2515e8 bellard
    va_start(ap, fmt);
125 7e2515e8 bellard
    term_vprintf(fmt, ap);
126 7e2515e8 bellard
    va_end(ap);
127 9dc39cba bellard
}
128 9dc39cba bellard
129 fef30743 ths
void term_print_filename(const char *filename)
130 fef30743 ths
{
131 fef30743 ths
    int i;
132 fef30743 ths
133 fef30743 ths
    for (i = 0; filename[i]; i++) {
134 fef30743 ths
        switch (filename[i]) {
135 fef30743 ths
        case ' ':
136 fef30743 ths
        case '"':
137 fef30743 ths
        case '\\':
138 fef30743 ths
            term_printf("\\%c", filename[i]);
139 fef30743 ths
            break;
140 fef30743 ths
        case '\t':
141 fef30743 ths
            term_printf("\\t");
142 fef30743 ths
            break;
143 fef30743 ths
        case '\r':
144 fef30743 ths
            term_printf("\\r");
145 fef30743 ths
            break;
146 fef30743 ths
        case '\n':
147 fef30743 ths
            term_printf("\\n");
148 fef30743 ths
            break;
149 fef30743 ths
        default:
150 fef30743 ths
            term_printf("%c", filename[i]);
151 fef30743 ths
            break;
152 fef30743 ths
        }
153 fef30743 ths
    }
154 fef30743 ths
}
155 fef30743 ths
156 7fe48483 bellard
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
157 7fe48483 bellard
{
158 7fe48483 bellard
    va_list ap;
159 7fe48483 bellard
    va_start(ap, fmt);
160 7fe48483 bellard
    term_vprintf(fmt, ap);
161 7fe48483 bellard
    va_end(ap);
162 7fe48483 bellard
    return 0;
163 7fe48483 bellard
}
164 7fe48483 bellard
165 9dc39cba bellard
static int compare_cmd(const char *name, const char *list)
166 9dc39cba bellard
{
167 9dc39cba bellard
    const char *p, *pstart;
168 9dc39cba bellard
    int len;
169 9dc39cba bellard
    len = strlen(name);
170 9dc39cba bellard
    p = list;
171 9dc39cba bellard
    for(;;) {
172 9dc39cba bellard
        pstart = p;
173 9dc39cba bellard
        p = strchr(p, '|');
174 9dc39cba bellard
        if (!p)
175 9dc39cba bellard
            p = pstart + strlen(pstart);
176 9dc39cba bellard
        if ((p - pstart) == len && !memcmp(pstart, name, len))
177 9dc39cba bellard
            return 1;
178 9dc39cba bellard
        if (*p == '\0')
179 9dc39cba bellard
            break;
180 9dc39cba bellard
        p++;
181 9dc39cba bellard
    }
182 9dc39cba bellard
    return 0;
183 9dc39cba bellard
}
184 9dc39cba bellard
185 9dc39cba bellard
static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
186 9dc39cba bellard
{
187 9dc39cba bellard
    term_cmd_t *cmd;
188 9dc39cba bellard
189 9dc39cba bellard
    for(cmd = cmds; cmd->name != NULL; cmd++) {
190 9dc39cba bellard
        if (!name || !strcmp(name, cmd->name))
191 9dc39cba bellard
            term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
192 9dc39cba bellard
    }
193 9dc39cba bellard
}
194 9dc39cba bellard
195 9dc39cba bellard
static void help_cmd(const char *name)
196 9dc39cba bellard
{
197 9dc39cba bellard
    if (name && !strcmp(name, "info")) {
198 9dc39cba bellard
        help_cmd1(info_cmds, "info ", NULL);
199 9dc39cba bellard
    } else {
200 9dc39cba bellard
        help_cmd1(term_cmds, "", name);
201 f193c797 bellard
        if (name && !strcmp(name, "log")) {
202 f193c797 bellard
            CPULogItem *item;
203 f193c797 bellard
            term_printf("Log items (comma separated):\n");
204 f193c797 bellard
            term_printf("%-10s %s\n", "none", "remove all logs");
205 f193c797 bellard
            for(item = cpu_log_items; item->mask != 0; item++) {
206 f193c797 bellard
                term_printf("%-10s %s\n", item->name, item->help);
207 f193c797 bellard
            }
208 f193c797 bellard
        }
209 9dc39cba bellard
    }
210 9dc39cba bellard
}
211 9dc39cba bellard
212 9307c4c1 bellard
static void do_help(const char *name)
213 9dc39cba bellard
{
214 9307c4c1 bellard
    help_cmd(name);
215 9dc39cba bellard
}
216 9dc39cba bellard
217 7954c734 bellard
static void do_commit(const char *device)
218 9dc39cba bellard
{
219 7954c734 bellard
    int i, all_devices;
220 2dc7b602 balrog
221 7954c734 bellard
    all_devices = !strcmp(device, "all");
222 e4bcb14c ths
    for (i = 0; i < nb_drives; i++) {
223 5fafdf24 ths
            if (all_devices ||
224 e4bcb14c ths
                !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
225 e4bcb14c ths
                bdrv_commit(drives_table[i].bdrv);
226 9dc39cba bellard
    }
227 9dc39cba bellard
}
228 9dc39cba bellard
229 9307c4c1 bellard
static void do_info(const char *item)
230 9dc39cba bellard
{
231 9dc39cba bellard
    term_cmd_t *cmd;
232 9dc39cba bellard
233 9307c4c1 bellard
    if (!item)
234 9dc39cba bellard
        goto help;
235 9dc39cba bellard
    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
236 5fafdf24 ths
        if (compare_cmd(item, cmd->name))
237 9dc39cba bellard
            goto found;
238 9dc39cba bellard
    }
239 9dc39cba bellard
 help:
240 9307c4c1 bellard
    help_cmd("info");
241 9dc39cba bellard
    return;
242 9dc39cba bellard
 found:
243 9307c4c1 bellard
    cmd->handler();
244 9dc39cba bellard
}
245 9dc39cba bellard
246 9bc9d1c7 bellard
static void do_info_version(void)
247 9bc9d1c7 bellard
{
248 9bc9d1c7 bellard
  term_printf("%s\n", QEMU_VERSION);
249 9bc9d1c7 bellard
}
250 9bc9d1c7 bellard
251 c35734b2 ths
static void do_info_name(void)
252 c35734b2 ths
{
253 c35734b2 ths
    if (qemu_name)
254 c35734b2 ths
        term_printf("%s\n", qemu_name);
255 c35734b2 ths
}
256 c35734b2 ths
257 9307c4c1 bellard
static void do_info_block(void)
258 9dc39cba bellard
{
259 9dc39cba bellard
    bdrv_info();
260 9dc39cba bellard
}
261 9dc39cba bellard
262 a36e69dd ths
static void do_info_blockstats(void)
263 a36e69dd ths
{
264 a36e69dd ths
    bdrv_info_stats();
265 a36e69dd ths
}
266 a36e69dd ths
267 6a00d601 bellard
/* get the current CPU defined by the user */
268 9596ebb7 pbrook
static int mon_set_cpu(int cpu_index)
269 6a00d601 bellard
{
270 6a00d601 bellard
    CPUState *env;
271 6a00d601 bellard
272 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
273 6a00d601 bellard
        if (env->cpu_index == cpu_index) {
274 6a00d601 bellard
            mon_cpu = env;
275 6a00d601 bellard
            return 0;
276 6a00d601 bellard
        }
277 6a00d601 bellard
    }
278 6a00d601 bellard
    return -1;
279 6a00d601 bellard
}
280 6a00d601 bellard
281 9596ebb7 pbrook
static CPUState *mon_get_cpu(void)
282 6a00d601 bellard
{
283 6a00d601 bellard
    if (!mon_cpu) {
284 6a00d601 bellard
        mon_set_cpu(0);
285 6a00d601 bellard
    }
286 6a00d601 bellard
    return mon_cpu;
287 6a00d601 bellard
}
288 6a00d601 bellard
289 9307c4c1 bellard
static void do_info_registers(void)
290 9307c4c1 bellard
{
291 6a00d601 bellard
    CPUState *env;
292 6a00d601 bellard
    env = mon_get_cpu();
293 6a00d601 bellard
    if (!env)
294 6a00d601 bellard
        return;
295 9307c4c1 bellard
#ifdef TARGET_I386
296 6a00d601 bellard
    cpu_dump_state(env, NULL, monitor_fprintf,
297 d24b15a8 bellard
                   X86_DUMP_FPU);
298 9307c4c1 bellard
#else
299 5fafdf24 ths
    cpu_dump_state(env, NULL, monitor_fprintf,
300 7fe48483 bellard
                   0);
301 9307c4c1 bellard
#endif
302 9307c4c1 bellard
}
303 9307c4c1 bellard
304 6a00d601 bellard
static void do_info_cpus(void)
305 6a00d601 bellard
{
306 6a00d601 bellard
    CPUState *env;
307 6a00d601 bellard
308 6a00d601 bellard
    /* just to set the default cpu if not already done */
309 6a00d601 bellard
    mon_get_cpu();
310 6a00d601 bellard
311 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
312 5fafdf24 ths
        term_printf("%c CPU #%d:",
313 6a00d601 bellard
                    (env == mon_cpu) ? '*' : ' ',
314 6a00d601 bellard
                    env->cpu_index);
315 6a00d601 bellard
#if defined(TARGET_I386)
316 6a00d601 bellard
        term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
317 e80e1cc4 bellard
#elif defined(TARGET_PPC)
318 e80e1cc4 bellard
        term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
319 ba3c64fb bellard
#elif defined(TARGET_SPARC)
320 ba3c64fb bellard
        term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
321 ead9360e ths
#elif defined(TARGET_MIPS)
322 ead9360e ths
        term_printf(" PC=0x" TARGET_FMT_lx, env->PC[env->current_tc]);
323 ce5232c5 bellard
#endif
324 ead9360e ths
        if (env->halted)
325 ead9360e ths
            term_printf(" (halted)");
326 6a00d601 bellard
        term_printf("\n");
327 6a00d601 bellard
    }
328 6a00d601 bellard
}
329 6a00d601 bellard
330 6a00d601 bellard
static void do_cpu_set(int index)
331 6a00d601 bellard
{
332 6a00d601 bellard
    if (mon_set_cpu(index) < 0)
333 6a00d601 bellard
        term_printf("Invalid CPU index\n");
334 6a00d601 bellard
}
335 6a00d601 bellard
336 e3db7226 bellard
static void do_info_jit(void)
337 e3db7226 bellard
{
338 e3db7226 bellard
    dump_exec_info(NULL, monitor_fprintf);
339 e3db7226 bellard
}
340 e3db7226 bellard
341 aa455485 bellard
static void do_info_history (void)
342 aa455485 bellard
{
343 aa455485 bellard
    int i;
344 7e2515e8 bellard
    const char *str;
345 3b46e624 ths
346 7e2515e8 bellard
    i = 0;
347 7e2515e8 bellard
    for(;;) {
348 7e2515e8 bellard
        str = readline_get_history(i);
349 7e2515e8 bellard
        if (!str)
350 7e2515e8 bellard
            break;
351 7e2515e8 bellard
        term_printf("%d: '%s'\n", i, str);
352 8e3a9fd2 bellard
        i++;
353 aa455485 bellard
    }
354 aa455485 bellard
}
355 aa455485 bellard
356 76a66253 j_mayer
#if defined(TARGET_PPC)
357 76a66253 j_mayer
/* XXX: not implemented in other targets */
358 76a66253 j_mayer
static void do_info_cpu_stats (void)
359 76a66253 j_mayer
{
360 76a66253 j_mayer
    CPUState *env;
361 76a66253 j_mayer
362 76a66253 j_mayer
    env = mon_get_cpu();
363 76a66253 j_mayer
    cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
364 76a66253 j_mayer
}
365 76a66253 j_mayer
#endif
366 76a66253 j_mayer
367 9307c4c1 bellard
static void do_quit(void)
368 9dc39cba bellard
{
369 9dc39cba bellard
    exit(0);
370 9dc39cba bellard
}
371 9dc39cba bellard
372 9dc39cba bellard
static int eject_device(BlockDriverState *bs, int force)
373 9dc39cba bellard
{
374 9dc39cba bellard
    if (bdrv_is_inserted(bs)) {
375 9dc39cba bellard
        if (!force) {
376 9dc39cba bellard
            if (!bdrv_is_removable(bs)) {
377 9dc39cba bellard
                term_printf("device is not removable\n");
378 9dc39cba bellard
                return -1;
379 9dc39cba bellard
            }
380 9dc39cba bellard
            if (bdrv_is_locked(bs)) {
381 9dc39cba bellard
                term_printf("device is locked\n");
382 9dc39cba bellard
                return -1;
383 9dc39cba bellard
            }
384 9dc39cba bellard
        }
385 9dc39cba bellard
        bdrv_close(bs);
386 9dc39cba bellard
    }
387 9dc39cba bellard
    return 0;
388 9dc39cba bellard
}
389 9dc39cba bellard
390 9307c4c1 bellard
static void do_eject(int force, const char *filename)
391 9dc39cba bellard
{
392 9dc39cba bellard
    BlockDriverState *bs;
393 9dc39cba bellard
394 9307c4c1 bellard
    bs = bdrv_find(filename);
395 9dc39cba bellard
    if (!bs) {
396 9dc39cba bellard
        term_printf("device not found\n");
397 9dc39cba bellard
        return;
398 9dc39cba bellard
    }
399 9dc39cba bellard
    eject_device(bs, force);
400 9dc39cba bellard
}
401 9dc39cba bellard
402 e25a5822 ths
static void do_change_block(const char *device, const char *filename)
403 9dc39cba bellard
{
404 9dc39cba bellard
    BlockDriverState *bs;
405 9dc39cba bellard
406 9307c4c1 bellard
    bs = bdrv_find(device);
407 9dc39cba bellard
    if (!bs) {
408 9dc39cba bellard
        term_printf("device not found\n");
409 9dc39cba bellard
        return;
410 9dc39cba bellard
    }
411 9dc39cba bellard
    if (eject_device(bs, 0) < 0)
412 9dc39cba bellard
        return;
413 9307c4c1 bellard
    bdrv_open(bs, filename, 0);
414 2bac6019 balrog
    qemu_key_check(bs, filename);
415 9dc39cba bellard
}
416 9dc39cba bellard
417 e25a5822 ths
static void do_change_vnc(const char *target)
418 e25a5822 ths
{
419 70848515 ths
    if (strcmp(target, "passwd") == 0 ||
420 70848515 ths
        strcmp(target, "password") == 0) {
421 70848515 ths
        char password[9];
422 70848515 ths
        monitor_readline("Password: ", 1, password, sizeof(password)-1);
423 70848515 ths
        password[sizeof(password)-1] = '\0';
424 70848515 ths
        if (vnc_display_password(NULL, password) < 0)
425 70848515 ths
            term_printf("could not set VNC server password\n");
426 70848515 ths
    } else {
427 70848515 ths
        if (vnc_display_open(NULL, target) < 0)
428 70848515 ths
            term_printf("could not start VNC server on %s\n", target);
429 70848515 ths
    }
430 e25a5822 ths
}
431 e25a5822 ths
432 e25a5822 ths
static void do_change(const char *device, const char *target)
433 e25a5822 ths
{
434 e25a5822 ths
    if (strcmp(device, "vnc") == 0) {
435 e25a5822 ths
        do_change_vnc(target);
436 e25a5822 ths
    } else {
437 e25a5822 ths
        do_change_block(device, target);
438 e25a5822 ths
    }
439 e25a5822 ths
}
440 e25a5822 ths
441 9307c4c1 bellard
static void do_screen_dump(const char *filename)
442 59a983b9 bellard
{
443 95219897 pbrook
    vga_hw_screen_dump(filename);
444 59a983b9 bellard
}
445 59a983b9 bellard
446 e735b91c pbrook
static void do_logfile(const char *filename)
447 e735b91c pbrook
{
448 e735b91c pbrook
    cpu_set_log_filename(filename);
449 e735b91c pbrook
}
450 e735b91c pbrook
451 9307c4c1 bellard
static void do_log(const char *items)
452 f193c797 bellard
{
453 f193c797 bellard
    int mask;
454 3b46e624 ths
455 9307c4c1 bellard
    if (!strcmp(items, "none")) {
456 f193c797 bellard
        mask = 0;
457 f193c797 bellard
    } else {
458 9307c4c1 bellard
        mask = cpu_str_to_log_mask(items);
459 f193c797 bellard
        if (!mask) {
460 9307c4c1 bellard
            help_cmd("log");
461 f193c797 bellard
            return;
462 f193c797 bellard
        }
463 f193c797 bellard
    }
464 f193c797 bellard
    cpu_set_log(mask);
465 f193c797 bellard
}
466 f193c797 bellard
467 9307c4c1 bellard
static void do_stop(void)
468 8a7ddc38 bellard
{
469 8a7ddc38 bellard
    vm_stop(EXCP_INTERRUPT);
470 8a7ddc38 bellard
}
471 8a7ddc38 bellard
472 9307c4c1 bellard
static void do_cont(void)
473 8a7ddc38 bellard
{
474 8a7ddc38 bellard
    vm_start();
475 8a7ddc38 bellard
}
476 8a7ddc38 bellard
477 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
478 cfc3475a pbrook
static void do_gdbserver(const char *port)
479 8a7ddc38 bellard
{
480 cfc3475a pbrook
    if (!port)
481 9307c4c1 bellard
        port = DEFAULT_GDBSTUB_PORT;
482 cfc3475a pbrook
    if (gdbserver_start(port) < 0) {
483 cfc3475a pbrook
        qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
484 8a7ddc38 bellard
    } else {
485 cfc3475a pbrook
        qemu_printf("Waiting gdb connection on port '%s'\n", port);
486 8a7ddc38 bellard
    }
487 8a7ddc38 bellard
}
488 67b915a5 bellard
#endif
489 8a7ddc38 bellard
490 9307c4c1 bellard
static void term_printc(int c)
491 9307c4c1 bellard
{
492 9307c4c1 bellard
    term_printf("'");
493 9307c4c1 bellard
    switch(c) {
494 9307c4c1 bellard
    case '\'':
495 9307c4c1 bellard
        term_printf("\\'");
496 9307c4c1 bellard
        break;
497 9307c4c1 bellard
    case '\\':
498 9307c4c1 bellard
        term_printf("\\\\");
499 9307c4c1 bellard
        break;
500 9307c4c1 bellard
    case '\n':
501 9307c4c1 bellard
        term_printf("\\n");
502 9307c4c1 bellard
        break;
503 9307c4c1 bellard
    case '\r':
504 9307c4c1 bellard
        term_printf("\\r");
505 9307c4c1 bellard
        break;
506 9307c4c1 bellard
    default:
507 9307c4c1 bellard
        if (c >= 32 && c <= 126) {
508 9307c4c1 bellard
            term_printf("%c", c);
509 9307c4c1 bellard
        } else {
510 9307c4c1 bellard
            term_printf("\\x%02x", c);
511 9307c4c1 bellard
        }
512 9307c4c1 bellard
        break;
513 9307c4c1 bellard
    }
514 9307c4c1 bellard
    term_printf("'");
515 9307c4c1 bellard
}
516 9307c4c1 bellard
517 5fafdf24 ths
static void memory_dump(int count, int format, int wsize,
518 7743e588 blueswir1
                        target_phys_addr_t addr, int is_physical)
519 9307c4c1 bellard
{
520 6a00d601 bellard
    CPUState *env;
521 9307c4c1 bellard
    int nb_per_line, l, line_size, i, max_digits, len;
522 9307c4c1 bellard
    uint8_t buf[16];
523 9307c4c1 bellard
    uint64_t v;
524 9307c4c1 bellard
525 9307c4c1 bellard
    if (format == 'i') {
526 9307c4c1 bellard
        int flags;
527 9307c4c1 bellard
        flags = 0;
528 6a00d601 bellard
        env = mon_get_cpu();
529 6a00d601 bellard
        if (!env && !is_physical)
530 6a00d601 bellard
            return;
531 9307c4c1 bellard
#ifdef TARGET_I386
532 4c27ba27 bellard
        if (wsize == 2) {
533 9307c4c1 bellard
            flags = 1;
534 4c27ba27 bellard
        } else if (wsize == 4) {
535 4c27ba27 bellard
            flags = 0;
536 4c27ba27 bellard
        } else {
537 6a15fd12 bellard
            /* as default we use the current CS size */
538 4c27ba27 bellard
            flags = 0;
539 6a15fd12 bellard
            if (env) {
540 6a15fd12 bellard
#ifdef TARGET_X86_64
541 5fafdf24 ths
                if ((env->efer & MSR_EFER_LMA) &&
542 6a15fd12 bellard
                    (env->segs[R_CS].flags & DESC_L_MASK))
543 6a15fd12 bellard
                    flags = 2;
544 6a15fd12 bellard
                else
545 6a15fd12 bellard
#endif
546 6a15fd12 bellard
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
547 6a15fd12 bellard
                    flags = 1;
548 6a15fd12 bellard
            }
549 4c27ba27 bellard
        }
550 4c27ba27 bellard
#endif
551 6a00d601 bellard
        monitor_disas(env, addr, count, is_physical, flags);
552 9307c4c1 bellard
        return;
553 9307c4c1 bellard
    }
554 9307c4c1 bellard
555 9307c4c1 bellard
    len = wsize * count;
556 9307c4c1 bellard
    if (wsize == 1)
557 9307c4c1 bellard
        line_size = 8;
558 9307c4c1 bellard
    else
559 9307c4c1 bellard
        line_size = 16;
560 9307c4c1 bellard
    nb_per_line = line_size / wsize;
561 9307c4c1 bellard
    max_digits = 0;
562 9307c4c1 bellard
563 9307c4c1 bellard
    switch(format) {
564 9307c4c1 bellard
    case 'o':
565 9307c4c1 bellard
        max_digits = (wsize * 8 + 2) / 3;
566 9307c4c1 bellard
        break;
567 9307c4c1 bellard
    default:
568 9307c4c1 bellard
    case 'x':
569 9307c4c1 bellard
        max_digits = (wsize * 8) / 4;
570 9307c4c1 bellard
        break;
571 9307c4c1 bellard
    case 'u':
572 9307c4c1 bellard
    case 'd':
573 9307c4c1 bellard
        max_digits = (wsize * 8 * 10 + 32) / 33;
574 9307c4c1 bellard
        break;
575 9307c4c1 bellard
    case 'c':
576 9307c4c1 bellard
        wsize = 1;
577 9307c4c1 bellard
        break;
578 9307c4c1 bellard
    }
579 9307c4c1 bellard
580 9307c4c1 bellard
    while (len > 0) {
581 7743e588 blueswir1
        if (is_physical)
582 7743e588 blueswir1
            term_printf(TARGET_FMT_plx ":", addr);
583 7743e588 blueswir1
        else
584 7743e588 blueswir1
            term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
585 9307c4c1 bellard
        l = len;
586 9307c4c1 bellard
        if (l > line_size)
587 9307c4c1 bellard
            l = line_size;
588 9307c4c1 bellard
        if (is_physical) {
589 9307c4c1 bellard
            cpu_physical_memory_rw(addr, buf, l, 0);
590 9307c4c1 bellard
        } else {
591 6a00d601 bellard
            env = mon_get_cpu();
592 6a00d601 bellard
            if (!env)
593 6a00d601 bellard
                break;
594 6a00d601 bellard
            cpu_memory_rw_debug(env, addr, buf, l, 0);
595 9307c4c1 bellard
        }
596 5fafdf24 ths
        i = 0;
597 9307c4c1 bellard
        while (i < l) {
598 9307c4c1 bellard
            switch(wsize) {
599 9307c4c1 bellard
            default:
600 9307c4c1 bellard
            case 1:
601 9307c4c1 bellard
                v = ldub_raw(buf + i);
602 9307c4c1 bellard
                break;
603 9307c4c1 bellard
            case 2:
604 9307c4c1 bellard
                v = lduw_raw(buf + i);
605 9307c4c1 bellard
                break;
606 9307c4c1 bellard
            case 4:
607 92a31b1f bellard
                v = (uint32_t)ldl_raw(buf + i);
608 9307c4c1 bellard
                break;
609 9307c4c1 bellard
            case 8:
610 9307c4c1 bellard
                v = ldq_raw(buf + i);
611 9307c4c1 bellard
                break;
612 9307c4c1 bellard
            }
613 9307c4c1 bellard
            term_printf(" ");
614 9307c4c1 bellard
            switch(format) {
615 9307c4c1 bellard
            case 'o':
616 26a76461 bellard
                term_printf("%#*" PRIo64, max_digits, v);
617 9307c4c1 bellard
                break;
618 9307c4c1 bellard
            case 'x':
619 26a76461 bellard
                term_printf("0x%0*" PRIx64, max_digits, v);
620 9307c4c1 bellard
                break;
621 9307c4c1 bellard
            case 'u':
622 26a76461 bellard
                term_printf("%*" PRIu64, max_digits, v);
623 9307c4c1 bellard
                break;
624 9307c4c1 bellard
            case 'd':
625 26a76461 bellard
                term_printf("%*" PRId64, max_digits, v);
626 9307c4c1 bellard
                break;
627 9307c4c1 bellard
            case 'c':
628 9307c4c1 bellard
                term_printc(v);
629 9307c4c1 bellard
                break;
630 9307c4c1 bellard
            }
631 9307c4c1 bellard
            i += wsize;
632 9307c4c1 bellard
        }
633 9307c4c1 bellard
        term_printf("\n");
634 9307c4c1 bellard
        addr += l;
635 9307c4c1 bellard
        len -= l;
636 9307c4c1 bellard
    }
637 9307c4c1 bellard
}
638 9307c4c1 bellard
639 92a31b1f bellard
#if TARGET_LONG_BITS == 64
640 92a31b1f bellard
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
641 92a31b1f bellard
#else
642 92a31b1f bellard
#define GET_TLONG(h, l) (l)
643 92a31b1f bellard
#endif
644 92a31b1f bellard
645 5fafdf24 ths
static void do_memory_dump(int count, int format, int size,
646 92a31b1f bellard
                           uint32_t addrh, uint32_t addrl)
647 9307c4c1 bellard
{
648 92a31b1f bellard
    target_long addr = GET_TLONG(addrh, addrl);
649 9307c4c1 bellard
    memory_dump(count, format, size, addr, 0);
650 9307c4c1 bellard
}
651 9307c4c1 bellard
652 7743e588 blueswir1
#if TARGET_PHYS_ADDR_BITS > 32
653 7743e588 blueswir1
#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
654 7743e588 blueswir1
#else
655 7743e588 blueswir1
#define GET_TPHYSADDR(h, l) (l)
656 7743e588 blueswir1
#endif
657 7743e588 blueswir1
658 92a31b1f bellard
static void do_physical_memory_dump(int count, int format, int size,
659 92a31b1f bellard
                                    uint32_t addrh, uint32_t addrl)
660 92a31b1f bellard
661 9307c4c1 bellard
{
662 7743e588 blueswir1
    target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
663 9307c4c1 bellard
    memory_dump(count, format, size, addr, 1);
664 9307c4c1 bellard
}
665 9307c4c1 bellard
666 92a31b1f bellard
static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
667 9307c4c1 bellard
{
668 7743e588 blueswir1
    target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
669 7743e588 blueswir1
#if TARGET_PHYS_ADDR_BITS == 32
670 9307c4c1 bellard
    switch(format) {
671 9307c4c1 bellard
    case 'o':
672 9307c4c1 bellard
        term_printf("%#o", val);
673 9307c4c1 bellard
        break;
674 9307c4c1 bellard
    case 'x':
675 9307c4c1 bellard
        term_printf("%#x", val);
676 9307c4c1 bellard
        break;
677 9307c4c1 bellard
    case 'u':
678 9307c4c1 bellard
        term_printf("%u", val);
679 9307c4c1 bellard
        break;
680 9307c4c1 bellard
    default:
681 9307c4c1 bellard
    case 'd':
682 9307c4c1 bellard
        term_printf("%d", val);
683 9307c4c1 bellard
        break;
684 9307c4c1 bellard
    case 'c':
685 9307c4c1 bellard
        term_printc(val);
686 9307c4c1 bellard
        break;
687 9307c4c1 bellard
    }
688 92a31b1f bellard
#else
689 92a31b1f bellard
    switch(format) {
690 92a31b1f bellard
    case 'o':
691 26a76461 bellard
        term_printf("%#" PRIo64, val);
692 92a31b1f bellard
        break;
693 92a31b1f bellard
    case 'x':
694 26a76461 bellard
        term_printf("%#" PRIx64, val);
695 92a31b1f bellard
        break;
696 92a31b1f bellard
    case 'u':
697 26a76461 bellard
        term_printf("%" PRIu64, val);
698 92a31b1f bellard
        break;
699 92a31b1f bellard
    default:
700 92a31b1f bellard
    case 'd':
701 26a76461 bellard
        term_printf("%" PRId64, val);
702 92a31b1f bellard
        break;
703 92a31b1f bellard
    case 'c':
704 92a31b1f bellard
        term_printc(val);
705 92a31b1f bellard
        break;
706 92a31b1f bellard
    }
707 92a31b1f bellard
#endif
708 9307c4c1 bellard
    term_printf("\n");
709 9307c4c1 bellard
}
710 9307c4c1 bellard
711 5fafdf24 ths
static void do_memory_save(unsigned int valh, unsigned int vall,
712 b371dc59 bellard
                           uint32_t size, const char *filename)
713 b371dc59 bellard
{
714 b371dc59 bellard
    FILE *f;
715 b371dc59 bellard
    target_long addr = GET_TLONG(valh, vall);
716 b371dc59 bellard
    uint32_t l;
717 b371dc59 bellard
    CPUState *env;
718 b371dc59 bellard
    uint8_t buf[1024];
719 b371dc59 bellard
720 b371dc59 bellard
    env = mon_get_cpu();
721 b371dc59 bellard
    if (!env)
722 b371dc59 bellard
        return;
723 b371dc59 bellard
724 b371dc59 bellard
    f = fopen(filename, "wb");
725 b371dc59 bellard
    if (!f) {
726 b371dc59 bellard
        term_printf("could not open '%s'\n", filename);
727 b371dc59 bellard
        return;
728 b371dc59 bellard
    }
729 b371dc59 bellard
    while (size != 0) {
730 b371dc59 bellard
        l = sizeof(buf);
731 b371dc59 bellard
        if (l > size)
732 b371dc59 bellard
            l = size;
733 b371dc59 bellard
        cpu_memory_rw_debug(env, addr, buf, l, 0);
734 b371dc59 bellard
        fwrite(buf, 1, l, f);
735 b371dc59 bellard
        addr += l;
736 b371dc59 bellard
        size -= l;
737 b371dc59 bellard
    }
738 b371dc59 bellard
    fclose(f);
739 b371dc59 bellard
}
740 b371dc59 bellard
741 a8bdf7a6 aurel32
static void do_physical_memory_save(unsigned int valh, unsigned int vall,
742 a8bdf7a6 aurel32
                                    uint32_t size, const char *filename)
743 a8bdf7a6 aurel32
{
744 a8bdf7a6 aurel32
    FILE *f;
745 a8bdf7a6 aurel32
    uint32_t l;
746 a8bdf7a6 aurel32
    uint8_t buf[1024];
747 339dea27 aurel32
    target_phys_addr_t addr = GET_TPHYSADDR(valh, vall); 
748 a8bdf7a6 aurel32
749 a8bdf7a6 aurel32
    f = fopen(filename, "wb");
750 a8bdf7a6 aurel32
    if (!f) {
751 a8bdf7a6 aurel32
        term_printf("could not open '%s'\n", filename);
752 a8bdf7a6 aurel32
        return;
753 a8bdf7a6 aurel32
    }
754 a8bdf7a6 aurel32
    while (size != 0) {
755 a8bdf7a6 aurel32
        l = sizeof(buf);
756 a8bdf7a6 aurel32
        if (l > size)
757 a8bdf7a6 aurel32
            l = size;
758 a8bdf7a6 aurel32
        cpu_physical_memory_rw(addr, buf, l, 0);
759 a8bdf7a6 aurel32
        fwrite(buf, 1, l, f);
760 a8bdf7a6 aurel32
        fflush(f);
761 a8bdf7a6 aurel32
        addr += l;
762 a8bdf7a6 aurel32
        size -= l;
763 a8bdf7a6 aurel32
    }
764 a8bdf7a6 aurel32
    fclose(f);
765 a8bdf7a6 aurel32
}
766 a8bdf7a6 aurel32
767 e4cf1adc bellard
static void do_sum(uint32_t start, uint32_t size)
768 e4cf1adc bellard
{
769 e4cf1adc bellard
    uint32_t addr;
770 e4cf1adc bellard
    uint8_t buf[1];
771 e4cf1adc bellard
    uint16_t sum;
772 e4cf1adc bellard
773 e4cf1adc bellard
    sum = 0;
774 e4cf1adc bellard
    for(addr = start; addr < (start + size); addr++) {
775 e4cf1adc bellard
        cpu_physical_memory_rw(addr, buf, 1, 0);
776 e4cf1adc bellard
        /* BSD sum algorithm ('sum' Unix command) */
777 e4cf1adc bellard
        sum = (sum >> 1) | (sum << 15);
778 e4cf1adc bellard
        sum += buf[0];
779 e4cf1adc bellard
    }
780 e4cf1adc bellard
    term_printf("%05d\n", sum);
781 e4cf1adc bellard
}
782 e4cf1adc bellard
783 a3a91a35 bellard
typedef struct {
784 a3a91a35 bellard
    int keycode;
785 a3a91a35 bellard
    const char *name;
786 a3a91a35 bellard
} KeyDef;
787 a3a91a35 bellard
788 a3a91a35 bellard
static const KeyDef key_defs[] = {
789 a3a91a35 bellard
    { 0x2a, "shift" },
790 a3a91a35 bellard
    { 0x36, "shift_r" },
791 3b46e624 ths
792 a3a91a35 bellard
    { 0x38, "alt" },
793 a3a91a35 bellard
    { 0xb8, "alt_r" },
794 a3a91a35 bellard
    { 0x1d, "ctrl" },
795 a3a91a35 bellard
    { 0x9d, "ctrl_r" },
796 a3a91a35 bellard
797 a3a91a35 bellard
    { 0xdd, "menu" },
798 a3a91a35 bellard
799 a3a91a35 bellard
    { 0x01, "esc" },
800 a3a91a35 bellard
801 a3a91a35 bellard
    { 0x02, "1" },
802 a3a91a35 bellard
    { 0x03, "2" },
803 a3a91a35 bellard
    { 0x04, "3" },
804 a3a91a35 bellard
    { 0x05, "4" },
805 a3a91a35 bellard
    { 0x06, "5" },
806 a3a91a35 bellard
    { 0x07, "6" },
807 a3a91a35 bellard
    { 0x08, "7" },
808 a3a91a35 bellard
    { 0x09, "8" },
809 a3a91a35 bellard
    { 0x0a, "9" },
810 a3a91a35 bellard
    { 0x0b, "0" },
811 64866c3d bellard
    { 0x0c, "minus" },
812 64866c3d bellard
    { 0x0d, "equal" },
813 a3a91a35 bellard
    { 0x0e, "backspace" },
814 a3a91a35 bellard
815 a3a91a35 bellard
    { 0x0f, "tab" },
816 a3a91a35 bellard
    { 0x10, "q" },
817 a3a91a35 bellard
    { 0x11, "w" },
818 a3a91a35 bellard
    { 0x12, "e" },
819 a3a91a35 bellard
    { 0x13, "r" },
820 a3a91a35 bellard
    { 0x14, "t" },
821 a3a91a35 bellard
    { 0x15, "y" },
822 a3a91a35 bellard
    { 0x16, "u" },
823 a3a91a35 bellard
    { 0x17, "i" },
824 a3a91a35 bellard
    { 0x18, "o" },
825 a3a91a35 bellard
    { 0x19, "p" },
826 a3a91a35 bellard
827 a3a91a35 bellard
    { 0x1c, "ret" },
828 a3a91a35 bellard
829 a3a91a35 bellard
    { 0x1e, "a" },
830 a3a91a35 bellard
    { 0x1f, "s" },
831 a3a91a35 bellard
    { 0x20, "d" },
832 a3a91a35 bellard
    { 0x21, "f" },
833 a3a91a35 bellard
    { 0x22, "g" },
834 a3a91a35 bellard
    { 0x23, "h" },
835 a3a91a35 bellard
    { 0x24, "j" },
836 a3a91a35 bellard
    { 0x25, "k" },
837 a3a91a35 bellard
    { 0x26, "l" },
838 a3a91a35 bellard
839 a3a91a35 bellard
    { 0x2c, "z" },
840 a3a91a35 bellard
    { 0x2d, "x" },
841 a3a91a35 bellard
    { 0x2e, "c" },
842 a3a91a35 bellard
    { 0x2f, "v" },
843 a3a91a35 bellard
    { 0x30, "b" },
844 a3a91a35 bellard
    { 0x31, "n" },
845 a3a91a35 bellard
    { 0x32, "m" },
846 3b46e624 ths
847 4d3b6f6e balrog
    { 0x37, "asterisk" },
848 4d3b6f6e balrog
849 a3a91a35 bellard
    { 0x39, "spc" },
850 00ffa62a bellard
    { 0x3a, "caps_lock" },
851 a3a91a35 bellard
    { 0x3b, "f1" },
852 a3a91a35 bellard
    { 0x3c, "f2" },
853 a3a91a35 bellard
    { 0x3d, "f3" },
854 a3a91a35 bellard
    { 0x3e, "f4" },
855 a3a91a35 bellard
    { 0x3f, "f5" },
856 a3a91a35 bellard
    { 0x40, "f6" },
857 a3a91a35 bellard
    { 0x41, "f7" },
858 a3a91a35 bellard
    { 0x42, "f8" },
859 a3a91a35 bellard
    { 0x43, "f9" },
860 a3a91a35 bellard
    { 0x44, "f10" },
861 00ffa62a bellard
    { 0x45, "num_lock" },
862 a3a91a35 bellard
    { 0x46, "scroll_lock" },
863 a3a91a35 bellard
864 64866c3d bellard
    { 0xb5, "kp_divide" },
865 64866c3d bellard
    { 0x37, "kp_multiply" },
866 0cfec834 ths
    { 0x4a, "kp_subtract" },
867 64866c3d bellard
    { 0x4e, "kp_add" },
868 64866c3d bellard
    { 0x9c, "kp_enter" },
869 64866c3d bellard
    { 0x53, "kp_decimal" },
870 64866c3d bellard
871 64866c3d bellard
    { 0x52, "kp_0" },
872 64866c3d bellard
    { 0x4f, "kp_1" },
873 64866c3d bellard
    { 0x50, "kp_2" },
874 64866c3d bellard
    { 0x51, "kp_3" },
875 64866c3d bellard
    { 0x4b, "kp_4" },
876 64866c3d bellard
    { 0x4c, "kp_5" },
877 64866c3d bellard
    { 0x4d, "kp_6" },
878 64866c3d bellard
    { 0x47, "kp_7" },
879 64866c3d bellard
    { 0x48, "kp_8" },
880 64866c3d bellard
    { 0x49, "kp_9" },
881 3b46e624 ths
882 a3a91a35 bellard
    { 0x56, "<" },
883 a3a91a35 bellard
884 a3a91a35 bellard
    { 0x57, "f11" },
885 a3a91a35 bellard
    { 0x58, "f12" },
886 a3a91a35 bellard
887 a3a91a35 bellard
    { 0xb7, "print" },
888 a3a91a35 bellard
889 a3a91a35 bellard
    { 0xc7, "home" },
890 a3a91a35 bellard
    { 0xc9, "pgup" },
891 a3a91a35 bellard
    { 0xd1, "pgdn" },
892 a3a91a35 bellard
    { 0xcf, "end" },
893 a3a91a35 bellard
894 a3a91a35 bellard
    { 0xcb, "left" },
895 a3a91a35 bellard
    { 0xc8, "up" },
896 a3a91a35 bellard
    { 0xd0, "down" },
897 a3a91a35 bellard
    { 0xcd, "right" },
898 a3a91a35 bellard
899 a3a91a35 bellard
    { 0xd2, "insert" },
900 a3a91a35 bellard
    { 0xd3, "delete" },
901 a3a91a35 bellard
    { 0, NULL },
902 a3a91a35 bellard
};
903 a3a91a35 bellard
904 a3a91a35 bellard
static int get_keycode(const char *key)
905 a3a91a35 bellard
{
906 a3a91a35 bellard
    const KeyDef *p;
907 64866c3d bellard
    char *endp;
908 64866c3d bellard
    int ret;
909 a3a91a35 bellard
910 a3a91a35 bellard
    for(p = key_defs; p->name != NULL; p++) {
911 a3a91a35 bellard
        if (!strcmp(key, p->name))
912 a3a91a35 bellard
            return p->keycode;
913 a3a91a35 bellard
    }
914 64866c3d bellard
    if (strstart(key, "0x", NULL)) {
915 64866c3d bellard
        ret = strtoul(key, &endp, 0);
916 64866c3d bellard
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
917 64866c3d bellard
            return ret;
918 64866c3d bellard
    }
919 a3a91a35 bellard
    return -1;
920 a3a91a35 bellard
}
921 a3a91a35 bellard
922 a3a91a35 bellard
static void do_send_key(const char *string)
923 a3a91a35 bellard
{
924 a3a91a35 bellard
    char keybuf[16], *q;
925 a3a91a35 bellard
    uint8_t keycodes[16];
926 a3a91a35 bellard
    const char *p;
927 a3a91a35 bellard
    int nb_keycodes, keycode, i;
928 3b46e624 ths
929 a3a91a35 bellard
    nb_keycodes = 0;
930 a3a91a35 bellard
    p = string;
931 a3a91a35 bellard
    while (*p != '\0') {
932 a3a91a35 bellard
        q = keybuf;
933 a3a91a35 bellard
        while (*p != '\0' && *p != '-') {
934 a3a91a35 bellard
            if ((q - keybuf) < sizeof(keybuf) - 1) {
935 a3a91a35 bellard
                *q++ = *p;
936 a3a91a35 bellard
            }
937 a3a91a35 bellard
            p++;
938 a3a91a35 bellard
        }
939 a3a91a35 bellard
        *q = '\0';
940 a3a91a35 bellard
        keycode = get_keycode(keybuf);
941 a3a91a35 bellard
        if (keycode < 0) {
942 a3a91a35 bellard
            term_printf("unknown key: '%s'\n", keybuf);
943 a3a91a35 bellard
            return;
944 a3a91a35 bellard
        }
945 a3a91a35 bellard
        keycodes[nb_keycodes++] = keycode;
946 a3a91a35 bellard
        if (*p == '\0')
947 a3a91a35 bellard
            break;
948 a3a91a35 bellard
        p++;
949 a3a91a35 bellard
    }
950 a3a91a35 bellard
    /* key down events */
951 a3a91a35 bellard
    for(i = 0; i < nb_keycodes; i++) {
952 a3a91a35 bellard
        keycode = keycodes[i];
953 a3a91a35 bellard
        if (keycode & 0x80)
954 a3a91a35 bellard
            kbd_put_keycode(0xe0);
955 a3a91a35 bellard
        kbd_put_keycode(keycode & 0x7f);
956 a3a91a35 bellard
    }
957 a3a91a35 bellard
    /* key up events */
958 a3a91a35 bellard
    for(i = nb_keycodes - 1; i >= 0; i--) {
959 a3a91a35 bellard
        keycode = keycodes[i];
960 a3a91a35 bellard
        if (keycode & 0x80)
961 a3a91a35 bellard
            kbd_put_keycode(0xe0);
962 a3a91a35 bellard
        kbd_put_keycode(keycode | 0x80);
963 a3a91a35 bellard
    }
964 a3a91a35 bellard
}
965 a3a91a35 bellard
966 13224a87 bellard
static int mouse_button_state;
967 13224a87 bellard
968 5fafdf24 ths
static void do_mouse_move(const char *dx_str, const char *dy_str,
969 13224a87 bellard
                          const char *dz_str)
970 13224a87 bellard
{
971 13224a87 bellard
    int dx, dy, dz;
972 13224a87 bellard
    dx = strtol(dx_str, NULL, 0);
973 13224a87 bellard
    dy = strtol(dy_str, NULL, 0);
974 13224a87 bellard
    dz = 0;
975 5fafdf24 ths
    if (dz_str)
976 13224a87 bellard
        dz = strtol(dz_str, NULL, 0);
977 13224a87 bellard
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
978 13224a87 bellard
}
979 13224a87 bellard
980 13224a87 bellard
static void do_mouse_button(int button_state)
981 13224a87 bellard
{
982 13224a87 bellard
    mouse_button_state = button_state;
983 13224a87 bellard
    kbd_mouse_event(0, 0, 0, mouse_button_state);
984 13224a87 bellard
}
985 13224a87 bellard
986 3440557b bellard
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
987 3440557b bellard
{
988 3440557b bellard
    uint32_t val;
989 3440557b bellard
    int suffix;
990 3440557b bellard
991 3440557b bellard
    if (has_index) {
992 3440557b bellard
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
993 3440557b bellard
        addr++;
994 3440557b bellard
    }
995 3440557b bellard
    addr &= 0xffff;
996 3440557b bellard
997 3440557b bellard
    switch(size) {
998 3440557b bellard
    default:
999 3440557b bellard
    case 1:
1000 3440557b bellard
        val = cpu_inb(NULL, addr);
1001 3440557b bellard
        suffix = 'b';
1002 3440557b bellard
        break;
1003 3440557b bellard
    case 2:
1004 3440557b bellard
        val = cpu_inw(NULL, addr);
1005 3440557b bellard
        suffix = 'w';
1006 3440557b bellard
        break;
1007 3440557b bellard
    case 4:
1008 3440557b bellard
        val = cpu_inl(NULL, addr);
1009 3440557b bellard
        suffix = 'l';
1010 3440557b bellard
        break;
1011 3440557b bellard
    }
1012 3440557b bellard
    term_printf("port%c[0x%04x] = %#0*x\n",
1013 3440557b bellard
                suffix, addr, size * 2, val);
1014 3440557b bellard
}
1015 a3a91a35 bellard
1016 0ecdffbb aurel32
static void do_boot_set(const char *bootdevice)
1017 0ecdffbb aurel32
{
1018 0ecdffbb aurel32
    int res;
1019 0ecdffbb aurel32
1020 0ecdffbb aurel32
    if (qemu_boot_set_handler)  {
1021 0ecdffbb aurel32
        res = qemu_boot_set_handler(bootdevice);
1022 0ecdffbb aurel32
        if (res == 0)
1023 0ecdffbb aurel32
            term_printf("boot device list now set to %s\n", bootdevice);
1024 0ecdffbb aurel32
        else
1025 0ecdffbb aurel32
            term_printf("setting boot device list failed with error %i\n", res);
1026 0ecdffbb aurel32
    } else {
1027 0ecdffbb aurel32
        term_printf("no function defined to set boot device list for this architecture\n");
1028 0ecdffbb aurel32
    }
1029 0ecdffbb aurel32
}
1030 0ecdffbb aurel32
1031 e4f9082b bellard
static void do_system_reset(void)
1032 e4f9082b bellard
{
1033 e4f9082b bellard
    qemu_system_reset_request();
1034 e4f9082b bellard
}
1035 e4f9082b bellard
1036 3475187d bellard
static void do_system_powerdown(void)
1037 3475187d bellard
{
1038 3475187d bellard
    qemu_system_powerdown_request();
1039 3475187d bellard
}
1040 3475187d bellard
1041 b86bda5b bellard
#if defined(TARGET_I386)
1042 b86bda5b bellard
static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1043 b86bda5b bellard
{
1044 5fafdf24 ths
    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1045 b86bda5b bellard
                addr,
1046 b86bda5b bellard
                pte & mask,
1047 b86bda5b bellard
                pte & PG_GLOBAL_MASK ? 'G' : '-',
1048 b86bda5b bellard
                pte & PG_PSE_MASK ? 'P' : '-',
1049 b86bda5b bellard
                pte & PG_DIRTY_MASK ? 'D' : '-',
1050 b86bda5b bellard
                pte & PG_ACCESSED_MASK ? 'A' : '-',
1051 b86bda5b bellard
                pte & PG_PCD_MASK ? 'C' : '-',
1052 b86bda5b bellard
                pte & PG_PWT_MASK ? 'T' : '-',
1053 b86bda5b bellard
                pte & PG_USER_MASK ? 'U' : '-',
1054 b86bda5b bellard
                pte & PG_RW_MASK ? 'W' : '-');
1055 b86bda5b bellard
}
1056 b86bda5b bellard
1057 b86bda5b bellard
static void tlb_info(void)
1058 b86bda5b bellard
{
1059 6a00d601 bellard
    CPUState *env;
1060 b86bda5b bellard
    int l1, l2;
1061 b86bda5b bellard
    uint32_t pgd, pde, pte;
1062 b86bda5b bellard
1063 6a00d601 bellard
    env = mon_get_cpu();
1064 6a00d601 bellard
    if (!env)
1065 6a00d601 bellard
        return;
1066 6a00d601 bellard
1067 b86bda5b bellard
    if (!(env->cr[0] & CR0_PG_MASK)) {
1068 b86bda5b bellard
        term_printf("PG disabled\n");
1069 b86bda5b bellard
        return;
1070 b86bda5b bellard
    }
1071 b86bda5b bellard
    pgd = env->cr[3] & ~0xfff;
1072 b86bda5b bellard
    for(l1 = 0; l1 < 1024; l1++) {
1073 b86bda5b bellard
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1074 b86bda5b bellard
        pde = le32_to_cpu(pde);
1075 b86bda5b bellard
        if (pde & PG_PRESENT_MASK) {
1076 b86bda5b bellard
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1077 b86bda5b bellard
                print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1078 b86bda5b bellard
            } else {
1079 b86bda5b bellard
                for(l2 = 0; l2 < 1024; l2++) {
1080 5fafdf24 ths
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1081 b86bda5b bellard
                                             (uint8_t *)&pte, 4);
1082 b86bda5b bellard
                    pte = le32_to_cpu(pte);
1083 b86bda5b bellard
                    if (pte & PG_PRESENT_MASK) {
1084 5fafdf24 ths
                        print_pte((l1 << 22) + (l2 << 12),
1085 5fafdf24 ths
                                  pte & ~PG_PSE_MASK,
1086 b86bda5b bellard
                                  ~0xfff);
1087 b86bda5b bellard
                    }
1088 b86bda5b bellard
                }
1089 b86bda5b bellard
            }
1090 b86bda5b bellard
        }
1091 b86bda5b bellard
    }
1092 b86bda5b bellard
}
1093 b86bda5b bellard
1094 5fafdf24 ths
static void mem_print(uint32_t *pstart, int *plast_prot,
1095 b86bda5b bellard
                      uint32_t end, int prot)
1096 b86bda5b bellard
{
1097 9746b15b bellard
    int prot1;
1098 9746b15b bellard
    prot1 = *plast_prot;
1099 9746b15b bellard
    if (prot != prot1) {
1100 b86bda5b bellard
        if (*pstart != -1) {
1101 b86bda5b bellard
            term_printf("%08x-%08x %08x %c%c%c\n",
1102 5fafdf24 ths
                        *pstart, end, end - *pstart,
1103 9746b15b bellard
                        prot1 & PG_USER_MASK ? 'u' : '-',
1104 b86bda5b bellard
                        'r',
1105 9746b15b bellard
                        prot1 & PG_RW_MASK ? 'w' : '-');
1106 b86bda5b bellard
        }
1107 b86bda5b bellard
        if (prot != 0)
1108 b86bda5b bellard
            *pstart = end;
1109 b86bda5b bellard
        else
1110 b86bda5b bellard
            *pstart = -1;
1111 b86bda5b bellard
        *plast_prot = prot;
1112 b86bda5b bellard
    }
1113 b86bda5b bellard
}
1114 b86bda5b bellard
1115 b86bda5b bellard
static void mem_info(void)
1116 b86bda5b bellard
{
1117 6a00d601 bellard
    CPUState *env;
1118 b86bda5b bellard
    int l1, l2, prot, last_prot;
1119 b86bda5b bellard
    uint32_t pgd, pde, pte, start, end;
1120 b86bda5b bellard
1121 6a00d601 bellard
    env = mon_get_cpu();
1122 6a00d601 bellard
    if (!env)
1123 6a00d601 bellard
        return;
1124 6a00d601 bellard
1125 b86bda5b bellard
    if (!(env->cr[0] & CR0_PG_MASK)) {
1126 b86bda5b bellard
        term_printf("PG disabled\n");
1127 b86bda5b bellard
        return;
1128 b86bda5b bellard
    }
1129 b86bda5b bellard
    pgd = env->cr[3] & ~0xfff;
1130 b86bda5b bellard
    last_prot = 0;
1131 b86bda5b bellard
    start = -1;
1132 b86bda5b bellard
    for(l1 = 0; l1 < 1024; l1++) {
1133 b86bda5b bellard
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1134 b86bda5b bellard
        pde = le32_to_cpu(pde);
1135 b86bda5b bellard
        end = l1 << 22;
1136 b86bda5b bellard
        if (pde & PG_PRESENT_MASK) {
1137 b86bda5b bellard
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1138 b86bda5b bellard
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1139 b86bda5b bellard
                mem_print(&start, &last_prot, end, prot);
1140 b86bda5b bellard
            } else {
1141 b86bda5b bellard
                for(l2 = 0; l2 < 1024; l2++) {
1142 5fafdf24 ths
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1143 b86bda5b bellard
                                             (uint8_t *)&pte, 4);
1144 b86bda5b bellard
                    pte = le32_to_cpu(pte);
1145 b86bda5b bellard
                    end = (l1 << 22) + (l2 << 12);
1146 b86bda5b bellard
                    if (pte & PG_PRESENT_MASK) {
1147 b86bda5b bellard
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1148 b86bda5b bellard
                    } else {
1149 b86bda5b bellard
                        prot = 0;
1150 b86bda5b bellard
                    }
1151 b86bda5b bellard
                    mem_print(&start, &last_prot, end, prot);
1152 b86bda5b bellard
                }
1153 b86bda5b bellard
            }
1154 b86bda5b bellard
        } else {
1155 b86bda5b bellard
            prot = 0;
1156 b86bda5b bellard
            mem_print(&start, &last_prot, end, prot);
1157 b86bda5b bellard
        }
1158 b86bda5b bellard
    }
1159 b86bda5b bellard
}
1160 b86bda5b bellard
#endif
1161 b86bda5b bellard
1162 0f4c6415 bellard
static void do_info_kqemu(void)
1163 0f4c6415 bellard
{
1164 0f4c6415 bellard
#ifdef USE_KQEMU
1165 6a00d601 bellard
    CPUState *env;
1166 0f4c6415 bellard
    int val;
1167 0f4c6415 bellard
    val = 0;
1168 6a00d601 bellard
    env = mon_get_cpu();
1169 6a00d601 bellard
    if (!env) {
1170 6a00d601 bellard
        term_printf("No cpu initialized yet");
1171 6a00d601 bellard
        return;
1172 6a00d601 bellard
    }
1173 6a00d601 bellard
    val = env->kqemu_enabled;
1174 5f1ce948 bellard
    term_printf("kqemu support: ");
1175 5f1ce948 bellard
    switch(val) {
1176 5f1ce948 bellard
    default:
1177 5f1ce948 bellard
    case 0:
1178 5f1ce948 bellard
        term_printf("disabled\n");
1179 5f1ce948 bellard
        break;
1180 5f1ce948 bellard
    case 1:
1181 5f1ce948 bellard
        term_printf("enabled for user code\n");
1182 5f1ce948 bellard
        break;
1183 5f1ce948 bellard
    case 2:
1184 5f1ce948 bellard
        term_printf("enabled for user and kernel code\n");
1185 5f1ce948 bellard
        break;
1186 5f1ce948 bellard
    }
1187 0f4c6415 bellard
#else
1188 5f1ce948 bellard
    term_printf("kqemu support: not compiled\n");
1189 0f4c6415 bellard
#endif
1190 5fafdf24 ths
}
1191 0f4c6415 bellard
1192 5f1ce948 bellard
#ifdef CONFIG_PROFILER
1193 5f1ce948 bellard
1194 5f1ce948 bellard
int64_t kqemu_time;
1195 5f1ce948 bellard
int64_t qemu_time;
1196 5f1ce948 bellard
int64_t kqemu_exec_count;
1197 5f1ce948 bellard
int64_t dev_time;
1198 5f1ce948 bellard
int64_t kqemu_ret_int_count;
1199 5f1ce948 bellard
int64_t kqemu_ret_excp_count;
1200 5f1ce948 bellard
int64_t kqemu_ret_intr_count;
1201 5f1ce948 bellard
1202 5f1ce948 bellard
static void do_info_profile(void)
1203 5f1ce948 bellard
{
1204 5f1ce948 bellard
    int64_t total;
1205 5f1ce948 bellard
    total = qemu_time;
1206 5f1ce948 bellard
    if (total == 0)
1207 5f1ce948 bellard
        total = 1;
1208 26a76461 bellard
    term_printf("async time  %" PRId64 " (%0.3f)\n",
1209 5f1ce948 bellard
                dev_time, dev_time / (double)ticks_per_sec);
1210 26a76461 bellard
    term_printf("qemu time   %" PRId64 " (%0.3f)\n",
1211 5f1ce948 bellard
                qemu_time, qemu_time / (double)ticks_per_sec);
1212 26a76461 bellard
    term_printf("kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1213 5f1ce948 bellard
                kqemu_time, kqemu_time / (double)ticks_per_sec,
1214 5f1ce948 bellard
                kqemu_time / (double)total * 100.0,
1215 5f1ce948 bellard
                kqemu_exec_count,
1216 5f1ce948 bellard
                kqemu_ret_int_count,
1217 5f1ce948 bellard
                kqemu_ret_excp_count,
1218 5f1ce948 bellard
                kqemu_ret_intr_count);
1219 5f1ce948 bellard
    qemu_time = 0;
1220 5f1ce948 bellard
    kqemu_time = 0;
1221 5f1ce948 bellard
    kqemu_exec_count = 0;
1222 5f1ce948 bellard
    dev_time = 0;
1223 5f1ce948 bellard
    kqemu_ret_int_count = 0;
1224 5f1ce948 bellard
    kqemu_ret_excp_count = 0;
1225 5f1ce948 bellard
    kqemu_ret_intr_count = 0;
1226 5f1ce948 bellard
#ifdef USE_KQEMU
1227 5f1ce948 bellard
    kqemu_record_dump();
1228 5f1ce948 bellard
#endif
1229 5f1ce948 bellard
}
1230 5f1ce948 bellard
#else
1231 5f1ce948 bellard
static void do_info_profile(void)
1232 5f1ce948 bellard
{
1233 5f1ce948 bellard
    term_printf("Internal profiler not compiled\n");
1234 5f1ce948 bellard
}
1235 5f1ce948 bellard
#endif
1236 5f1ce948 bellard
1237 ec36b695 bellard
/* Capture support */
1238 ec36b695 bellard
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1239 ec36b695 bellard
1240 ec36b695 bellard
static void do_info_capture (void)
1241 ec36b695 bellard
{
1242 ec36b695 bellard
    int i;
1243 ec36b695 bellard
    CaptureState *s;
1244 ec36b695 bellard
1245 ec36b695 bellard
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1246 ec36b695 bellard
        term_printf ("[%d]: ", i);
1247 ec36b695 bellard
        s->ops.info (s->opaque);
1248 ec36b695 bellard
    }
1249 ec36b695 bellard
}
1250 ec36b695 bellard
1251 ec36b695 bellard
static void do_stop_capture (int n)
1252 ec36b695 bellard
{
1253 ec36b695 bellard
    int i;
1254 ec36b695 bellard
    CaptureState *s;
1255 ec36b695 bellard
1256 ec36b695 bellard
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1257 ec36b695 bellard
        if (i == n) {
1258 ec36b695 bellard
            s->ops.destroy (s->opaque);
1259 ec36b695 bellard
            LIST_REMOVE (s, entries);
1260 ec36b695 bellard
            qemu_free (s);
1261 ec36b695 bellard
            return;
1262 ec36b695 bellard
        }
1263 ec36b695 bellard
    }
1264 ec36b695 bellard
}
1265 ec36b695 bellard
1266 ec36b695 bellard
#ifdef HAS_AUDIO
1267 ec36b695 bellard
int wav_start_capture (CaptureState *s, const char *path, int freq,
1268 ec36b695 bellard
                       int bits, int nchannels);
1269 ec36b695 bellard
1270 ec36b695 bellard
static void do_wav_capture (const char *path,
1271 ec36b695 bellard
                            int has_freq, int freq,
1272 ec36b695 bellard
                            int has_bits, int bits,
1273 ec36b695 bellard
                            int has_channels, int nchannels)
1274 ec36b695 bellard
{
1275 ec36b695 bellard
    CaptureState *s;
1276 ec36b695 bellard
1277 ec36b695 bellard
    s = qemu_mallocz (sizeof (*s));
1278 ec36b695 bellard
    if (!s) {
1279 ec36b695 bellard
        term_printf ("Not enough memory to add wave capture\n");
1280 ec36b695 bellard
        return;
1281 ec36b695 bellard
    }
1282 ec36b695 bellard
1283 ec36b695 bellard
    freq = has_freq ? freq : 44100;
1284 ec36b695 bellard
    bits = has_bits ? bits : 16;
1285 ec36b695 bellard
    nchannels = has_channels ? nchannels : 2;
1286 ec36b695 bellard
1287 ec36b695 bellard
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1288 ec36b695 bellard
        term_printf ("Faied to add wave capture\n");
1289 ec36b695 bellard
        qemu_free (s);
1290 ec36b695 bellard
    }
1291 ec36b695 bellard
    LIST_INSERT_HEAD (&capture_head, s, entries);
1292 ec36b695 bellard
}
1293 ec36b695 bellard
#endif
1294 ec36b695 bellard
1295 dc1c0b74 aurel32
#if defined(TARGET_I386)
1296 dc1c0b74 aurel32
static void do_inject_nmi(int cpu_index)
1297 dc1c0b74 aurel32
{
1298 dc1c0b74 aurel32
    CPUState *env;
1299 dc1c0b74 aurel32
1300 dc1c0b74 aurel32
    for (env = first_cpu; env != NULL; env = env->next_cpu)
1301 dc1c0b74 aurel32
        if (env->cpu_index == cpu_index) {
1302 dc1c0b74 aurel32
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1303 dc1c0b74 aurel32
            break;
1304 dc1c0b74 aurel32
        }
1305 dc1c0b74 aurel32
}
1306 dc1c0b74 aurel32
#endif
1307 dc1c0b74 aurel32
1308 9dc39cba bellard
static term_cmd_t term_cmds[] = {
1309 5fafdf24 ths
    { "help|?", "s?", do_help,
1310 9dc39cba bellard
      "[cmd]", "show the help" },
1311 5fafdf24 ths
    { "commit", "s", do_commit,
1312 7954c734 bellard
      "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1313 9307c4c1 bellard
    { "info", "s?", do_info,
1314 9dc39cba bellard
      "subcommand", "show various information about the system state" },
1315 9307c4c1 bellard
    { "q|quit", "", do_quit,
1316 9dc39cba bellard
      "", "quit the emulator" },
1317 81d0912d bellard
    { "eject", "-fB", do_eject,
1318 e598752a ths
      "[-f] device", "eject a removable medium (use -f to force it)" },
1319 81d0912d bellard
    { "change", "BF", do_change,
1320 e598752a ths
      "device filename", "change a removable medium" },
1321 5fafdf24 ths
    { "screendump", "F", do_screen_dump,
1322 59a983b9 bellard
      "filename", "save screen into PPM image 'filename'" },
1323 788228c0 balrog
    { "logfile", "F", do_logfile,
1324 e735b91c pbrook
      "filename", "output logs to 'filename'" },
1325 9307c4c1 bellard
    { "log", "s", do_log,
1326 5fafdf24 ths
      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1327 faea38e7 bellard
    { "savevm", "s?", do_savevm,
1328 5fafdf24 ths
      "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1329 faea38e7 bellard
    { "loadvm", "s", do_loadvm,
1330 5fafdf24 ths
      "tag|id", "restore a VM snapshot from its tag or id" },
1331 faea38e7 bellard
    { "delvm", "s", do_delvm,
1332 5fafdf24 ths
      "tag|id", "delete a VM snapshot from its tag or id" },
1333 5fafdf24 ths
    { "stop", "", do_stop,
1334 9307c4c1 bellard
      "", "stop emulation", },
1335 5fafdf24 ths
    { "c|cont", "", do_cont,
1336 9307c4c1 bellard
      "", "resume emulation", },
1337 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
1338 5fafdf24 ths
    { "gdbserver", "s?", do_gdbserver,
1339 9307c4c1 bellard
      "[port]", "start gdbserver session (default port=1234)", },
1340 67b915a5 bellard
#endif
1341 5fafdf24 ths
    { "x", "/l", do_memory_dump,
1342 9307c4c1 bellard
      "/fmt addr", "virtual memory dump starting at 'addr'", },
1343 5fafdf24 ths
    { "xp", "/l", do_physical_memory_dump,
1344 9307c4c1 bellard
      "/fmt addr", "physical memory dump starting at 'addr'", },
1345 5fafdf24 ths
    { "p|print", "/l", do_print,
1346 9307c4c1 bellard
      "/fmt expr", "print expression value (use $reg for CPU register access)", },
1347 5fafdf24 ths
    { "i", "/ii.", do_ioport_read,
1348 3440557b bellard
      "/fmt addr", "I/O port read" },
1349 3440557b bellard
1350 5fafdf24 ths
    { "sendkey", "s", do_send_key,
1351 a3a91a35 bellard
      "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1352 5fafdf24 ths
    { "system_reset", "", do_system_reset,
1353 e4f9082b bellard
      "", "reset the system" },
1354 5fafdf24 ths
    { "system_powerdown", "", do_system_powerdown,
1355 3475187d bellard
      "", "send system power down event" },
1356 5fafdf24 ths
    { "sum", "ii", do_sum,
1357 e4cf1adc bellard
      "addr size", "compute the checksum of a memory region" },
1358 a594cfbf bellard
    { "usb_add", "s", do_usb_add,
1359 a594cfbf bellard
      "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1360 a594cfbf bellard
    { "usb_del", "s", do_usb_del,
1361 a594cfbf bellard
      "device", "remove USB device 'bus.addr'" },
1362 5fafdf24 ths
    { "cpu", "i", do_cpu_set,
1363 6a00d601 bellard
      "index", "set the default CPU" },
1364 5fafdf24 ths
    { "mouse_move", "sss?", do_mouse_move,
1365 13224a87 bellard
      "dx dy [dz]", "send mouse move events" },
1366 5fafdf24 ths
    { "mouse_button", "i", do_mouse_button,
1367 13224a87 bellard
      "state", "change mouse button state (1=L, 2=M, 4=R)" },
1368 455204eb ths
    { "mouse_set", "i", do_mouse_set,
1369 455204eb ths
      "index", "set which mouse device receives events" },
1370 ec36b695 bellard
#ifdef HAS_AUDIO
1371 ec36b695 bellard
    { "wavcapture", "si?i?i?", do_wav_capture,
1372 ec36b695 bellard
      "path [frequency bits channels]",
1373 ec36b695 bellard
      "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1374 ec36b695 bellard
#endif
1375 ec36b695 bellard
     { "stopcapture", "i", do_stop_capture,
1376 ec36b695 bellard
       "capture index", "stop capture" },
1377 5fafdf24 ths
    { "memsave", "lis", do_memory_save,
1378 b371dc59 bellard
      "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1379 a8bdf7a6 aurel32
    { "pmemsave", "lis", do_physical_memory_save,
1380 a8bdf7a6 aurel32
      "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1381 0ecdffbb aurel32
    { "boot_set", "s", do_boot_set,
1382 0ecdffbb aurel32
      "bootdevice", "define new values for the boot device list" },
1383 dc1c0b74 aurel32
#if defined(TARGET_I386)
1384 dc1c0b74 aurel32
    { "nmi", "i", do_inject_nmi,
1385 dc1c0b74 aurel32
      "cpu", "inject an NMI on the given CPU", },
1386 dc1c0b74 aurel32
#endif
1387 5fafdf24 ths
    { NULL, NULL, },
1388 9dc39cba bellard
};
1389 9dc39cba bellard
1390 9dc39cba bellard
static term_cmd_t info_cmds[] = {
1391 9bc9d1c7 bellard
    { "version", "", do_info_version,
1392 9bc9d1c7 bellard
      "", "show the version of qemu" },
1393 9307c4c1 bellard
    { "network", "", do_info_network,
1394 9dc39cba bellard
      "", "show the network state" },
1395 9307c4c1 bellard
    { "block", "", do_info_block,
1396 9dc39cba bellard
      "", "show the block devices" },
1397 a36e69dd ths
    { "blockstats", "", do_info_blockstats,
1398 a36e69dd ths
      "", "show block device statistics" },
1399 9307c4c1 bellard
    { "registers", "", do_info_registers,
1400 9307c4c1 bellard
      "", "show the cpu registers" },
1401 6a00d601 bellard
    { "cpus", "", do_info_cpus,
1402 6a00d601 bellard
      "", "show infos for each CPU" },
1403 aa455485 bellard
    { "history", "", do_info_history,
1404 aa455485 bellard
      "", "show the command line history", },
1405 4a0fb71e bellard
    { "irq", "", irq_info,
1406 4a0fb71e bellard
      "", "show the interrupts statistics (if available)", },
1407 4c27ba27 bellard
    { "pic", "", pic_info,
1408 4c27ba27 bellard
      "", "show i8259 (PIC) state", },
1409 86e0c048 bellard
    { "pci", "", pci_info,
1410 86e0c048 bellard
      "", "show PCI info", },
1411 b86bda5b bellard
#if defined(TARGET_I386)
1412 b86bda5b bellard
    { "tlb", "", tlb_info,
1413 b86bda5b bellard
      "", "show virtual to physical memory mappings", },
1414 b86bda5b bellard
    { "mem", "", mem_info,
1415 b86bda5b bellard
      "", "show the active virtual memory mappings", },
1416 b86bda5b bellard
#endif
1417 e3db7226 bellard
    { "jit", "", do_info_jit,
1418 e3db7226 bellard
      "", "show dynamic compiler info", },
1419 0f4c6415 bellard
    { "kqemu", "", do_info_kqemu,
1420 0f4c6415 bellard
      "", "show kqemu information", },
1421 a594cfbf bellard
    { "usb", "", usb_info,
1422 a594cfbf bellard
      "", "show guest USB devices", },
1423 a594cfbf bellard
    { "usbhost", "", usb_host_info,
1424 a594cfbf bellard
      "", "show host USB devices", },
1425 5f1ce948 bellard
    { "profile", "", do_info_profile,
1426 5f1ce948 bellard
      "", "show profiling information", },
1427 ec36b695 bellard
    { "capture", "", do_info_capture,
1428 17100159 bellard
      "", "show capture information" },
1429 faea38e7 bellard
    { "snapshots", "", do_info_snapshots,
1430 17100159 bellard
      "", "show the currently saved VM snapshots" },
1431 201a51fc balrog
    { "pcmcia", "", pcmcia_info,
1432 201a51fc balrog
      "", "show guest PCMCIA status" },
1433 455204eb ths
    { "mice", "", do_info_mice,
1434 455204eb ths
      "", "show which guest mouse is receiving events" },
1435 a9ce8590 bellard
    { "vnc", "", do_info_vnc,
1436 a9ce8590 bellard
      "", "show the vnc server status"},
1437 c35734b2 ths
    { "name", "", do_info_name,
1438 c35734b2 ths
      "", "show the current VM name" },
1439 76a66253 j_mayer
#if defined(TARGET_PPC)
1440 76a66253 j_mayer
    { "cpustats", "", do_info_cpu_stats,
1441 76a66253 j_mayer
      "", "show CPU statistics", },
1442 76a66253 j_mayer
#endif
1443 31a60e22 blueswir1
#if defined(CONFIG_SLIRP)
1444 31a60e22 blueswir1
    { "slirp", "", do_info_slirp,
1445 31a60e22 blueswir1
      "", "show SLIRP statistics", },
1446 31a60e22 blueswir1
#endif
1447 9dc39cba bellard
    { NULL, NULL, },
1448 9dc39cba bellard
};
1449 9dc39cba bellard
1450 9307c4c1 bellard
/*******************************************************************/
1451 9307c4c1 bellard
1452 9307c4c1 bellard
static const char *pch;
1453 9307c4c1 bellard
static jmp_buf expr_env;
1454 9307c4c1 bellard
1455 92a31b1f bellard
#define MD_TLONG 0
1456 92a31b1f bellard
#define MD_I32   1
1457 92a31b1f bellard
1458 9307c4c1 bellard
typedef struct MonitorDef {
1459 9307c4c1 bellard
    const char *name;
1460 9307c4c1 bellard
    int offset;
1461 92a31b1f bellard
    target_long (*get_value)(struct MonitorDef *md, int val);
1462 92a31b1f bellard
    int type;
1463 9307c4c1 bellard
} MonitorDef;
1464 9307c4c1 bellard
1465 57206fd4 bellard
#if defined(TARGET_I386)
1466 92a31b1f bellard
static target_long monitor_get_pc (struct MonitorDef *md, int val)
1467 57206fd4 bellard
{
1468 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1469 6a00d601 bellard
    if (!env)
1470 6a00d601 bellard
        return 0;
1471 6a00d601 bellard
    return env->eip + env->segs[R_CS].base;
1472 57206fd4 bellard
}
1473 57206fd4 bellard
#endif
1474 57206fd4 bellard
1475 a541f297 bellard
#if defined(TARGET_PPC)
1476 92a31b1f bellard
static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1477 a541f297 bellard
{
1478 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1479 a541f297 bellard
    unsigned int u;
1480 a541f297 bellard
    int i;
1481 a541f297 bellard
1482 6a00d601 bellard
    if (!env)
1483 6a00d601 bellard
        return 0;
1484 6a00d601 bellard
1485 a541f297 bellard
    u = 0;
1486 a541f297 bellard
    for (i = 0; i < 8; i++)
1487 6a00d601 bellard
        u |= env->crf[i] << (32 - (4 * i));
1488 a541f297 bellard
1489 a541f297 bellard
    return u;
1490 a541f297 bellard
}
1491 a541f297 bellard
1492 92a31b1f bellard
static target_long monitor_get_msr (struct MonitorDef *md, int val)
1493 a541f297 bellard
{
1494 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1495 6a00d601 bellard
    if (!env)
1496 6a00d601 bellard
        return 0;
1497 0411a972 j_mayer
    return env->msr;
1498 a541f297 bellard
}
1499 a541f297 bellard
1500 92a31b1f bellard
static target_long monitor_get_xer (struct MonitorDef *md, int val)
1501 a541f297 bellard
{
1502 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1503 6a00d601 bellard
    if (!env)
1504 6a00d601 bellard
        return 0;
1505 ff937dba j_mayer
    return ppc_load_xer(env);
1506 a541f297 bellard
}
1507 9fddaa0c bellard
1508 92a31b1f bellard
static target_long monitor_get_decr (struct MonitorDef *md, int val)
1509 9fddaa0c bellard
{
1510 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1511 6a00d601 bellard
    if (!env)
1512 6a00d601 bellard
        return 0;
1513 6a00d601 bellard
    return cpu_ppc_load_decr(env);
1514 9fddaa0c bellard
}
1515 9fddaa0c bellard
1516 92a31b1f bellard
static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1517 9fddaa0c bellard
{
1518 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1519 6a00d601 bellard
    if (!env)
1520 6a00d601 bellard
        return 0;
1521 6a00d601 bellard
    return cpu_ppc_load_tbu(env);
1522 9fddaa0c bellard
}
1523 9fddaa0c bellard
1524 92a31b1f bellard
static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1525 9fddaa0c bellard
{
1526 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1527 6a00d601 bellard
    if (!env)
1528 6a00d601 bellard
        return 0;
1529 6a00d601 bellard
    return cpu_ppc_load_tbl(env);
1530 9fddaa0c bellard
}
1531 a541f297 bellard
#endif
1532 a541f297 bellard
1533 e95c8d51 bellard
#if defined(TARGET_SPARC)
1534 7b936c0c bellard
#ifndef TARGET_SPARC64
1535 92a31b1f bellard
static target_long monitor_get_psr (struct MonitorDef *md, int val)
1536 e95c8d51 bellard
{
1537 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1538 6a00d601 bellard
    if (!env)
1539 6a00d601 bellard
        return 0;
1540 6a00d601 bellard
    return GET_PSR(env);
1541 e95c8d51 bellard
}
1542 7b936c0c bellard
#endif
1543 e95c8d51 bellard
1544 92a31b1f bellard
static target_long monitor_get_reg(struct MonitorDef *md, int val)
1545 e95c8d51 bellard
{
1546 6a00d601 bellard
    CPUState *env = mon_get_cpu();
1547 6a00d601 bellard
    if (!env)
1548 6a00d601 bellard
        return 0;
1549 6a00d601 bellard
    return env->regwptr[val];
1550 e95c8d51 bellard
}
1551 e95c8d51 bellard
#endif
1552 e95c8d51 bellard
1553 9307c4c1 bellard
static MonitorDef monitor_defs[] = {
1554 9307c4c1 bellard
#ifdef TARGET_I386
1555 57206fd4 bellard
1556 57206fd4 bellard
#define SEG(name, seg) \
1557 92a31b1f bellard
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1558 57206fd4 bellard
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1559 92a31b1f bellard
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1560 57206fd4 bellard
1561 9307c4c1 bellard
    { "eax", offsetof(CPUState, regs[0]) },
1562 9307c4c1 bellard
    { "ecx", offsetof(CPUState, regs[1]) },
1563 9307c4c1 bellard
    { "edx", offsetof(CPUState, regs[2]) },
1564 9307c4c1 bellard
    { "ebx", offsetof(CPUState, regs[3]) },
1565 9307c4c1 bellard
    { "esp|sp", offsetof(CPUState, regs[4]) },
1566 9307c4c1 bellard
    { "ebp|fp", offsetof(CPUState, regs[5]) },
1567 9307c4c1 bellard
    { "esi", offsetof(CPUState, regs[6]) },
1568 01038d2a bellard
    { "edi", offsetof(CPUState, regs[7]) },
1569 92a31b1f bellard
#ifdef TARGET_X86_64
1570 92a31b1f bellard
    { "r8", offsetof(CPUState, regs[8]) },
1571 92a31b1f bellard
    { "r9", offsetof(CPUState, regs[9]) },
1572 92a31b1f bellard
    { "r10", offsetof(CPUState, regs[10]) },
1573 92a31b1f bellard
    { "r11", offsetof(CPUState, regs[11]) },
1574 92a31b1f bellard
    { "r12", offsetof(CPUState, regs[12]) },
1575 92a31b1f bellard
    { "r13", offsetof(CPUState, regs[13]) },
1576 92a31b1f bellard
    { "r14", offsetof(CPUState, regs[14]) },
1577 92a31b1f bellard
    { "r15", offsetof(CPUState, regs[15]) },
1578 92a31b1f bellard
#endif
1579 9307c4c1 bellard
    { "eflags", offsetof(CPUState, eflags) },
1580 57206fd4 bellard
    { "eip", offsetof(CPUState, eip) },
1581 57206fd4 bellard
    SEG("cs", R_CS)
1582 57206fd4 bellard
    SEG("ds", R_DS)
1583 57206fd4 bellard
    SEG("es", R_ES)
1584 01038d2a bellard
    SEG("ss", R_SS)
1585 57206fd4 bellard
    SEG("fs", R_FS)
1586 57206fd4 bellard
    SEG("gs", R_GS)
1587 57206fd4 bellard
    { "pc", 0, monitor_get_pc, },
1588 a541f297 bellard
#elif defined(TARGET_PPC)
1589 ff937dba j_mayer
    /* General purpose registers */
1590 a541f297 bellard
    { "r0", offsetof(CPUState, gpr[0]) },
1591 a541f297 bellard
    { "r1", offsetof(CPUState, gpr[1]) },
1592 a541f297 bellard
    { "r2", offsetof(CPUState, gpr[2]) },
1593 a541f297 bellard
    { "r3", offsetof(CPUState, gpr[3]) },
1594 a541f297 bellard
    { "r4", offsetof(CPUState, gpr[4]) },
1595 a541f297 bellard
    { "r5", offsetof(CPUState, gpr[5]) },
1596 a541f297 bellard
    { "r6", offsetof(CPUState, gpr[6]) },
1597 a541f297 bellard
    { "r7", offsetof(CPUState, gpr[7]) },
1598 a541f297 bellard
    { "r8", offsetof(CPUState, gpr[8]) },
1599 a541f297 bellard
    { "r9", offsetof(CPUState, gpr[9]) },
1600 a541f297 bellard
    { "r10", offsetof(CPUState, gpr[10]) },
1601 a541f297 bellard
    { "r11", offsetof(CPUState, gpr[11]) },
1602 a541f297 bellard
    { "r12", offsetof(CPUState, gpr[12]) },
1603 a541f297 bellard
    { "r13", offsetof(CPUState, gpr[13]) },
1604 a541f297 bellard
    { "r14", offsetof(CPUState, gpr[14]) },
1605 a541f297 bellard
    { "r15", offsetof(CPUState, gpr[15]) },
1606 a541f297 bellard
    { "r16", offsetof(CPUState, gpr[16]) },
1607 a541f297 bellard
    { "r17", offsetof(CPUState, gpr[17]) },
1608 a541f297 bellard
    { "r18", offsetof(CPUState, gpr[18]) },
1609 a541f297 bellard
    { "r19", offsetof(CPUState, gpr[19]) },
1610 a541f297 bellard
    { "r20", offsetof(CPUState, gpr[20]) },
1611 a541f297 bellard
    { "r21", offsetof(CPUState, gpr[21]) },
1612 a541f297 bellard
    { "r22", offsetof(CPUState, gpr[22]) },
1613 a541f297 bellard
    { "r23", offsetof(CPUState, gpr[23]) },
1614 a541f297 bellard
    { "r24", offsetof(CPUState, gpr[24]) },
1615 a541f297 bellard
    { "r25", offsetof(CPUState, gpr[25]) },
1616 a541f297 bellard
    { "r26", offsetof(CPUState, gpr[26]) },
1617 a541f297 bellard
    { "r27", offsetof(CPUState, gpr[27]) },
1618 a541f297 bellard
    { "r28", offsetof(CPUState, gpr[28]) },
1619 a541f297 bellard
    { "r29", offsetof(CPUState, gpr[29]) },
1620 a541f297 bellard
    { "r30", offsetof(CPUState, gpr[30]) },
1621 a541f297 bellard
    { "r31", offsetof(CPUState, gpr[31]) },
1622 ff937dba j_mayer
    /* Floating point registers */
1623 ff937dba j_mayer
    { "f0", offsetof(CPUState, fpr[0]) },
1624 ff937dba j_mayer
    { "f1", offsetof(CPUState, fpr[1]) },
1625 ff937dba j_mayer
    { "f2", offsetof(CPUState, fpr[2]) },
1626 ff937dba j_mayer
    { "f3", offsetof(CPUState, fpr[3]) },
1627 ff937dba j_mayer
    { "f4", offsetof(CPUState, fpr[4]) },
1628 ff937dba j_mayer
    { "f5", offsetof(CPUState, fpr[5]) },
1629 ff937dba j_mayer
    { "f6", offsetof(CPUState, fpr[6]) },
1630 ff937dba j_mayer
    { "f7", offsetof(CPUState, fpr[7]) },
1631 ff937dba j_mayer
    { "f8", offsetof(CPUState, fpr[8]) },
1632 ff937dba j_mayer
    { "f9", offsetof(CPUState, fpr[9]) },
1633 ff937dba j_mayer
    { "f10", offsetof(CPUState, fpr[10]) },
1634 ff937dba j_mayer
    { "f11", offsetof(CPUState, fpr[11]) },
1635 ff937dba j_mayer
    { "f12", offsetof(CPUState, fpr[12]) },
1636 ff937dba j_mayer
    { "f13", offsetof(CPUState, fpr[13]) },
1637 ff937dba j_mayer
    { "f14", offsetof(CPUState, fpr[14]) },
1638 ff937dba j_mayer
    { "f15", offsetof(CPUState, fpr[15]) },
1639 ff937dba j_mayer
    { "f16", offsetof(CPUState, fpr[16]) },
1640 ff937dba j_mayer
    { "f17", offsetof(CPUState, fpr[17]) },
1641 ff937dba j_mayer
    { "f18", offsetof(CPUState, fpr[18]) },
1642 ff937dba j_mayer
    { "f19", offsetof(CPUState, fpr[19]) },
1643 ff937dba j_mayer
    { "f20", offsetof(CPUState, fpr[20]) },
1644 ff937dba j_mayer
    { "f21", offsetof(CPUState, fpr[21]) },
1645 ff937dba j_mayer
    { "f22", offsetof(CPUState, fpr[22]) },
1646 ff937dba j_mayer
    { "f23", offsetof(CPUState, fpr[23]) },
1647 ff937dba j_mayer
    { "f24", offsetof(CPUState, fpr[24]) },
1648 ff937dba j_mayer
    { "f25", offsetof(CPUState, fpr[25]) },
1649 ff937dba j_mayer
    { "f26", offsetof(CPUState, fpr[26]) },
1650 ff937dba j_mayer
    { "f27", offsetof(CPUState, fpr[27]) },
1651 ff937dba j_mayer
    { "f28", offsetof(CPUState, fpr[28]) },
1652 ff937dba j_mayer
    { "f29", offsetof(CPUState, fpr[29]) },
1653 ff937dba j_mayer
    { "f30", offsetof(CPUState, fpr[30]) },
1654 ff937dba j_mayer
    { "f31", offsetof(CPUState, fpr[31]) },
1655 ff937dba j_mayer
    { "fpscr", offsetof(CPUState, fpscr) },
1656 ff937dba j_mayer
    /* Next instruction pointer */
1657 57206fd4 bellard
    { "nip|pc", offsetof(CPUState, nip) },
1658 a541f297 bellard
    { "lr", offsetof(CPUState, lr) },
1659 a541f297 bellard
    { "ctr", offsetof(CPUState, ctr) },
1660 9fddaa0c bellard
    { "decr", 0, &monitor_get_decr, },
1661 a541f297 bellard
    { "ccr", 0, &monitor_get_ccr, },
1662 ff937dba j_mayer
    /* Machine state register */
1663 a541f297 bellard
    { "msr", 0, &monitor_get_msr, },
1664 a541f297 bellard
    { "xer", 0, &monitor_get_xer, },
1665 9fddaa0c bellard
    { "tbu", 0, &monitor_get_tbu, },
1666 9fddaa0c bellard
    { "tbl", 0, &monitor_get_tbl, },
1667 ff937dba j_mayer
#if defined(TARGET_PPC64)
1668 ff937dba j_mayer
    /* Address space register */
1669 ff937dba j_mayer
    { "asr", offsetof(CPUState, asr) },
1670 ff937dba j_mayer
#endif
1671 ff937dba j_mayer
    /* Segment registers */
1672 a541f297 bellard
    { "sdr1", offsetof(CPUState, sdr1) },
1673 a541f297 bellard
    { "sr0", offsetof(CPUState, sr[0]) },
1674 a541f297 bellard
    { "sr1", offsetof(CPUState, sr[1]) },
1675 a541f297 bellard
    { "sr2", offsetof(CPUState, sr[2]) },
1676 a541f297 bellard
    { "sr3", offsetof(CPUState, sr[3]) },
1677 a541f297 bellard
    { "sr4", offsetof(CPUState, sr[4]) },
1678 a541f297 bellard
    { "sr5", offsetof(CPUState, sr[5]) },
1679 a541f297 bellard
    { "sr6", offsetof(CPUState, sr[6]) },
1680 a541f297 bellard
    { "sr7", offsetof(CPUState, sr[7]) },
1681 a541f297 bellard
    { "sr8", offsetof(CPUState, sr[8]) },
1682 a541f297 bellard
    { "sr9", offsetof(CPUState, sr[9]) },
1683 a541f297 bellard
    { "sr10", offsetof(CPUState, sr[10]) },
1684 a541f297 bellard
    { "sr11", offsetof(CPUState, sr[11]) },
1685 a541f297 bellard
    { "sr12", offsetof(CPUState, sr[12]) },
1686 a541f297 bellard
    { "sr13", offsetof(CPUState, sr[13]) },
1687 a541f297 bellard
    { "sr14", offsetof(CPUState, sr[14]) },
1688 a541f297 bellard
    { "sr15", offsetof(CPUState, sr[15]) },
1689 a541f297 bellard
    /* Too lazy to put BATs and SPRs ... */
1690 e95c8d51 bellard
#elif defined(TARGET_SPARC)
1691 e95c8d51 bellard
    { "g0", offsetof(CPUState, gregs[0]) },
1692 e95c8d51 bellard
    { "g1", offsetof(CPUState, gregs[1]) },
1693 e95c8d51 bellard
    { "g2", offsetof(CPUState, gregs[2]) },
1694 e95c8d51 bellard
    { "g3", offsetof(CPUState, gregs[3]) },
1695 e95c8d51 bellard
    { "g4", offsetof(CPUState, gregs[4]) },
1696 e95c8d51 bellard
    { "g5", offsetof(CPUState, gregs[5]) },
1697 e95c8d51 bellard
    { "g6", offsetof(CPUState, gregs[6]) },
1698 e95c8d51 bellard
    { "g7", offsetof(CPUState, gregs[7]) },
1699 e95c8d51 bellard
    { "o0", 0, monitor_get_reg },
1700 e95c8d51 bellard
    { "o1", 1, monitor_get_reg },
1701 e95c8d51 bellard
    { "o2", 2, monitor_get_reg },
1702 e95c8d51 bellard
    { "o3", 3, monitor_get_reg },
1703 e95c8d51 bellard
    { "o4", 4, monitor_get_reg },
1704 e95c8d51 bellard
    { "o5", 5, monitor_get_reg },
1705 e95c8d51 bellard
    { "o6", 6, monitor_get_reg },
1706 e95c8d51 bellard
    { "o7", 7, monitor_get_reg },
1707 e95c8d51 bellard
    { "l0", 8, monitor_get_reg },
1708 e95c8d51 bellard
    { "l1", 9, monitor_get_reg },
1709 e95c8d51 bellard
    { "l2", 10, monitor_get_reg },
1710 e95c8d51 bellard
    { "l3", 11, monitor_get_reg },
1711 e95c8d51 bellard
    { "l4", 12, monitor_get_reg },
1712 e95c8d51 bellard
    { "l5", 13, monitor_get_reg },
1713 e95c8d51 bellard
    { "l6", 14, monitor_get_reg },
1714 e95c8d51 bellard
    { "l7", 15, monitor_get_reg },
1715 e95c8d51 bellard
    { "i0", 16, monitor_get_reg },
1716 e95c8d51 bellard
    { "i1", 17, monitor_get_reg },
1717 e95c8d51 bellard
    { "i2", 18, monitor_get_reg },
1718 e95c8d51 bellard
    { "i3", 19, monitor_get_reg },
1719 e95c8d51 bellard
    { "i4", 20, monitor_get_reg },
1720 e95c8d51 bellard
    { "i5", 21, monitor_get_reg },
1721 e95c8d51 bellard
    { "i6", 22, monitor_get_reg },
1722 e95c8d51 bellard
    { "i7", 23, monitor_get_reg },
1723 e95c8d51 bellard
    { "pc", offsetof(CPUState, pc) },
1724 e95c8d51 bellard
    { "npc", offsetof(CPUState, npc) },
1725 e95c8d51 bellard
    { "y", offsetof(CPUState, y) },
1726 7b936c0c bellard
#ifndef TARGET_SPARC64
1727 e95c8d51 bellard
    { "psr", 0, &monitor_get_psr, },
1728 e95c8d51 bellard
    { "wim", offsetof(CPUState, wim) },
1729 7b936c0c bellard
#endif
1730 e95c8d51 bellard
    { "tbr", offsetof(CPUState, tbr) },
1731 e95c8d51 bellard
    { "fsr", offsetof(CPUState, fsr) },
1732 e95c8d51 bellard
    { "f0", offsetof(CPUState, fpr[0]) },
1733 e95c8d51 bellard
    { "f1", offsetof(CPUState, fpr[1]) },
1734 e95c8d51 bellard
    { "f2", offsetof(CPUState, fpr[2]) },
1735 e95c8d51 bellard
    { "f3", offsetof(CPUState, fpr[3]) },
1736 e95c8d51 bellard
    { "f4", offsetof(CPUState, fpr[4]) },
1737 e95c8d51 bellard
    { "f5", offsetof(CPUState, fpr[5]) },
1738 e95c8d51 bellard
    { "f6", offsetof(CPUState, fpr[6]) },
1739 e95c8d51 bellard
    { "f7", offsetof(CPUState, fpr[7]) },
1740 e95c8d51 bellard
    { "f8", offsetof(CPUState, fpr[8]) },
1741 e95c8d51 bellard
    { "f9", offsetof(CPUState, fpr[9]) },
1742 e95c8d51 bellard
    { "f10", offsetof(CPUState, fpr[10]) },
1743 e95c8d51 bellard
    { "f11", offsetof(CPUState, fpr[11]) },
1744 e95c8d51 bellard
    { "f12", offsetof(CPUState, fpr[12]) },
1745 e95c8d51 bellard
    { "f13", offsetof(CPUState, fpr[13]) },
1746 e95c8d51 bellard
    { "f14", offsetof(CPUState, fpr[14]) },
1747 e95c8d51 bellard
    { "f15", offsetof(CPUState, fpr[15]) },
1748 e95c8d51 bellard
    { "f16", offsetof(CPUState, fpr[16]) },
1749 e95c8d51 bellard
    { "f17", offsetof(CPUState, fpr[17]) },
1750 e95c8d51 bellard
    { "f18", offsetof(CPUState, fpr[18]) },
1751 e95c8d51 bellard
    { "f19", offsetof(CPUState, fpr[19]) },
1752 e95c8d51 bellard
    { "f20", offsetof(CPUState, fpr[20]) },
1753 e95c8d51 bellard
    { "f21", offsetof(CPUState, fpr[21]) },
1754 e95c8d51 bellard
    { "f22", offsetof(CPUState, fpr[22]) },
1755 e95c8d51 bellard
    { "f23", offsetof(CPUState, fpr[23]) },
1756 e95c8d51 bellard
    { "f24", offsetof(CPUState, fpr[24]) },
1757 e95c8d51 bellard
    { "f25", offsetof(CPUState, fpr[25]) },
1758 e95c8d51 bellard
    { "f26", offsetof(CPUState, fpr[26]) },
1759 e95c8d51 bellard
    { "f27", offsetof(CPUState, fpr[27]) },
1760 e95c8d51 bellard
    { "f28", offsetof(CPUState, fpr[28]) },
1761 e95c8d51 bellard
    { "f29", offsetof(CPUState, fpr[29]) },
1762 e95c8d51 bellard
    { "f30", offsetof(CPUState, fpr[30]) },
1763 e95c8d51 bellard
    { "f31", offsetof(CPUState, fpr[31]) },
1764 7b936c0c bellard
#ifdef TARGET_SPARC64
1765 7b936c0c bellard
    { "f32", offsetof(CPUState, fpr[32]) },
1766 7b936c0c bellard
    { "f34", offsetof(CPUState, fpr[34]) },
1767 7b936c0c bellard
    { "f36", offsetof(CPUState, fpr[36]) },
1768 7b936c0c bellard
    { "f38", offsetof(CPUState, fpr[38]) },
1769 7b936c0c bellard
    { "f40", offsetof(CPUState, fpr[40]) },
1770 7b936c0c bellard
    { "f42", offsetof(CPUState, fpr[42]) },
1771 7b936c0c bellard
    { "f44", offsetof(CPUState, fpr[44]) },
1772 7b936c0c bellard
    { "f46", offsetof(CPUState, fpr[46]) },
1773 7b936c0c bellard
    { "f48", offsetof(CPUState, fpr[48]) },
1774 7b936c0c bellard
    { "f50", offsetof(CPUState, fpr[50]) },
1775 7b936c0c bellard
    { "f52", offsetof(CPUState, fpr[52]) },
1776 7b936c0c bellard
    { "f54", offsetof(CPUState, fpr[54]) },
1777 7b936c0c bellard
    { "f56", offsetof(CPUState, fpr[56]) },
1778 7b936c0c bellard
    { "f58", offsetof(CPUState, fpr[58]) },
1779 7b936c0c bellard
    { "f60", offsetof(CPUState, fpr[60]) },
1780 7b936c0c bellard
    { "f62", offsetof(CPUState, fpr[62]) },
1781 7b936c0c bellard
    { "asi", offsetof(CPUState, asi) },
1782 7b936c0c bellard
    { "pstate", offsetof(CPUState, pstate) },
1783 7b936c0c bellard
    { "cansave", offsetof(CPUState, cansave) },
1784 7b936c0c bellard
    { "canrestore", offsetof(CPUState, canrestore) },
1785 7b936c0c bellard
    { "otherwin", offsetof(CPUState, otherwin) },
1786 7b936c0c bellard
    { "wstate", offsetof(CPUState, wstate) },
1787 7b936c0c bellard
    { "cleanwin", offsetof(CPUState, cleanwin) },
1788 7b936c0c bellard
    { "fprs", offsetof(CPUState, fprs) },
1789 7b936c0c bellard
#endif
1790 9307c4c1 bellard
#endif
1791 9307c4c1 bellard
    { NULL },
1792 9307c4c1 bellard
};
1793 9307c4c1 bellard
1794 5fafdf24 ths
static void expr_error(const char *fmt)
1795 9dc39cba bellard
{
1796 9307c4c1 bellard
    term_printf(fmt);
1797 9307c4c1 bellard
    term_printf("\n");
1798 9307c4c1 bellard
    longjmp(expr_env, 1);
1799 9307c4c1 bellard
}
1800 9307c4c1 bellard
1801 6a00d601 bellard
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
1802 92a31b1f bellard
static int get_monitor_def(target_long *pval, const char *name)
1803 9307c4c1 bellard
{
1804 9307c4c1 bellard
    MonitorDef *md;
1805 92a31b1f bellard
    void *ptr;
1806 92a31b1f bellard
1807 9307c4c1 bellard
    for(md = monitor_defs; md->name != NULL; md++) {
1808 9307c4c1 bellard
        if (compare_cmd(name, md->name)) {
1809 9307c4c1 bellard
            if (md->get_value) {
1810 e95c8d51 bellard
                *pval = md->get_value(md, md->offset);
1811 9307c4c1 bellard
            } else {
1812 6a00d601 bellard
                CPUState *env = mon_get_cpu();
1813 6a00d601 bellard
                if (!env)
1814 6a00d601 bellard
                    return -2;
1815 6a00d601 bellard
                ptr = (uint8_t *)env + md->offset;
1816 92a31b1f bellard
                switch(md->type) {
1817 92a31b1f bellard
                case MD_I32:
1818 92a31b1f bellard
                    *pval = *(int32_t *)ptr;
1819 92a31b1f bellard
                    break;
1820 92a31b1f bellard
                case MD_TLONG:
1821 92a31b1f bellard
                    *pval = *(target_long *)ptr;
1822 92a31b1f bellard
                    break;
1823 92a31b1f bellard
                default:
1824 92a31b1f bellard
                    *pval = 0;
1825 92a31b1f bellard
                    break;
1826 92a31b1f bellard
                }
1827 9307c4c1 bellard
            }
1828 9307c4c1 bellard
            return 0;
1829 9307c4c1 bellard
        }
1830 9307c4c1 bellard
    }
1831 9307c4c1 bellard
    return -1;
1832 9307c4c1 bellard
}
1833 9307c4c1 bellard
1834 9307c4c1 bellard
static void next(void)
1835 9307c4c1 bellard
{
1836 9307c4c1 bellard
    if (pch != '\0') {
1837 9307c4c1 bellard
        pch++;
1838 9307c4c1 bellard
        while (isspace(*pch))
1839 9307c4c1 bellard
            pch++;
1840 9307c4c1 bellard
    }
1841 9307c4c1 bellard
}
1842 9307c4c1 bellard
1843 c2efc95d blueswir1
static int64_t expr_sum(void);
1844 9307c4c1 bellard
1845 c2efc95d blueswir1
static int64_t expr_unary(void)
1846 9307c4c1 bellard
{
1847 c2efc95d blueswir1
    int64_t n;
1848 9307c4c1 bellard
    char *p;
1849 6a00d601 bellard
    int ret;
1850 9307c4c1 bellard
1851 9307c4c1 bellard
    switch(*pch) {
1852 9307c4c1 bellard
    case '+':
1853 9307c4c1 bellard
        next();
1854 9307c4c1 bellard
        n = expr_unary();
1855 9307c4c1 bellard
        break;
1856 9307c4c1 bellard
    case '-':
1857 9307c4c1 bellard
        next();
1858 9307c4c1 bellard
        n = -expr_unary();
1859 9307c4c1 bellard
        break;
1860 9307c4c1 bellard
    case '~':
1861 9307c4c1 bellard
        next();
1862 9307c4c1 bellard
        n = ~expr_unary();
1863 9307c4c1 bellard
        break;
1864 9307c4c1 bellard
    case '(':
1865 9307c4c1 bellard
        next();
1866 9307c4c1 bellard
        n = expr_sum();
1867 9307c4c1 bellard
        if (*pch != ')') {
1868 9307c4c1 bellard
            expr_error("')' expected");
1869 9307c4c1 bellard
        }
1870 9307c4c1 bellard
        next();
1871 9307c4c1 bellard
        break;
1872 81d0912d bellard
    case '\'':
1873 81d0912d bellard
        pch++;
1874 81d0912d bellard
        if (*pch == '\0')
1875 81d0912d bellard
            expr_error("character constant expected");
1876 81d0912d bellard
        n = *pch;
1877 81d0912d bellard
        pch++;
1878 81d0912d bellard
        if (*pch != '\'')
1879 81d0912d bellard
            expr_error("missing terminating \' character");
1880 81d0912d bellard
        next();
1881 81d0912d bellard
        break;
1882 9307c4c1 bellard
    case '$':
1883 9307c4c1 bellard
        {
1884 9307c4c1 bellard
            char buf[128], *q;
1885 69b34976 ths
            target_long reg=0;
1886 3b46e624 ths
1887 9307c4c1 bellard
            pch++;
1888 9307c4c1 bellard
            q = buf;
1889 9307c4c1 bellard
            while ((*pch >= 'a' && *pch <= 'z') ||
1890 9307c4c1 bellard
                   (*pch >= 'A' && *pch <= 'Z') ||
1891 9307c4c1 bellard
                   (*pch >= '0' && *pch <= '9') ||
1892 57206fd4 bellard
                   *pch == '_' || *pch == '.') {
1893 9307c4c1 bellard
                if ((q - buf) < sizeof(buf) - 1)
1894 9307c4c1 bellard
                    *q++ = *pch;
1895 9307c4c1 bellard
                pch++;
1896 9307c4c1 bellard
            }
1897 9307c4c1 bellard
            while (isspace(*pch))
1898 9307c4c1 bellard
                pch++;
1899 9307c4c1 bellard
            *q = 0;
1900 7743e588 blueswir1
            ret = get_monitor_def(&reg, buf);
1901 6a00d601 bellard
            if (ret == -1)
1902 9307c4c1 bellard
                expr_error("unknown register");
1903 5fafdf24 ths
            else if (ret == -2)
1904 6a00d601 bellard
                expr_error("no cpu defined");
1905 7743e588 blueswir1
            n = reg;
1906 9307c4c1 bellard
        }
1907 9307c4c1 bellard
        break;
1908 9307c4c1 bellard
    case '\0':
1909 9307c4c1 bellard
        expr_error("unexpected end of expression");
1910 9307c4c1 bellard
        n = 0;
1911 9307c4c1 bellard
        break;
1912 9307c4c1 bellard
    default:
1913 7743e588 blueswir1
#if TARGET_PHYS_ADDR_BITS > 32
1914 4f4fbf77 bellard
        n = strtoull(pch, &p, 0);
1915 4f4fbf77 bellard
#else
1916 9307c4c1 bellard
        n = strtoul(pch, &p, 0);
1917 4f4fbf77 bellard
#endif
1918 9307c4c1 bellard
        if (pch == p) {
1919 9307c4c1 bellard
            expr_error("invalid char in expression");
1920 9307c4c1 bellard
        }
1921 9307c4c1 bellard
        pch = p;
1922 9307c4c1 bellard
        while (isspace(*pch))
1923 9307c4c1 bellard
            pch++;
1924 9307c4c1 bellard
        break;
1925 9307c4c1 bellard
    }
1926 9307c4c1 bellard
    return n;
1927 9307c4c1 bellard
}
1928 9307c4c1 bellard
1929 9307c4c1 bellard
1930 c2efc95d blueswir1
static int64_t expr_prod(void)
1931 9307c4c1 bellard
{
1932 c2efc95d blueswir1
    int64_t val, val2;
1933 92a31b1f bellard
    int op;
1934 3b46e624 ths
1935 9307c4c1 bellard
    val = expr_unary();
1936 9307c4c1 bellard
    for(;;) {
1937 9307c4c1 bellard
        op = *pch;
1938 9307c4c1 bellard
        if (op != '*' && op != '/' && op != '%')
1939 9307c4c1 bellard
            break;
1940 9307c4c1 bellard
        next();
1941 9307c4c1 bellard
        val2 = expr_unary();
1942 9307c4c1 bellard
        switch(op) {
1943 9307c4c1 bellard
        default:
1944 9307c4c1 bellard
        case '*':
1945 9307c4c1 bellard
            val *= val2;
1946 9307c4c1 bellard
            break;
1947 9307c4c1 bellard
        case '/':
1948 9307c4c1 bellard
        case '%':
1949 5fafdf24 ths
            if (val2 == 0)
1950 5b60212f bellard
                expr_error("division by zero");
1951 9307c4c1 bellard
            if (op == '/')
1952 9307c4c1 bellard
                val /= val2;
1953 9307c4c1 bellard
            else
1954 9307c4c1 bellard
                val %= val2;
1955 9307c4c1 bellard
            break;
1956 9307c4c1 bellard
        }
1957 9307c4c1 bellard
    }
1958 9307c4c1 bellard
    return val;
1959 9307c4c1 bellard
}
1960 9307c4c1 bellard
1961 c2efc95d blueswir1
static int64_t expr_logic(void)
1962 9307c4c1 bellard
{
1963 c2efc95d blueswir1
    int64_t val, val2;
1964 92a31b1f bellard
    int op;
1965 9307c4c1 bellard
1966 9307c4c1 bellard
    val = expr_prod();
1967 9307c4c1 bellard
    for(;;) {
1968 9307c4c1 bellard
        op = *pch;
1969 9307c4c1 bellard
        if (op != '&' && op != '|' && op != '^')
1970 9307c4c1 bellard
            break;
1971 9307c4c1 bellard
        next();
1972 9307c4c1 bellard
        val2 = expr_prod();
1973 9307c4c1 bellard
        switch(op) {
1974 9307c4c1 bellard
        default:
1975 9307c4c1 bellard
        case '&':
1976 9307c4c1 bellard
            val &= val2;
1977 9307c4c1 bellard
            break;
1978 9307c4c1 bellard
        case '|':
1979 9307c4c1 bellard
            val |= val2;
1980 9307c4c1 bellard
            break;
1981 9307c4c1 bellard
        case '^':
1982 9307c4c1 bellard
            val ^= val2;
1983 9307c4c1 bellard
            break;
1984 9307c4c1 bellard
        }
1985 9307c4c1 bellard
    }
1986 9307c4c1 bellard
    return val;
1987 9307c4c1 bellard
}
1988 9307c4c1 bellard
1989 c2efc95d blueswir1
static int64_t expr_sum(void)
1990 9307c4c1 bellard
{
1991 c2efc95d blueswir1
    int64_t val, val2;
1992 92a31b1f bellard
    int op;
1993 9307c4c1 bellard
1994 9307c4c1 bellard
    val = expr_logic();
1995 9307c4c1 bellard
    for(;;) {
1996 9307c4c1 bellard
        op = *pch;
1997 9307c4c1 bellard
        if (op != '+' && op != '-')
1998 9307c4c1 bellard
            break;
1999 9307c4c1 bellard
        next();
2000 9307c4c1 bellard
        val2 = expr_logic();
2001 9307c4c1 bellard
        if (op == '+')
2002 9307c4c1 bellard
            val += val2;
2003 9307c4c1 bellard
        else
2004 9307c4c1 bellard
            val -= val2;
2005 9307c4c1 bellard
    }
2006 9307c4c1 bellard
    return val;
2007 9307c4c1 bellard
}
2008 9307c4c1 bellard
2009 c2efc95d blueswir1
static int get_expr(int64_t *pval, const char **pp)
2010 9307c4c1 bellard
{
2011 9307c4c1 bellard
    pch = *pp;
2012 9307c4c1 bellard
    if (setjmp(expr_env)) {
2013 9307c4c1 bellard
        *pp = pch;
2014 9307c4c1 bellard
        return -1;
2015 9307c4c1 bellard
    }
2016 9307c4c1 bellard
    while (isspace(*pch))
2017 9307c4c1 bellard
        pch++;
2018 9307c4c1 bellard
    *pval = expr_sum();
2019 9307c4c1 bellard
    *pp = pch;
2020 9307c4c1 bellard
    return 0;
2021 9307c4c1 bellard
}
2022 9307c4c1 bellard
2023 9307c4c1 bellard
static int get_str(char *buf, int buf_size, const char **pp)
2024 9307c4c1 bellard
{
2025 9307c4c1 bellard
    const char *p;
2026 9307c4c1 bellard
    char *q;
2027 9307c4c1 bellard
    int c;
2028 9307c4c1 bellard
2029 81d0912d bellard
    q = buf;
2030 9307c4c1 bellard
    p = *pp;
2031 9307c4c1 bellard
    while (isspace(*p))
2032 9307c4c1 bellard
        p++;
2033 9307c4c1 bellard
    if (*p == '\0') {
2034 9307c4c1 bellard
    fail:
2035 81d0912d bellard
        *q = '\0';
2036 9307c4c1 bellard
        *pp = p;
2037 9307c4c1 bellard
        return -1;
2038 9307c4c1 bellard
    }
2039 9307c4c1 bellard
    if (*p == '\"') {
2040 9307c4c1 bellard
        p++;
2041 9307c4c1 bellard
        while (*p != '\0' && *p != '\"') {
2042 9307c4c1 bellard
            if (*p == '\\') {
2043 9307c4c1 bellard
                p++;
2044 9307c4c1 bellard
                c = *p++;
2045 9307c4c1 bellard
                switch(c) {
2046 9307c4c1 bellard
                case 'n':
2047 9307c4c1 bellard
                    c = '\n';
2048 9307c4c1 bellard
                    break;
2049 9307c4c1 bellard
                case 'r':
2050 9307c4c1 bellard
                    c = '\r';
2051 9307c4c1 bellard
                    break;
2052 9307c4c1 bellard
                case '\\':
2053 9307c4c1 bellard
                case '\'':
2054 9307c4c1 bellard
                case '\"':
2055 9307c4c1 bellard
                    break;
2056 9307c4c1 bellard
                default:
2057 9307c4c1 bellard
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
2058 9307c4c1 bellard
                    goto fail;
2059 9307c4c1 bellard
                }
2060 9307c4c1 bellard
                if ((q - buf) < buf_size - 1) {
2061 9307c4c1 bellard
                    *q++ = c;
2062 9307c4c1 bellard
                }
2063 9307c4c1 bellard
            } else {
2064 9307c4c1 bellard
                if ((q - buf) < buf_size - 1) {
2065 9307c4c1 bellard
                    *q++ = *p;
2066 9307c4c1 bellard
                }
2067 9307c4c1 bellard
                p++;
2068 9307c4c1 bellard
            }
2069 9307c4c1 bellard
        }
2070 9307c4c1 bellard
        if (*p != '\"') {
2071 5b60212f bellard
            qemu_printf("unterminated string\n");
2072 9307c4c1 bellard
            goto fail;
2073 9307c4c1 bellard
        }
2074 9307c4c1 bellard
        p++;
2075 9307c4c1 bellard
    } else {
2076 9307c4c1 bellard
        while (*p != '\0' && !isspace(*p)) {
2077 9307c4c1 bellard
            if ((q - buf) < buf_size - 1) {
2078 9307c4c1 bellard
                *q++ = *p;
2079 9307c4c1 bellard
            }
2080 9307c4c1 bellard
            p++;
2081 9307c4c1 bellard
        }
2082 9307c4c1 bellard
    }
2083 81d0912d bellard
    *q = '\0';
2084 9307c4c1 bellard
    *pp = p;
2085 9307c4c1 bellard
    return 0;
2086 9307c4c1 bellard
}
2087 9307c4c1 bellard
2088 9307c4c1 bellard
static int default_fmt_format = 'x';
2089 9307c4c1 bellard
static int default_fmt_size = 4;
2090 9307c4c1 bellard
2091 9307c4c1 bellard
#define MAX_ARGS 16
2092 9307c4c1 bellard
2093 7e2515e8 bellard
static void monitor_handle_command(const char *cmdline)
2094 9307c4c1 bellard
{
2095 9307c4c1 bellard
    const char *p, *pstart, *typestr;
2096 9307c4c1 bellard
    char *q;
2097 9307c4c1 bellard
    int c, nb_args, len, i, has_arg;
2098 9dc39cba bellard
    term_cmd_t *cmd;
2099 9307c4c1 bellard
    char cmdname[256];
2100 9307c4c1 bellard
    char buf[1024];
2101 9307c4c1 bellard
    void *str_allocated[MAX_ARGS];
2102 9307c4c1 bellard
    void *args[MAX_ARGS];
2103 9dc39cba bellard
2104 9dc39cba bellard
#ifdef DEBUG
2105 9dc39cba bellard
    term_printf("command='%s'\n", cmdline);
2106 9dc39cba bellard
#endif
2107 3b46e624 ths
2108 9307c4c1 bellard
    /* extract the command name */
2109 9dc39cba bellard
    p = cmdline;
2110 9307c4c1 bellard
    q = cmdname;
2111 9307c4c1 bellard
    while (isspace(*p))
2112 9307c4c1 bellard
        p++;
2113 9307c4c1 bellard
    if (*p == '\0')
2114 9307c4c1 bellard
        return;
2115 9307c4c1 bellard
    pstart = p;
2116 9307c4c1 bellard
    while (*p != '\0' && *p != '/' && !isspace(*p))
2117 9307c4c1 bellard
        p++;
2118 9307c4c1 bellard
    len = p - pstart;
2119 9307c4c1 bellard
    if (len > sizeof(cmdname) - 1)
2120 9307c4c1 bellard
        len = sizeof(cmdname) - 1;
2121 9307c4c1 bellard
    memcpy(cmdname, pstart, len);
2122 9307c4c1 bellard
    cmdname[len] = '\0';
2123 3b46e624 ths
2124 9307c4c1 bellard
    /* find the command */
2125 9307c4c1 bellard
    for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2126 5fafdf24 ths
        if (compare_cmd(cmdname, cmd->name))
2127 9307c4c1 bellard
            goto found;
2128 9307c4c1 bellard
    }
2129 9307c4c1 bellard
    term_printf("unknown command: '%s'\n", cmdname);
2130 9307c4c1 bellard
    return;
2131 9307c4c1 bellard
 found:
2132 9307c4c1 bellard
2133 9307c4c1 bellard
    for(i = 0; i < MAX_ARGS; i++)
2134 9307c4c1 bellard
        str_allocated[i] = NULL;
2135 3b46e624 ths
2136 9307c4c1 bellard
    /* parse the parameters */
2137 9307c4c1 bellard
    typestr = cmd->args_type;
2138 9307c4c1 bellard
    nb_args = 0;
2139 9dc39cba bellard
    for(;;) {
2140 9307c4c1 bellard
        c = *typestr;
2141 9307c4c1 bellard
        if (c == '\0')
2142 9dc39cba bellard
            break;
2143 9307c4c1 bellard
        typestr++;
2144 9307c4c1 bellard
        switch(c) {
2145 9307c4c1 bellard
        case 'F':
2146 81d0912d bellard
        case 'B':
2147 9307c4c1 bellard
        case 's':
2148 9307c4c1 bellard
            {
2149 9307c4c1 bellard
                int ret;
2150 9307c4c1 bellard
                char *str;
2151 3b46e624 ths
2152 5fafdf24 ths
                while (isspace(*p))
2153 9307c4c1 bellard
                    p++;
2154 9307c4c1 bellard
                if (*typestr == '?') {
2155 9307c4c1 bellard
                    typestr++;
2156 9307c4c1 bellard
                    if (*p == '\0') {
2157 9307c4c1 bellard
                        /* no optional string: NULL argument */
2158 9307c4c1 bellard
                        str = NULL;
2159 9307c4c1 bellard
                        goto add_str;
2160 9307c4c1 bellard
                    }
2161 9307c4c1 bellard
                }
2162 9307c4c1 bellard
                ret = get_str(buf, sizeof(buf), &p);
2163 9307c4c1 bellard
                if (ret < 0) {
2164 81d0912d bellard
                    switch(c) {
2165 81d0912d bellard
                    case 'F':
2166 9307c4c1 bellard
                        term_printf("%s: filename expected\n", cmdname);
2167 81d0912d bellard
                        break;
2168 81d0912d bellard
                    case 'B':
2169 81d0912d bellard
                        term_printf("%s: block device name expected\n", cmdname);
2170 81d0912d bellard
                        break;
2171 81d0912d bellard
                    default:
2172 9307c4c1 bellard
                        term_printf("%s: string expected\n", cmdname);
2173 81d0912d bellard
                        break;
2174 81d0912d bellard
                    }
2175 9307c4c1 bellard
                    goto fail;
2176 9307c4c1 bellard
                }
2177 9307c4c1 bellard
                str = qemu_malloc(strlen(buf) + 1);
2178 9307c4c1 bellard
                strcpy(str, buf);
2179 9307c4c1 bellard
                str_allocated[nb_args] = str;
2180 9307c4c1 bellard
            add_str:
2181 9307c4c1 bellard
                if (nb_args >= MAX_ARGS) {
2182 9307c4c1 bellard
                error_args:
2183 9307c4c1 bellard
                    term_printf("%s: too many arguments\n", cmdname);
2184 9307c4c1 bellard
                    goto fail;
2185 9307c4c1 bellard
                }
2186 9307c4c1 bellard
                args[nb_args++] = str;
2187 9307c4c1 bellard
            }
2188 9dc39cba bellard
            break;
2189 9307c4c1 bellard
        case '/':
2190 9307c4c1 bellard
            {
2191 9307c4c1 bellard
                int count, format, size;
2192 3b46e624 ths
2193 9307c4c1 bellard
                while (isspace(*p))
2194 9307c4c1 bellard
                    p++;
2195 9307c4c1 bellard
                if (*p == '/') {
2196 9307c4c1 bellard
                    /* format found */
2197 9307c4c1 bellard
                    p++;
2198 9307c4c1 bellard
                    count = 1;
2199 9307c4c1 bellard
                    if (isdigit(*p)) {
2200 9307c4c1 bellard
                        count = 0;
2201 9307c4c1 bellard
                        while (isdigit(*p)) {
2202 9307c4c1 bellard
                            count = count * 10 + (*p - '0');
2203 9307c4c1 bellard
                            p++;
2204 9307c4c1 bellard
                        }
2205 9307c4c1 bellard
                    }
2206 9307c4c1 bellard
                    size = -1;
2207 9307c4c1 bellard
                    format = -1;
2208 9307c4c1 bellard
                    for(;;) {
2209 9307c4c1 bellard
                        switch(*p) {
2210 9307c4c1 bellard
                        case 'o':
2211 9307c4c1 bellard
                        case 'd':
2212 9307c4c1 bellard
                        case 'u':
2213 9307c4c1 bellard
                        case 'x':
2214 9307c4c1 bellard
                        case 'i':
2215 9307c4c1 bellard
                        case 'c':
2216 9307c4c1 bellard
                            format = *p++;
2217 9307c4c1 bellard
                            break;
2218 9307c4c1 bellard
                        case 'b':
2219 9307c4c1 bellard
                            size = 1;
2220 9307c4c1 bellard
                            p++;
2221 9307c4c1 bellard
                            break;
2222 9307c4c1 bellard
                        case 'h':
2223 9307c4c1 bellard
                            size = 2;
2224 9307c4c1 bellard
                            p++;
2225 9307c4c1 bellard
                            break;
2226 9307c4c1 bellard
                        case 'w':
2227 9307c4c1 bellard
                            size = 4;
2228 9307c4c1 bellard
                            p++;
2229 9307c4c1 bellard
                            break;
2230 9307c4c1 bellard
                        case 'g':
2231 9307c4c1 bellard
                        case 'L':
2232 9307c4c1 bellard
                            size = 8;
2233 9307c4c1 bellard
                            p++;
2234 9307c4c1 bellard
                            break;
2235 9307c4c1 bellard
                        default:
2236 9307c4c1 bellard
                            goto next;
2237 9307c4c1 bellard
                        }
2238 9307c4c1 bellard
                    }
2239 9307c4c1 bellard
                next:
2240 9307c4c1 bellard
                    if (*p != '\0' && !isspace(*p)) {
2241 9307c4c1 bellard
                        term_printf("invalid char in format: '%c'\n", *p);
2242 9307c4c1 bellard
                        goto fail;
2243 9307c4c1 bellard
                    }
2244 9307c4c1 bellard
                    if (format < 0)
2245 9307c4c1 bellard
                        format = default_fmt_format;
2246 4c27ba27 bellard
                    if (format != 'i') {
2247 4c27ba27 bellard
                        /* for 'i', not specifying a size gives -1 as size */
2248 4c27ba27 bellard
                        if (size < 0)
2249 4c27ba27 bellard
                            size = default_fmt_size;
2250 4c27ba27 bellard
                    }
2251 9307c4c1 bellard
                    default_fmt_size = size;
2252 9307c4c1 bellard
                    default_fmt_format = format;
2253 9307c4c1 bellard
                } else {
2254 9307c4c1 bellard
                    count = 1;
2255 9307c4c1 bellard
                    format = default_fmt_format;
2256 4c27ba27 bellard
                    if (format != 'i') {
2257 4c27ba27 bellard
                        size = default_fmt_size;
2258 4c27ba27 bellard
                    } else {
2259 4c27ba27 bellard
                        size = -1;
2260 4c27ba27 bellard
                    }
2261 9307c4c1 bellard
                }
2262 9307c4c1 bellard
                if (nb_args + 3 > MAX_ARGS)
2263 9307c4c1 bellard
                    goto error_args;
2264 1c5bf3bf j_mayer
                args[nb_args++] = (void*)(long)count;
2265 1c5bf3bf j_mayer
                args[nb_args++] = (void*)(long)format;
2266 1c5bf3bf j_mayer
                args[nb_args++] = (void*)(long)size;
2267 9307c4c1 bellard
            }
2268 9dc39cba bellard
            break;
2269 9307c4c1 bellard
        case 'i':
2270 92a31b1f bellard
        case 'l':
2271 9307c4c1 bellard
            {
2272 c2efc95d blueswir1
                int64_t val;
2273 7743e588 blueswir1
2274 5fafdf24 ths
                while (isspace(*p))
2275 9307c4c1 bellard
                    p++;
2276 3440557b bellard
                if (*typestr == '?' || *typestr == '.') {
2277 3440557b bellard
                    if (*typestr == '?') {
2278 3440557b bellard
                        if (*p == '\0')
2279 3440557b bellard
                            has_arg = 0;
2280 3440557b bellard
                        else
2281 3440557b bellard
                            has_arg = 1;
2282 3440557b bellard
                    } else {
2283 3440557b bellard
                        if (*p == '.') {
2284 3440557b bellard
                            p++;
2285 5fafdf24 ths
                            while (isspace(*p))
2286 3440557b bellard
                                p++;
2287 3440557b bellard
                            has_arg = 1;
2288 3440557b bellard
                        } else {
2289 3440557b bellard
                            has_arg = 0;
2290 3440557b bellard
                        }
2291 3440557b bellard
                    }
2292 13224a87 bellard
                    typestr++;
2293 9307c4c1 bellard
                    if (nb_args >= MAX_ARGS)
2294 9307c4c1 bellard
                        goto error_args;
2295 1c5bf3bf j_mayer
                    args[nb_args++] = (void *)(long)has_arg;
2296 9307c4c1 bellard
                    if (!has_arg) {
2297 9307c4c1 bellard
                        if (nb_args >= MAX_ARGS)
2298 9307c4c1 bellard
                            goto error_args;
2299 9307c4c1 bellard
                        val = -1;
2300 9307c4c1 bellard
                        goto add_num;
2301 9307c4c1 bellard
                    }
2302 9307c4c1 bellard
                }
2303 9307c4c1 bellard
                if (get_expr(&val, &p))
2304 9307c4c1 bellard
                    goto fail;
2305 9307c4c1 bellard
            add_num:
2306 92a31b1f bellard
                if (c == 'i') {
2307 92a31b1f bellard
                    if (nb_args >= MAX_ARGS)
2308 92a31b1f bellard
                        goto error_args;
2309 1c5bf3bf j_mayer
                    args[nb_args++] = (void *)(long)val;
2310 92a31b1f bellard
                } else {
2311 92a31b1f bellard
                    if ((nb_args + 1) >= MAX_ARGS)
2312 92a31b1f bellard
                        goto error_args;
2313 7743e588 blueswir1
#if TARGET_PHYS_ADDR_BITS > 32
2314 1c5bf3bf j_mayer
                    args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2315 92a31b1f bellard
#else
2316 92a31b1f bellard
                    args[nb_args++] = (void *)0;
2317 92a31b1f bellard
#endif
2318 1c5bf3bf j_mayer
                    args[nb_args++] = (void *)(long)(val & 0xffffffff);
2319 92a31b1f bellard
                }
2320 9307c4c1 bellard
            }
2321 9307c4c1 bellard
            break;
2322 9307c4c1 bellard
        case '-':
2323 9307c4c1 bellard
            {
2324 9307c4c1 bellard
                int has_option;
2325 9307c4c1 bellard
                /* option */
2326 3b46e624 ths
2327 9307c4c1 bellard
                c = *typestr++;
2328 9307c4c1 bellard
                if (c == '\0')
2329 9307c4c1 bellard
                    goto bad_type;
2330 5fafdf24 ths
                while (isspace(*p))
2331 9307c4c1 bellard
                    p++;
2332 9307c4c1 bellard
                has_option = 0;
2333 9307c4c1 bellard
                if (*p == '-') {
2334 9307c4c1 bellard
                    p++;
2335 9307c4c1 bellard
                    if (*p != c) {
2336 5fafdf24 ths
                        term_printf("%s: unsupported option -%c\n",
2337 9307c4c1 bellard
                                    cmdname, *p);
2338 9307c4c1 bellard
                        goto fail;
2339 9307c4c1 bellard
                    }
2340 9307c4c1 bellard
                    p++;
2341 9307c4c1 bellard
                    has_option = 1;
2342 9307c4c1 bellard
                }
2343 9307c4c1 bellard
                if (nb_args >= MAX_ARGS)
2344 9307c4c1 bellard
                    goto error_args;
2345 1c5bf3bf j_mayer
                args[nb_args++] = (void *)(long)has_option;
2346 9307c4c1 bellard
            }
2347 9307c4c1 bellard
            break;
2348 9307c4c1 bellard
        default:
2349 9307c4c1 bellard
        bad_type:
2350 9307c4c1 bellard
            term_printf("%s: unknown type '%c'\n", cmdname, c);
2351 9307c4c1 bellard
            goto fail;
2352 9307c4c1 bellard
        }
2353 9dc39cba bellard
    }
2354 9307c4c1 bellard
    /* check that all arguments were parsed */
2355 9307c4c1 bellard
    while (isspace(*p))
2356 9307c4c1 bellard
        p++;
2357 9307c4c1 bellard
    if (*p != '\0') {
2358 5fafdf24 ths
        term_printf("%s: extraneous characters at the end of line\n",
2359 9307c4c1 bellard
                    cmdname);
2360 9307c4c1 bellard
        goto fail;
2361 9dc39cba bellard
    }
2362 9307c4c1 bellard
2363 9307c4c1 bellard
    switch(nb_args) {
2364 9307c4c1 bellard
    case 0:
2365 9307c4c1 bellard
        cmd->handler();
2366 9307c4c1 bellard
        break;
2367 9307c4c1 bellard
    case 1:
2368 9307c4c1 bellard
        cmd->handler(args[0]);
2369 9307c4c1 bellard
        break;
2370 9307c4c1 bellard
    case 2:
2371 9307c4c1 bellard
        cmd->handler(args[0], args[1]);
2372 9307c4c1 bellard
        break;
2373 9307c4c1 bellard
    case 3:
2374 9307c4c1 bellard
        cmd->handler(args[0], args[1], args[2]);
2375 9307c4c1 bellard
        break;
2376 9307c4c1 bellard
    case 4:
2377 9307c4c1 bellard
        cmd->handler(args[0], args[1], args[2], args[3]);
2378 9307c4c1 bellard
        break;
2379 9307c4c1 bellard
    case 5:
2380 9307c4c1 bellard
        cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2381 9307c4c1 bellard
        break;
2382 3440557b bellard
    case 6:
2383 3440557b bellard
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2384 3440557b bellard
        break;
2385 ec36b695 bellard
    case 7:
2386 ec36b695 bellard
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2387 ec36b695 bellard
        break;
2388 9307c4c1 bellard
    default:
2389 9307c4c1 bellard
        term_printf("unsupported number of arguments: %d\n", nb_args);
2390 9307c4c1 bellard
        goto fail;
2391 9dc39cba bellard
    }
2392 9307c4c1 bellard
 fail:
2393 9307c4c1 bellard
    for(i = 0; i < MAX_ARGS; i++)
2394 9307c4c1 bellard
        qemu_free(str_allocated[i]);
2395 9dc39cba bellard
    return;
2396 9dc39cba bellard
}
2397 9dc39cba bellard
2398 81d0912d bellard
static void cmd_completion(const char *name, const char *list)
2399 81d0912d bellard
{
2400 81d0912d bellard
    const char *p, *pstart;
2401 81d0912d bellard
    char cmd[128];
2402 81d0912d bellard
    int len;
2403 81d0912d bellard
2404 81d0912d bellard
    p = list;
2405 81d0912d bellard
    for(;;) {
2406 81d0912d bellard
        pstart = p;
2407 81d0912d bellard
        p = strchr(p, '|');
2408 81d0912d bellard
        if (!p)
2409 81d0912d bellard
            p = pstart + strlen(pstart);
2410 81d0912d bellard
        len = p - pstart;
2411 81d0912d bellard
        if (len > sizeof(cmd) - 2)
2412 81d0912d bellard
            len = sizeof(cmd) - 2;
2413 81d0912d bellard
        memcpy(cmd, pstart, len);
2414 81d0912d bellard
        cmd[len] = '\0';
2415 81d0912d bellard
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2416 81d0912d bellard
            add_completion(cmd);
2417 81d0912d bellard
        }
2418 81d0912d bellard
        if (*p == '\0')
2419 81d0912d bellard
            break;
2420 81d0912d bellard
        p++;
2421 81d0912d bellard
    }
2422 81d0912d bellard
}
2423 81d0912d bellard
2424 81d0912d bellard
static void file_completion(const char *input)
2425 81d0912d bellard
{
2426 81d0912d bellard
    DIR *ffs;
2427 81d0912d bellard
    struct dirent *d;
2428 81d0912d bellard
    char path[1024];
2429 81d0912d bellard
    char file[1024], file_prefix[1024];
2430 81d0912d bellard
    int input_path_len;
2431 81d0912d bellard
    const char *p;
2432 81d0912d bellard
2433 5fafdf24 ths
    p = strrchr(input, '/');
2434 81d0912d bellard
    if (!p) {
2435 81d0912d bellard
        input_path_len = 0;
2436 81d0912d bellard
        pstrcpy(file_prefix, sizeof(file_prefix), input);
2437 81d0912d bellard
        strcpy(path, ".");
2438 81d0912d bellard
    } else {
2439 81d0912d bellard
        input_path_len = p - input + 1;
2440 81d0912d bellard
        memcpy(path, input, input_path_len);
2441 81d0912d bellard
        if (input_path_len > sizeof(path) - 1)
2442 81d0912d bellard
            input_path_len = sizeof(path) - 1;
2443 81d0912d bellard
        path[input_path_len] = '\0';
2444 81d0912d bellard
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2445 81d0912d bellard
    }
2446 81d0912d bellard
#ifdef DEBUG_COMPLETION
2447 81d0912d bellard
    term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2448 81d0912d bellard
#endif
2449 81d0912d bellard
    ffs = opendir(path);
2450 81d0912d bellard
    if (!ffs)
2451 81d0912d bellard
        return;
2452 81d0912d bellard
    for(;;) {
2453 81d0912d bellard
        struct stat sb;
2454 81d0912d bellard
        d = readdir(ffs);
2455 81d0912d bellard
        if (!d)
2456 81d0912d bellard
            break;
2457 81d0912d bellard
        if (strstart(d->d_name, file_prefix, NULL)) {
2458 81d0912d bellard
            memcpy(file, input, input_path_len);
2459 81d0912d bellard
            strcpy(file + input_path_len, d->d_name);
2460 81d0912d bellard
            /* stat the file to find out if it's a directory.
2461 81d0912d bellard
             * In that case add a slash to speed up typing long paths
2462 81d0912d bellard
             */
2463 81d0912d bellard
            stat(file, &sb);
2464 81d0912d bellard
            if(S_ISDIR(sb.st_mode))
2465 81d0912d bellard
                strcat(file, "/");
2466 81d0912d bellard
            add_completion(file);
2467 81d0912d bellard
        }
2468 81d0912d bellard
    }
2469 81d0912d bellard
    closedir(ffs);
2470 81d0912d bellard
}
2471 81d0912d bellard
2472 81d0912d bellard
static void block_completion_it(void *opaque, const char *name)
2473 81d0912d bellard
{
2474 81d0912d bellard
    const char *input = opaque;
2475 81d0912d bellard
2476 81d0912d bellard
    if (input[0] == '\0' ||
2477 81d0912d bellard
        !strncmp(name, (char *)input, strlen(input))) {
2478 81d0912d bellard
        add_completion(name);
2479 81d0912d bellard
    }
2480 81d0912d bellard
}
2481 81d0912d bellard
2482 81d0912d bellard
/* NOTE: this parser is an approximate form of the real command parser */
2483 81d0912d bellard
static void parse_cmdline(const char *cmdline,
2484 81d0912d bellard
                         int *pnb_args, char **args)
2485 81d0912d bellard
{
2486 81d0912d bellard
    const char *p;
2487 81d0912d bellard
    int nb_args, ret;
2488 81d0912d bellard
    char buf[1024];
2489 81d0912d bellard
2490 81d0912d bellard
    p = cmdline;
2491 81d0912d bellard
    nb_args = 0;
2492 81d0912d bellard
    for(;;) {
2493 81d0912d bellard
        while (isspace(*p))
2494 81d0912d bellard
            p++;
2495 81d0912d bellard
        if (*p == '\0')
2496 81d0912d bellard
            break;
2497 81d0912d bellard
        if (nb_args >= MAX_ARGS)
2498 81d0912d bellard
            break;
2499 81d0912d bellard
        ret = get_str(buf, sizeof(buf), &p);
2500 81d0912d bellard
        args[nb_args] = qemu_strdup(buf);
2501 81d0912d bellard
        nb_args++;
2502 81d0912d bellard
        if (ret < 0)
2503 81d0912d bellard
            break;
2504 81d0912d bellard
    }
2505 81d0912d bellard
    *pnb_args = nb_args;
2506 81d0912d bellard
}
2507 81d0912d bellard
2508 7e2515e8 bellard
void readline_find_completion(const char *cmdline)
2509 81d0912d bellard
{
2510 81d0912d bellard
    const char *cmdname;
2511 81d0912d bellard
    char *args[MAX_ARGS];
2512 81d0912d bellard
    int nb_args, i, len;
2513 81d0912d bellard
    const char *ptype, *str;
2514 81d0912d bellard
    term_cmd_t *cmd;
2515 64866c3d bellard
    const KeyDef *key;
2516 81d0912d bellard
2517 81d0912d bellard
    parse_cmdline(cmdline, &nb_args, args);
2518 81d0912d bellard
#ifdef DEBUG_COMPLETION
2519 81d0912d bellard
    for(i = 0; i < nb_args; i++) {
2520 81d0912d bellard
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2521 81d0912d bellard
    }
2522 81d0912d bellard
#endif
2523 81d0912d bellard
2524 81d0912d bellard
    /* if the line ends with a space, it means we want to complete the
2525 81d0912d bellard
       next arg */
2526 81d0912d bellard
    len = strlen(cmdline);
2527 81d0912d bellard
    if (len > 0 && isspace(cmdline[len - 1])) {
2528 81d0912d bellard
        if (nb_args >= MAX_ARGS)
2529 81d0912d bellard
            return;
2530 81d0912d bellard
        args[nb_args++] = qemu_strdup("");
2531 81d0912d bellard
    }
2532 81d0912d bellard
    if (nb_args <= 1) {
2533 81d0912d bellard
        /* command completion */
2534 81d0912d bellard
        if (nb_args == 0)
2535 81d0912d bellard
            cmdname = "";
2536 81d0912d bellard
        else
2537 81d0912d bellard
            cmdname = args[0];
2538 81d0912d bellard
        completion_index = strlen(cmdname);
2539 81d0912d bellard
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2540 81d0912d bellard
            cmd_completion(cmdname, cmd->name);
2541 81d0912d bellard
        }
2542 81d0912d bellard
    } else {
2543 81d0912d bellard
        /* find the command */
2544 81d0912d bellard
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2545 81d0912d bellard
            if (compare_cmd(args[0], cmd->name))
2546 81d0912d bellard
                goto found;
2547 81d0912d bellard
        }
2548 81d0912d bellard
        return;
2549 81d0912d bellard
    found:
2550 81d0912d bellard
        ptype = cmd->args_type;
2551 81d0912d bellard
        for(i = 0; i < nb_args - 2; i++) {
2552 81d0912d bellard
            if (*ptype != '\0') {
2553 81d0912d bellard
                ptype++;
2554 81d0912d bellard
                while (*ptype == '?')
2555 81d0912d bellard
                    ptype++;
2556 81d0912d bellard
            }
2557 81d0912d bellard
        }
2558 81d0912d bellard
        str = args[nb_args - 1];
2559 81d0912d bellard
        switch(*ptype) {
2560 81d0912d bellard
        case 'F':
2561 81d0912d bellard
            /* file completion */
2562 81d0912d bellard
            completion_index = strlen(str);
2563 81d0912d bellard
            file_completion(str);
2564 81d0912d bellard
            break;
2565 81d0912d bellard
        case 'B':
2566 81d0912d bellard
            /* block device name completion */
2567 81d0912d bellard
            completion_index = strlen(str);
2568 81d0912d bellard
            bdrv_iterate(block_completion_it, (void *)str);
2569 81d0912d bellard
            break;
2570 7fe48483 bellard
        case 's':
2571 7fe48483 bellard
            /* XXX: more generic ? */
2572 7fe48483 bellard
            if (!strcmp(cmd->name, "info")) {
2573 7fe48483 bellard
                completion_index = strlen(str);
2574 7fe48483 bellard
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2575 7fe48483 bellard
                    cmd_completion(str, cmd->name);
2576 7fe48483 bellard
                }
2577 64866c3d bellard
            } else if (!strcmp(cmd->name, "sendkey")) {
2578 64866c3d bellard
                completion_index = strlen(str);
2579 64866c3d bellard
                for(key = key_defs; key->name != NULL; key++) {
2580 64866c3d bellard
                    cmd_completion(str, key->name);
2581 64866c3d bellard
                }
2582 7fe48483 bellard
            }
2583 7fe48483 bellard
            break;
2584 81d0912d bellard
        default:
2585 81d0912d bellard
            break;
2586 81d0912d bellard
        }
2587 81d0912d bellard
    }
2588 81d0912d bellard
    for(i = 0; i < nb_args; i++)
2589 81d0912d bellard
        qemu_free(args[i]);
2590 81d0912d bellard
}
2591 81d0912d bellard
2592 7e2515e8 bellard
static int term_can_read(void *opaque)
2593 9dc39cba bellard
{
2594 7e2515e8 bellard
    return 128;
2595 9dc39cba bellard
}
2596 9dc39cba bellard
2597 7e2515e8 bellard
static void term_read(void *opaque, const uint8_t *buf, int size)
2598 9dc39cba bellard
{
2599 7e2515e8 bellard
    int i;
2600 7e2515e8 bellard
    for(i = 0; i < size; i++)
2601 7e2515e8 bellard
        readline_handle_byte(buf[i]);
2602 9dc39cba bellard
}
2603 9dc39cba bellard
2604 7e2515e8 bellard
static void monitor_start_input(void);
2605 9dc39cba bellard
2606 7e2515e8 bellard
static void monitor_handle_command1(void *opaque, const char *cmdline)
2607 aa455485 bellard
{
2608 7e2515e8 bellard
    monitor_handle_command(cmdline);
2609 7e2515e8 bellard
    monitor_start_input();
2610 aa455485 bellard
}
2611 aa455485 bellard
2612 7e2515e8 bellard
static void monitor_start_input(void)
2613 aa455485 bellard
{
2614 7e2515e8 bellard
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2615 aa455485 bellard
}
2616 aa455485 bellard
2617 86e94dea ths
static void term_event(void *opaque, int event)
2618 86e94dea ths
{
2619 86e94dea ths
    if (event != CHR_EVENT_RESET)
2620 86e94dea ths
        return;
2621 86e94dea ths
2622 86e94dea ths
    if (!hide_banner)
2623 86e94dea ths
            term_printf("QEMU %s monitor - type 'help' for more information\n",
2624 86e94dea ths
                        QEMU_VERSION);
2625 86e94dea ths
    monitor_start_input();
2626 86e94dea ths
}
2627 86e94dea ths
2628 20d8a3ed ths
static int is_first_init = 1;
2629 20d8a3ed ths
2630 7e2515e8 bellard
void monitor_init(CharDriverState *hd, int show_banner)
2631 aa455485 bellard
{
2632 20d8a3ed ths
    int i;
2633 20d8a3ed ths
2634 20d8a3ed ths
    if (is_first_init) {
2635 20d8a3ed ths
        for (i = 0; i < MAX_MON; i++) {
2636 20d8a3ed ths
            monitor_hd[i] = NULL;
2637 20d8a3ed ths
        }
2638 20d8a3ed ths
        is_first_init = 0;
2639 20d8a3ed ths
    }
2640 20d8a3ed ths
    for (i = 0; i < MAX_MON; i++) {
2641 20d8a3ed ths
        if (monitor_hd[i] == NULL) {
2642 20d8a3ed ths
            monitor_hd[i] = hd;
2643 20d8a3ed ths
            break;
2644 20d8a3ed ths
        }
2645 20d8a3ed ths
    }
2646 20d8a3ed ths
2647 86e94dea ths
    hide_banner = !show_banner;
2648 86e94dea ths
2649 e5b0bc44 pbrook
    qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2650 ad8efe4b blueswir1
2651 ad8efe4b blueswir1
    readline_start("", 0, monitor_handle_command1, NULL);
2652 aa455485 bellard
}
2653 aa455485 bellard
2654 7e2515e8 bellard
/* XXX: use threads ? */
2655 7e2515e8 bellard
/* modal monitor readline */
2656 7e2515e8 bellard
static int monitor_readline_started;
2657 7e2515e8 bellard
static char *monitor_readline_buf;
2658 7e2515e8 bellard
static int monitor_readline_buf_size;
2659 81d0912d bellard
2660 7e2515e8 bellard
static void monitor_readline_cb(void *opaque, const char *input)
2661 81d0912d bellard
{
2662 7e2515e8 bellard
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2663 7e2515e8 bellard
    monitor_readline_started = 0;
2664 81d0912d bellard
}
2665 81d0912d bellard
2666 7e2515e8 bellard
void monitor_readline(const char *prompt, int is_password,
2667 7e2515e8 bellard
                      char *buf, int buf_size)
2668 9dc39cba bellard
{
2669 20d8a3ed ths
    int i;
2670 20d8a3ed ths
2671 7e2515e8 bellard
    if (is_password) {
2672 20d8a3ed ths
        for (i = 0; i < MAX_MON; i++)
2673 20d8a3ed ths
            if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2674 20d8a3ed ths
                qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2675 9dc39cba bellard
    }
2676 7e2515e8 bellard
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
2677 7e2515e8 bellard
    monitor_readline_buf = buf;
2678 7e2515e8 bellard
    monitor_readline_buf_size = buf_size;
2679 7e2515e8 bellard
    monitor_readline_started = 1;
2680 7e2515e8 bellard
    while (monitor_readline_started) {
2681 7e2515e8 bellard
        main_loop_wait(10);
2682 9dc39cba bellard
    }
2683 9dc39cba bellard
}