Statistics
| Branch: | Revision:

root / monitor.c @ c8f79b67

History | View | Annotate | Download (72.5 kB)

1
/*
2
 * QEMU monitor
3
 *
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include "hw/hw.h"
25
#include "hw/usb.h"
26
#include "hw/pcmcia.h"
27
#include "hw/pc.h"
28
#include "hw/pci.h"
29
#include "gdbstub.h"
30
#include "net.h"
31
#include "qemu-char.h"
32
#include "sysemu.h"
33
#include "console.h"
34
#include "block.h"
35
#include "audio/audio.h"
36
#include "disas.h"
37
#include <dirent.h>
38
#include "qemu-timer.h"
39

    
40
//#define DEBUG
41
//#define DEBUG_COMPLETION
42

    
43
#ifndef offsetof
44
#define offsetof(type, field) ((size_t) &((type *)0)->field)
45
#endif
46

    
47
/*
48
 * Supported types:
49
 *
50
 * 'F'          filename
51
 * 'B'          block device name
52
 * 's'          string (accept optional quote)
53
 * 'i'          32 bit integer
54
 * 'l'          target long (32 or 64 bit)
55
 * '/'          optional gdb-like print format (like "/10x")
56
 *
57
 * '?'          optional type (for 'F', 's' and 'i')
58
 *
59
 */
60

    
61
typedef struct term_cmd_t {
62
    const char *name;
63
    const char *args_type;
64
    void *handler;
65
    const char *params;
66
    const char *help;
67
} term_cmd_t;
68

    
69
#define MAX_MON 4
70
static CharDriverState *monitor_hd[MAX_MON];
71
static int hide_banner;
72

    
73
static term_cmd_t term_cmds[];
74
static term_cmd_t info_cmds[];
75

    
76
static uint8_t term_outbuf[1024];
77
static int term_outbuf_index;
78

    
79
CPUState *mon_cpu = NULL;
80

    
81
void term_flush(void)
82
{
83
    int i;
84
    if (term_outbuf_index > 0) {
85
        for (i = 0; i < MAX_MON; i++)
86
            if (monitor_hd[i] && monitor_hd[i]->focus == 0)
87
                qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
88
        term_outbuf_index = 0;
89
    }
90
}
91

    
92
/* flush at every end of line or if the buffer is full */
93
void term_puts(const char *str)
94
{
95
    char c;
96
    for(;;) {
97
        c = *str++;
98
        if (c == '\0')
99
            break;
100
        if (c == '\n')
101
            term_outbuf[term_outbuf_index++] = '\r';
102
        term_outbuf[term_outbuf_index++] = c;
103
        if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
104
            c == '\n')
105
            term_flush();
106
    }
107
}
108

    
109
void term_vprintf(const char *fmt, va_list ap)
110
{
111
    char buf[4096];
112
    vsnprintf(buf, sizeof(buf), fmt, ap);
113
    term_puts(buf);
114
}
115

    
116
void term_printf(const char *fmt, ...)
117
{
118
    va_list ap;
119
    va_start(ap, fmt);
120
    term_vprintf(fmt, ap);
121
    va_end(ap);
122
}
123

    
124
void term_print_filename(const char *filename)
125
{
126
    int i;
127

    
128
    for (i = 0; filename[i]; i++) {
129
        switch (filename[i]) {
130
        case ' ':
131
        case '"':
132
        case '\\':
133
            term_printf("\\%c", filename[i]);
134
            break;
135
        case '\t':
136
            term_printf("\\t");
137
            break;
138
        case '\r':
139
            term_printf("\\r");
140
            break;
141
        case '\n':
142
            term_printf("\\n");
143
            break;
144
        default:
145
            term_printf("%c", filename[i]);
146
            break;
147
        }
148
    }
149
}
150

    
151
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
152
{
153
    va_list ap;
154
    va_start(ap, fmt);
155
    term_vprintf(fmt, ap);
156
    va_end(ap);
157
    return 0;
158
}
159

    
160
static int compare_cmd(const char *name, const char *list)
161
{
162
    const char *p, *pstart;
163
    int len;
164
    len = strlen(name);
165
    p = list;
166
    for(;;) {
167
        pstart = p;
168
        p = strchr(p, '|');
169
        if (!p)
170
            p = pstart + strlen(pstart);
171
        if ((p - pstart) == len && !memcmp(pstart, name, len))
172
            return 1;
173
        if (*p == '\0')
174
            break;
175
        p++;
176
    }
177
    return 0;
178
}
179

    
180
static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
181
{
182
    term_cmd_t *cmd;
183

    
184
    for(cmd = cmds; cmd->name != NULL; cmd++) {
185
        if (!name || !strcmp(name, cmd->name))
186
            term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
187
    }
188
}
189

    
190
static void help_cmd(const char *name)
191
{
192
    if (name && !strcmp(name, "info")) {
193
        help_cmd1(info_cmds, "info ", NULL);
194
    } else {
195
        help_cmd1(term_cmds, "", name);
196
        if (name && !strcmp(name, "log")) {
197
            CPULogItem *item;
198
            term_printf("Log items (comma separated):\n");
199
            term_printf("%-10s %s\n", "none", "remove all logs");
200
            for(item = cpu_log_items; item->mask != 0; item++) {
201
                term_printf("%-10s %s\n", item->name, item->help);
202
            }
203
        }
204
    }
205
}
206

    
207
static void do_help(const char *name)
208
{
209
    help_cmd(name);
210
}
211

    
212
static void do_commit(const char *device)
213
{
214
    int i, all_devices;
215

    
216
    all_devices = !strcmp(device, "all");
217
    for (i = 0; i < nb_drives; i++) {
218
            if (all_devices ||
219
                !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
220
                bdrv_commit(drives_table[i].bdrv);
221
    }
222
}
223

    
224
static void do_info(const char *item)
225
{
226
    term_cmd_t *cmd;
227
    void (*handler)(void);
228

    
229
    if (!item)
230
        goto help;
231
    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
232
        if (compare_cmd(item, cmd->name))
233
            goto found;
234
    }
235
 help:
236
    help_cmd("info");
237
    return;
238
 found:
239
    handler = cmd->handler;
240
    handler();
241
}
242

    
243
static void do_info_version(void)
244
{
245
  term_printf("%s\n", QEMU_VERSION);
246
}
247

    
248
static void do_info_name(void)
249
{
250
    if (qemu_name)
251
        term_printf("%s\n", qemu_name);
252
}
253

    
254
static void do_info_block(void)
255
{
256
    bdrv_info();
257
}
258

    
259
static void do_info_blockstats(void)
260
{
261
    bdrv_info_stats();
262
}
263

    
264
/* get the current CPU defined by the user */
265
static int mon_set_cpu(int cpu_index)
266
{
267
    CPUState *env;
268

    
269
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
270
        if (env->cpu_index == cpu_index) {
271
            mon_cpu = env;
272
            return 0;
273
        }
274
    }
275
    return -1;
276
}
277

    
278
static CPUState *mon_get_cpu(void)
279
{
280
    if (!mon_cpu) {
281
        mon_set_cpu(0);
282
    }
283
    return mon_cpu;
284
}
285

    
286
static void do_info_registers(void)
287
{
288
    CPUState *env;
289
    env = mon_get_cpu();
290
    if (!env)
291
        return;
292
#ifdef TARGET_I386
293
    cpu_dump_state(env, NULL, monitor_fprintf,
294
                   X86_DUMP_FPU);
295
#else
296
    cpu_dump_state(env, NULL, monitor_fprintf,
297
                   0);
298
#endif
299
}
300

    
301
static void do_info_cpus(void)
302
{
303
    CPUState *env;
304

    
305
    /* just to set the default cpu if not already done */
306
    mon_get_cpu();
307

    
308
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
309
        term_printf("%c CPU #%d:",
310
                    (env == mon_cpu) ? '*' : ' ',
311
                    env->cpu_index);
312
#if defined(TARGET_I386)
313
        term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
314
#elif defined(TARGET_PPC)
315
        term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
316
#elif defined(TARGET_SPARC)
317
        term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
318
#elif defined(TARGET_MIPS)
319
        term_printf(" PC=0x" TARGET_FMT_lx, env->active_tc.PC);
320
#endif
321
        if (env->halted)
322
            term_printf(" (halted)");
323
        term_printf("\n");
324
    }
325
}
326

    
327
static void do_cpu_set(int index)
328
{
329
    if (mon_set_cpu(index) < 0)
330
        term_printf("Invalid CPU index\n");
331
}
332

    
333
static void do_info_jit(void)
334
{
335
    dump_exec_info(NULL, monitor_fprintf);
336
}
337

    
338
static void do_info_history (void)
339
{
340
    int i;
341
    const char *str;
342

    
343
    i = 0;
344
    for(;;) {
345
        str = readline_get_history(i);
346
        if (!str)
347
            break;
348
        term_printf("%d: '%s'\n", i, str);
349
        i++;
350
    }
351
}
352

    
353
#if defined(TARGET_PPC)
354
/* XXX: not implemented in other targets */
355
static void do_info_cpu_stats (void)
356
{
357
    CPUState *env;
358

    
359
    env = mon_get_cpu();
360
    cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
361
}
362
#endif
363

    
364
static void do_quit(void)
365
{
366
    exit(0);
367
}
368

    
369
static int eject_device(BlockDriverState *bs, int force)
370
{
371
    if (bdrv_is_inserted(bs)) {
372
        if (!force) {
373
            if (!bdrv_is_removable(bs)) {
374
                term_printf("device is not removable\n");
375
                return -1;
376
            }
377
            if (bdrv_is_locked(bs)) {
378
                term_printf("device is locked\n");
379
                return -1;
380
            }
381
        }
382
        bdrv_close(bs);
383
    }
384
    return 0;
385
}
386

    
387
static void do_eject(int force, const char *filename)
388
{
389
    BlockDriverState *bs;
390

    
391
    bs = bdrv_find(filename);
392
    if (!bs) {
393
        term_printf("device not found\n");
394
        return;
395
    }
396
    eject_device(bs, force);
397
}
398

    
399
static void do_change_block(const char *device, const char *filename, const char *fmt)
400
{
401
    BlockDriverState *bs;
402
    BlockDriver *drv = NULL;
403

    
404
    bs = bdrv_find(device);
405
    if (!bs) {
406
        term_printf("device not found\n");
407
        return;
408
    }
409
    if (fmt) {
410
        drv = bdrv_find_format(fmt);
411
        if (!drv) {
412
            term_printf("invalid format %s\n", fmt);
413
            return;
414
        }
415
    }
416
    if (eject_device(bs, 0) < 0)
417
        return;
418
    bdrv_open2(bs, filename, 0, drv);
419
    qemu_key_check(bs, filename);
420
}
421

    
422
static void do_change_vnc(const char *target)
423
{
424
    if (strcmp(target, "passwd") == 0 ||
425
        strcmp(target, "password") == 0) {
426
        char password[9];
427
        monitor_readline("Password: ", 1, password, sizeof(password)-1);
428
        password[sizeof(password)-1] = '\0';
429
        if (vnc_display_password(NULL, password) < 0)
430
            term_printf("could not set VNC server password\n");
431
    } else {
432
        if (vnc_display_open(NULL, target) < 0)
433
            term_printf("could not start VNC server on %s\n", target);
434
    }
435
}
436

    
437
static void do_change(const char *device, const char *target, const char *fmt)
438
{
439
    if (strcmp(device, "vnc") == 0) {
440
        do_change_vnc(target);
441
    } else {
442
        do_change_block(device, target, fmt);
443
    }
444
}
445

    
446
static void do_screen_dump(const char *filename)
447
{
448
    vga_hw_screen_dump(filename);
449
}
450

    
451
static void do_logfile(const char *filename)
452
{
453
    cpu_set_log_filename(filename);
454
}
455

    
456
static void do_log(const char *items)
457
{
458
    int mask;
459

    
460
    if (!strcmp(items, "none")) {
461
        mask = 0;
462
    } else {
463
        mask = cpu_str_to_log_mask(items);
464
        if (!mask) {
465
            help_cmd("log");
466
            return;
467
        }
468
    }
469
    cpu_set_log(mask);
470
}
471

    
472
static void do_stop(void)
473
{
474
    vm_stop(EXCP_INTERRUPT);
475
}
476

    
477
static void do_cont(void)
478
{
479
    vm_start();
480
}
481

    
482
#ifdef CONFIG_GDBSTUB
483
static void do_gdbserver(const char *port)
484
{
485
    if (!port)
486
        port = DEFAULT_GDBSTUB_PORT;
487
    if (gdbserver_start(port) < 0) {
488
        qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
489
    } else {
490
        qemu_printf("Waiting gdb connection on port '%s'\n", port);
491
    }
492
}
493
#endif
494

    
495
static void term_printc(int c)
496
{
497
    term_printf("'");
498
    switch(c) {
499
    case '\'':
500
        term_printf("\\'");
501
        break;
502
    case '\\':
503
        term_printf("\\\\");
504
        break;
505
    case '\n':
506
        term_printf("\\n");
507
        break;
508
    case '\r':
509
        term_printf("\\r");
510
        break;
511
    default:
512
        if (c >= 32 && c <= 126) {
513
            term_printf("%c", c);
514
        } else {
515
            term_printf("\\x%02x", c);
516
        }
517
        break;
518
    }
519
    term_printf("'");
520
}
521

    
522
static void memory_dump(int count, int format, int wsize,
523
                        target_phys_addr_t addr, int is_physical)
