Statistics
| Branch: | Revision:

root / monitor.c @ ef397e88

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