Statistics
| Branch: | Revision:

root / monitor.c @ 1c5bf3bf

History | View | Annotate | Download (65.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 "vl.h"
25
#include "disas.h"
26
#include <dirent.h>
27

    
28
//#define DEBUG
29
//#define DEBUG_COMPLETION
30

    
31
#ifndef offsetof
32
#define offsetof(type, field) ((size_t) &((type *)0)->field)
33
#endif
34

    
35
/*
36
 * Supported types:
37
 * 
38
 * 'F'          filename
39
 * 'B'          block device name
40
 * 's'          string (accept optional quote)
41
 * 'i'          32 bit integer
42
 * 'l'          target long (32 or 64 bit)
43
 * '/'          optional gdb-like print format (like "/10x")
44
 *
45
 * '?'          optional type (for 'F', 's' and 'i')
46
 *
47
 */
48

    
49
typedef struct term_cmd_t {
50
    const char *name;
51
    const char *args_type;
52
    void (*handler)();
53
    const char *params;
54
    const char *help;
55
} term_cmd_t;
56

    
57
#define MAX_MON 4
58
static CharDriverState *monitor_hd[MAX_MON];
59
static int hide_banner;
60

    
61
static term_cmd_t term_cmds[];
62
static term_cmd_t info_cmds[];
63

    
64
static char term_outbuf[1024];
65
static int term_outbuf_index;
66

    
67
static void monitor_start_input(void);
68

    
69
CPUState *mon_cpu = NULL;
70

    
71
void term_flush(void)
72
{
73
    int i;
74
    if (term_outbuf_index > 0) {
75
        for (i = 0; i < MAX_MON; i++)
76
            if (monitor_hd[i] && monitor_hd[i]->focus == 0)
77
                qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
78
        term_outbuf_index = 0;
79
    }
80
}
81

    
82
/* flush at every end of line or if the buffer is full */
83
void term_puts(const char *str)
84
{
85
    int c;
86
    for(;;) {
87
        c = *str++;
88
        if (c == '\0')
89
            break;
90
        if (c == '\n')
91
            term_outbuf[term_outbuf_index++] = '\r';
92
        term_outbuf[term_outbuf_index++] = c;
93
        if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
94
            c == '\n')
95
            term_flush();
96
    }
97
}
98

    
99
void term_vprintf(const char *fmt, va_list ap)
100
{
101
    char buf[4096];
102
    vsnprintf(buf, sizeof(buf), fmt, ap);
103
    term_puts(buf);
104
}
105

    
106
void term_printf(const char *fmt, ...)
107
{
108
    va_list ap;
109
    va_start(ap, fmt);
110
    term_vprintf(fmt, ap);
111
    va_end(ap);
112
}
113

    
114
void term_print_filename(const char *filename)
115
{
116
    int i;
117

    
118
    for (i = 0; filename[i]; i++) {
119
        switch (filename[i]) {
120
        case ' ':
121
        case '"':
122
        case '\\':
123
            term_printf("\\%c", filename[i]);
124
            break;
125
        case '\t':
126
            term_printf("\\t");
127
            break;
128
        case '\r':
129
            term_printf("\\r");
130
            break;
131
        case '\n':
132
            term_printf("\\n");
133
            break;
134
        default:
135
            term_printf("%c", filename[i]);
136
            break;
137
        }
138
    }
139
}
140

    
141
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
142
{
143
    va_list ap;
144
    va_start(ap, fmt);
145
    term_vprintf(fmt, ap);
146
    va_end(ap);
147
    return 0;
148
}
149

    
150
static int compare_cmd(const char *name, const char *list)
151
{
152
    const char *p, *pstart;
153
    int len;
154
    len = strlen(name);
155
    p = list;
156
    for(;;) {
157
        pstart = p;
158
        p = strchr(p, '|');
159
        if (!p)
160
            p = pstart + strlen(pstart);
161
        if ((p - pstart) == len && !memcmp(pstart, name, len))
162
            return 1;
163
        if (*p == '\0')
164
            break;
165
        p++;
166
    }
167
    return 0;
168
}
169

    
170
static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
171
{
172
    term_cmd_t *cmd;
173

    
174
    for(cmd = cmds; cmd->name != NULL; cmd++) {
175
        if (!name || !strcmp(name, cmd->name))
176
            term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
177
    }
178
}
179

    
180
static void help_cmd(const char *name)
181
{
182
    if (name && !strcmp(name, "info")) {
183
        help_cmd1(info_cmds, "info ", NULL);
184
    } else {
185
        help_cmd1(term_cmds, "", name);
186
        if (name && !strcmp(name, "log")) {
187
            CPULogItem *item;
188
            term_printf("Log items (comma separated):\n");
189
            term_printf("%-10s %s\n", "none", "remove all logs");
190
            for(item = cpu_log_items; item->mask != 0; item++) {
191
                term_printf("%-10s %s\n", item->name, item->help);
192
            }
193
        }
194
    }
195
}
196

    
197
static void do_help(const char *name)
198
{
199
    help_cmd(name);
200
}
201

    
202
static void do_commit(const char *device)
203
{
204
    int i, all_devices;
205
    
206
    all_devices = !strcmp(device, "all");
207
    for (i = 0; i < MAX_DISKS; i++) {
208
        if (bs_table[i]) {
209
            if (all_devices || 
210
                !strcmp(bdrv_get_device_name(bs_table[i]), device))
211
                bdrv_commit(bs_table[i]);
212
        }
213
    }
214
}
215

    
216
static void do_info(const char *item)
217
{
218
    term_cmd_t *cmd;
219

    
220
    if (!item)
221
        goto help;
222
    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
223
        if (compare_cmd(item, cmd->name)) 
224
            goto found;
225
    }
226
 help:
227
    help_cmd("info");
228
    return;
229
 found:
230
    cmd->handler();
231
}
232

    
233
static void do_info_version(void)
234
{
235
  term_printf("%s\n", QEMU_VERSION);
236
}
237

    
238
static void do_info_name(void)
239
{
240
    if (qemu_name)
241
        term_printf("%s\n", qemu_name);
242
}
243

    
244
static void do_info_block(void)
245
{
246
    bdrv_info();
247
}
248

    
249
/* get the current CPU defined by the user */
250
int mon_set_cpu(int cpu_index)
251
{
252
    CPUState *env;
253

    
254
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
255
        if (env->cpu_index == cpu_index) {
256
            mon_cpu = env;
257
            return 0;
258
        }
259
    }
260
    return -1;
261
}
262

    
263
CPUState *mon_get_cpu(void)
264
{
265
    if (!mon_cpu) {
266
        mon_set_cpu(0);
267
    }
268
    return mon_cpu;
269
}
270

    
271
static void do_info_registers(void)
272
{
273
    CPUState *env;
274
    env = mon_get_cpu();
275
    if (!env)
276
        return;
277
#ifdef TARGET_I386
278
    cpu_dump_state(env, NULL, monitor_fprintf,
279
                   X86_DUMP_FPU);
280
#else
281
    cpu_dump_state(env, NULL, monitor_fprintf, 
282
                   0);
283
#endif
284
}
285

    
286
static void do_info_cpus(void)
287
{
288
    CPUState *env;
289

    
290
    /* just to set the default cpu if not already done */
291
    mon_get_cpu();
292

    
293
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
294
        term_printf("%c CPU #%d:", 
295
                    (env == mon_cpu) ? '*' : ' ',
296
                    env->cpu_index);
297
#if defined(TARGET_I386)
298
        term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
299
        if (env->hflags & HF_HALTED_MASK)
300
            term_printf(" (halted)");
301
#elif defined(TARGET_PPC)
302
        term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
303
        if (env->halted)
304
            term_printf(" (halted)");
305
#elif defined(TARGET_SPARC)
306
        term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
307
        if (env->halted)
308
            term_printf(" (halted)");
309
#endif
310
        term_printf("\n");
311
    }
312
}
313

    
314
static void do_cpu_set(int index)
315
{
316
    if (mon_set_cpu(index) < 0)
317
        term_printf("Invalid CPU index\n");
318
}
319

    
320
static void do_info_jit(void)
321
{
322
    dump_exec_info(NULL, monitor_fprintf);
323
}
324

    
325
static void do_info_history (void)
326
{
327
    int i;
328
    const char *str;
329
    
330
    i = 0;
331
    for(;;) {
332
        str = readline_get_history(i);
333
        if (!str)
334
            break;
335
        term_printf("%d: '%s'\n", i, str);
336
        i++;
337
    }
338
}
339

    
340
#if defined(TARGET_PPC)
341
/* XXX: not implemented in other targets */
342
static void do_info_cpu_stats (void)
343
{
344
    CPUState *env;
345

    
346
    env = mon_get_cpu();
347
    cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
348
}
349
#endif
350

    
351
static void do_quit(void)
352
{
353
    exit(0);
354
}
355

    
356
static int eject_device(BlockDriverState *bs, int force)
357
{
358
    if (bdrv_is_inserted(bs)) {
359
        if (!force) {
360
            if (!bdrv_is_removable(bs)) {
361
                term_printf("device is not removable\n");
362
                return -1;
363
            }
364
            if (bdrv_is_locked(bs)) {
365
                term_printf("device is locked\n");
366
                return -1;
367
            }
368
        }
369
        bdrv_close(bs);
370
    }
371
    return 0;
372
}
373

    
374
static void do_eject(int force, const char *filename)
375
{
376
    BlockDriverState *bs;
377

    
378
    bs = bdrv_find(filename);
379
    if (!bs) {
380
        term_printf("device not found\n");
381
        return;
382
    }
383
    eject_device(bs, force);
384
}
385

    
386
static void do_change(const char *device, const char *filename)
387
{
388
    BlockDriverState *bs;
389
    int i;
390
    char password[256];
391

    
392
    bs = bdrv_find(device);
393
    if (!bs) {
394
        term_printf("device not found\n");
395
        return;
396
    }
397
    if (eject_device(bs, 0) < 0)
398
        return;
399
    bdrv_open(bs, filename, 0);
400
    if (bdrv_is_encrypted(bs)) {
401
        term_printf("%s is encrypted.\n", device);
402
        for(i = 0; i < 3; i++) {
403
            monitor_readline("Password: ", 1, password, sizeof(password));
404
            if (bdrv_set_key(bs, password) == 0)
405
                break;
406
            term_printf("invalid password\n");
407
        }
408
    }
409
}
410

    
411
static void do_screen_dump(const char *filename)
412
{
413
    vga_hw_screen_dump(filename);
414
}
415

    
416
static void do_log(const char *items)
417
{
418
    int mask;
419
    
420
    if (!strcmp(items, "none")) {
421
        mask = 0;
422
    } else {
423
        mask = cpu_str_to_log_mask(items);
424
        if (!mask) {
425
            help_cmd("log");
426
            return;
427
        }
428
    }
429
    cpu_set_log(mask);
430
}
431

    
432
static void do_stop(void)
433
{
434
    vm_stop(EXCP_INTERRUPT);
435
}
436

    
437
static void do_cont(void)
438
{
439
    vm_start();
440
}
441

    
442
#ifdef CONFIG_GDBSTUB
443
static void do_gdbserver(const char *port)
444
{
445
    if (!port)
446
        port = DEFAULT_GDBSTUB_PORT;
447
    if (gdbserver_start(port) < 0) {
448
        qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
449
    } else {
450
        qemu_printf("Waiting gdb connection on port '%s'\n", port);
451
    }
452
}
453
#endif
454

    
455
static void term_printc(int c)
456
{
457
    term_printf("'");
458
    switch(c) {
459
    case '\'':
460
        term_printf("\\'");
461
        break;
462
    case '\\':
463
        term_printf("\\\\");
464
        break;
465
    case '\n':
466
        term_printf("\\n");
467
        break;
468
    case '\r':
469
        term_printf("\\r");
470
        break;
471
    default:
472
        if (c >= 32 && c <= 126) {
473
            term_printf("%c", c);
474
        } else {
475
            term_printf("\\x%02x", c);
476
        }
477
        break;
478
    }
479
    term_printf("'");
480
}
481

    
482
static void memory_dump(int count, int format, int wsize, 
483
                        target_ulong addr, int is_physical)