524
{
525
    CPUState *env;
526
    int nb_per_line, l, line_size, i, max_digits, len;
527
    uint8_t buf[16];
528
    uint64_t v;
529

    
530
    if (format == 'i') {
531
        int flags;
532
        flags = 0;
533
        env = mon_get_cpu();
534
        if (!env && !is_physical)
535
            return;
536
#ifdef TARGET_I386
537
        if (wsize == 2) {
538
            flags = 1;
539
        } else if (wsize == 4) {
540
            flags = 0;
541
        } else {
542
            /* as default we use the current CS size */
543
            flags = 0;
544
            if (env) {
545
#ifdef TARGET_X86_64
546
                if ((env->efer & MSR_EFER_LMA) &&
547
                    (env->segs[R_CS].flags & DESC_L_MASK))
548
                    flags = 2;
549
                else
550
#endif
551
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
552
                    flags = 1;
553
            }
554
        }
555
#endif
556
        monitor_disas(env, addr, count, is_physical, flags);
557
        return;
558
    }
559

    
560
    len = wsize * count;
561
    if (wsize == 1)
562
        line_size = 8;
563
    else
564
        line_size = 16;
565
    nb_per_line = line_size / wsize;
566
    max_digits = 0;
567

    
568
    switch(format) {
569
    case 'o':
570
        max_digits = (wsize * 8 + 2) / 3;
571
        break;
572
    default:
573
    case 'x':
574
        max_digits = (wsize * 8) / 4;
575
        break;
576
    case 'u':
577
    case 'd':
578
        max_digits = (wsize * 8 * 10 + 32) / 33;
579
        break;
580
    case 'c':
581
        wsize = 1;
582
        break;
583
    }
584

    
585
    while (len > 0) {
586
        if (is_physical)
587
            term_printf(TARGET_FMT_plx ":", addr);
588
        else
589
            term_printf(TARGET_FMT_lx ":", (target_ulong)addr);
590
        l = len;
591
        if (l > line_size)
592
            l = line_size;
593
        if (is_physical) {
594
            cpu_physical_memory_rw(addr, buf, l, 0);
595
        } else {
596
            env = mon_get_cpu();
597
            if (!env)
598
                break;
599
            if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
600
                term_printf(" Cannot access memory\n");
601
                break;
602
            }
603
        }
604
        i = 0;
605
        while (i < l) {
606
            switch(wsize) {
607
            default:
608
            case 1:
609
                v = ldub_raw(buf + i);
610
                break;
611
            case 2:
612
                v = lduw_raw(buf + i);
613
                break;
614
            case 4:
615
                v = (uint32_t)ldl_raw(buf + i);
616
                break;
617
            case 8:
618
                v = ldq_raw(buf + i);
619
                break;
620
            }
621
            term_printf(" ");
622
            switch(format) {
623
            case 'o':
624
                term_printf("%#*" PRIo64, max_digits, v);
625
                break;
626
            case 'x':
627
                term_printf("0x%0*" PRIx64, max_digits, v);
628
                break;
629
            case 'u':
630
                term_printf("%*" PRIu64, max_digits, v);
631
                break;
632
            case 'd':
633
                term_printf("%*" PRId64, max_digits, v);
634
                break;
635
            case 'c':
636
                term_printc(v);
637
                break;
638
            }
639
            i += wsize;
640
        }
641
        term_printf("\n");
642
        addr += l;
643
        len -= l;
644
    }
645
}
646

    
647
#if TARGET_LONG_BITS == 64
648
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
649
#else
650
#define GET_TLONG(h, l) (l)
651
#endif
652

    
653
static void do_memory_dump(int count, int format, int size,
654
                           uint32_t addrh, uint32_t addrl)
655
{
656
    target_long addr = GET_TLONG(addrh, addrl);
657
    memory_dump(count, format, size, addr, 0);
658
}
659

    
660
#if TARGET_PHYS_ADDR_BITS > 32
661
#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
662
#else
663
#define GET_TPHYSADDR(h, l) (l)
664
#endif
665

    
666
static void do_physical_memory_dump(int count, int format, int size,
667
                                    uint32_t addrh, uint32_t addrl)
668

    
669
{
670
    target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
671
    memory_dump(count, format, size, addr, 1);
672
}
673

    
674
static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
675
{
676
    target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
677
#if TARGET_PHYS_ADDR_BITS == 32
678
    switch(format) {
679
    case 'o':
680
        term_printf("%#o", val);
681
        break;
682
    case 'x':
683
        term_printf("%#x", val);
684
        break;
685
    case 'u':
686
        term_printf("%u", val);
687
        break;
688
    default:
689
    case 'd':
690
        term_printf("%d", val);
691
        break;
692
    case 'c':
693
        term_printc(val);
694
        break;
695
    }
696
#else
697
    switch(format) {
698
    case 'o':
699
        term_printf("%#" PRIo64, val);
700
        break;
701
    case 'x':
702
        term_printf("%#" PRIx64, val);
703
        break;
704
    case 'u':
705
        term_printf("%" PRIu64, val);
706
        break;
707
    default:
708
    case 'd':
709
        term_printf("%" PRId64, val);
710
        break;
711
    case 'c':
712
        term_printc(val);
713
        break;
714
    }
715
#endif
716
    term_printf("\n");
717
}
718

    
719
static void do_memory_save(unsigned int valh, unsigned int vall,
720
                           uint32_t size, const char *filename)
721
{
722
    FILE *f;
723
    target_long addr = GET_TLONG(valh, vall);
724
    uint32_t l;
725
    CPUState *env;
726
    uint8_t buf[1024];
727

    
728
    env = mon_get_cpu();
729
    if (!env)
730
        return;
731

    
732
    f = fopen(filename, "wb");
733
    if (!f) {
734
        term_printf("could not open '%s'\n", filename);
735
        return;
736
    }
737
    while (size != 0) {
738
        l = sizeof(buf);
739
        if (l > size)
740
            l = size;
741
        cpu_memory_rw_debug(env, addr, buf, l, 0);
742
        fwrite(buf, 1, l, f);
743
        addr += l;
744
        size -= l;
745
    }
746
    fclose(f);
747
}
748

    
749
static void do_physical_memory_save(unsigned int valh, unsigned int vall,
750
                                    uint32_t size, const char *filename)
