Statistics
| Branch: | Revision:

root / monitor.c @ 3d7b417e

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