484
{
485
    CPUState *env;
486
    int nb_per_line, l, line_size, i, max_digits, len;
487
    uint8_t buf[16];
488
    uint64_t v;
489

    
490
    if (format == 'i') {
491
        int flags;
492
        flags = 0;
493
        env = mon_get_cpu();
494
        if (!env && !is_physical)
495
            return;
496
#ifdef TARGET_I386
497
        if (wsize == 2) {
498
            flags = 1;
499
        } else if (wsize == 4) {
500
            flags = 0;
501
        } else {
502
            /* as default we use the current CS size */
503
            flags = 0;
504
            if (env) {
505
#ifdef TARGET_X86_64
506
                if ((env->efer & MSR_EFER_LMA) && 
507
                    (env->segs[R_CS].flags & DESC_L_MASK))
508
                    flags = 2;
509
                else
510
#endif
511
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
512
                    flags = 1;
513
            }
514
        }
515
#endif
516
        monitor_disas(env, addr, count, is_physical, flags);
517
        return;
518
    }
519

    
520
    len = wsize * count;
521
    if (wsize == 1)
522
        line_size = 8;
523
    else
524
        line_size = 16;
525
    nb_per_line = line_size / wsize;
526
    max_digits = 0;
527

    
528
    switch(format) {
529
    case 'o':
530
        max_digits = (wsize * 8 + 2) / 3;
531
        break;
532
    default:
533
    case 'x':
534
        max_digits = (wsize * 8) / 4;
535
        break;
536
    case 'u':
537
    case 'd':
538
        max_digits = (wsize * 8 * 10 + 32) / 33;
539
        break;
540
    case 'c':
541
        wsize = 1;
542
        break;
543
    }
544

    
545
    while (len > 0) {
546
        term_printf(TARGET_FMT_lx ":", addr);
547
        l = len;
548
        if (l > line_size)
549
            l = line_size;
550
        if (is_physical) {
551
            cpu_physical_memory_rw(addr, buf, l, 0);
552
        } else {
553
            env = mon_get_cpu();
554
            if (!env)
555
                break;
556
            cpu_memory_rw_debug(env, addr, buf, l, 0);
557
        }
558
        i = 0; 
559
        while (i < l) {
560
            switch(wsize) {
561
            default:
562
            case 1:
563
                v = ldub_raw(buf + i);
564
                break;
565
            case 2:
566
                v = lduw_raw(buf + i);
567
                break;
568
            case 4:
569
                v = (uint32_t)ldl_raw(buf + i);
570
                break;
571
            case 8:
572
                v = ldq_raw(buf + i);
573
                break;
574
            }
575
            term_printf(" ");
576
            switch(format) {
577
            case 'o':
578
                term_printf("%#*" PRIo64, max_digits, v);
579
                break;
580
            case 'x':
581
                term_printf("0x%0*" PRIx64, max_digits, v);
582
                break;
583
            case 'u':
584
                term_printf("%*" PRIu64, max_digits, v);
585
                break;
586
            case 'd':
587
                term_printf("%*" PRId64, max_digits, v);
588
                break;
589
            case 'c':
590
                term_printc(v);
591
                break;
592
            }
593
            i += wsize;
594
        }
595
        term_printf("\n");
596
        addr += l;
597
        len -= l;
598
    }
599
}
600

    
601
#if TARGET_LONG_BITS == 64
602
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
603
#else
604
#define GET_TLONG(h, l) (l)
605
#endif
606

    
607
static void do_memory_dump(int count, int format, int size, 
608
                           uint32_t addrh, uint32_t addrl)
609
{
610
    target_long addr = GET_TLONG(addrh, addrl);
611
    memory_dump(count, format, size, addr, 0);
612
}
613

    
614
static void do_physical_memory_dump(int count, int format, int size,
615
                                    uint32_t addrh, uint32_t addrl)
616

    
617
{
618
    target_long addr = GET_TLONG(addrh, addrl);
619
    memory_dump(count, format, size, addr, 1);
620
}
621

    
622
static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
623
{
624
    target_long val = GET_TLONG(valh, vall);
625
#if TARGET_LONG_BITS == 32
626
    switch(format) {
627
    case 'o':
628
        term_printf("%#o", val);
629
        break;
630
    case 'x':
631
        term_printf("%#x", val);
632
        break;
633
    case 'u':
634
        term_printf("%u", val);
635
        break;
636
    default:
637
    case 'd':
638
        term_printf("%d", val);
639
        break;
640
    case 'c':
641
        term_printc(val);
642
        break;
643
    }
644
#else
645
    switch(format) {
646
    case 'o':
647
        term_printf("%#" PRIo64, val);
648
        break;
649
    case 'x':
650
        term_printf("%#" PRIx64, val);
651
        break;
652
    case 'u':
653
        term_printf("%" PRIu64, val);
654
        break;
655
    default:
656
    case 'd':
657
        term_printf("%" PRId64, val);
658
        break;
659
    case 'c':
660
        term_printc(val);
661
        break;
662
    }
663
#endif
664
    term_printf("\n");
665
}
666

    
667
static void do_memory_save(unsigned int valh, unsigned int vall, 
668
                           uint32_t size, const char *filename)
