Statistics
| Branch: | Revision:

root / monitor.c @ 8c5e95d8

History | View | Annotate | Download (71.3 kB)

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