Statistics
| Branch: | Revision:

root / monitor.c @ 298e01b6

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