Statistics
| Branch: | Revision:

root / monitor.c @ f46f15bc

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