669
{
670
    FILE *f;
671
    target_long addr = GET_TLONG(valh, vall);
672
    uint32_t l;
673
    CPUState *env;
674
    uint8_t buf[1024];
675

    
676
    env = mon_get_cpu();
677
    if (!env)
678
        return;
679

    
680
    f = fopen(filename, "wb");
681
    if (!f) {
682
        term_printf("could not open '%s'\n", filename);
683
        return;
684
    }
685
    while (size != 0) {
686
        l = sizeof(buf);
687
        if (l > size)
688
            l = size;
689
        cpu_memory_rw_debug(env, addr, buf, l, 0);
690
        fwrite(buf, 1, l, f);
691
        addr += l;
692
        size -= l;
693
    }
694
    fclose(f);
695
}
696

    
697
static void do_sum(uint32_t start, uint32_t size)
698
{
699
    uint32_t addr;
700
    uint8_t buf[1];
701
    uint16_t sum;
702

    
703
    sum = 0;
704
    for(addr = start; addr < (start + size); addr++) {
705
        cpu_physical_memory_rw(addr, buf, 1, 0);
706
        /* BSD sum algorithm ('sum' Unix command) */
707
        sum = (sum >> 1) | (sum << 15);
708
        sum += buf[0];
709
    }
710
    term_printf("%05d\n", sum);
711
}
712

    
713
typedef struct {
714
    int keycode;
715
    const char *name;
716
} KeyDef;
717

    
718
static const KeyDef key_defs[] = {
719
    { 0x2a, "shift" },
720
    { 0x36, "shift_r" },
721
    
722
    { 0x38, "alt" },
723
    { 0xb8, "alt_r" },
724
    { 0x1d, "ctrl" },
725
    { 0x9d, "ctrl_r" },
726

    
727
    { 0xdd, "menu" },
728

    
729
    { 0x01, "esc" },
730

    
731
    { 0x02, "1" },
732
    { 0x03, "2" },
733
    { 0x04, "3" },
734
    { 0x05, "4" },
735
    { 0x06, "5" },
736
    { 0x07, "6" },
737
    { 0x08, "7" },
738
    { 0x09, "8" },
739
    { 0x0a, "9" },
740
    { 0x0b, "0" },
741
    { 0x0c, "minus" },
742
    { 0x0d, "equal" },
743
    { 0x0e, "backspace" },
744

    
745
    { 0x0f, "tab" },
746
    { 0x10, "q" },
747
    { 0x11, "w" },
748
    { 0x12, "e" },
749
    { 0x13, "r" },
750
    { 0x14, "t" },
751
    { 0x15, "y" },
752
    { 0x16, "u" },
753
    { 0x17, "i" },
754
    { 0x18, "o" },
755
    { 0x19, "p" },
756

    
757
    { 0x1c, "ret" },
758

    
759
    { 0x1e, "a" },
760
    { 0x1f, "s" },
761
    { 0x20, "d" },
762
    { 0x21, "f" },
763
    { 0x22, "g" },
764
    { 0x23, "h" },
765
    { 0x24, "j" },
766
    { 0x25, "k" },
767
    { 0x26, "l" },
768

    
769
    { 0x2c, "z" },
770
    { 0x2d, "x" },
771
    { 0x2e, "c" },
772
    { 0x2f, "v" },
773
    { 0x30, "b" },
774
    { 0x31, "n" },
775
    { 0x32, "m" },
776
    
777
    { 0x39, "spc" },
778
    { 0x3a, "caps_lock" },
779
    { 0x3b, "f1" },
780
    { 0x3c, "f2" },
781
    { 0x3d, "f3" },
782
    { 0x3e, "f4" },
783
    { 0x3f, "f5" },
784
    { 0x40, "f6" },
785
    { 0x41, "f7" },
786
    { 0x42, "f8" },
787
    { 0x43, "f9" },
788
    { 0x44, "f10" },
789
    { 0x45, "num_lock" },
790
    { 0x46, "scroll_lock" },
791

    
792
    { 0xb5, "kp_divide" },
793
    { 0x37, "kp_multiply" },
794
    { 0x4a, "kp_substract" },
795
    { 0x4e, "kp_add" },
796
    { 0x9c, "kp_enter" },
797
    { 0x53, "kp_decimal" },
798

    
799
    { 0x52, "kp_0" },
800
    { 0x4f, "kp_1" },
801
    { 0x50, "kp_2" },
802
    { 0x51, "kp_3" },
803
    { 0x4b, "kp_4" },
804
    { 0x4c, "kp_5" },
805
    { 0x4d, "kp_6" },
806
    { 0x47, "kp_7" },
807
    { 0x48, "kp_8" },
808
    { 0x49, "kp_9" },
809
    
810
    { 0x56, "<" },
811

    
812
    { 0x57, "f11" },
813
    { 0x58, "f12" },
814

    
815
    { 0xb7, "print" },
816

    
817
    { 0xc7, "home" },
818
    { 0xc9, "pgup" },
819
    { 0xd1, "pgdn" },
820
    { 0xcf, "end" },
821

    
822
    { 0xcb, "left" },
823
    { 0xc8, "up" },
824
    { 0xd0, "down" },
825
    { 0xcd, "right" },
826

    
827
    { 0xd2, "insert" },
828
    { 0xd3, "delete" },
829
    { 0, NULL },
830
};
831

    
832
static int get_keycode(const char *key)
833
{
834
    const KeyDef *p;
835
    char *endp;
836
    int ret;
837

    
838
    for(p = key_defs; p->name != NULL; p++) {
839
        if (!strcmp(key, p->name))
840
            return p->keycode;
841
    }
842
    if (strstart(key, "0x", NULL)) {
843
        ret = strtoul(key, &endp, 0);
844
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
845
            return ret;
846
    }
847
    return -1;
848
}
849

    
850
static void do_send_key(const char *string)
851
{
852
    char keybuf[16], *q;
853
    uint8_t keycodes[16];
854
    const char *p;
855
    int nb_keycodes, keycode, i;
856
    
857
    nb_keycodes = 0;
858
    p = string;
859
    while (*p != '\0') {
860
        q = keybuf;
861
        while (*p != '\0' && *p != '-') {
862
            if ((q - keybuf) < sizeof(keybuf) - 1) {
863
                *q++ = *p;
864
            }
865
            p++;
866
        }
867
        *q = '\0';
868
        keycode = get_keycode(keybuf);
869
        if (keycode < 0) {
870
            term_printf("unknown key: '%s'\n", keybuf);
871
            return;
872
        }
873
        keycodes[nb_keycodes++] = keycode;
874
        if (*p == '\0')
875
            break;
876
        p++;
877
    }
878
    /* key down events */
879
    for(i = 0; i < nb_keycodes; i++) {
880
        keycode = keycodes[i];
881
        if (keycode & 0x80)
882
            kbd_put_keycode(0xe0);
883
        kbd_put_keycode(keycode & 0x7f);
884
    }
885
    /* key up events */
886
    for(i = nb_keycodes - 1; i >= 0; i--) {
887
        keycode = keycodes[i];
888
        if (keycode & 0x80)
889
            kbd_put_keycode(0xe0);
890
        kbd_put_keycode(keycode | 0x80);
891
    }
892
}
893

    
894
static int mouse_button_state;
895

    
896
static void do_mouse_move(const char *dx_str, const char *dy_str, 
897
                          const char *dz_str)
898
{
899
    int dx, dy, dz;
900
    dx = strtol(dx_str, NULL, 0);
901
    dy = strtol(dy_str, NULL, 0);
902
    dz = 0;
903
    if (dz_str) 
904
        dz = strtol(dz_str, NULL, 0);
905
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
906
}
907

    
908
static void do_mouse_button(int button_state)
909
{
910
    mouse_button_state = button_state;
911
    kbd_mouse_event(0, 0, 0, mouse_button_state);
912
}
913

    
914
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
915
{
916
    uint32_t val;
917
    int suffix;
918

    
919
    if (has_index) {
920
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
921
        addr++;
922
    }
923
    addr &= 0xffff;
924

    
925
    switch(size) {
926
    default:
927
    case 1:
928
        val = cpu_inb(NULL, addr);
929
        suffix = 'b';
930
        break;
931
    case 2:
932
        val = cpu_inw(NULL, addr);
933
        suffix = 'w';
934
        break;
935
    case 4:
936
        val = cpu_inl(NULL, addr);
937
        suffix = 'l';
938
        break;
939
    }
940
    term_printf("port%c[0x%04x] = %#0*x\n",
941
                suffix, addr, size * 2, val);
942
}
943

    
944
static void do_system_reset(void)
945
{
946
    qemu_system_reset_request();
947
}
948

    
949
static void do_system_powerdown(void)
950
{
951
    qemu_system_powerdown_request();
952
}
953

    
954
#if defined(TARGET_I386)
955
static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
956
{
957
    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n", 
958
                addr,
959
                pte & mask,
960
                pte & PG_GLOBAL_MASK ? 'G' : '-',
961
                pte & PG_PSE_MASK ? 'P' : '-',
962
                pte & PG_DIRTY_MASK ? 'D' : '-',
963
                pte & PG_ACCESSED_MASK ? 'A' : '-',
964
                pte & PG_PCD_MASK ? 'C' : '-',
965
                pte & PG_PWT_MASK ? 'T' : '-',
966
                pte & PG_USER_MASK ? 'U' : '-',
967
                pte & PG_RW_MASK ? 'W' : '-');
968
}
969

    
970
static void tlb_info(void)
971
{
972
    CPUState *env;
973
    int l1, l2;
974
    uint32_t pgd, pde, pte;
975

    
976
    env = mon_get_cpu();
977
    if (!env)
978
        return;
979

    
980
    if (!(env->cr[0] & CR0_PG_MASK)) {
981
        term_printf("PG disabled\n");
982
        return;
983
    }
984
    pgd = env->cr[3] & ~0xfff;
985
    for(l1 = 0; l1 < 1024; l1++) {
986
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
987
        pde = le32_to_cpu(pde);
988
        if (pde & PG_PRESENT_MASK) {
989
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
990
                print_pte((l1 << 22), pde, ~((1 << 20) - 1));
991
            } else {
992
                for(l2 = 0; l2 < 1024; l2++) {
993
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
994
                                             (uint8_t *)&pte, 4);
995
                    pte = le32_to_cpu(pte);
996
                    if (pte & PG_PRESENT_MASK) {
997
                        print_pte((l1 << 22) + (l2 << 12), 
998
                                  pte & ~PG_PSE_MASK, 
999
                                  ~0xfff);
1000
                    }
1001
                }
1002
            }
1003
        }
1004
    }
1005
}
1006

    
1007
static void mem_print(uint32_t *pstart, int *plast_prot, 
1008
                      uint32_t end, int prot)
1009
{
1010
    int prot1;
1011
    prot1 = *plast_prot;
1012
    if (prot != prot1) {
1013
        if (*pstart != -1) {
1014
            term_printf("%08x-%08x %08x %c%c%c\n",
1015
                        *pstart, end, end - *pstart, 
1016
                        prot1 & PG_USER_MASK ? 'u' : '-',
1017
                        'r',
1018
                        prot1 & PG_RW_MASK ? 'w' : '-');
1019
        }
1020
        if (prot != 0)
1021
            *pstart = end;
1022
        else
1023
            *pstart = -1;
1024
        *plast_prot = prot;
1025
    }
1026
}
1027

    
1028
static void mem_info(void)
1029
{
1030
    CPUState *env;
1031
    int l1, l2, prot, last_prot;
1032
    uint32_t pgd, pde, pte, start, end;
1033

    
1034
    env = mon_get_cpu();
1035
    if (!env)
1036
        return;
1037

    
1038
    if (!(env->cr[0] & CR0_PG_MASK)) {
1039
        term_printf("PG disabled\n");
1040
        return;
1041
    }
1042
    pgd = env->cr[3] & ~0xfff;
1043
    last_prot = 0;
1044
    start = -1;
1045
    for(l1 = 0; l1 < 1024; l1++) {
1046
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1047
        pde = le32_to_cpu(pde);
1048
        end = l1 << 22;
1049
        if (pde & PG_PRESENT_MASK) {
1050
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1051
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1052
                mem_print(&start, &last_prot, end, prot);
1053
            } else {
1054
                for(l2 = 0; l2 < 1024; l2++) {
1055
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
1056
                                             (uint8_t *)&pte, 4);
1057
                    pte = le32_to_cpu(pte);
1058
                    end = (l1 << 22) + (l2 << 12);
1059
                    if (pte & PG_PRESENT_MASK) {
1060
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1061
                    } else {
1062
                        prot = 0;
1063
                    }
1064
                    mem_print(&start, &last_prot, end, prot);
1065
                }
1066
            }
1067
        } else {
1068
            prot = 0;
1069
            mem_print(&start, &last_prot, end, prot);
1070
        }
1071
    }