751
{
752
    FILE *f;
753
    uint32_t l;
754
    uint8_t buf[1024];
755
    target_phys_addr_t addr = GET_TPHYSADDR(valh, vall); 
756

    
757
    f = fopen(filename, "wb");
758
    if (!f) {
759
        term_printf("could not open '%s'\n", filename);
760
        return;
761
    }
762
    while (size != 0) {
763
        l = sizeof(buf);
764
        if (l > size)
765
            l = size;
766
        cpu_physical_memory_rw(addr, buf, l, 0);
767
        fwrite(buf, 1, l, f);
768
        fflush(f);
769
        addr += l;
770
        size -= l;
771
    }
772
    fclose(f);
773
}
774

    
775
static void do_sum(uint32_t start, uint32_t size)
776
{
777
    uint32_t addr;
778
    uint8_t buf[1];
779
    uint16_t sum;
780

    
781
    sum = 0;
782
    for(addr = start; addr < (start + size); addr++) {
783
        cpu_physical_memory_rw(addr, buf, 1, 0);
784
        /* BSD sum algorithm ('sum' Unix command) */
785
        sum = (sum >> 1) | (sum << 15);
786
        sum += buf[0];
787
    }
788
    term_printf("%05d\n", sum);
789
}
790

    
791
typedef struct {
792
    int keycode;
793
    const char *name;
794
} KeyDef;
795

    
796
static const KeyDef key_defs[] = {
797
    { 0x2a, "shift" },
798
    { 0x36, "shift_r" },
799

    
800
    { 0x38, "alt" },
801
    { 0xb8, "alt_r" },
802
    { 0x64, "altgr" },
803
    { 0xe4, "altgr_r" },
804
    { 0x1d, "ctrl" },
805
    { 0x9d, "ctrl_r" },
806

    
807
    { 0xdd, "menu" },
808

    
809
    { 0x01, "esc" },
810

    
811
    { 0x02, "1" },
812
    { 0x03, "2" },
813
    { 0x04, "3" },
814
    { 0x05, "4" },
815
    { 0x06, "5" },
816
    { 0x07, "6" },
817
    { 0x08, "7" },
818
    { 0x09, "8" },
819
    { 0x0a, "9" },
820
    { 0x0b, "0" },
821
    { 0x0c, "minus" },
822
    { 0x0d, "equal" },
823
    { 0x0e, "backspace" },
824

    
825
    { 0x0f, "tab" },
826
    { 0x10, "q" },
827
    { 0x11, "w" },
828
    { 0x12, "e" },
829
    { 0x13, "r" },
830
    { 0x14, "t" },
831
    { 0x15, "y" },
832
    { 0x16, "u" },
833
    { 0x17, "i" },
834
    { 0x18, "o" },
835
    { 0x19, "p" },
836

    
837
    { 0x1c, "ret" },
838

    
839
    { 0x1e, "a" },
840
    { 0x1f, "s" },
841
    { 0x20, "d" },
842
    { 0x21, "f" },
843
    { 0x22, "g" },
844
    { 0x23, "h" },
845
    { 0x24, "j" },
846
    { 0x25, "k" },
847
    { 0x26, "l" },
848

    
849
    { 0x2c, "z" },
850
    { 0x2d, "x" },
851
    { 0x2e, "c" },
852
    { 0x2f, "v" },
853
    { 0x30, "b" },
854
    { 0x31, "n" },
855
    { 0x32, "m" },
856

    
857
    { 0x37, "asterisk" },
858

    
859
    { 0x39, "spc" },
860
    { 0x3a, "caps_lock" },
861
    { 0x3b, "f1" },
862
    { 0x3c, "f2" },
863
    { 0x3d, "f3" },
864
    { 0x3e, "f4" },
865
    { 0x3f, "f5" },
866
    { 0x40, "f6" },
867
    { 0x41, "f7" },
868
    { 0x42, "f8" },
869
    { 0x43, "f9" },
870
    { 0x44, "f10" },
871
    { 0x45, "num_lock" },
872
    { 0x46, "scroll_lock" },
873

    
874
    { 0xb5, "kp_divide" },
875
    { 0x37, "kp_multiply" },
876
    { 0x4a, "kp_subtract" },
877
    { 0x4e, "kp_add" },
878
    { 0x9c, "kp_enter" },
879
    { 0x53, "kp_decimal" },
880
    { 0x54, "sysrq" },
881

    
882
    { 0x52, "kp_0" },
883
    { 0x4f, "kp_1" },
884
    { 0x50, "kp_2" },
885
    { 0x51, "kp_3" },
886
    { 0x4b, "kp_4" },
887
    { 0x4c, "kp_5" },
888
    { 0x4d, "kp_6" },
889
    { 0x47, "kp_7" },
890
    { 0x48, "kp_8" },
891
    { 0x49, "kp_9" },
892

    
893
    { 0x56, "<" },
894

    
895
    { 0x57, "f11" },
896
    { 0x58, "f12" },
897

    
898
    { 0xb7, "print" },
899

    
900
    { 0xc7, "home" },
901
    { 0xc9, "pgup" },
902
    { 0xd1, "pgdn" },
903
    { 0xcf, "end" },
904

    
905
    { 0xcb, "left" },
906
    { 0xc8, "up" },
907
    { 0xd0, "down" },
908
    { 0xcd, "right" },
909

    
910
    { 0xd2, "insert" },
911
    { 0xd3, "delete" },
912
#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
913
    { 0xf0, "stop" },
914
    { 0xf1, "again" },
915
    { 0xf2, "props" },
916
    { 0xf3, "undo" },
917
    { 0xf4, "front" },
918
    { 0xf5, "copy" },
919
    { 0xf6, "open" },
920
    { 0xf7, "paste" },
921
    { 0xf8, "find" },
922
    { 0xf9, "cut" },
923
    { 0xfa, "lf" },
924
    { 0xfb, "help" },
925
    { 0xfc, "meta_l" },
926
    { 0xfd, "meta_r" },
927
    { 0xfe, "compose" },
928
#endif
929
    { 0, NULL },
930
};
931

    
932
static int get_keycode(const char *key)
933
{
934
    const KeyDef *p;
935
    char *endp;
936
    int ret;
937

    
938
    for(p = key_defs; p->name != NULL; p++) {
939
        if (!strcmp(key, p->name))
940
            return p->keycode;
941
    }
942
    if (strstart(key, "0x", NULL)) {
943
        ret = strtoul(key, &endp, 0);
944
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
945
            return ret;
946
    }
947
    return -1;
948
}
949

    
950
#define MAX_KEYCODES 16
951
static uint8_t keycodes[MAX_KEYCODES];
952
static int nb_pending_keycodes;
953
static QEMUTimer *key_timer;
954

    
955
static void release_keys(void *opaque)
956
{
957
    int keycode;
958

    
959
    while (nb_pending_keycodes > 0) {
960
        nb_pending_keycodes--;
961
        keycode = keycodes[nb_pending_keycodes];
962
        if (keycode & 0x80)
963
            kbd_put_keycode(0xe0);
964
        kbd_put_keycode(keycode | 0x80);
965
    }
966
}
967

    
968
static void do_sendkey(const char *string, int has_hold_time, int hold_time)
969
{
970
    char keyname_buf[16];
971
    char *separator;
972
    int keyname_len, keycode, i;
973

    
974
    if (nb_pending_keycodes > 0) {
975
        qemu_del_timer(key_timer);
976
        release_keys(NULL);
977
    }
978
    if (!has_hold_time)
979
        hold_time = 100;
980
    i = 0;
981
    while (1) {
982
        separator = strchr(string, '-');
983
        keyname_len = separator ? separator - string : strlen(string);
984
        if (keyname_len > 0) {
985
            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
986
            if (keyname_len > sizeof(keyname_buf) - 1) {
987
                term_printf("invalid key: '%s...'\n", keyname_buf);
988
                return;
989
            }
990
            if (i == MAX_KEYCODES) {
991
                term_printf("too many keys\n");
992
                return;
993
            }
994
            keyname_buf[keyname_len] = 0;
995
            keycode = get_keycode(keyname_buf);
996
            if (keycode < 0) {
997
                term_printf("unknown key: '%s'\n", keyname_buf);
998
                return;
999
            }
1000
            keycodes[i++] = keycode;
1001
        }
1002
        if (!separator)
1003
            break;
1004
        string = separator + 1;
1005
    }
1006
    nb_pending_keycodes = i;
1007
    /* key down events */
1008
    for (i = 0; i < nb_pending_keycodes; i++) {
1009
        keycode = keycodes[i];
1010
        if (keycode & 0x80)
1011
            kbd_put_keycode(0xe0);
1012
        kbd_put_keycode(keycode & 0x7f);
1013
    }
1014
    /* delayed key up events */
1015
    qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1016
                    muldiv64(ticks_per_sec, hold_time, 1000));
1017
}
1018

    
1019
static int mouse_button_state;
1020

    
1021
static void do_mouse_move(const char *dx_str, const char *dy_str,
1022
                          const char *dz_str)
1023
{
1024
    int dx, dy, dz;
1025
    dx = strtol(dx_str, NULL, 0);
1026
    dy = strtol(dy_str, NULL, 0);
1027
    dz = 0;
1028
    if (dz_str)
1029
        dz = strtol(dz_str, NULL, 0);
1030
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
1031
}
1032

    
1033
static void do_mouse_button(int button_state)
1034
{
1035
    mouse_button_state = button_state;
1036
    kbd_mouse_event(0, 0, 0, mouse_button_state);
1037
}
1038

    
1039
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
1040
{
1041
    uint32_t val;
1042
    int suffix;
1043

    
1044
    if (has_index) {
1045
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
1046
        addr++;
1047
    }
1048
    addr &= 0xffff;
1049

    
1050
    switch(size) {
1051
    default:
1052
    case 1:
1053
        val = cpu_inb(NULL, addr);
1054
        suffix = 'b';
1055
        break;
1056
    case 2:
1057
        val = cpu_inw(NULL, addr);
1058
        suffix = 'w';
1059
        break;
1060
    case 4:
1061
        val = cpu_inl(NULL, addr);
1062
        suffix = 'l';
1063
        break;
1064
    }
1065
    term_printf("port%c[0x%04x] = %#0*x\n",
1066
                suffix, addr, size * 2, val);
1067
}
1068

    
1069
/* boot_set handler */
1070
static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1071
static void *boot_opaque;
1072

    
1073
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1074
{
1075
    qemu_boot_set_handler = func;
1076
    boot_opaque = opaque;
1077
}
1078

    
1079
static void do_boot_set(const char *bootdevice)
1080
{
1081
    int res;
1082

    
1083
    if (qemu_boot_set_handler)  {
1084
        res = qemu_boot_set_handler(boot_opaque, bootdevice);
1085
        if (res == 0)
1086
            term_printf("boot device list now set to %s\n", bootdevice);
1087
        else
1088
            term_printf("setting boot device list failed with error %i\n", res);
1089
    } else {
1090
        term_printf("no function defined to set boot device list for this architecture\n");
1091
    }
1092
}
1093

    
1094
static void do_system_reset(void)
1095
{
1096
    qemu_system_reset_request();
1097
}
1098

    
1099
static void do_system_powerdown(void)
1100
{
1101
    qemu_system_powerdown_request();
1102
}
1103

    
1104
#if defined(TARGET_I386)
1105
static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
1106
{
1107
    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1108
                addr,
1109
                pte & mask,
1110
                pte & PG_GLOBAL_MASK ? 'G' : '-',
1111
                pte & PG_PSE_MASK ? 'P' : '-',
1112
                pte & PG_DIRTY_MASK ? 'D' : '-',
1113
                pte & PG_ACCESSED_MASK ? 'A' : '-',
1114
                pte & PG_PCD_MASK ? 'C' : '-',
1115
                pte & PG_PWT_MASK ? 'T' : '-',
1116
                pte & PG_USER_MASK ? 'U' : '-',
1117
                pte & PG_RW_MASK ? 'W' : '-');
1118
}
1119

    
1120
static void tlb_info(void)
1121
{
1122
    CPUState *env;
1123
    int l1, l2;
1124
    uint32_t pgd, pde, pte;
1125

    
1126
    env = mon_get_cpu();
1127
    if (!env)
1128
        return;
1129

    
1130
    if (!(env->cr[0] & CR0_PG_MASK)) {
1131
        term_printf("PG disabled\n");
1132
        return;
1133
    }
1134
    pgd = env->cr[3] & ~0xfff;
1135
    for(l1 = 0; l1 < 1024; l1++) {
1136
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1137
        pde = le32_to_cpu(pde);
1138
        if (pde & PG_PRESENT_MASK) {
1139
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1140
                print_pte((l1 << 22), pde, ~((1 << 20) - 1));
1141
            } else {
1142
                for(l2 = 0; l2 < 1024; l2++) {
1143
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1144
                                             (uint8_t *)&pte, 4);
1145
                    pte = le32_to_cpu(pte);
1146
                    if (pte & PG_PRESENT_MASK) {
1147
                        print_pte((l1 << 22) + (l2 << 12),
1148
                                  pte & ~PG_PSE_MASK,
1149
                                  ~0xfff);
1150
                    }
1151
                }
1152
            }
1153
        }
1154
    }
1155
}
1156

    
1157
static void mem_print(uint32_t *pstart, int *plast_prot,
1158
                      uint32_t end, int prot)
1159
{
1160
    int prot1;
1161
    prot1 = *plast_prot;
1162
    if (prot != prot1) {
1163
        if (*pstart != -1) {
1164
            term_printf("%08x-%08x %08x %c%c%c\n",
1165
                        *pstart, end, end - *pstart,
1166
                        prot1 & PG_USER_MASK ? 'u' : '-',
1167
                        'r',
1168
                        prot1 & PG_RW_MASK ? 'w' : '-');
1169
        }
1170
        if (prot != 0)
1171
            *pstart = end;
1172
        else
1173
            *pstart = -1;
1174
        *plast_prot = prot;
1175
    }
1176
}
1177

    
1178
static void mem_info(void)
1179
{
1180
    CPUState *env;
1181
    int l1, l2, prot, last_prot;
1182
    uint32_t pgd, pde, pte, start, end;
1183

    
1184
    env = mon_get_cpu();
1185
    if (!env)
1186
        return;
1187

    
1188
    if (!(env->cr[0] & CR0_PG_MASK)) {
1189
        term_printf("PG disabled\n");
1190
        return;
1191
    }
1192
    pgd = env->cr[3] & ~0xfff;
1193
    last_prot = 0;
1194
    start = -1;
1195
    for(l1 = 0; l1 < 1024; l1++) {
1196
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1197
        pde = le32_to_cpu(pde);
1198
        end = l1 << 22;
1199
        if (pde & PG_PRESENT_MASK) {
1200
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1201
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1202
                mem_print(&start, &last_prot, end, prot);
1203
            } else {
1204
                for(l2 = 0; l2 < 1024; l2++) {
1205
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1206
                                             (uint8_t *)&pte, 4);
1207
                    pte = le32_to_cpu(pte);
1208
                    end = (l1 << 22) + (l2 << 12);
1209
                    if (pte & PG_PRESENT_MASK) {
1210
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1211
                    } else {
1212
                        prot = 0;
1213
                    }
1214
                    mem_print(&start, &last_prot, end, prot);
1215
                }
1216
            }
1217
        } else {
1218
            prot = 0;
1219
            mem_print(&start, &last_prot, end, prot);
1220
        }
