Statistics
| Branch: | Revision:

root / monitor.c @ bd7a7b33

History | View | Annotate | Download (69.6 kB)

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