1072
}
1073
#endif
1074

    
1075
static void do_info_kqemu(void)
1076
{
1077
#ifdef USE_KQEMU
1078
    CPUState *env;
1079
    int val;
1080
    val = 0;
1081
    env = mon_get_cpu();
1082
    if (!env) {
1083
        term_printf("No cpu initialized yet");
1084
        return;
1085
    }
1086
    val = env->kqemu_enabled;
1087
    term_printf("kqemu support: ");
1088
    switch(val) {
1089
    default:
1090
    case 0:
1091
        term_printf("disabled\n");
1092
        break;
1093
    case 1:
1094
        term_printf("enabled for user code\n");
1095
        break;
1096
    case 2:
1097
        term_printf("enabled for user and kernel code\n");
1098
        break;
1099
    }
1100
#else
1101
    term_printf("kqemu support: not compiled\n");
1102
#endif
1103
} 
1104

    
1105
#ifdef CONFIG_PROFILER
1106

    
1107
int64_t kqemu_time;
1108
int64_t qemu_time;
1109
int64_t kqemu_exec_count;
1110
int64_t dev_time;
1111
int64_t kqemu_ret_int_count;
1112
int64_t kqemu_ret_excp_count;
1113
int64_t kqemu_ret_intr_count;
1114

    
1115
static void do_info_profile(void)
1116
{
1117
    int64_t total;
1118
    total = qemu_time;
1119
    if (total == 0)
1120
        total = 1;
1121
    term_printf("async time  %" PRId64 " (%0.3f)\n",
1122
                dev_time, dev_time / (double)ticks_per_sec);
1123
    term_printf("qemu time   %" PRId64 " (%0.3f)\n",
1124
                qemu_time, qemu_time / (double)ticks_per_sec);
1125
    term_printf("kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1126
                kqemu_time, kqemu_time / (double)ticks_per_sec,
1127
                kqemu_time / (double)total * 100.0,
1128
                kqemu_exec_count,
1129
                kqemu_ret_int_count,
1130
                kqemu_ret_excp_count,
1131
                kqemu_ret_intr_count);
1132
    qemu_time = 0;
1133
    kqemu_time = 0;
1134
    kqemu_exec_count = 0;
1135
    dev_time = 0;
1136
    kqemu_ret_int_count = 0;
1137
    kqemu_ret_excp_count = 0;
1138
    kqemu_ret_intr_count = 0;
1139
#ifdef USE_KQEMU
1140
    kqemu_record_dump();
1141
#endif
1142
}
1143
#else
1144
static void do_info_profile(void)
1145
{
1146
    term_printf("Internal profiler not compiled\n");
1147
}
1148
#endif
1149

    
1150
/* Capture support */
1151
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1152

    
1153
static void do_info_capture (void)
1154
{
1155
    int i;
1156
    CaptureState *s;
1157

    
1158
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1159
        term_printf ("[%d]: ", i);
1160
        s->ops.info (s->opaque);
1161
    }
1162
}
1163

    
1164
static void do_stop_capture (int n)
1165
{
1166
    int i;
1167
    CaptureState *s;
1168

    
1169
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1170
        if (i == n) {
1171
            s->ops.destroy (s->opaque);
1172
            LIST_REMOVE (s, entries);
1173
            qemu_free (s);
1174
            return;
1175
        }
1176
    }
1177
}
1178

    
1179
#ifdef HAS_AUDIO
1180
int wav_start_capture (CaptureState *s, const char *path, int freq,
1181
                       int bits, int nchannels);
1182

    
1183
static void do_wav_capture (const char *path,
1184
                            int has_freq, int freq,
1185
                            int has_bits, int bits,
1186
                            int has_channels, int nchannels)
1187
{
1188
    CaptureState *s;
1189

    
1190
    s = qemu_mallocz (sizeof (*s));
1191
    if (!s) {
1192
        term_printf ("Not enough memory to add wave capture\n");
1193
        return;
1194
    }
1195

    
1196
    freq = has_freq ? freq : 44100;
1197
    bits = has_bits ? bits : 16;
1198
    nchannels = has_channels ? nchannels : 2;
1199

    
1200
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1201
        term_printf ("Faied to add wave capture\n");
1202
        qemu_free (s);
1203
    }
1204
    LIST_INSERT_HEAD (&capture_head, s, entries);
