Statistics
| Branch: | Revision:

root / monitor.c @ 5d794885

History | View | Annotate | Download (70.4 kB)

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