1221
    }
1222
}
1223
#endif
1224

    
1225
static void do_info_kqemu(void)
1226
{
1227
#ifdef USE_KQEMU
1228
    CPUState *env;
1229
    int val;
1230
    val = 0;
1231
    env = mon_get_cpu();
1232
    if (!env) {
1233
        term_printf("No cpu initialized yet");
1234
        return;
1235
    }
1236
    val = env->kqemu_enabled;
1237
    term_printf("kqemu support: ");
1238
    switch(val) {
1239
    default:
1240
    case 0:
1241
        term_printf("disabled\n");
1242
        break;
1243
    case 1:
1244
        term_printf("enabled for user code\n");
1245
        break;
1246
    case 2:
1247
        term_printf("enabled for user and kernel code\n");
1248
        break;
1249
    }
1250
#else
1251
    term_printf("kqemu support: not compiled\n");
1252
#endif
1253
}
1254

    
1255
#ifdef CONFIG_PROFILER
1256

    
1257
int64_t kqemu_time;
1258
int64_t qemu_time;
1259
int64_t kqemu_exec_count;
1260
int64_t dev_time;
1261
int64_t kqemu_ret_int_count;
1262
int64_t kqemu_ret_excp_count;
1263
int64_t kqemu_ret_intr_count;
1264

    
1265
static void do_info_profile(void)
1266
{
1267
    int64_t total;
1268
    total = qemu_time;
1269
    if (total == 0)
1270
        total = 1;
1271
    term_printf("async time  %" PRId64 " (%0.3f)\n",
1272
                dev_time, dev_time / (double)ticks_per_sec);
1273
    term_printf("qemu time   %" PRId64 " (%0.3f)\n",
1274
                qemu_time, qemu_time / (double)ticks_per_sec);
1275
    term_printf("kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1276
                kqemu_time, kqemu_time / (double)ticks_per_sec,
1277
                kqemu_time / (double)total * 100.0,
1278
                kqemu_exec_count,
1279
                kqemu_ret_int_count,
1280
                kqemu_ret_excp_count,
1281
                kqemu_ret_intr_count);
1282
    qemu_time = 0;
1283
    kqemu_time = 0;
1284
    kqemu_exec_count = 0;
1285
    dev_time = 0;
1286
    kqemu_ret_int_count = 0;
1287
    kqemu_ret_excp_count = 0;
1288
    kqemu_ret_intr_count = 0;
1289
#ifdef USE_KQEMU
1290
    kqemu_record_dump();
1291
#endif
1292
}
1293
#else
1294
static void do_info_profile(void)
1295
{
1296
    term_printf("Internal profiler not compiled\n");
1297
}
1298
#endif
1299

    
1300
/* Capture support */
1301
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1302

    
1303
static void do_info_capture (void)
1304
{
1305
    int i;
1306
    CaptureState *s;
1307

    
1308
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1309
        term_printf ("[%d]: ", i);
1310
        s->ops.info (s->opaque);
1311
    }
1312
}
1313

    
1314
static void do_stop_capture (int n)
1315
{
1316
    int i;
1317
    CaptureState *s;
1318

    
1319
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1320
        if (i == n) {
1321
            s->ops.destroy (s->opaque);
1322
            LIST_REMOVE (s, entries);
1323
            qemu_free (s);
1324
            return;
1325
        }
1326
    }
1327
}
1328

    
1329
#ifdef HAS_AUDIO
1330
int wav_start_capture (CaptureState *s, const char *path, int freq,
1331
                       int bits, int nchannels);
1332

    
1333
static void do_wav_capture (const char *path,
1334
                            int has_freq, int freq,
1335
                            int has_bits, int bits,
1336
                            int has_channels, int nchannels)
1337
{
1338
    CaptureState *s;
1339

    
1340
    s = qemu_mallocz (sizeof (*s));
1341
    if (!s) {
1342
        term_printf ("Not enough memory to add wave capture\n");
1343
        return;
1344
    }
1345

    
1346
    freq = has_freq ? freq : 44100;
1347
    bits = has_bits ? bits : 16;
1348
    nchannels = has_channels ? nchannels : 2;
1349

    
1350
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1351
        term_printf ("Faied to add wave capture\n");
1352
        qemu_free (s);
1353
    }
1354
    LIST_INSERT_HEAD (&capture_head, s, entries);