1205
}
1206
#endif
1207

    
1208
static term_cmd_t term_cmds[] = {
1209
    { "help|?", "s?", do_help, 
1210
      "[cmd]", "show the help" },
1211
    { "commit", "s", do_commit, 
1212
      "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1213
    { "info", "s?", do_info,
1214
      "subcommand", "show various information about the system state" },
1215
    { "q|quit", "", do_quit,
1216
      "", "quit the emulator" },
1217
    { "eject", "-fB", do_eject,
1218
      "[-f] device", "eject a removable medium (use -f to force it)" },
1219
    { "change", "BF", do_change,
1220
      "device filename", "change a removable medium" },
1221
    { "screendump", "F", do_screen_dump, 
1222
      "filename", "save screen into PPM image 'filename'" },
1223
    { "log", "s", do_log,
1224
      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, 
1225
    { "savevm", "s?", do_savevm,
1226
      "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" }, 
1227
    { "loadvm", "s", do_loadvm,
1228
      "tag|id", "restore a VM snapshot from its tag or id" }, 
1229
    { "delvm", "s", do_delvm,
1230
      "tag|id", "delete a VM snapshot from its tag or id" }, 
1231
    { "stop", "", do_stop, 
1232
      "", "stop emulation", },
1233
    { "c|cont", "", do_cont, 
1234
      "", "resume emulation", },
1235
#ifdef CONFIG_GDBSTUB
1236
    { "gdbserver", "s?", do_gdbserver, 
1237
      "[port]", "start gdbserver session (default port=1234)", },
1238
#endif
1239
    { "x", "/l", do_memory_dump, 
1240
      "/fmt addr", "virtual memory dump starting at 'addr'", },
1241
    { "xp", "/l", do_physical_memory_dump, 
1242
      "/fmt addr", "physical memory dump starting at 'addr'", },
1243
    { "p|print", "/l", do_print, 
1244
      "/fmt expr", "print expression value (use $reg for CPU register access)", },
1245
    { "i", "/ii.", do_ioport_read, 
1246
      "/fmt addr", "I/O port read" },
1247

    
1248
    { "sendkey", "s", do_send_key, 
1249
      "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1250
    { "system_reset", "", do_system_reset, 
1251
      "", "reset the system" },
1252
    { "system_powerdown", "", do_system_powerdown, 
1253
      "", "send system power down event" },
1254
    { "sum", "ii", do_sum, 
1255
      "addr size", "compute the checksum of a memory region" },
1256
    { "usb_add", "s", do_usb_add,
1257
      "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1258
    { "usb_del", "s", do_usb_del,
1259
      "device", "remove USB device 'bus.addr'" },
1260
    { "cpu", "i", do_cpu_set, 
1261
      "index", "set the default CPU" },
1262
    { "mouse_move", "sss?", do_mouse_move, 
1263
      "dx dy [dz]", "send mouse move events" },
1264
    { "mouse_button", "i", do_mouse_button, 
1265
      "state", "change mouse button state (1=L, 2=M, 4=R)" },
1266
    { "mouse_set", "i", do_mouse_set,
1267
      "index", "set which mouse device receives events" },
1268
#ifdef HAS_AUDIO
1269
    { "wavcapture", "si?i?i?", do_wav_capture,
1270
      "path [frequency bits channels]",
1271
      "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1272
#endif
1273
     { "stopcapture", "i", do_stop_capture,
1274
       "capture index", "stop capture" },
1275
    { "memsave", "lis", do_memory_save, 
1276
      "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1277
    { NULL, NULL, }, 
1278
};
1279

    
1280
static term_cmd_t info_cmds[] = {
1281
    { "version", "", do_info_version,
1282
      "", "show the version of qemu" },
1283
    { "network", "", do_info_network,
1284
      "", "show the network state" },
1285
    { "block", "", do_info_block,
1286
      "", "show the block devices" },
1287
    { "registers", "", do_info_registers,
1288
      "", "show the cpu registers" },
1289
    { "cpus", "", do_info_cpus,
1290
      "", "show infos for each CPU" },
1291
    { "history", "", do_info_history,
1292
      "", "show the command line history", },
1293
    { "irq", "", irq_info,
1294
      "", "show the interrupts statistics (if available)", },
1295
    { "pic", "", pic_info,
1296
      "", "show i8259 (PIC) state", },
1297
    { "pci", "", pci_info,
1298
      "", "show PCI info", },
1299
#if defined(TARGET_I386)
1300
    { "tlb", "", tlb_info,
1301
      "", "show virtual to physical memory mappings", },
1302
    { "mem", "", mem_info,
1303
      "", "show the active virtual memory mappings", },
1304
#endif
1305
    { "jit", "", do_info_jit,
1306
      "", "show dynamic compiler info", },
1307
    { "kqemu", "", do_info_kqemu,
1308
      "", "show kqemu information", },
1309
    { "usb", "", usb_info,
1310
      "", "show guest USB devices", },
1311
    { "usbhost", "", usb_host_info,
1312
      "", "show host USB devices", },
1313
    { "profile", "", do_info_profile,
1314
      "", "show profiling information", },
1315
    { "capture", "", do_info_capture,
1316
      "", "show capture information" },
1317
    { "snapshots", "", do_info_snapshots,
1318
      "", "show the currently saved VM snapshots" },
1319
    { "mice", "", do_info_mice,
1320
      "", "show which guest mouse is receiving events" },
1321
    { "vnc", "", do_info_vnc,
1322
      "", "show the vnc server status"},
1323
    { "name", "", do_info_name,
1324
      "", "show the current VM name" },
1325
#if defined(TARGET_PPC)
1326
    { "cpustats", "", do_info_cpu_stats,
1327
      "", "show CPU statistics", },
1328
#endif
1329
    { NULL, NULL, },
1330
};
1331

    
1332
/*******************************************************************/
1333

    
1334
static const char *pch;
1335
static jmp_buf expr_env;
1336

    
1337
#define MD_TLONG 0
1338
#define MD_I32   1
1339

    
1340
typedef struct MonitorDef {
1341
    const char *name;
1342
    int offset;
1343
    target_long (*get_value)(struct MonitorDef *md, int val);
1344
    int type;
1345
} MonitorDef;
1346

    
1347
#if defined(TARGET_I386)
1348
static target_long monitor_get_pc (struct MonitorDef *md, int val)
1349
{
1350
    CPUState *env = mon_get_cpu();
1351
    if (!env)
1352
        return 0;
1353
    return env->eip + env->segs[R_CS].base;
1354
}
1355
#endif
1356

    
1357
#if defined(TARGET_PPC)
1358
static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1359
{
1360
    CPUState *env = mon_get_cpu();
1361
    unsigned int u;
1362
    int i;
1363

    
1364
    if (!env)
1365
        return 0;
1366

    
1367
    u = 0;
1368
    for (i = 0; i < 8; i++)
1369
        u |= env->crf[i] << (32 - (4 * i));
1370

    
1371
    return u;
1372
}
1373

    
1374
static target_long monitor_get_msr (struct MonitorDef *md, int val)
1375
{
1376
    CPUState *env = mon_get_cpu();
1377
    if (!env)
1378
        return 0;
1379
    return (env->msr[MSR_POW] << MSR_POW) |
1380
        (env->msr[MSR_ILE] << MSR_ILE) |
1381
        (env->msr[MSR_EE] << MSR_EE) |
1382
        (env->msr[MSR_PR] << MSR_PR) |
1383
        (env->msr[MSR_FP] << MSR_FP) |
1384
        (env->msr[MSR_ME] << MSR_ME) |
1385
        (env->msr[MSR_FE0] << MSR_FE0) |
1386
        (env->msr[MSR_SE] << MSR_SE) |
1387
        (env->msr[MSR_BE] << MSR_BE) |
1388
        (env->msr[MSR_FE1] << MSR_FE1) |
1389
        (env->msr[MSR_IP] << MSR_IP) |
1390
        (env->msr[MSR_IR] << MSR_IR) |
1391
        (env->msr[MSR_DR] << MSR_DR) |
1392
        (env->msr[MSR_RI] << MSR_RI) |
1393
        (env->msr[MSR_LE] << MSR_LE);
1394
}
1395

    
1396
static target_long monitor_get_xer (struct MonitorDef *md, int val)
1397
{
1398
    CPUState *env = mon_get_cpu();
1399
    if (!env)
1400
        return 0;
1401
    return (env->xer[XER_SO] << XER_SO) |
1402
        (env->xer[XER_OV] << XER_OV) |
1403
        (env->xer[XER_CA] << XER_CA) |
1404
        (env->xer[XER_BC] << XER_BC);
1405
}
1406

    
1407
static target_long monitor_get_decr (struct MonitorDef *md, int val)
1408
{
1409
    CPUState *env = mon_get_cpu();
1410
    if (!env)
1411
        return 0;
1412
    return cpu_ppc_load_decr(env);
1413
}
1414

    
1415
static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1416
{
1417
    CPUState *env = mon_get_cpu();
1418
    if (!env)
1419
        return 0;
1420
    return cpu_ppc_load_tbu(env);
1421
}
1422

    
1423
static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1424
{
1425
    CPUState *env = mon_get_cpu();
1426
    if (!env)
1427
        return 0;
1428
    return cpu_ppc_load_tbl(env);
1429
}
1430
#endif
1431

    
1432
#if defined(TARGET_SPARC)
1433
#ifndef TARGET_SPARC64
1434
static target_long monitor_get_psr (struct MonitorDef *md, int val)
1435
{
1436
    CPUState *env = mon_get_cpu();
1437
    if (!env)
1438
        return 0;
1439
    return GET_PSR(env);
1440
}
1441
#endif
1442

    
1443
static target_long monitor_get_reg(struct MonitorDef *md, int val)
1444
{
1445
    CPUState *env = mon_get_cpu();
1446
    if (!env)
1447
        return 0;
1448
    return env->regwptr[val];
1449
}
1450
#endif
1451

    
1452
static MonitorDef monitor_defs[] = {
1453
#ifdef TARGET_I386
1454

    
1455
#define SEG(name, seg) \
1456
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1457
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1458
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1459

    
1460
    { "eax", offsetof(CPUState, regs[0]) },
1461
    { "ecx", offsetof(CPUState, regs[1]) },
1462
    { "edx", offsetof(CPUState, regs[2]) },
1463
    { "ebx", offsetof(CPUState, regs[3]) },
1464
    { "esp|sp", offsetof(CPUState, regs[4]) },
1465
    { "ebp|fp", offsetof(CPUState, regs[5]) },
1466
    { "esi", offsetof(CPUState, regs[6]) },
1467
    { "edi", offsetof(CPUState, regs[7]) },
1468
#ifdef TARGET_X86_64
1469
    { "r8", offsetof(CPUState, regs[8]) },
1470
    { "r9", offsetof(CPUState, regs[9]) },
1471
    { "r10", offsetof(CPUState, regs[10]) },
1472
    { "r11", offsetof(CPUState, regs[11]) },
1473
    { "r12", offsetof(CPUState, regs[12]) },
1474
    { "r13", offsetof(CPUState, regs[13]) },
1475
    { "r14", offsetof(CPUState, regs[14]) },
1476
    { "r15", offsetof(CPUState, regs[15]) },
1477
#endif
1478
    { "eflags", offsetof(CPUState, eflags) },
1479
    { "eip", offsetof(CPUState, eip) },
1480
    SEG("cs", R_CS)
1481
    SEG("ds", R_DS)
1482
    SEG("es", R_ES)
1483
    SEG("ss", R_SS)
1484
    SEG("fs", R_FS)
1485
    SEG("gs", R_GS)
1486
    { "pc", 0, monitor_get_pc, },
1487
#elif defined(TARGET_PPC)
1488
    { "r0", offsetof(CPUState, gpr[0]) },
1489
    { "r1", offsetof(CPUState, gpr[1]) },
1490
    { "r2", offsetof(CPUState, gpr[2]) },
1491
    { "r3", offsetof(CPUState, gpr[3]) },
1492
    { "r4", offsetof(CPUState, gpr[4]) },
1493
    { "r5", offsetof(CPUState, gpr[5]) },
1494
    { "r6", offsetof(CPUState, gpr[6]) },
1495
    { "r7", offsetof(CPUState, gpr[7]) },
1496
    { "r8", offsetof(CPUState, gpr[8]) },
1497
    { "r9", offsetof(CPUState, gpr[9]) },
1498
    { "r10", offsetof(CPUState, gpr[10]) },
1499
    { "r11", offsetof(CPUState, gpr[11]) },
1500
    { "r12", offsetof(CPUState, gpr[12]) },
1501
    { "r13", offsetof(CPUState, gpr[13]) },
1502
    { "r14", offsetof(CPUState, gpr[14]) },
1503
    { "r15", offsetof(CPUState, gpr[15]) },
1504
    { "r16", offsetof(CPUState, gpr[16]) },
1505
    { "r17", offsetof(CPUState, gpr[17]) },
1506
    { "r18", offsetof(CPUState, gpr[18]) },
1507
    { "r19", offsetof(CPUState, gpr[19]) },
1508
    { "r20", offsetof(CPUState, gpr[20]) },
1509
    { "r21", offsetof(CPUState, gpr[21]) },
1510
    { "r22", offsetof(CPUState, gpr[22]) },
1511
    { "r23", offsetof(CPUState, gpr[23]) },
1512
    { "r24", offsetof(CPUState, gpr[24]) },
1513
    { "r25", offsetof(CPUState, gpr[25]) },
1514
    { "r26", offsetof(CPUState, gpr[26]) },
1515
    { "r27", offsetof(CPUState, gpr[27]) },
1516
    { "r28", offsetof(CPUState, gpr[28]) },
1517
    { "r29", offsetof(CPUState, gpr[29]) },
1518
    { "r30", offsetof(CPUState, gpr[30]) },
1519
    { "r31", offsetof(CPUState, gpr[31]) },
1520
    { "nip|pc", offsetof(CPUState, nip) },
1521
    { "lr", offsetof(CPUState, lr) },
1522
    { "ctr", offsetof(CPUState, ctr) },
1523
    { "decr", 0, &monitor_get_decr, },
1524
    { "ccr", 0, &monitor_get_ccr, },
1525
    { "msr", 0, &monitor_get_msr, },
1526
    { "xer", 0, &monitor_get_xer, },
1527
    { "tbu", 0, &monitor_get_tbu, },
1528
    { "tbl", 0, &monitor_get_tbl, },
1529
    { "sdr1", offsetof(CPUState, sdr1) },
1530
    { "sr0", offsetof(CPUState, sr[0]) },
1531
    { "sr1", offsetof(CPUState, sr[1]) },
1532
    { "sr2", offsetof(CPUState, sr[2]) },
1533
    { "sr3", offsetof(CPUState, sr[3]) },
1534
    { "sr4", offsetof(CPUState, sr[4]) },
1535
    { "sr5", offsetof(CPUState, sr[5]) },
1536
    { "sr6", offsetof(CPUState, sr[6]) },
1537
    { "sr7", offsetof(CPUState, sr[7]) },
1538
    { "sr8", offsetof(CPUState, sr[8]) },
1539
    { "sr9", offsetof(CPUState, sr[9]) },
1540
    { "sr10", offsetof(CPUState, sr[10]) },
1541
    { "sr11", offsetof(CPUState, sr[11]) },
1542
    { "sr12", offsetof(CPUState, sr[12]) },
1543
    { "sr13", offsetof(CPUState, sr[13]) },
1544
    { "sr14", offsetof(CPUState, sr[14]) },
1545
    { "sr15", offsetof(CPUState, sr[15]) },
1546
    /* Too lazy to put BATs and SPRs ... */
1547
#elif defined(TARGET_SPARC)
1548
    { "g0", offsetof(CPUState, gregs[0]) },
1549
    { "g1", offsetof(CPUState, gregs[1]) },
1550
    { "g2", offsetof(CPUState, gregs[2]) },
1551
    { "g3", offsetof(CPUState, gregs[3]) },
1552
    { "g4", offsetof(CPUState, gregs[4]) },
1553
    { "g5", offsetof(CPUState, gregs[5]) },
1554
    { "g6", offsetof(CPUState, gregs[6]) },
1555
    { "g7", offsetof(CPUState, gregs[7]) },
1556
    { "o0", 0, monitor_get_reg },
1557
    { "o1", 1, monitor_get_reg },
1558
    { "o2", 2, monitor_get_reg },
1559
    { "o3", 3, monitor_get_reg },
1560
    { "o4", 4, monitor_get_reg },
1561
    { "o5", 5, monitor_get_reg },
1562
    { "o6", 6, monitor_get_reg },
1563
    { "o7", 7, monitor_get_reg },
1564
    { "l0", 8, monitor_get_reg },
1565
    { "l1", 9, monitor_get_reg },
1566
    { "l2", 10, monitor_get_reg },
1567
    { "l3", 11, monitor_get_reg },
1568
    { "l4", 12, monitor_get_reg },
1569
    { "l5", 13, monitor_get_reg },
1570
    { "l6", 14, monitor_get_reg },
1571
    { "l7", 15, monitor_get_reg },
1572
    { "i0", 16, monitor_get_reg },
1573
    { "i1", 17, monitor_get_reg },
1574
    { "i2", 18, monitor_get_reg },
1575
    { "i3", 19, monitor_get_reg },
1576
    { "i4", 20, monitor_get_reg },
1577
    { "i5", 21, monitor_get_reg },
1578
    { "i6", 22, monitor_get_reg },
1579
    { "i7", 23, monitor_get_reg },
1580
    { "pc", offsetof(CPUState, pc) },
1581
    { "npc", offsetof(CPUState, npc) },
1582
    { "y", offsetof(CPUState, y) },
1583
#ifndef TARGET_SPARC64
1584
    { "psr", 0, &monitor_get_psr, },
1585
    { "wim", offsetof(CPUState, wim) },
1586
#endif
1587
    { "tbr", offsetof(CPUState, tbr) },
1588
    { "fsr", offsetof(CPUState, fsr) },
1589
    { "f0", offsetof(CPUState, fpr[0]) },
1590
    { "f1", offsetof(CPUState, fpr[1]) },
1591
    { "f2", offsetof(CPUState, fpr[2]) },
1592
    { "f3", offsetof(CPUState, fpr[3]) },
1593
    { "f4", offsetof(CPUState, fpr[4]) },
1594
    { "f5", offsetof(CPUState, fpr[5]) },
1595
    { "f6", offsetof(CPUState, fpr[6]) },
1596
    { "f7", offsetof(CPUState, fpr[7]) },
1597
    { "f8", offsetof(CPUState, fpr[8]) },
1598
    { "f9", offsetof(CPUState, fpr[9]) },
1599
    { "f10", offsetof(CPUState, fpr[10]) },
1600
    { "f11", offsetof(CPUState, fpr[11]) },
1601
    { "f12", offsetof(CPUState, fpr[12]) },
1602
    { "f13", offsetof(CPUState, fpr[13]) },
1603
    { "f14", offsetof(CPUState, fpr[14]) },
1604
    { "f15", offsetof(CPUState, fpr[15]) },
1605
    { "f16", offsetof(CPUState, fpr[16]) },
1606
    { "f17", offsetof(CPUState, fpr[17]) },
1607
    { "f18", offsetof(CPUState, fpr[18]) },
1608
    { "f19", offsetof(CPUState, fpr[19]) },
1609
    { "f20", offsetof(CPUState, fpr[20]) },
1610
    { "f21", offsetof(CPUState, fpr[21]) },
1611
    { "f22", offsetof(CPUState, fpr[22]) },
1612
    { "f23", offsetof(CPUState, fpr[23]) },
1613
    { "f24", offsetof(CPUState, fpr[24]) },
1614
    { "f25", offsetof(CPUState, fpr[25]) },
1615
    { "f26", offsetof(CPUState, fpr[26]) },
1616
    { "f27", offsetof(CPUState, fpr[27]) },
1617
    { "f28", offsetof(CPUState, fpr[28]) },
1618
    { "f29", offsetof(CPUState, fpr[29]) },
1619
    { "f30", offsetof(CPUState, fpr[30]) },
1620
    { "f31", offsetof(CPUState, fpr[31]) },
1621
#ifdef TARGET_SPARC64
1622
    { "f32", offsetof(CPUState, fpr[32]) },
1623
    { "f34", offsetof(CPUState, fpr[34]) },
1624
    { "f36", offsetof(CPUState, fpr[36]) },
1625
    { "f38", offsetof(CPUState, fpr[38]) },
1626
    { "f40", offsetof(CPUState, fpr[40]) },
1627
    { "f42", offsetof(CPUState, fpr[42]) },
1628
    { "f44", offsetof(CPUState, fpr[44]) },
1629
    { "f46", offsetof(CPUState, fpr[46]) },
1630
    { "f48", offsetof(CPUState, fpr[48]) },
1631
    { "f50", offsetof(CPUState, fpr[50]) },
1632
    { "f52", offsetof(CPUState, fpr[52]) },
1633
    { "f54", offsetof(CPUState, fpr[54]) },
1634
    { "f56", offsetof(CPUState, fpr[56]) },
1635
    { "f58", offsetof(CPUState, fpr[58]) },
1636
    { "f60", offsetof(CPUState, fpr[60]) },
1637
    { "f62", offsetof(CPUState, fpr[62]) },
1638
    { "asi", offsetof(CPUState, asi) },
1639
    { "pstate", offsetof(CPUState, pstate) },
1640
    { "cansave", offsetof(CPUState, cansave) },
1641
    { "canrestore", offsetof(CPUState, canrestore) },
1642
    { "otherwin", offsetof(CPUState, otherwin) },
1643
    { "wstate", offsetof(CPUState, wstate) },
1644
    { "cleanwin", offsetof(CPUState, cleanwin) },
1645
    { "fprs", offsetof(CPUState, fprs) },
1646
#endif
1647
#endif
1648
    { NULL },
1649
};
1650

    
1651
static void expr_error(const char *fmt) 
1652
{
1653
    term_printf(fmt);
1654
    term_printf("\n");
1655
    longjmp(expr_env, 1);
1656
}
1657

    
1658
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
1659
static int get_monitor_def(target_long *pval, const char *name)
1660
{
1661
    MonitorDef *md;
1662
    void *ptr;
1663

    
1664
    for(md = monitor_defs; md->name != NULL; md++) {
1665
        if (compare_cmd(name, md->name)) {
1666
            if (md->get_value) {
1667
                *pval = md->get_value(md, md->offset);
1668
            } else {
1669
                CPUState *env = mon_get_cpu();
1670
                if (!env)
1671
                    return -2;
1672
                ptr = (uint8_t *)env + md->offset;
1673
                switch(md->type) {
1674
                case MD_I32:
1675
                    *pval = *(int32_t *)ptr;
1676
                    break;
1677
                case MD_TLONG:
1678
                    *pval = *(target_long *)ptr;
1679
                    break;
1680
                default:
1681
                    *pval = 0;
1682
                    break;
1683
                }
1684
            }
1685
            return 0;
1686
        }
1687
    }
1688
    return -1;
1689
}
1690

    
1691
static void next(void)
1692
{
1693
    if (pch != '\0') {
1694
        pch++;
1695
        while (isspace(*pch))
1696
            pch++;
1697
    }
1698
}
1699

    
1700
static target_long expr_sum(void);
1701

    
1702
static target_long expr_unary(void)
1703
{
1704
    target_long n;
1705
    char *p;
1706
    int ret;
1707

    
1708
    switch(*pch) {
1709
    case '+':
1710
        next();
1711
        n = expr_unary();
1712
        break;
1713
    case '-':
1714
        next();
1715
        n = -expr_unary();
1716
        break;
1717
    case '~':
1718
        next();
1719
        n = ~expr_unary();
1720
        break;
1721
    case '(':
1722
        next();
1723
        n = expr_sum();
1724
        if (*pch != ')') {
1725
            expr_error("')' expected");
1726
        }
1727
        next();
1728
        break;
1729
    case '\'':
1730
        pch++;
1731
        if (*pch == '\0')
1732
            expr_error("character constant expected");
1733
        n = *pch;
1734
        pch++;
1735
        if (*pch != '\'')
1736
            expr_error("missing terminating \' character");
1737
        next();
1738
        break;
1739
    case '$':
1740
        {
1741
            char buf[128], *q;
1742
            
1743
            pch++;
1744
            q = buf;
1745
            while ((*pch >= 'a' && *pch <= 'z') ||
1746
                   (*pch >= 'A' && *pch <= 'Z') ||
1747
                   (*pch >= '0' && *pch <= '9') ||
1748
                   *pch == '_' || *pch == '.') {
1749
                if ((q - buf) < sizeof(buf) - 1)
1750
                    *q++ = *pch;
1751
                pch++;
1752
            }
1753
            while (isspace(*pch))
1754
                pch++;
1755
            *q = 0;
1756
            ret = get_monitor_def(&n, buf);
1757
            if (ret == -1)
1758
                expr_error("unknown register");
1759
            else if (ret == -2) 
1760
                expr_error("no cpu defined");
1761
        }
1762
        break;
1763
    case '\0':
1764
        expr_error("unexpected end of expression");
1765
        n = 0;
1766
        break;
1767
    default:
1768
#if TARGET_LONG_BITS == 64
1769
        n = strtoull(pch, &p, 0);
1770
#else
1771
        n = strtoul(pch, &p, 0);
1772
#endif
1773
        if (pch == p) {
1774
            expr_error("invalid char in expression");
1775
        }
1776
        pch = p;
1777
        while (isspace(*pch))
1778
            pch++;
1779
        break;
1780
    }
1781
    return n;
1782
}
1783

    
1784

    
1785
static target_long expr_prod(void)
1786
{
1787
    target_long val, val2;
1788
    int op;
1789
    
1790
    val = expr_unary();
1791
    for(;;) {
1792
        op = *pch;
1793
        if (op != '*' && op != '/' && op != '%')
1794
            break;
1795
        next();
1796
        val2 = expr_unary();
1797
        switch(op) {
1798
        default:
1799
        case '*':
1800
            val *= val2;
1801
            break;
1802
        case '/':
1803
        case '%':
1804
            if (val2 == 0) 
1805
                expr_error("division by zero");
1806
            if (op == '/')
1807
                val /= val2;
1808
            else
1809
                val %= val2;
1810
            break;
1811
        }
1812
    }
1813
    return val;
1814
}
1815

    
1816
static target_long expr_logic(void)
1817
{
1818
    target_long val, val2;
1819
    int op;
1820

    
1821
    val = expr_prod();
1822
    for(;;) {
1823
        op = *pch;
1824
        if (op != '&' && op != '|' && op != '^')
1825
            break;
1826
        next();
1827
        val2 = expr_prod();
1828
        switch(op) {
1829
        default:
1830
        case '&':
1831
            val &= val2;
1832
            break;
1833
        case '|':
1834
            val |= val2;
1835
            break;
1836
        case '^':
1837
            val ^= val2;
1838
            break;
1839
        }
1840
    }
1841
    return val;
1842
}
1843

    
1844
static target_long expr_sum(void)
1845
{
1846
    target_long val, val2;
1847
    int op;
1848

    
1849
    val = expr_logic();
1850
    for(;;) {
1851
        op = *pch;
1852
        if (op != '+' && op != '-')
1853
            break;
1854
        next();
1855
        val2 = expr_logic();
1856
        if (op == '+')
1857
            val += val2;
1858
        else
1859
            val -= val2;
1860
    }
1861
    return val;
1862
}
1863

    
1864
static int get_expr(target_long *pval, const char **pp)
1865
{
1866
    pch = *pp;
1867
    if (setjmp(expr_env)) {
1868
        *pp = pch;
1869
        return -1;
1870
    }
1871
    while (isspace(*pch))
1872
        pch++;
1873
    *pval = expr_sum();
1874
    *pp = pch;
1875
    return 0;
1876
}
1877

    
1878
static int get_str(char *buf, int buf_size, const char **pp)
1879
{
1880
    const char *p;
1881
    char *q;
1882
    int c;
1883

    
1884
    q = buf;
1885
    p = *pp;
1886
    while (isspace(*p))
1887
        p++;
1888
    if (*p == '\0') {
1889
    fail:
1890
        *q = '\0';
1891
        *pp = p;
1892
        return -1;
1893
    }
1894
    if (*p == '\"') {
1895
        p++;
1896
        while (*p != '\0' && *p != '\"') {
1897
            if (*p == '\\') {
1898
                p++;
1899
                c = *p++;
1900
                switch(c) {
1901
                case 'n':
1902
                    c = '\n';
1903
                    break;
1904
                case 'r':
1905
                    c = '\r';
1906
                    break;
1907
                case '\\':
1908
                case '\'':
1909
                case '\"':
1910
                    break;
1911
                default:
1912
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
1913
                    goto fail;
1914
                }
1915
                if ((q - buf) < buf_size - 1) {
1916
                    *q++ = c;
1917
                }
1918
            } else {
1919
                if ((q - buf) < buf_size - 1) {
1920
                    *q++ = *p;
1921
                }
1922
                p++;
1923
            }
1924
        }
1925
        if (*p != '\"') {
1926
            qemu_printf("unterminated string\n");
1927
            goto fail;
1928
        }
1929
        p++;
1930
    } else {
1931
        while (*p != '\0' && !isspace(*p)) {
1932
            if ((q - buf) < buf_size - 1) {
1933
                *q++ = *p;
1934
            }
1935
            p++;
1936
        }
1937
    }
1938
    *q = '\0';
1939
    *pp = p;
1940
    return 0;
1941
}
1942

    
1943
static int default_fmt_format = 'x';
1944
static int default_fmt_size = 4;
1945

    
1946
#define MAX_ARGS 16
1947

    
1948
static void monitor_handle_command(const char *cmdline)
1949
{
1950
    const char *p, *pstart, *typestr;
1951
    char *q;
1952
    int c, nb_args, len, i, has_arg;
1953
    term_cmd_t *cmd;
1954
    char cmdname[256];
1955
    char buf[1024];
1956
    void *str_allocated[MAX_ARGS];
1957
    void *args[MAX_ARGS];
1958

    
1959
#ifdef DEBUG
1960
    term_printf("command='%s'\n", cmdline);
1961
#endif
1962
    
1963
    /* extract the command name */
1964
    p = cmdline;
1965
    q = cmdname;
1966
    while (isspace(*p))
1967
        p++;
1968
    if (*p == '\0')
1969
        return;
1970
    pstart = p;
1971
    while (*p != '\0' && *p != '/' && !isspace(*p))
1972
        p++;
1973
    len = p - pstart;
1974
    if (len > sizeof(cmdname) - 1)
1975
        len = sizeof(cmdname) - 1;
1976
    memcpy(cmdname, pstart, len);
1977
    cmdname[len] = '\0';
1978
    
1979
    /* find the command */
1980
    for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1981
        if (compare_cmd(cmdname, cmd->name)) 
1982
            goto found;
1983
    }
1984
    term_printf("unknown command: '%s'\n", cmdname);
1985
    return;
1986
 found:
1987

    
1988
    for(i = 0; i < MAX_ARGS; i++)
1989
        str_allocated[i] = NULL;
1990
    
1991
    /* parse the parameters */
1992
    typestr = cmd->args_type;
1993
    nb_args = 0;
1994
    for(;;) {
1995
        c = *typestr;
1996
        if (c == '\0')
1997
            break;
1998
        typestr++;
1999
        switch(c) {
2000
        case 'F':
2001
        case 'B':
2002
        case 's':
2003
            {
2004
                int ret;
2005
                char *str;
2006
                
2007
                while (isspace(*p)) 
2008
                    p++;
2009
                if (*typestr == '?') {
2010
                    typestr++;
2011
                    if (*p == '\0') {
2012
                        /* no optional string: NULL argument */
2013
                        str = NULL;
2014
                        goto add_str;
2015
                    }
2016
                }
2017
                ret = get_str(buf, sizeof(buf), &p);
2018
                if (ret < 0) {
2019
                    switch(c) {
2020
                    case 'F':
2021
                        term_printf("%s: filename expected\n", cmdname);
2022
                        break;
2023
                    case 'B':
2024
                        term_printf("%s: block device name expected\n", cmdname);
2025
                        break;
2026
                    default:
2027
                        term_printf("%s: string expected\n", cmdname);
2028
                        break;
2029
                    }
2030
                    goto fail;
2031
                }
2032
                str = qemu_malloc(strlen(buf) + 1);
2033
                strcpy(str, buf);
2034
                str_allocated[nb_args] = str;
2035
            add_str:
2036
                if (nb_args >= MAX_ARGS) {
2037
                error_args:
2038
                    term_printf("%s: too many arguments\n", cmdname);
2039
                    goto fail;
2040
                }
2041
                args[nb_args++] = str;
2042
            }
2043
            break;
2044
        case '/':
2045
            {
2046
                int count, format, size;
2047
                
2048
                while (isspace(*p))
2049
                    p++;
2050
                if (*p == '/') {
2051
                    /* format found */
2052
                    p++;
2053
                    count = 1;
2054
                    if (isdigit(*p)) {
2055
                        count = 0;
2056
                        while (isdigit(*p)) {
2057
                            count = count * 10 + (*p - '0');
2058
                            p++;
2059
                        }
2060
                    }
2061
                    size = -1;
2062
                    format = -1;
2063
                    for(;;) {
2064
                        switch(*p) {
2065
                        case 'o':
2066
                        case 'd':
2067
                        case 'u':
2068
                        case 'x':
2069
                        case 'i':
2070
                        case 'c':
2071
                            format = *p++;
2072
                            break;
2073
                        case 'b':
2074
                            size = 1;
2075
                            p++;
2076
                            break;
2077
                        case 'h':
2078
                            size = 2;
2079
                            p++;
2080
                            break;
2081
                        case 'w':
2082
                            size = 4;
2083
                            p++;
2084
                            break;
2085
                        case 'g':
2086
                        case 'L':
2087
                            size = 8;
2088
                            p++;
2089
                            break;
2090
                        default:
2091
                            goto next;
2092
                        }
2093
                    }
2094
                next:
2095
                    if (*p != '\0' && !isspace(*p)) {
2096
                        term_printf("invalid char in format: '%c'\n", *p);
2097
                        goto fail;
2098
                    }
2099
                    if (format < 0)
2100
                        format = default_fmt_format;
2101
                    if (format != 'i') {
2102
                        /* for 'i', not specifying a size gives -1 as size */
2103
                        if (size < 0)
2104
                            size = default_fmt_size;
2105
                    }
2106
                    default_fmt_size = size;
2107
                    default_fmt_format = format;
2108
                } else {
2109
                    count = 1;
2110
                    format = default_fmt_format;
2111
                    if (format != 'i') {
2112
                        size = default_fmt_size;
2113
                    } else {
2114
                        size = -1;
2115
                    }
2116
                }
2117
                if (nb_args + 3 > MAX_ARGS)
2118
                    goto error_args;
2119
                args[nb_args++] = (void*)(long)count;
2120
                args[nb_args++] = (void*)(long)format;
2121
                args[nb_args++] = (void*)(long)size;
2122
            }
2123
            break;
2124
        case 'i':
2125
        case 'l':
2126
            {
2127
                target_long val;
2128
                while (isspace(*p)) 
2129
                    p++;
2130
                if (*typestr == '?' || *typestr == '.') {
2131
                    if (*typestr == '?') {
2132
                        if (*p == '\0')
2133
                            has_arg = 0;
2134
                        else
2135
                            has_arg = 1;
2136
                    } else {
2137
                        if (*p == '.') {
2138
                            p++;
2139
                            while (isspace(*p)) 
2140
                                p++;
2141
                            has_arg = 1;
2142
                        } else {
2143
                            has_arg = 0;
2144
                        }
2145
                    }
2146
                    typestr++;
2147
                    if (nb_args >= MAX_ARGS)
2148
                        goto error_args;
2149
                    args[nb_args++] = (void *)(long)has_arg;
2150
                    if (!has_arg) {
2151
                        if (nb_args >= MAX_ARGS)
2152
                            goto error_args;
2153
                        val = -1;
2154
                        goto add_num;
2155
                    }
2156
                }
2157
                if (get_expr(&val, &p))
2158
                    goto fail;
2159
            add_num:
2160
                if (c == 'i') {
2161
                    if (nb_args >= MAX_ARGS)
2162
                        goto error_args;
2163
                    args[nb_args++] = (void *)(long)val;
2164
                } else {
2165
                    if ((nb_args + 1) >= MAX_ARGS)
2166
                        goto error_args;
2167
#if TARGET_LONG_BITS == 64
2168
                    args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2169
#else
2170
                    args[nb_args++] = (void *)0;
2171
#endif
2172
                    args[nb_args++] = (void *)(long)(val & 0xffffffff);
2173
                }
2174
            }
2175
            break;
2176
        case '-':
2177
            {
2178
                int has_option;
2179
                /* option */
2180
                
2181
                c = *typestr++;
2182
                if (c == '\0')
2183
                    goto bad_type;
2184
                while (isspace(*p)) 
2185
                    p++;
2186
                has_option = 0;
2187
                if (*p == '-') {
2188
                    p++;
2189
                    if (*p != c) {
2190
                        term_printf("%s: unsupported option -%c\n", 
2191
                                    cmdname, *p);
2192
                        goto fail;
2193
                    }
2194
                    p++;
2195
                    has_option = 1;
2196
                }
2197
                if (nb_args >= MAX_ARGS)
2198
                    goto error_args;
2199
                args[nb_args++] = (void *)(long)has_option;
2200
            }
2201
            break;
2202
        default:
2203
        bad_type:
2204
            term_printf("%s: unknown type '%c'\n", cmdname, c);
2205
            goto fail;
2206
        }
2207
    }
2208
    /* check that all arguments were parsed */
2209
    while (isspace(*p))
2210
        p++;
2211
    if (*p != '\0') {
2212
        term_printf("%s: extraneous characters at the end of line\n", 
2213
                    cmdname);
2214
        goto fail;
2215
    }
2216

    
2217
    switch(nb_args) {
2218
    case 0:
2219
        cmd->handler();
2220
        break;
2221
    case 1:
2222
        cmd->handler(args[0]);
2223
        break;
2224
    case 2:
2225
        cmd->handler(args[0], args[1]);
2226
        break;
2227
    case 3:
2228
        cmd->handler(args[0], args[1], args[2]);
2229
        break;
2230
    case 4:
2231
        cmd->handler(args[0], args[1], args[2], args[3]);
2232
        break;
2233
    case 5:
2234
        cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2235
        break;
2236
    case 6:
2237
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2238
        break;
2239
    case 7:
2240
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2241
        break;
2242
    default:
2243
        term_printf("unsupported number of arguments: %d\n", nb_args);
2244
        goto fail;
2245
    }
2246
 fail:
2247
    for(i = 0; i < MAX_ARGS; i++)
2248
        qemu_free(str_allocated[i]);
2249
    return;
2250
}
2251

    
2252
static void cmd_completion(const char *name, const char *list)
2253
{
2254
    const char *p, *pstart;
2255
    char cmd[128];
2256
    int len;
2257

    
2258
    p = list;
2259
    for(;;) {
2260
        pstart = p;
2261
        p = strchr(p, '|');
2262
        if (!p)
2263
            p = pstart + strlen(pstart);
2264
        len = p - pstart;
2265
        if (len > sizeof(cmd) - 2)
2266
            len = sizeof(cmd) - 2;
2267
        memcpy(cmd, pstart, len);
2268
        cmd[len] = '\0';
2269
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2270
            add_completion(cmd);
2271
        }
2272
        if (*p == '\0')
2273
            break;
2274
        p++;
2275
    }
2276
}
2277

    
2278
static void file_completion(const char *input)
2279
{
2280
    DIR *ffs;
2281
    struct dirent *d;
2282
    char path[1024];
2283
    char file[1024], file_prefix[1024];
2284
    int input_path_len;
2285
    const char *p;
2286

    
2287
    p = strrchr(input, '/'); 
2288
    if (!p) {
2289
        input_path_len = 0;
2290
        pstrcpy(file_prefix, sizeof(file_prefix), input);
2291
        strcpy(path, ".");
2292
    } else {
2293
        input_path_len = p - input + 1;
2294
        memcpy(path, input, input_path_len);
2295
        if (input_path_len > sizeof(path) - 1)
2296
            input_path_len = sizeof(path) - 1;
2297
        path[input_path_len] = '\0';
2298
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2299
    }
2300
#ifdef DEBUG_COMPLETION
2301
    term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2302
#endif
2303
    ffs = opendir(path);
2304
    if (!ffs)
2305
        return;
2306
    for(;;) {
2307
        struct stat sb;
2308
        d = readdir(ffs);
2309
        if (!d)
2310
            break;
2311
        if (strstart(d->d_name, file_prefix, NULL)) {
2312
            memcpy(file, input, input_path_len);
2313
            strcpy(file + input_path_len, d->d_name);
2314
            /* stat the file to find out if it's a directory.
2315
             * In that case add a slash to speed up typing long paths
2316
             */
2317
            stat(file, &sb);
2318
            if(S_ISDIR(sb.st_mode))
2319
                strcat(file, "/");
2320
            add_completion(file);
2321
        }
2322
    }
2323
    closedir(ffs);
2324
}
2325

    
2326
static void block_completion_it(void *opaque, const char *name)
2327
{
2328
    const char *input = opaque;
2329

    
2330
    if (input[0] == '\0' ||
2331
        !strncmp(name, (char *)input, strlen(input))) {
2332
        add_completion(name);
2333
    }
2334
}
2335

    
2336
/* NOTE: this parser is an approximate form of the real command parser */
2337
static void parse_cmdline(const char *cmdline,
2338
                         int *pnb_args, char **args)
2339
{
2340
    const char *p;
2341
    int nb_args, ret;
2342
    char buf[1024];
2343

    
2344
    p = cmdline;
2345
    nb_args = 0;
2346
    for(;;) {
2347
        while (isspace(*p))
2348
            p++;
2349
        if (*p == '\0')
2350
            break;
2351
        if (nb_args >= MAX_ARGS)
2352
            break;
2353
        ret = get_str(buf, sizeof(buf), &p);
2354
        args[nb_args] = qemu_strdup(buf);
2355
        nb_args++;
2356
        if (ret < 0)
2357
            break;
2358
    }
2359
    *pnb_args = nb_args;
2360
}
2361

    
2362
void readline_find_completion(const char *cmdline)
2363
{
2364
    const char *cmdname;
2365
    char *args[MAX_ARGS];
2366
    int nb_args, i, len;
2367
    const char *ptype, *str;
2368
    term_cmd_t *cmd;
2369
    const KeyDef *key;
2370

    
2371
    parse_cmdline(cmdline, &nb_args, args);
2372
#ifdef DEBUG_COMPLETION
2373
    for(i = 0; i < nb_args; i++) {
2374
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2375
    }
2376
#endif
2377

    
2378
    /* if the line ends with a space, it means we want to complete the
2379
       next arg */
2380
    len = strlen(cmdline);
2381
    if (len > 0 && isspace(cmdline[len - 1])) {
2382
        if (nb_args >= MAX_ARGS)
2383
            return;
2384
        args[nb_args++] = qemu_strdup("");
2385
    }
2386
    if (nb_args <= 1) {
2387
        /* command completion */
2388
        if (nb_args == 0)
2389
            cmdname = "";
2390
        else
2391
            cmdname = args[0];
2392
        completion_index = strlen(cmdname);
2393
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2394
            cmd_completion(cmdname, cmd->name);
2395
        }
2396
    } else {
2397
        /* find the command */
2398
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2399
            if (compare_cmd(args[0], cmd->name))
2400
                goto found;
2401
        }
2402
        return;
2403
    found:
2404
        ptype = cmd->args_type;
2405
        for(i = 0; i < nb_args - 2; i++) {
2406
            if (*ptype != '\0') {
2407
                ptype++;
2408
                while (*ptype == '?')
2409
                    ptype++;
2410
            }
2411
        }
2412
        str = args[nb_args - 1];
2413
        switch(*ptype) {
2414
        case 'F':
2415
            /* file completion */
2416
            completion_index = strlen(str);
2417
            file_completion(str);
2418
            break;
2419
        case 'B':
2420
            /* block device name completion */
2421
            completion_index = strlen(str);
2422
            bdrv_iterate(block_completion_it, (void *)str);
2423
            break;
2424
        case 's':
2425
            /* XXX: more generic ? */
2426
            if (!strcmp(cmd->name, "info")) {
2427
                completion_index = strlen(str);
2428
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2429
                    cmd_completion(str, cmd->name);
2430
                }
2431
            } else if (!strcmp(cmd->name, "sendkey")) {
2432
                completion_index = strlen(str);
2433
                for(key = key_defs; key->name != NULL; key++) {
2434
                    cmd_completion(str, key->name);
2435
                }
2436
            }
2437
            break;
2438
        default:
2439
            break;
2440
        }
2441
    }
2442
    for(i = 0; i < nb_args; i++)
2443
        qemu_free(args[i]);
2444
}
2445

    
2446
static int term_can_read(void *opaque)
2447
{
2448
    return 128;
2449
}
2450

    
2451
static void term_read(void *opaque, const uint8_t *buf, int size)
2452
{
2453
    int i;
2454
    for(i = 0; i < size; i++)
2455
        readline_handle_byte(buf[i]);
2456
}
2457

    
2458
static void monitor_start_input(void);
2459

    
2460
static void monitor_handle_command1(void *opaque, const char *cmdline)
2461
{
2462
    monitor_handle_command(cmdline);
2463
    monitor_start_input();
2464
}
2465

    
2466
static void monitor_start_input(void)
2467
{
2468
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2469
}
2470

    
2471
static void term_event(void *opaque, int event)
2472
{
2473
    if (event != CHR_EVENT_RESET)
2474
        return;
2475

    
2476
    if (!hide_banner)
2477
            term_printf("QEMU %s monitor - type 'help' for more information\n",
2478
                        QEMU_VERSION);
2479
    monitor_start_input();
2480
}
2481

    
2482
static int is_first_init = 1;
2483

    
2484
void monitor_init(CharDriverState *hd, int show_banner)
2485
{
2486
    int i;
2487

    
2488
    if (is_first_init) {
2489
        for (i = 0; i < MAX_MON; i++) {
2490
            monitor_hd[i] = NULL;
2491
        }
2492
        is_first_init = 0;
2493
    }
2494
    for (i = 0; i < MAX_MON; i++) {
2495
        if (monitor_hd[i] == NULL) {
2496
            monitor_hd[i] = hd;
2497
            break;
2498
        }
2499
    }
2500

    
2501
    hide_banner = !show_banner;
2502

    
2503
    qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2504
}
2505

    
2506
/* XXX: use threads ? */
2507
/* modal monitor readline */
2508
static int monitor_readline_started;
2509
static char *monitor_readline_buf;
2510
static int monitor_readline_buf_size;
2511

    
2512
static void monitor_readline_cb(void *opaque, const char *input)
2513
{
2514
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2515
    monitor_readline_started = 0;
2516
}
2517

    
2518
void monitor_readline(const char *prompt, int is_password,
2519
                      char *buf, int buf_size)
2520
{
2521
    int i;
2522

    
2523
    if (is_password) {
2524
        for (i = 0; i < MAX_MON; i++)
2525
            if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2526
                qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2527
    }
2528
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
2529
    monitor_readline_buf = buf;
2530
    monitor_readline_buf_size = buf_size;
2531
    monitor_readline_started = 1;
2532
    while (monitor_readline_started) {
2533
        main_loop_wait(10);
2534
    }
2535
}