1355
}
1356
#endif
1357

    
1358
#if defined(TARGET_I386)
1359
static void do_inject_nmi(int cpu_index)
1360
{
1361
    CPUState *env;
1362

    
1363
    for (env = first_cpu; env != NULL; env = env->next_cpu)
1364
        if (env->cpu_index == cpu_index) {
1365
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1366
            break;
1367
        }
1368
}
1369
#endif
1370

    
1371
static term_cmd_t term_cmds[] = {
1372
    { "help|?", "s?", do_help,
1373
      "[cmd]", "show the help" },
1374
    { "commit", "s", do_commit,
1375
      "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1376
    { "info", "s?", do_info,
1377
      "subcommand", "show various information about the system state" },
1378
    { "q|quit", "", do_quit,
1379
      "", "quit the emulator" },
1380
    { "eject", "-fB", do_eject,
1381
      "[-f] device", "eject a removable medium (use -f to force it)" },
1382
    { "change", "BFs?", do_change,
1383
      "device filename [format]", "change a removable medium, optional format" },
1384
    { "screendump", "F", do_screen_dump,
1385
      "filename", "save screen into PPM image 'filename'" },
1386
    { "logfile", "F", do_logfile,
1387
      "filename", "output logs to 'filename'" },
1388
    { "log", "s", do_log,
1389
      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1390
    { "savevm", "s?", do_savevm,
1391
      "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1392
    { "loadvm", "s", do_loadvm,
1393
      "tag|id", "restore a VM snapshot from its tag or id" },
1394
    { "delvm", "s", do_delvm,
1395
      "tag|id", "delete a VM snapshot from its tag or id" },
1396
    { "stop", "", do_stop,
1397
      "", "stop emulation", },
1398
    { "c|cont", "", do_cont,
1399
      "", "resume emulation", },
1400
#ifdef CONFIG_GDBSTUB
1401
    { "gdbserver", "s?", do_gdbserver,
1402
      "[port]", "start gdbserver session (default port=1234)", },
1403
#endif
1404
    { "x", "/l", do_memory_dump,
1405
      "/fmt addr", "virtual memory dump starting at 'addr'", },
1406
    { "xp", "/l", do_physical_memory_dump,
1407
      "/fmt addr", "physical memory dump starting at 'addr'", },
1408
    { "p|print", "/l", do_print,
1409
      "/fmt expr", "print expression value (use $reg for CPU register access)", },
1410
    { "i", "/ii.", do_ioport_read,
1411
      "/fmt addr", "I/O port read" },
1412

    
1413
    { "sendkey", "si?", do_sendkey,
1414
      "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1415
    { "system_reset", "", do_system_reset,
1416
      "", "reset the system" },
1417
    { "system_powerdown", "", do_system_powerdown,
1418
      "", "send system power down event" },
1419
    { "sum", "ii", do_sum,
1420
      "addr size", "compute the checksum of a memory region" },
1421
    { "usb_add", "s", do_usb_add,
1422
      "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1423
    { "usb_del", "s", do_usb_del,
1424
      "device", "remove USB device 'bus.addr'" },
1425
    { "cpu", "i", do_cpu_set,
1426
      "index", "set the default CPU" },
1427
    { "mouse_move", "sss?", do_mouse_move,
1428
      "dx dy [dz]", "send mouse move events" },
1429
    { "mouse_button", "i", do_mouse_button,
1430
      "state", "change mouse button state (1=L, 2=M, 4=R)" },
1431
    { "mouse_set", "i", do_mouse_set,
1432
      "index", "set which mouse device receives events" },
1433
#ifdef HAS_AUDIO
1434
    { "wavcapture", "si?i?i?", do_wav_capture,
1435
      "path [frequency bits channels]",
1436
      "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1437
#endif
1438
     { "stopcapture", "i", do_stop_capture,
1439
       "capture index", "stop capture" },
1440
    { "memsave", "lis", do_memory_save,
1441
      "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1442
    { "pmemsave", "lis", do_physical_memory_save,
1443
      "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1444
    { "boot_set", "s", do_boot_set,
1445
      "bootdevice", "define new values for the boot device list" },
1446
#if defined(TARGET_I386)
1447
    { "nmi", "i", do_inject_nmi,
1448
      "cpu", "inject an NMI on the given CPU", },
1449
#endif
1450
    { NULL, NULL, },
1451
};
1452

    
1453
static term_cmd_t info_cmds[] = {
1454
    { "version", "", do_info_version,
1455
      "", "show the version of qemu" },
1456
    { "network", "", do_info_network,
1457
      "", "show the network state" },
1458
    { "block", "", do_info_block,
1459
      "", "show the block devices" },
1460
    { "blockstats", "", do_info_blockstats,
1461
      "", "show block device statistics" },
1462
    { "registers", "", do_info_registers,
1463
      "", "show the cpu registers" },
1464
    { "cpus", "", do_info_cpus,
1465
      "", "show infos for each CPU" },
1466
    { "history", "", do_info_history,
1467
      "", "show the command line history", },
1468
    { "irq", "", irq_info,
1469
      "", "show the interrupts statistics (if available)", },
1470
    { "pic", "", pic_info,
1471
      "", "show i8259 (PIC) state", },
1472
    { "pci", "", pci_info,
1473
      "", "show PCI info", },
1474
#if defined(TARGET_I386)
1475
    { "tlb", "", tlb_info,
1476
      "", "show virtual to physical memory mappings", },
1477
    { "mem", "", mem_info,
1478
      "", "show the active virtual memory mappings", },
1479
#endif
1480
    { "jit", "", do_info_jit,
1481
      "", "show dynamic compiler info", },
1482
    { "kqemu", "", do_info_kqemu,
1483
      "", "show kqemu information", },
1484
    { "usb", "", usb_info,
1485
      "", "show guest USB devices", },
1486
    { "usbhost", "", usb_host_info,
1487
      "", "show host USB devices", },
1488
    { "profile", "", do_info_profile,
1489
      "", "show profiling information", },
1490
    { "capture", "", do_info_capture,
1491
      "", "show capture information" },
1492
    { "snapshots", "", do_info_snapshots,
1493
      "", "show the currently saved VM snapshots" },
1494
    { "pcmcia", "", pcmcia_info,
1495
      "", "show guest PCMCIA status" },
1496
    { "mice", "", do_info_mice,
1497
      "", "show which guest mouse is receiving events" },
1498
    { "vnc", "", do_info_vnc,
1499
      "", "show the vnc server status"},
1500
    { "name", "", do_info_name,
1501
      "", "show the current VM name" },
1502
#if defined(TARGET_PPC)
1503
    { "cpustats", "", do_info_cpu_stats,
1504
      "", "show CPU statistics", },
1505
#endif
1506
#if defined(CONFIG_SLIRP)
1507
    { "slirp", "", do_info_slirp,
1508
      "", "show SLIRP statistics", },
1509
#endif
1510
    { NULL, NULL, },
1511
};
1512

    
1513
/*******************************************************************/
1514

    
1515
static const char *pch;
1516
static jmp_buf expr_env;
1517

    
1518
#define MD_TLONG 0
1519
#define MD_I32   1
1520

    
1521
typedef struct MonitorDef {
1522
    const char *name;
1523
    int offset;
1524
    target_long (*get_value)(struct MonitorDef *md, int val);
1525
    int type;
1526
} MonitorDef;
1527

    
1528
#if defined(TARGET_I386)
1529
static target_long monitor_get_pc (struct MonitorDef *md, int val)
1530
{
1531
    CPUState *env = mon_get_cpu();
1532
    if (!env)
1533
        return 0;
1534
    return env->eip + env->segs[R_CS].base;
1535
}
1536
#endif
1537

    
1538
#if defined(TARGET_PPC)
1539
static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1540
{
1541
    CPUState *env = mon_get_cpu();
1542
    unsigned int u;
1543
    int i;
1544

    
1545
    if (!env)
1546
        return 0;
1547

    
1548
    u = 0;
1549
    for (i = 0; i < 8; i++)
1550
        u |= env->crf[i] << (32 - (4 * i));
1551

    
1552
    return u;
1553
}
1554

    
1555
static target_long monitor_get_msr (struct MonitorDef *md, int val)
1556
{
1557
    CPUState *env = mon_get_cpu();
1558
    if (!env)
1559
        return 0;
1560
    return env->msr;
1561
}
1562

    
1563
static target_long monitor_get_xer (struct MonitorDef *md, int val)
1564
{
1565
    CPUState *env = mon_get_cpu();
1566
    if (!env)
1567
        return 0;
1568
    return ppc_load_xer(env);
1569
}
1570

    
1571
static target_long monitor_get_decr (struct MonitorDef *md, int val)
1572
{
1573
    CPUState *env = mon_get_cpu();
1574
    if (!env)
1575
        return 0;
1576
    return cpu_ppc_load_decr(env);
1577
}
1578

    
1579
static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1580
{
1581
    CPUState *env = mon_get_cpu();
1582
    if (!env)
1583
        return 0;
1584
    return cpu_ppc_load_tbu(env);
1585
}
1586

    
1587
static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1588
{
1589
    CPUState *env = mon_get_cpu();
1590
    if (!env)
1591
        return 0;
1592
    return cpu_ppc_load_tbl(env);
1593
}
1594
#endif
1595

    
1596
#if defined(TARGET_SPARC)
1597
#ifndef TARGET_SPARC64
1598
static target_long monitor_get_psr (struct MonitorDef *md, int val)
1599
{
1600
    CPUState *env = mon_get_cpu();
1601
    if (!env)
1602
        return 0;
1603
    return GET_PSR(env);
1604
}
1605
#endif
1606

    
1607
static target_long monitor_get_reg(struct MonitorDef *md, int val)
1608
{
1609
    CPUState *env = mon_get_cpu();
1610
    if (!env)
1611
        return 0;
1612
    return env->regwptr[val];
1613
}
1614
#endif
1615

    
1616
static MonitorDef monitor_defs[] = {
1617
#ifdef TARGET_I386
1618

    
1619
#define SEG(name, seg) \
1620
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1621
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1622
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1623

    
1624
    { "eax", offsetof(CPUState, regs[0]) },
1625
    { "ecx", offsetof(CPUState, regs[1]) },
1626
    { "edx", offsetof(CPUState, regs[2]) },
1627
    { "ebx", offsetof(CPUState, regs[3]) },
1628
    { "esp|sp", offsetof(CPUState, regs[4]) },
1629
    { "ebp|fp", offsetof(CPUState, regs[5]) },
1630
    { "esi", offsetof(CPUState, regs[6]) },
1631
    { "edi", offsetof(CPUState, regs[7]) },
1632
#ifdef TARGET_X86_64
1633
    { "r8", offsetof(CPUState, regs[8]) },
1634
    { "r9", offsetof(CPUState, regs[9]) },
1635
    { "r10", offsetof(CPUState, regs[10]) },
1636
    { "r11", offsetof(CPUState, regs[11]) },
1637
    { "r12", offsetof(CPUState, regs[12]) },
1638
    { "r13", offsetof(CPUState, regs[13]) },
1639
    { "r14", offsetof(CPUState, regs[14]) },
1640
    { "r15", offsetof(CPUState, regs[15]) },
1641
#endif
1642
    { "eflags", offsetof(CPUState, eflags) },
1643
    { "eip", offsetof(CPUState, eip) },
1644
    SEG("cs", R_CS)
1645
    SEG("ds", R_DS)
1646
    SEG("es", R_ES)
1647
    SEG("ss", R_SS)
1648
    SEG("fs", R_FS)
1649
    SEG("gs", R_GS)
1650
    { "pc", 0, monitor_get_pc, },
1651
#elif defined(TARGET_PPC)
1652
    /* General purpose registers */
1653
    { "r0", offsetof(CPUState, gpr[0]) },
1654
    { "r1", offsetof(CPUState, gpr[1]) },
1655
    { "r2", offsetof(CPUState, gpr[2]) },
1656
    { "r3", offsetof(CPUState, gpr[3]) },
1657
    { "r4", offsetof(CPUState, gpr[4]) },
1658
    { "r5", offsetof(CPUState, gpr[5]) },
1659
    { "r6", offsetof(CPUState, gpr[6]) },
1660
    { "r7", offsetof(CPUState, gpr[7]) },
1661
    { "r8", offsetof(CPUState, gpr[8]) },
1662
    { "r9", offsetof(CPUState, gpr[9]) },
1663
    { "r10", offsetof(CPUState, gpr[10]) },
1664
    { "r11", offsetof(CPUState, gpr[11]) },
1665
    { "r12", offsetof(CPUState, gpr[12]) },
1666
    { "r13", offsetof(CPUState, gpr[13]) },
1667
    { "r14", offsetof(CPUState, gpr[14]) },
1668
    { "r15", offsetof(CPUState, gpr[15]) },
1669
    { "r16", offsetof(CPUState, gpr[16]) },
1670
    { "r17", offsetof(CPUState, gpr[17]) },
1671
    { "r18", offsetof(CPUState, gpr[18]) },
1672
    { "r19", offsetof(CPUState, gpr[19]) },
1673
    { "r20", offsetof(CPUState, gpr[20]) },
1674
    { "r21", offsetof(CPUState, gpr[21]) },
1675
    { "r22", offsetof(CPUState, gpr[22]) },
1676
    { "r23", offsetof(CPUState, gpr[23]) },
1677
    { "r24", offsetof(CPUState, gpr[24]) },
1678
    { "r25", offsetof(CPUState, gpr[25]) },
1679
    { "r26", offsetof(CPUState, gpr[26]) },
1680
    { "r27", offsetof(CPUState, gpr[27]) },
1681
    { "r28", offsetof(CPUState, gpr[28]) },
1682
    { "r29", offsetof(CPUState, gpr[29]) },
1683
    { "r30", offsetof(CPUState, gpr[30]) },
1684
    { "r31", offsetof(CPUState, gpr[31]) },
1685
    /* Floating point registers */
1686
    { "f0", offsetof(CPUState, fpr[0]) },
1687
    { "f1", offsetof(CPUState, fpr[1]) },
1688
    { "f2", offsetof(CPUState, fpr[2]) },
1689
    { "f3", offsetof(CPUState, fpr[3]) },
1690
    { "f4", offsetof(CPUState, fpr[4]) },
1691
    { "f5", offsetof(CPUState, fpr[5]) },
1692
    { "f6", offsetof(CPUState, fpr[6]) },
1693
    { "f7", offsetof(CPUState, fpr[7]) },
1694
    { "f8", offsetof(CPUState, fpr[8]) },
1695
    { "f9", offsetof(CPUState, fpr[9]) },
1696
    { "f10", offsetof(CPUState, fpr[10]) },
1697
    { "f11", offsetof(CPUState, fpr[11]) },
1698
    { "f12", offsetof(CPUState, fpr[12]) },
1699
    { "f13", offsetof(CPUState, fpr[13]) },
1700
    { "f14", offsetof(CPUState, fpr[14]) },
1701
    { "f15", offsetof(CPUState, fpr[15]) },
1702
    { "f16", offsetof(CPUState, fpr[16]) },
1703
    { "f17", offsetof(CPUState, fpr[17]) },
1704
    { "f18", offsetof(CPUState, fpr[18]) },
1705
    { "f19", offsetof(CPUState, fpr[19]) },
1706
    { "f20", offsetof(CPUState, fpr[20]) },
1707
    { "f21", offsetof(CPUState, fpr[21]) },
1708
    { "f22", offsetof(CPUState, fpr[22]) },
1709
    { "f23", offsetof(CPUState, fpr[23]) },
1710
    { "f24", offsetof(CPUState, fpr[24]) },
1711
    { "f25", offsetof(CPUState, fpr[25]) },
1712
    { "f26", offsetof(CPUState, fpr[26]) },
1713
    { "f27", offsetof(CPUState, fpr[27]) },
1714
    { "f28", offsetof(CPUState, fpr[28]) },
1715
    { "f29", offsetof(CPUState, fpr[29]) },
1716
    { "f30", offsetof(CPUState, fpr[30]) },
1717
    { "f31", offsetof(CPUState, fpr[31]) },
1718
    { "fpscr", offsetof(CPUState, fpscr) },
1719
    /* Next instruction pointer */
1720
    { "nip|pc", offsetof(CPUState, nip) },
1721
    { "lr", offsetof(CPUState, lr) },
1722
    { "ctr", offsetof(CPUState, ctr) },
1723
    { "decr", 0, &monitor_get_decr, },
1724
    { "ccr", 0, &monitor_get_ccr, },
1725
    /* Machine state register */
1726
    { "msr", 0, &monitor_get_msr, },
1727
    { "xer", 0, &monitor_get_xer, },
1728
    { "tbu", 0, &monitor_get_tbu, },
1729
    { "tbl", 0, &monitor_get_tbl, },
1730
#if defined(TARGET_PPC64)
1731
    /* Address space register */
1732
    { "asr", offsetof(CPUState, asr) },
1733
#endif
1734
    /* Segment registers */
1735
    { "sdr1", offsetof(CPUState, sdr1) },
1736
    { "sr0", offsetof(CPUState, sr[0]) },
1737
    { "sr1", offsetof(CPUState, sr[1]) },
1738
    { "sr2", offsetof(CPUState, sr[2]) },
1739
    { "sr3", offsetof(CPUState, sr[3]) },
1740
    { "sr4", offsetof(CPUState, sr[4]) },
1741
    { "sr5", offsetof(CPUState, sr[5]) },
1742
    { "sr6", offsetof(CPUState, sr[6]) },
1743
    { "sr7", offsetof(CPUState, sr[7]) },
1744
    { "sr8", offsetof(CPUState, sr[8]) },
1745
    { "sr9", offsetof(CPUState, sr[9]) },
1746
    { "sr10", offsetof(CPUState, sr[10]) },
1747
    { "sr11", offsetof(CPUState, sr[11]) },
1748
    { "sr12", offsetof(CPUState, sr[12]) },
1749
    { "sr13", offsetof(CPUState, sr[13]) },
1750
    { "sr14", offsetof(CPUState, sr[14]) },
1751
    { "sr15", offsetof(CPUState, sr[15]) },
1752
    /* Too lazy to put BATs and SPRs ... */
1753
#elif defined(TARGET_SPARC)
1754
    { "g0", offsetof(CPUState, gregs[0]) },
1755
    { "g1", offsetof(CPUState, gregs[1]) },
1756
    { "g2", offsetof(CPUState, gregs[2]) },
1757
    { "g3", offsetof(CPUState, gregs[3]) },
1758
    { "g4", offsetof(CPUState, gregs[4]) },
1759
    { "g5", offsetof(CPUState, gregs[5]) },
1760
    { "g6", offsetof(CPUState, gregs[6]) },
1761
    { "g7", offsetof(CPUState, gregs[7]) },
1762
    { "o0", 0, monitor_get_reg },
1763
    { "o1", 1, monitor_get_reg },
1764
    { "o2", 2, monitor_get_reg },
1765
    { "o3", 3, monitor_get_reg },
1766
    { "o4", 4, monitor_get_reg },
1767
    { "o5", 5, monitor_get_reg },
1768
    { "o6", 6, monitor_get_reg },
1769
    { "o7", 7, monitor_get_reg },
1770
    { "l0", 8, monitor_get_reg },
1771
    { "l1", 9, monitor_get_reg },
1772
    { "l2", 10, monitor_get_reg },
1773
    { "l3", 11, monitor_get_reg },
1774
    { "l4", 12, monitor_get_reg },
1775
    { "l5", 13, monitor_get_reg },
1776
    { "l6", 14, monitor_get_reg },
1777
    { "l7", 15, monitor_get_reg },
1778
    { "i0", 16, monitor_get_reg },
1779
    { "i1", 17, monitor_get_reg },
1780
    { "i2", 18, monitor_get_reg },
1781
    { "i3", 19, monitor_get_reg },
1782
    { "i4", 20, monitor_get_reg },
1783
    { "i5", 21, monitor_get_reg },
1784
    { "i6", 22, monitor_get_reg },
1785
    { "i7", 23, monitor_get_reg },
1786
    { "pc", offsetof(CPUState, pc) },
1787
    { "npc", offsetof(CPUState, npc) },
1788
    { "y", offsetof(CPUState, y) },
1789
#ifndef TARGET_SPARC64
1790
    { "psr", 0, &monitor_get_psr, },
1791
    { "wim", offsetof(CPUState, wim) },
1792
#endif
1793
    { "tbr", offsetof(CPUState, tbr) },
1794
    { "fsr", offsetof(CPUState, fsr) },
1795
    { "f0", offsetof(CPUState, fpr[0]) },
1796
    { "f1", offsetof(CPUState, fpr[1]) },
1797
    { "f2", offsetof(CPUState, fpr[2]) },
1798
    { "f3", offsetof(CPUState, fpr[3]) },
1799
    { "f4", offsetof(CPUState, fpr[4]) },
1800
    { "f5", offsetof(CPUState, fpr[5]) },
1801
    { "f6", offsetof(CPUState, fpr[6]) },
1802
    { "f7", offsetof(CPUState, fpr[7]) },
1803
    { "f8", offsetof(CPUState, fpr[8]) },
1804
    { "f9", offsetof(CPUState, fpr[9]) },
1805
    { "f10", offsetof(CPUState, fpr[10]) },
1806
    { "f11", offsetof(CPUState, fpr[11]) },
1807
    { "f12", offsetof(CPUState, fpr[12]) },
1808
    { "f13", offsetof(CPUState, fpr[13]) },
1809
    { "f14", offsetof(CPUState, fpr[14]) },
1810
    { "f15", offsetof(CPUState, fpr[15]) },
1811
    { "f16", offsetof(CPUState, fpr[16]) },
1812
    { "f17", offsetof(CPUState, fpr[17]) },
1813
    { "f18", offsetof(CPUState, fpr[18]) },
1814
    { "f19", offsetof(CPUState, fpr[19]) },
1815
    { "f20", offsetof(CPUState, fpr[20]) },
1816
    { "f21", offsetof(CPUState, fpr[21]) },
1817
    { "f22", offsetof(CPUState, fpr[22]) },
1818
    { "f23", offsetof(CPUState, fpr[23]) },
1819
    { "f24", offsetof(CPUState, fpr[24]) },
1820
    { "f25", offsetof(CPUState, fpr[25]) },
1821
    { "f26", offsetof(CPUState, fpr[26]) },
1822
    { "f27", offsetof(CPUState, fpr[27]) },
1823
    { "f28", offsetof(CPUState, fpr[28]) },
1824
    { "f29", offsetof(CPUState, fpr[29]) },
1825
    { "f30", offsetof(CPUState, fpr[30]) },
1826
    { "f31", offsetof(CPUState, fpr[31]) },
1827
#ifdef TARGET_SPARC64
1828
    { "f32", offsetof(CPUState, fpr[32]) },
1829
    { "f34", offsetof(CPUState, fpr[34]) },
1830
    { "f36", offsetof(CPUState, fpr[36]) },
1831
    { "f38", offsetof(CPUState, fpr[38]) },
1832
    { "f40", offsetof(CPUState, fpr[40]) },
1833
    { "f42", offsetof(CPUState, fpr[42]) },
1834
    { "f44", offsetof(CPUState, fpr[44]) },
1835
    { "f46", offsetof(CPUState, fpr[46]) },
1836
    { "f48", offsetof(CPUState, fpr[48]) },
1837
    { "f50", offsetof(CPUState, fpr[50]) },
1838
    { "f52", offsetof(CPUState, fpr[52]) },
1839
    { "f54", offsetof(CPUState, fpr[54]) },
1840
    { "f56", offsetof(CPUState, fpr[56]) },
1841
    { "f58", offsetof(CPUState, fpr[58]) },
1842
    { "f60", offsetof(CPUState, fpr[60]) },
1843
    { "f62", offsetof(CPUState, fpr[62]) },
1844
    { "asi", offsetof(CPUState, asi) },
1845
    { "pstate", offsetof(CPUState, pstate) },
1846
    { "cansave", offsetof(CPUState, cansave) },
1847
    { "canrestore", offsetof(CPUState, canrestore) },
1848
    { "otherwin", offsetof(CPUState, otherwin) },
1849
    { "wstate", offsetof(CPUState, wstate) },
1850
    { "cleanwin", offsetof(CPUState, cleanwin) },
1851
    { "fprs", offsetof(CPUState, fprs) },
1852
#endif
1853
#endif
1854
    { NULL },
1855
};
1856

    
1857
static void expr_error(const char *fmt)
1858
{
1859
    term_printf(fmt);
1860
    term_printf("\n");
1861
    longjmp(expr_env, 1);
1862
}
1863

    
1864
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
1865
static int get_monitor_def(target_long *pval, const char *name)
1866
{
1867
    MonitorDef *md;
1868
    void *ptr;
1869

    
1870
    for(md = monitor_defs; md->name != NULL; md++) {
1871
        if (compare_cmd(name, md->name)) {
1872
            if (md->get_value) {
1873
                *pval = md->get_value(md, md->offset);
1874
            } else {
1875
                CPUState *env = mon_get_cpu();
1876
                if (!env)
1877
                    return -2;
1878
                ptr = (uint8_t *)env + md->offset;
1879
                switch(md->type) {
1880
                case MD_I32:
1881
                    *pval = *(int32_t *)ptr;
1882
                    break;
1883
                case MD_TLONG:
1884
                    *pval = *(target_long *)ptr;
1885
                    break;
1886
                default:
1887
                    *pval = 0;
1888
                    break;
1889
                }
1890
            }
1891
            return 0;
1892
        }
1893
    }
1894
    return -1;
1895
}
1896

    
1897
static void next(void)
1898
{
1899
    if (pch != '\0') {
1900
        pch++;
1901
        while (isspace(*pch))
1902
            pch++;
1903
    }
1904
}
1905

    
1906
static int64_t expr_sum(void);
1907

    
1908
static int64_t expr_unary(void)
1909
{
1910
    int64_t n;
1911
    char *p;
1912
    int ret;
1913

    
1914
    switch(*pch) {
1915
    case '+':
1916
        next();
1917
        n = expr_unary();
1918
        break;
1919
    case '-':
1920
        next();
1921
        n = -expr_unary();
1922
        break;
1923
    case '~':
1924
        next();
1925
        n = ~expr_unary();
1926
        break;
1927
    case '(':
1928
        next();
1929
        n = expr_sum();
1930
        if (*pch != ')') {
1931
            expr_error("')' expected");
1932
        }
1933
        next();
1934
        break;
1935
    case '\'':
1936
        pch++;
1937
        if (*pch == '\0')
1938
            expr_error("character constant expected");
1939
        n = *pch;
1940
        pch++;
1941
        if (*pch != '\'')
1942
            expr_error("missing terminating \' character");
1943
        next();
1944
        break;
1945
    case '$':
1946
        {
1947
            char buf[128], *q;
1948
            target_long reg=0;
1949

    
1950
            pch++;
1951
            q = buf;
1952
            while ((*pch >= 'a' && *pch <= 'z') ||
1953
                   (*pch >= 'A' && *pch <= 'Z') ||
1954
                   (*pch >= '0' && *pch <= '9') ||
1955
                   *pch == '_' || *pch == '.') {
1956
                if ((q - buf) < sizeof(buf) - 1)
1957
                    *q++ = *pch;
1958
                pch++;
1959
            }
1960
            while (isspace(*pch))
1961
                pch++;
1962
            *q = 0;
1963
            ret = get_monitor_def(&reg, buf);
1964
            if (ret == -1)
1965
                expr_error("unknown register");
1966
            else if (ret == -2)
1967
                expr_error("no cpu defined");
1968
            n = reg;
1969
        }
1970
        break;
1971
    case '\0':
1972
        expr_error("unexpected end of expression");
1973
        n = 0;
1974
        break;
1975
    default:
1976
#if TARGET_PHYS_ADDR_BITS > 32
1977
        n = strtoull(pch, &p, 0);
1978
#else
1979
        n = strtoul(pch, &p, 0);
1980
#endif
1981
        if (pch == p) {
1982
            expr_error("invalid char in expression");
1983
        }
1984
        pch = p;
1985
        while (isspace(*pch))
1986
            pch++;
1987
        break;
1988
    }
1989
    return n;
1990
}
1991

    
1992

    
1993
static int64_t expr_prod(void)
1994
{
1995
    int64_t val, val2;
1996
    int op;
1997

    
1998
    val = expr_unary();
1999
    for(;;) {
2000
        op = *pch;
2001
        if (op != '*' && op != '/' && op != '%')
2002
            break;
2003
        next();
2004
        val2 = expr_unary();
2005
        switch(op) {
2006
        default:
2007
        case '*':
2008
            val *= val2;
2009
            break;
2010
        case '/':
2011
        case '%':
2012
            if (val2 == 0)
2013
                expr_error("division by zero");
2014
            if (op == '/')
2015
                val /= val2;
2016
            else
2017
                val %= val2;
2018
            break;
2019
        }
2020
    }
2021
    return val;
2022
}
2023

    
2024
static int64_t expr_logic(void)
2025
{
2026
    int64_t val, val2;
2027
    int op;
2028

    
2029
    val = expr_prod();
2030
    for(;;) {
2031
        op = *pch;
2032
        if (op != '&' && op != '|' && op != '^')
2033
            break;
2034
        next();
2035
        val2 = expr_prod();
2036
        switch(op) {
2037
        default:
2038
        case '&':
2039
            val &= val2;
2040
            break;
2041
        case '|':
2042
            val |= val2;
2043
            break;
2044
        case '^':
2045
            val ^= val2;
2046
            break;
2047
        }
2048
    }
2049
    return val;
2050
}
2051

    
2052
static int64_t expr_sum(void)
2053
{
2054
    int64_t val, val2;
2055
    int op;
2056

    
2057
    val = expr_logic();
2058
    for(;;) {
2059
        op = *pch;
2060
        if (op != '+' && op != '-')
2061
            break;
2062
        next();
2063
        val2 = expr_logic();
2064
        if (op == '+')
2065
            val += val2;
2066
        else
2067
            val -= val2;
2068
    }
2069
    return val;
2070
}
2071

    
2072
static int get_expr(int64_t *pval, const char **pp)
2073
{
2074
    pch = *pp;
2075
    if (setjmp(expr_env)) {
2076
        *pp = pch;
2077
        return -1;
2078
    }
2079
    while (isspace(*pch))
2080
        pch++;
2081
    *pval = expr_sum();
2082
    *pp = pch;
2083
    return 0;
2084
}
2085

    
2086
static int get_str(char *buf, int buf_size, const char **pp)
2087
{
2088
    const char *p;
2089
    char *q;
2090
    int c;
2091

    
2092
    q = buf;
2093
    p = *pp;
2094
    while (isspace(*p))
2095
        p++;
2096
    if (*p == '\0') {
2097
    fail:
2098
        *q = '\0';
2099
        *pp = p;
2100
        return -1;
2101
    }
2102
    if (*p == '\"') {
2103
        p++;
2104
        while (*p != '\0' && *p != '\"') {
2105
            if (*p == '\\') {
2106
                p++;
2107
                c = *p++;
2108
                switch(c) {
2109
                case 'n':
2110
                    c = '\n';
2111
                    break;
2112
                case 'r':
2113
                    c = '\r';
2114
                    break;
2115
                case '\\':
2116
                case '\'':
2117
                case '\"':
2118
                    break;
2119
                default:
2120
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
2121
                    goto fail;
2122
                }
2123
                if ((q - buf) < buf_size - 1) {
2124
                    *q++ = c;
2125
                }
2126
            } else {
2127
                if ((q - buf) < buf_size - 1) {
2128
                    *q++ = *p;
2129
                }
2130
                p++;
2131
            }
2132
        }
2133
        if (*p != '\"') {
2134
            qemu_printf("unterminated string\n");
2135
            goto fail;
2136
        }
2137
        p++;
2138
    } else {
2139
        while (*p != '\0' && !isspace(*p)) {
2140
            if ((q - buf) < buf_size - 1) {
2141
                *q++ = *p;
2142
            }
2143
            p++;
2144
        }
2145
    }
2146
    *q = '\0';
2147
    *pp = p;
2148
    return 0;
2149
}
2150

    
2151
static int default_fmt_format = 'x';
2152
static int default_fmt_size = 4;
2153

    
2154
#define MAX_ARGS 16
2155

    
2156
static void monitor_handle_command(const char *cmdline)
2157
{
2158
    const char *p, *pstart, *typestr;
2159
    char *q;
2160
    int c, nb_args, len, i, has_arg;
2161
    term_cmd_t *cmd;
2162
    char cmdname[256];
2163
    char buf[1024];
2164
    void *str_allocated[MAX_ARGS];
2165
    void *args[MAX_ARGS];
2166
    void (*handler_0)(void);
2167
    void (*handler_1)(void *arg0);
2168
    void (*handler_2)(void *arg0, void *arg1);
2169
    void (*handler_3)(void *arg0, void *arg1, void *arg2);
2170
    void (*handler_4)(void *arg0, void *arg1, void *arg2, void *arg3);
2171
    void (*handler_5)(void *arg0, void *arg1, void *arg2, void *arg3,
2172
                      void *arg4);
2173
    void (*handler_6)(void *arg0, void *arg1, void *arg2, void *arg3,
2174
                      void *arg4, void *arg5);
2175
    void (*handler_7)(void *arg0, void *arg1, void *arg2, void *arg3,
2176
                      void *arg4, void *arg5, void *arg6);
2177

    
2178
#ifdef DEBUG
2179
    term_printf("command='%s'\n", cmdline);
2180
#endif
2181

    
2182
    /* extract the command name */
2183
    p = cmdline;
2184
    q = cmdname;
2185
    while (isspace(*p))
2186
        p++;
2187
    if (*p == '\0')
2188
        return;
2189
    pstart = p;
2190
    while (*p != '\0' && *p != '/' && !isspace(*p))
2191
        p++;
2192
    len = p - pstart;
2193
    if (len > sizeof(cmdname) - 1)
2194
        len = sizeof(cmdname) - 1;
2195
    memcpy(cmdname, pstart, len);
2196
    cmdname[len] = '\0';
2197

    
2198
    /* find the command */
2199
    for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2200
        if (compare_cmd(cmdname, cmd->name))
2201
            goto found;
2202
    }
2203
    term_printf("unknown command: '%s'\n", cmdname);
2204
    return;
2205
 found:
2206

    
2207
    for(i = 0; i < MAX_ARGS; i++)
2208
        str_allocated[i] = NULL;
2209

    
2210
    /* parse the parameters */
2211
    typestr = cmd->args_type;
2212
    nb_args = 0;
2213
    for(;;) {
2214
        c = *typestr;
2215
        if (c == '\0')
2216
            break;
2217
        typestr++;
2218
        switch(c) {
2219
        case 'F':
2220
        case 'B':
2221
        case 's':
2222
            {
2223
                int ret;
2224
                char *str;
2225

    
2226
                while (isspace(*p))
2227
                    p++;
2228
                if (*typestr == '?') {
2229
                    typestr++;
2230
                    if (*p == '\0') {
2231
                        /* no optional string: NULL argument */
2232
                        str = NULL;
2233
                        goto add_str;
2234
                    }
2235
                }
2236
                ret = get_str(buf, sizeof(buf), &p);
2237
                if (ret < 0) {
2238
                    switch(c) {
2239
                    case 'F':
2240
                        term_printf("%s: filename expected\n", cmdname);
2241
                        break;
2242
                    case 'B':
2243
                        term_printf("%s: block device name expected\n", cmdname);
2244
                        break;
2245
                    default:
2246
                        term_printf("%s: string expected\n", cmdname);
2247
                        break;
2248
                    }
2249
                    goto fail;
2250
                }
2251
                str = qemu_malloc(strlen(buf) + 1);
2252
                strcpy(str, buf);
2253
                str_allocated[nb_args] = str;
2254
            add_str:
2255
                if (nb_args >= MAX_ARGS) {
2256
                error_args:
2257
                    term_printf("%s: too many arguments\n", cmdname);
2258
                    goto fail;
2259
                }
2260
                args[nb_args++] = str;
2261
            }
2262
            break;
2263
        case '/':
2264
            {
2265
                int count, format, size;
2266

    
2267
                while (isspace(*p))
2268
                    p++;
2269
                if (*p == '/') {
2270
                    /* format found */
2271
                    p++;
2272
                    count = 1;
2273
                    if (isdigit(*p)) {
2274
                        count = 0;
2275
                        while (isdigit(*p)) {
2276
                            count = count * 10 + (*p - '0');
2277
                            p++;
2278
                        }
2279
                    }
2280
                    size = -1;
2281
                    format = -1;
2282
                    for(;;) {
2283
                        switch(*p) {
2284
                        case 'o':
2285
                        case 'd':
2286
                        case 'u':
2287
                        case 'x':
2288
                        case 'i':
2289
                        case 'c':
2290
                            format = *p++;
2291
                            break;
2292
                        case 'b':
2293
                            size = 1;
2294
                            p++;
2295
                            break;
2296
                        case 'h':
2297
                            size = 2;
2298
                            p++;
2299
                            break;
2300
                        case 'w':
2301
                            size = 4;
2302
                            p++;
2303
                            break;
2304
                        case 'g':
2305
                        case 'L':
2306
                            size = 8;
2307
                            p++;
2308
                            break;
2309
                        default:
2310
                            goto next;
2311
                        }
2312
                    }
2313
                next:
2314
                    if (*p != '\0' && !isspace(*p)) {
2315
                        term_printf("invalid char in format: '%c'\n", *p);
2316
                        goto fail;
2317
                    }
2318
                    if (format < 0)
2319
                        format = default_fmt_format;
2320
                    if (format != 'i') {
2321
                        /* for 'i', not specifying a size gives -1 as size */
2322
                        if (size < 0)
2323
                            size = default_fmt_size;
2324
                    }
2325
                    default_fmt_size = size;
2326
                    default_fmt_format = format;
2327
                } else {
2328
                    count = 1;
2329
                    format = default_fmt_format;
2330
                    if (format != 'i') {
2331
                        size = default_fmt_size;
2332
                    } else {
2333
                        size = -1;
2334
                    }
2335
                }
2336
                if (nb_args + 3 > MAX_ARGS)
2337
                    goto error_args;
2338
                args[nb_args++] = (void*)(long)count;
2339
                args[nb_args++] = (void*)(long)format;
2340
                args[nb_args++] = (void*)(long)size;
2341
            }
2342
            break;
2343
        case 'i':
2344
        case 'l':
2345
            {
2346
                int64_t val;
2347

    
2348
                while (isspace(*p))
2349
                    p++;
2350
                if (*typestr == '?' || *typestr == '.') {
2351
                    if (*typestr == '?') {
2352
                        if (*p == '\0')
2353
                            has_arg = 0;
2354
                        else
2355
                            has_arg = 1;
2356
                    } else {
2357
                        if (*p == '.') {
2358
                            p++;
2359
                            while (isspace(*p))
2360
                                p++;
2361
                            has_arg = 1;
2362
                        } else {
2363
                            has_arg = 0;
2364
                        }
2365
                    }
2366
                    typestr++;
2367
                    if (nb_args >= MAX_ARGS)
2368
                        goto error_args;
2369
                    args[nb_args++] = (void *)(long)has_arg;
2370
                    if (!has_arg) {
2371
                        if (nb_args >= MAX_ARGS)
2372
                            goto error_args;
2373
                        val = -1;
2374
                        goto add_num;
2375
                    }
2376
                }
2377
                if (get_expr(&val, &p))
2378
                    goto fail;
2379
            add_num:
2380
                if (c == 'i') {
2381
                    if (nb_args >= MAX_ARGS)
2382
                        goto error_args;
2383
                    args[nb_args++] = (void *)(long)val;
2384
                } else {
2385
                    if ((nb_args + 1) >= MAX_ARGS)
2386
                        goto error_args;
2387
#if TARGET_PHYS_ADDR_BITS > 32
2388
                    args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2389
#else
2390
                    args[nb_args++] = (void *)0;
2391
#endif
2392
                    args[nb_args++] = (void *)(long)(val & 0xffffffff);
2393
                }
2394
            }
2395
            break;
2396
        case '-':
2397
            {
2398
                int has_option;
2399
                /* option */
2400

    
2401
                c = *typestr++;
2402
                if (c == '\0')
2403
                    goto bad_type;
2404
                while (isspace(*p))
2405
                    p++;
2406
                has_option = 0;
2407
                if (*p == '-') {
2408
                    p++;
2409
                    if (*p != c) {
2410
                        term_printf("%s: unsupported option -%c\n",
2411
                                    cmdname, *p);
2412
                        goto fail;
2413
                    }
2414
                    p++;
2415
                    has_option = 1;
2416
                }
2417
                if (nb_args >= MAX_ARGS)
2418
                    goto error_args;
2419
                args[nb_args++] = (void *)(long)has_option;
2420
            }
2421
            break;
2422
        default:
2423
        bad_type:
2424
            term_printf("%s: unknown type '%c'\n", cmdname, c);
2425
            goto fail;
2426
        }
2427
    }
2428
    /* check that all arguments were parsed */
2429
    while (isspace(*p))
2430
        p++;
2431
    if (*p != '\0') {
2432
        term_printf("%s: extraneous characters at the end of line\n",
2433
                    cmdname);
2434
        goto fail;
2435
    }
2436

    
2437
    switch(nb_args) {
2438
    case 0:
2439
        handler_0 = cmd->handler;
2440
        handler_0();
2441
        break;
2442
    case 1:
2443
        handler_1 = cmd->handler;
2444
        handler_1(args[0]);
2445
        break;
2446
    case 2:
2447
        handler_2 = cmd->handler;
2448
        handler_2(args[0], args[1]);
2449
        break;
2450
    case 3:
2451
        handler_3 = cmd->handler;
2452
        handler_3(args[0], args[1], args[2]);
2453
        break;
2454
    case 4:
2455
        handler_4 = cmd->handler;
2456
        handler_4(args[0], args[1], args[2], args[3]);
2457
        break;
2458
    case 5:
2459
        handler_5 = cmd->handler;
2460
        handler_5(args[0], args[1], args[2], args[3], args[4]);
2461
        break;
2462
    case 6:
2463
        handler_6 = cmd->handler;
2464
        handler_6(args[0], args[1], args[2], args[3], args[4], args[5]);
2465
        break;
2466
    case 7:
2467
        handler_7 = cmd->handler;
2468
        handler_7(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2469
        break;
2470
    default:
2471
        term_printf("unsupported number of arguments: %d\n", nb_args);
2472
        goto fail;
2473
    }
2474
 fail:
2475
    for(i = 0; i < MAX_ARGS; i++)
2476
        qemu_free(str_allocated[i]);
2477
    return;
2478
}
2479

    
2480
static void cmd_completion(const char *name, const char *list)
2481
{
2482
    const char *p, *pstart;
2483
    char cmd[128];
2484
    int len;
2485

    
2486
    p = list;
2487
    for(;;) {
2488
        pstart = p;
2489
        p = strchr(p, '|');
2490
        if (!p)
2491
            p = pstart + strlen(pstart);
2492
        len = p - pstart;
2493
        if (len > sizeof(cmd) - 2)
2494
            len = sizeof(cmd) - 2;
2495
        memcpy(cmd, pstart, len);
2496
        cmd[len] = '\0';
2497
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2498
            add_completion(cmd);
2499
        }
2500
        if (*p == '\0')
2501
            break;
2502
        p++;
2503
    }
2504
}
2505

    
2506
static void file_completion(const char *input)
2507
{
2508
    DIR *ffs;
2509
    struct dirent *d;
2510
    char path[1024];
2511
    char file[1024], file_prefix[1024];
2512
    int input_path_len;
2513
    const char *p;
2514

    
2515
    p = strrchr(input, '/');
2516
    if (!p) {
2517
        input_path_len = 0;
2518
        pstrcpy(file_prefix, sizeof(file_prefix), input);
2519
        strcpy(path, ".");
2520
    } else {
2521
        input_path_len = p - input + 1;
2522
        memcpy(path, input, input_path_len);
2523
        if (input_path_len > sizeof(path) - 1)
2524
            input_path_len = sizeof(path) - 1;
2525
        path[input_path_len] = '\0';
2526
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2527
    }
2528
#ifdef DEBUG_COMPLETION
2529
    term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2530
#endif
2531
    ffs = opendir(path);
2532
    if (!ffs)
2533
        return;
2534
    for(;;) {
2535
        struct stat sb;
2536
        d = readdir(ffs);
2537
        if (!d)
2538
            break;
2539
        if (strstart(d->d_name, file_prefix, NULL)) {
2540
            memcpy(file, input, input_path_len);
2541
            strcpy(file + input_path_len, d->d_name);
2542
            /* stat the file to find out if it's a directory.
2543
             * In that case add a slash to speed up typing long paths
2544
             */
2545
            stat(file, &sb);
2546
            if(S_ISDIR(sb.st_mode))
2547
                strcat(file, "/");
2548
            add_completion(file);
2549
        }
2550
    }
2551
    closedir(ffs);
2552
}
2553

    
2554
static void block_completion_it(void *opaque, const char *name)
2555
{
2556
    const char *input = opaque;
2557

    
2558
    if (input[0] == '\0' ||
2559
        !strncmp(name, (char *)input, strlen(input))) {
2560
        add_completion(name);
2561
    }
2562
}
2563

    
2564
/* NOTE: this parser is an approximate form of the real command parser */
2565
static void parse_cmdline(const char *cmdline,
2566
                         int *pnb_args, char **args)
2567
{
2568
    const char *p;
2569
    int nb_args, ret;
2570
    char buf[1024];
2571

    
2572
    p = cmdline;
2573
    nb_args = 0;
2574
    for(;;) {
2575
        while (isspace(*p))
2576
            p++;
2577
        if (*p == '\0')
2578
            break;
2579
        if (nb_args >= MAX_ARGS)
2580
            break;
2581
        ret = get_str(buf, sizeof(buf), &p);
2582
        args[nb_args] = qemu_strdup(buf);
2583
        nb_args++;
2584
        if (ret < 0)
2585
            break;
2586
    }
2587
    *pnb_args = nb_args;
2588
}
2589

    
2590
void readline_find_completion(const char *cmdline)
2591
{
2592
    const char *cmdname;
2593
    char *args[MAX_ARGS];
2594
    int nb_args, i, len;
2595
    const char *ptype, *str;
2596
    term_cmd_t *cmd;
2597
    const KeyDef *key;
2598

    
2599
    parse_cmdline(cmdline, &nb_args, args);
2600
#ifdef DEBUG_COMPLETION
2601
    for(i = 0; i < nb_args; i++) {
2602
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2603
    }
2604
#endif
2605

    
2606
    /* if the line ends with a space, it means we want to complete the
2607
       next arg */
2608
    len = strlen(cmdline);
2609
    if (len > 0 && isspace(cmdline[len - 1])) {
2610
        if (nb_args >= MAX_ARGS)
2611
            return;
2612
        args[nb_args++] = qemu_strdup("");
2613
    }
2614
    if (nb_args <= 1) {
2615
        /* command completion */
2616
        if (nb_args == 0)
2617
            cmdname = "";
2618
        else
2619
            cmdname = args[0];
2620
        completion_index = strlen(cmdname);
2621
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2622
            cmd_completion(cmdname, cmd->name);
2623
        }
2624
    } else {
2625
        /* find the command */
2626
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2627
            if (compare_cmd(args[0], cmd->name))
2628
                goto found;
2629
        }
2630
        return;
2631
    found:
2632
        ptype = cmd->args_type;
2633
        for(i = 0; i < nb_args - 2; i++) {
2634
            if (*ptype != '\0') {
2635
                ptype++;
2636
                while (*ptype == '?')
2637
                    ptype++;
2638
            }
2639
        }
2640
        str = args[nb_args - 1];
2641
        switch(*ptype) {
2642
        case 'F':
2643
            /* file completion */
2644
            completion_index = strlen(str);
2645
            file_completion(str);
2646
            break;
2647
        case 'B':
2648
            /* block device name completion */
2649
            completion_index = strlen(str);
2650
            bdrv_iterate(block_completion_it, (void *)str);
2651
            break;
2652
        case 's':
2653
            /* XXX: more generic ? */
2654
            if (!strcmp(cmd->name, "info")) {
2655
                completion_index = strlen(str);
2656
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2657
                    cmd_completion(str, cmd->name);
2658
                }
2659
            } else if (!strcmp(cmd->name, "sendkey")) {
2660
                completion_index = strlen(str);
2661
                for(key = key_defs; key->name != NULL; key++) {
2662
                    cmd_completion(str, key->name);
2663
                }
2664
            }
2665
            break;
2666
        default:
2667
            break;
2668
        }
2669
    }
2670
    for(i = 0; i < nb_args; i++)
2671
        qemu_free(args[i]);
2672
}
2673

    
2674
static int term_can_read(void *opaque)
2675
{
2676
    return 128;
2677
}
2678

    
2679
static void term_read(void *opaque, const uint8_t *buf, int size)
2680
{
2681
    int i;
2682
    for(i = 0; i < size; i++)
2683
        readline_handle_byte(buf[i]);
2684
}
2685

    
2686
static void monitor_handle_command1(void *opaque, const char *cmdline)
2687
{
2688
    monitor_handle_command(cmdline);
2689
    monitor_start_input();
2690
}
2691

    
2692
void monitor_start_input(void)
2693
{
2694
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2695
}
2696

    
2697
static void term_event(void *opaque, int event)
2698
{
2699
    if (event != CHR_EVENT_RESET)
2700
        return;
2701

    
2702
    if (!hide_banner)
2703
            term_printf("QEMU %s monitor - type 'help' for more information\n",
2704
                        QEMU_VERSION);
2705
    monitor_start_input();
2706
}
2707

    
2708
static int is_first_init = 1;
2709

    
2710
void monitor_init(CharDriverState *hd, int show_banner)
2711
{
2712
    int i;
2713

    
2714
    if (is_first_init) {
2715
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2716
        if (!key_timer)
2717
            return;
2718
        for (i = 0; i < MAX_MON; i++) {
2719
            monitor_hd[i] = NULL;
2720
        }
2721
        is_first_init = 0;
2722
    }
2723
    for (i = 0; i < MAX_MON; i++) {
2724
        if (monitor_hd[i] == NULL) {
2725
            monitor_hd[i] = hd;
2726
            break;
2727
        }
2728
    }
2729

    
2730
    hide_banner = !show_banner;
2731

    
2732
    qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2733
}
2734

    
2735
/* XXX: use threads ? */
2736
/* modal monitor readline */
2737
static int monitor_readline_started;
2738
static char *monitor_readline_buf;
2739
static int monitor_readline_buf_size;
2740

    
2741
static void monitor_readline_cb(void *opaque, const char *input)
2742
{
2743
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2744
    monitor_readline_started = 0;
2745
}
2746

    
2747
void monitor_readline(const char *prompt, int is_password,
2748
                      char *buf, int buf_size)
2749
{
2750
    int i;
2751
    int old_focus[MAX_MON];
2752

    
2753
    if (is_password) {
2754
        for (i = 0; i < MAX_MON; i++) {
2755
            old_focus[i] = 0;
2756
            if (monitor_hd[i]) {
2757
                old_focus[i] = monitor_hd[i]->focus;
2758
                monitor_hd[i]->focus = 0;
2759
                qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2760
            }
2761
        }
2762
    }
2763

    
2764
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
2765
    monitor_readline_buf = buf;
2766
    monitor_readline_buf_size = buf_size;
2767
    monitor_readline_started = 1;
2768
    while (monitor_readline_started) {
2769
        main_loop_wait(10);
2770
    }
2771
    /* restore original focus */
2772
    if (is_password) {
2773
        for (i = 0; i < MAX_MON; i++)
2774
            if (old_focus[i])
2775
                monitor_hd[i]->focus = old_focus[i];
2776
    }
2777
}