Statistics
| Branch: | Revision:

root / monitor.c @ 2bac6019

History | View | Annotate | Download (65.2 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

    
390
    bs = bdrv_find(device);
391
    if (!bs) {
392
        term_printf("device not found\n");
393
        return;
394
    }
395
    if (eject_device(bs, 0) < 0)
396
        return;
397
    bdrv_open(bs, filename, 0);
398
    qemu_key_check(bs, filename);
399
}
400

    
401
static void do_screen_dump(const char *filename)
402
{
403
    vga_hw_screen_dump(filename);
404
}
405

    
406
static void do_log(const char *items)
407
{
408
    int mask;
409
    
410
    if (!strcmp(items, "none")) {
411
        mask = 0;
412
    } else {
413
        mask = cpu_str_to_log_mask(items);
414
        if (!mask) {
415
            help_cmd("log");
416
            return;
417
        }
418
    }
419
    cpu_set_log(mask);
420
}
421

    
422
static void do_stop(void)
423
{
424
    vm_stop(EXCP_INTERRUPT);
425
}
426

    
427
static void do_cont(void)
428
{
429
    vm_start();
430
}
431

    
432
#ifdef CONFIG_GDBSTUB
433
static void do_gdbserver(const char *port)
434
{
435
    if (!port)
436
        port = DEFAULT_GDBSTUB_PORT;
437
    if (gdbserver_start(port) < 0) {
438
        qemu_printf("Could not open gdbserver socket on port '%s'\n", port);
439
    } else {
440
        qemu_printf("Waiting gdb connection on port '%s'\n", port);
441
    }
442
}
443
#endif
444

    
445
static void term_printc(int c)
446
{
447
    term_printf("'");
448
    switch(c) {
449
    case '\'':
450
        term_printf("\\'");
451
        break;
452
    case '\\':
453
        term_printf("\\\\");
454
        break;
455
    case '\n':
456
        term_printf("\\n");
457
        break;
458
    case '\r':
459
        term_printf("\\r");
460
        break;
461
    default:
462
        if (c >= 32 && c <= 126) {
463
            term_printf("%c", c);
464
        } else {
465
            term_printf("\\x%02x", c);
466
        }
467
        break;
468
    }
469
    term_printf("'");
470
}
471

    
472
static void memory_dump(int count, int format, int wsize, 
473
                        target_ulong addr, int is_physical)
474
{
475
    CPUState *env;
476
    int nb_per_line, l, line_size, i, max_digits, len;
477
    uint8_t buf[16];
478
    uint64_t v;
479

    
480
    if (format == 'i') {
481
        int flags;
482
        flags = 0;
483
        env = mon_get_cpu();
484
        if (!env && !is_physical)
485
            return;
486
#ifdef TARGET_I386
487
        if (wsize == 2) {
488
            flags = 1;
489
        } else if (wsize == 4) {
490
            flags = 0;
491
        } else {
492
            /* as default we use the current CS size */
493
            flags = 0;
494
            if (env) {
495
#ifdef TARGET_X86_64
496
                if ((env->efer & MSR_EFER_LMA) && 
497
                    (env->segs[R_CS].flags & DESC_L_MASK))
498
                    flags = 2;
499
                else
500
#endif
501
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
502
                    flags = 1;
503
            }
504
        }
505
#endif
506
        monitor_disas(env, addr, count, is_physical, flags);
507
        return;
508
    }
509

    
510
    len = wsize * count;
511
    if (wsize == 1)
512
        line_size = 8;
513
    else
514
        line_size = 16;
515
    nb_per_line = line_size / wsize;
516
    max_digits = 0;
517

    
518
    switch(format) {
519
    case 'o':
520
        max_digits = (wsize * 8 + 2) / 3;
521
        break;
522
    default:
523
    case 'x':
524
        max_digits = (wsize * 8) / 4;
525
        break;
526
    case 'u':
527
    case 'd':
528
        max_digits = (wsize * 8 * 10 + 32) / 33;
529
        break;
530
    case 'c':
531
        wsize = 1;
532
        break;
533
    }
534

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

    
591
#if TARGET_LONG_BITS == 64
592
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
593
#else
594
#define GET_TLONG(h, l) (l)
595
#endif
596

    
597
static void do_memory_dump(int count, int format, int size, 
598
                           uint32_t addrh, uint32_t addrl)
599
{
600
    target_long addr = GET_TLONG(addrh, addrl);
601
    memory_dump(count, format, size, addr, 0);
602
}
603

    
604
static void do_physical_memory_dump(int count, int format, int size,
605
                                    uint32_t addrh, uint32_t addrl)
606

    
607
{
608
    target_long addr = GET_TLONG(addrh, addrl);
609
    memory_dump(count, format, size, addr, 1);
610
}
611

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

    
657
static void do_memory_save(unsigned int valh, unsigned int vall, 
658
                           uint32_t size, const char *filename)
659
{
660
    FILE *f;
661
    target_long addr = GET_TLONG(valh, vall);
662
    uint32_t l;
663
    CPUState *env;
664
    uint8_t buf[1024];
665

    
666
    env = mon_get_cpu();
667
    if (!env)
668
        return;
669

    
670
    f = fopen(filename, "wb");
671
    if (!f) {
672
        term_printf("could not open '%s'\n", filename);
673
        return;
674
    }
675
    while (size != 0) {
676
        l = sizeof(buf);
677
        if (l > size)
678
            l = size;
679
        cpu_memory_rw_debug(env, addr, buf, l, 0);
680
        fwrite(buf, 1, l, f);
681
        addr += l;
682
        size -= l;
683
    }
684
    fclose(f);
685
}
686

    
687
static void do_sum(uint32_t start, uint32_t size)
688
{
689
    uint32_t addr;
690
    uint8_t buf[1];
691
    uint16_t sum;
692

    
693
    sum = 0;
694
    for(addr = start; addr < (start + size); addr++) {
695
        cpu_physical_memory_rw(addr, buf, 1, 0);
696
        /* BSD sum algorithm ('sum' Unix command) */
697
        sum = (sum >> 1) | (sum << 15);
698
        sum += buf[0];
699
    }
700
    term_printf("%05d\n", sum);
701
}
702

    
703
typedef struct {
704
    int keycode;
705
    const char *name;
706
} KeyDef;
707

    
708
static const KeyDef key_defs[] = {
709
    { 0x2a, "shift" },
710
    { 0x36, "shift_r" },
711
    
712
    { 0x38, "alt" },
713
    { 0xb8, "alt_r" },
714
    { 0x1d, "ctrl" },
715
    { 0x9d, "ctrl_r" },
716

    
717
    { 0xdd, "menu" },
718

    
719
    { 0x01, "esc" },
720

    
721
    { 0x02, "1" },
722
    { 0x03, "2" },
723
    { 0x04, "3" },
724
    { 0x05, "4" },
725
    { 0x06, "5" },
726
    { 0x07, "6" },
727
    { 0x08, "7" },
728
    { 0x09, "8" },
729
    { 0x0a, "9" },
730
    { 0x0b, "0" },
731
    { 0x0c, "minus" },
732
    { 0x0d, "equal" },
733
    { 0x0e, "backspace" },
734

    
735
    { 0x0f, "tab" },
736
    { 0x10, "q" },
737
    { 0x11, "w" },
738
    { 0x12, "e" },
739
    { 0x13, "r" },
740
    { 0x14, "t" },
741
    { 0x15, "y" },
742
    { 0x16, "u" },
743
    { 0x17, "i" },
744
    { 0x18, "o" },
745
    { 0x19, "p" },
746

    
747
    { 0x1c, "ret" },
748

    
749
    { 0x1e, "a" },
750
    { 0x1f, "s" },
751
    { 0x20, "d" },
752
    { 0x21, "f" },
753
    { 0x22, "g" },
754
    { 0x23, "h" },
755
    { 0x24, "j" },
756
    { 0x25, "k" },
757
    { 0x26, "l" },
758

    
759
    { 0x2c, "z" },
760
    { 0x2d, "x" },
761
    { 0x2e, "c" },
762
    { 0x2f, "v" },
763
    { 0x30, "b" },
764
    { 0x31, "n" },
765
    { 0x32, "m" },
766
    
767
    { 0x39, "spc" },
768
    { 0x3a, "caps_lock" },
769
    { 0x3b, "f1" },
770
    { 0x3c, "f2" },
771
    { 0x3d, "f3" },
772
    { 0x3e, "f4" },
773
    { 0x3f, "f5" },
774
    { 0x40, "f6" },
775
    { 0x41, "f7" },
776
    { 0x42, "f8" },
777
    { 0x43, "f9" },
778
    { 0x44, "f10" },
779
    { 0x45, "num_lock" },
780
    { 0x46, "scroll_lock" },
781

    
782
    { 0xb5, "kp_divide" },
783
    { 0x37, "kp_multiply" },
784
    { 0x4a, "kp_substract" },
785
    { 0x4e, "kp_add" },
786
    { 0x9c, "kp_enter" },
787
    { 0x53, "kp_decimal" },
788

    
789
    { 0x52, "kp_0" },
790
    { 0x4f, "kp_1" },
791
    { 0x50, "kp_2" },
792
    { 0x51, "kp_3" },
793
    { 0x4b, "kp_4" },
794
    { 0x4c, "kp_5" },
795
    { 0x4d, "kp_6" },
796
    { 0x47, "kp_7" },
797
    { 0x48, "kp_8" },
798
    { 0x49, "kp_9" },
799
    
800
    { 0x56, "<" },
801

    
802
    { 0x57, "f11" },
803
    { 0x58, "f12" },
804

    
805
    { 0xb7, "print" },
806

    
807
    { 0xc7, "home" },
808
    { 0xc9, "pgup" },
809
    { 0xd1, "pgdn" },
810
    { 0xcf, "end" },
811

    
812
    { 0xcb, "left" },
813
    { 0xc8, "up" },
814
    { 0xd0, "down" },
815
    { 0xcd, "right" },
816

    
817
    { 0xd2, "insert" },
818
    { 0xd3, "delete" },
819
    { 0, NULL },
820
};
821

    
822
static int get_keycode(const char *key)
823
{
824
    const KeyDef *p;
825
    char *endp;
826
    int ret;
827

    
828
    for(p = key_defs; p->name != NULL; p++) {
829
        if (!strcmp(key, p->name))
830
            return p->keycode;
831
    }
832
    if (strstart(key, "0x", NULL)) {
833
        ret = strtoul(key, &endp, 0);
834
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
835
            return ret;
836
    }
837
    return -1;
838
}
839

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

    
884
static int mouse_button_state;
885

    
886
static void do_mouse_move(const char *dx_str, const char *dy_str, 
887
                          const char *dz_str)
888
{
889
    int dx, dy, dz;
890
    dx = strtol(dx_str, NULL, 0);
891
    dy = strtol(dy_str, NULL, 0);
892
    dz = 0;
893
    if (dz_str) 
894
        dz = strtol(dz_str, NULL, 0);
895
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
896
}
897

    
898
static void do_mouse_button(int button_state)
899
{
900
    mouse_button_state = button_state;
901
    kbd_mouse_event(0, 0, 0, mouse_button_state);
902
}
903

    
904
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
905
{
906
    uint32_t val;
907
    int suffix;
908

    
909
    if (has_index) {
910
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
911
        addr++;
912
    }
913
    addr &= 0xffff;
914

    
915
    switch(size) {
916
    default:
917
    case 1:
918
        val = cpu_inb(NULL, addr);
919
        suffix = 'b';
920
        break;
921
    case 2:
922
        val = cpu_inw(NULL, addr);
923
        suffix = 'w';
924
        break;
925
    case 4:
926
        val = cpu_inl(NULL, addr);
927
        suffix = 'l';
928
        break;
929
    }
930
    term_printf("port%c[0x%04x] = %#0*x\n",
931
                suffix, addr, size * 2, val);
932
}
933

    
934
static void do_system_reset(void)
935
{
936
    qemu_system_reset_request();
937
}
938

    
939
static void do_system_powerdown(void)
940
{
941
    qemu_system_powerdown_request();
942
}
943

    
944
#if defined(TARGET_I386)
945
static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
946
{
947
    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n", 
948
                addr,
949
                pte & mask,
950
                pte & PG_GLOBAL_MASK ? 'G' : '-',
951
                pte & PG_PSE_MASK ? 'P' : '-',
952
                pte & PG_DIRTY_MASK ? 'D' : '-',
953
                pte & PG_ACCESSED_MASK ? 'A' : '-',
954
                pte & PG_PCD_MASK ? 'C' : '-',
955
                pte & PG_PWT_MASK ? 'T' : '-',
956
                pte & PG_USER_MASK ? 'U' : '-',
957
                pte & PG_RW_MASK ? 'W' : '-');
958
}
959

    
960
static void tlb_info(void)
961
{
962
    CPUState *env;
963
    int l1, l2;
964
    uint32_t pgd, pde, pte;
965

    
966
    env = mon_get_cpu();
967
    if (!env)
968
        return;
969

    
970
    if (!(env->cr[0] & CR0_PG_MASK)) {
971
        term_printf("PG disabled\n");
972
        return;
973
    }
974
    pgd = env->cr[3] & ~0xfff;
975
    for(l1 = 0; l1 < 1024; l1++) {
976
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
977
        pde = le32_to_cpu(pde);
978
        if (pde & PG_PRESENT_MASK) {
979
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
980
                print_pte((l1 << 22), pde, ~((1 << 20) - 1));
981
            } else {
982
                for(l2 = 0; l2 < 1024; l2++) {
983
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
984
                                             (uint8_t *)&pte, 4);
985
                    pte = le32_to_cpu(pte);
986
                    if (pte & PG_PRESENT_MASK) {
987
                        print_pte((l1 << 22) + (l2 << 12), 
988
                                  pte & ~PG_PSE_MASK, 
989
                                  ~0xfff);
990
                    }
991
                }
992
            }
993
        }
994
    }
995
}
996

    
997
static void mem_print(uint32_t *pstart, int *plast_prot, 
998
                      uint32_t end, int prot)
999
{
1000
    int prot1;
1001
    prot1 = *plast_prot;
1002
    if (prot != prot1) {
1003
        if (*pstart != -1) {
1004
            term_printf("%08x-%08x %08x %c%c%c\n",
1005
                        *pstart, end, end - *pstart, 
1006
                        prot1 & PG_USER_MASK ? 'u' : '-',
1007
                        'r',
1008
                        prot1 & PG_RW_MASK ? 'w' : '-');
1009
        }
1010
        if (prot != 0)
1011
            *pstart = end;
1012
        else
1013
            *pstart = -1;
1014
        *plast_prot = prot;
1015
    }
1016
}
1017

    
1018
static void mem_info(void)
1019
{
1020
    CPUState *env;
1021
    int l1, l2, prot, last_prot;
1022
    uint32_t pgd, pde, pte, start, end;
1023

    
1024
    env = mon_get_cpu();
1025
    if (!env)
1026
        return;
1027

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

    
1065
static void do_info_kqemu(void)
1066
{
1067
#ifdef USE_KQEMU
1068
    CPUState *env;
1069
    int val;
1070
    val = 0;
1071
    env = mon_get_cpu();
1072
    if (!env) {
1073
        term_printf("No cpu initialized yet");
1074
        return;
1075
    }
1076
    val = env->kqemu_enabled;
1077
    term_printf("kqemu support: ");
1078
    switch(val) {
1079
    default:
1080
    case 0:
1081
        term_printf("disabled\n");
1082
        break;
1083
    case 1:
1084
        term_printf("enabled for user code\n");
1085
        break;
1086
    case 2:
1087
        term_printf("enabled for user and kernel code\n");
1088
        break;
1089
    }
1090
#else
1091
    term_printf("kqemu support: not compiled\n");
1092
#endif
1093
} 
1094

    
1095
#ifdef CONFIG_PROFILER
1096

    
1097
int64_t kqemu_time;
1098
int64_t qemu_time;
1099
int64_t kqemu_exec_count;
1100
int64_t dev_time;
1101
int64_t kqemu_ret_int_count;
1102
int64_t kqemu_ret_excp_count;
1103
int64_t kqemu_ret_intr_count;
1104

    
1105
static void do_info_profile(void)
1106
{
1107
    int64_t total;
1108
    total = qemu_time;
1109
    if (total == 0)
1110
        total = 1;
1111
    term_printf("async time  %" PRId64 " (%0.3f)\n",
1112
                dev_time, dev_time / (double)ticks_per_sec);
1113
    term_printf("qemu time   %" PRId64 " (%0.3f)\n",
1114
                qemu_time, qemu_time / (double)ticks_per_sec);
1115
    term_printf("kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1116
                kqemu_time, kqemu_time / (double)ticks_per_sec,
1117
                kqemu_time / (double)total * 100.0,
1118
                kqemu_exec_count,
1119
                kqemu_ret_int_count,
1120
                kqemu_ret_excp_count,
1121
                kqemu_ret_intr_count);
1122
    qemu_time = 0;
1123
    kqemu_time = 0;
1124
    kqemu_exec_count = 0;
1125
    dev_time = 0;
1126
    kqemu_ret_int_count = 0;
1127
    kqemu_ret_excp_count = 0;
1128
    kqemu_ret_intr_count = 0;
1129
#ifdef USE_KQEMU
1130
    kqemu_record_dump();
1131
#endif
1132
}
1133
#else
1134
static void do_info_profile(void)
1135
{
1136
    term_printf("Internal profiler not compiled\n");
1137
}
1138
#endif
1139

    
1140
/* Capture support */
1141
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1142

    
1143
static void do_info_capture (void)
1144
{
1145
    int i;
1146
    CaptureState *s;
1147

    
1148
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1149
        term_printf ("[%d]: ", i);
1150
        s->ops.info (s->opaque);
1151
    }
1152
}
1153

    
1154
static void do_stop_capture (int n)
1155
{
1156
    int i;
1157
    CaptureState *s;
1158

    
1159
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1160
        if (i == n) {
1161
            s->ops.destroy (s->opaque);
1162
            LIST_REMOVE (s, entries);
1163
            qemu_free (s);
1164
            return;
1165
        }
1166
    }
1167
}
1168

    
1169
#ifdef HAS_AUDIO
1170
int wav_start_capture (CaptureState *s, const char *path, int freq,
1171
                       int bits, int nchannels);
1172

    
1173
static void do_wav_capture (const char *path,
1174
                            int has_freq, int freq,
1175
                            int has_bits, int bits,
1176
                            int has_channels, int nchannels)
1177
{
1178
    CaptureState *s;
1179

    
1180
    s = qemu_mallocz (sizeof (*s));
1181
    if (!s) {
1182
        term_printf ("Not enough memory to add wave capture\n");
1183
        return;
1184
    }
1185

    
1186
    freq = has_freq ? freq : 44100;
1187
    bits = has_bits ? bits : 16;
1188
    nchannels = has_channels ? nchannels : 2;
1189

    
1190
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1191
        term_printf ("Faied to add wave capture\n");
1192
        qemu_free (s);
1193
    }
1194
    LIST_INSERT_HEAD (&capture_head, s, entries);
1195
}
1196
#endif
1197

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

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

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

    
1324
/*******************************************************************/
1325

    
1326
static const char *pch;
1327
static jmp_buf expr_env;
1328

    
1329
#define MD_TLONG 0
1330
#define MD_I32   1
1331

    
1332
typedef struct MonitorDef {
1333
    const char *name;
1334
    int offset;
1335
    target_long (*get_value)(struct MonitorDef *md, int val);
1336
    int type;
1337
} MonitorDef;
1338

    
1339
#if defined(TARGET_I386)
1340
static target_long monitor_get_pc (struct MonitorDef *md, int val)
1341
{
1342
    CPUState *env = mon_get_cpu();
1343
    if (!env)
1344
        return 0;
1345
    return env->eip + env->segs[R_CS].base;
1346
}
1347
#endif
1348

    
1349
#if defined(TARGET_PPC)
1350
static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1351
{
1352
    CPUState *env = mon_get_cpu();
1353
    unsigned int u;
1354
    int i;
1355

    
1356
    if (!env)
1357
        return 0;
1358

    
1359
    u = 0;
1360
    for (i = 0; i < 8; i++)
1361
        u |= env->crf[i] << (32 - (4 * i));
1362

    
1363
    return u;
1364
}
1365

    
1366
static target_long monitor_get_msr (struct MonitorDef *md, int val)
1367
{
1368
    CPUState *env = mon_get_cpu();
1369
    if (!env)
1370
        return 0;
1371
    return (env->msr[MSR_POW] << MSR_POW) |
1372
        (env->msr[MSR_ILE] << MSR_ILE) |
1373
        (env->msr[MSR_EE] << MSR_EE) |
1374
        (env->msr[MSR_PR] << MSR_PR) |
1375
        (env->msr[MSR_FP] << MSR_FP) |
1376
        (env->msr[MSR_ME] << MSR_ME) |
1377
        (env->msr[MSR_FE0] << MSR_FE0) |
1378
        (env->msr[MSR_SE] << MSR_SE) |
1379
        (env->msr[MSR_BE] << MSR_BE) |
1380
        (env->msr[MSR_FE1] << MSR_FE1) |
1381
        (env->msr[MSR_IP] << MSR_IP) |
1382
        (env->msr[MSR_IR] << MSR_IR) |
1383
        (env->msr[MSR_DR] << MSR_DR) |
1384
        (env->msr[MSR_RI] << MSR_RI) |
1385
        (env->msr[MSR_LE] << MSR_LE);
1386
}
1387

    
1388
static target_long monitor_get_xer (struct MonitorDef *md, int val)
1389
{
1390
    CPUState *env = mon_get_cpu();
1391
    if (!env)
1392
        return 0;
1393
    return (env->xer[XER_SO] << XER_SO) |
1394
        (env->xer[XER_OV] << XER_OV) |
1395
        (env->xer[XER_CA] << XER_CA) |
1396
        (env->xer[XER_BC] << XER_BC);
1397
}
1398

    
1399
static target_long monitor_get_decr (struct MonitorDef *md, int val)
1400
{
1401
    CPUState *env = mon_get_cpu();
1402
    if (!env)
1403
        return 0;
1404
    return cpu_ppc_load_decr(env);
1405
}
1406

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

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

    
1424
#if defined(TARGET_SPARC)
1425
#ifndef TARGET_SPARC64
1426
static target_long monitor_get_psr (struct MonitorDef *md, int val)
1427
{
1428
    CPUState *env = mon_get_cpu();
1429
    if (!env)
1430
        return 0;
1431
    return GET_PSR(env);
1432
}
1433
#endif
1434

    
1435
static target_long monitor_get_reg(struct MonitorDef *md, int val)
1436
{
1437
    CPUState *env = mon_get_cpu();
1438
    if (!env)
1439
        return 0;
1440
    return env->regwptr[val];
1441
}
1442
#endif
1443

    
1444
static MonitorDef monitor_defs[] = {
1445
#ifdef TARGET_I386
1446

    
1447
#define SEG(name, seg) \
1448
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1449
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1450
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1451

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

    
1643
static void expr_error(const char *fmt) 
1644
{
1645
    term_printf(fmt);
1646
    term_printf("\n");
1647
    longjmp(expr_env, 1);
1648
}
1649

    
1650
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
1651
static int get_monitor_def(target_long *pval, const char *name)
1652
{
1653
    MonitorDef *md;
1654
    void *ptr;
1655

    
1656
    for(md = monitor_defs; md->name != NULL; md++) {
1657
        if (compare_cmd(name, md->name)) {
1658
            if (md->get_value) {
1659
                *pval = md->get_value(md, md->offset);
1660
            } else {
1661
                CPUState *env = mon_get_cpu();
1662
                if (!env)
1663
                    return -2;
1664
                ptr = (uint8_t *)env + md->offset;
1665
                switch(md->type) {
1666
                case MD_I32:
1667
                    *pval = *(int32_t *)ptr;
1668
                    break;
1669
                case MD_TLONG:
1670
                    *pval = *(target_long *)ptr;
1671
                    break;
1672
                default:
1673
                    *pval = 0;
1674
                    break;
1675
                }
1676
            }
1677
            return 0;
1678
        }
1679
    }
1680
    return -1;
1681
}
1682

    
1683
static void next(void)
1684
{
1685
    if (pch != '\0') {
1686
        pch++;
1687
        while (isspace(*pch))
1688
            pch++;
1689
    }
1690
}
1691

    
1692
static target_long expr_sum(void);
1693

    
1694
static target_long expr_unary(void)
1695
{
1696
    target_long n;
1697
    char *p;
1698
    int ret;
1699

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

    
1776

    
1777
static target_long expr_prod(void)
1778
{
1779
    target_long val, val2;
1780
    int op;
1781
    
1782
    val = expr_unary();
1783
    for(;;) {
1784
        op = *pch;
1785
        if (op != '*' && op != '/' && op != '%')
1786
            break;
1787
        next();
1788
        val2 = expr_unary();
1789
        switch(op) {
1790
        default:
1791
        case '*':
1792
            val *= val2;
1793
            break;
1794
        case '/':
1795
        case '%':
1796
            if (val2 == 0) 
1797
                expr_error("division by zero");
1798
            if (op == '/')
1799
                val /= val2;
1800
            else
1801
                val %= val2;
1802
            break;
1803
        }
1804
    }
1805
    return val;
1806
}
1807

    
1808
static target_long expr_logic(void)
1809
{
1810
    target_long val, val2;
1811
    int op;
1812

    
1813
    val = expr_prod();
1814
    for(;;) {
1815
        op = *pch;
1816
        if (op != '&' && op != '|' && op != '^')
1817
            break;
1818
        next();
1819
        val2 = expr_prod();
1820
        switch(op) {
1821
        default:
1822
        case '&':
1823
            val &= val2;
1824
            break;
1825
        case '|':
1826
            val |= val2;
1827
            break;
1828
        case '^':
1829
            val ^= val2;
1830
            break;
1831
        }
1832
    }
1833
    return val;
1834
}
1835

    
1836
static target_long expr_sum(void)
1837
{
1838
    target_long val, val2;
1839
    int op;
1840

    
1841
    val = expr_logic();
1842
    for(;;) {
1843
        op = *pch;
1844
        if (op != '+' && op != '-')
1845
            break;
1846
        next();
1847
        val2 = expr_logic();
1848
        if (op == '+')
1849
            val += val2;
1850
        else
1851
            val -= val2;
1852
    }
1853
    return val;
1854
}
1855

    
1856
static int get_expr(target_long *pval, const char **pp)
1857
{
1858
    pch = *pp;
1859
    if (setjmp(expr_env)) {
1860
        *pp = pch;
1861
        return -1;
1862
    }
1863
    while (isspace(*pch))
1864
        pch++;
1865
    *pval = expr_sum();
1866
    *pp = pch;
1867
    return 0;
1868
}
1869

    
1870
static int get_str(char *buf, int buf_size, const char **pp)
1871
{
1872
    const char *p;
1873
    char *q;
1874
    int c;
1875

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

    
1935
static int default_fmt_format = 'x';
1936
static int default_fmt_size = 4;
1937

    
1938
#define MAX_ARGS 16
1939

    
1940
static void monitor_handle_command(const char *cmdline)
1941
{
1942
    const char *p, *pstart, *typestr;
1943
    char *q;
1944
    int c, nb_args, len, i, has_arg;
1945
    term_cmd_t *cmd;
1946
    char cmdname[256];
1947
    char buf[1024];
1948
    void *str_allocated[MAX_ARGS];
1949
    void *args[MAX_ARGS];
1950

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

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

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

    
2244
static void cmd_completion(const char *name, const char *list)
2245
{
2246
    const char *p, *pstart;
2247
    char cmd[128];
2248
    int len;
2249

    
2250
    p = list;
2251
    for(;;) {
2252
        pstart = p;
2253
        p = strchr(p, '|');
2254
        if (!p)
2255
            p = pstart + strlen(pstart);
2256
        len = p - pstart;
2257
        if (len > sizeof(cmd) - 2)
2258
            len = sizeof(cmd) - 2;
2259
        memcpy(cmd, pstart, len);
2260
        cmd[len] = '\0';
2261
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2262
            add_completion(cmd);
2263
        }
2264
        if (*p == '\0')
2265
            break;
2266
        p++;
2267
    }
2268
}
2269

    
2270
static void file_completion(const char *input)
2271
{
2272
    DIR *ffs;
2273
    struct dirent *d;
2274
    char path[1024];
2275
    char file[1024], file_prefix[1024];
2276
    int input_path_len;
2277
    const char *p;
2278

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

    
2318
static void block_completion_it(void *opaque, const char *name)
2319
{
2320
    const char *input = opaque;
2321

    
2322
    if (input[0] == '\0' ||
2323
        !strncmp(name, (char *)input, strlen(input))) {
2324
        add_completion(name);
2325
    }
2326
}
2327

    
2328
/* NOTE: this parser is an approximate form of the real command parser */
2329
static void parse_cmdline(const char *cmdline,
2330
                         int *pnb_args, char **args)
2331
{
2332
    const char *p;
2333
    int nb_args, ret;
2334
    char buf[1024];
2335

    
2336
    p = cmdline;
2337
    nb_args = 0;
2338
    for(;;) {
2339
        while (isspace(*p))
2340
            p++;
2341
        if (*p == '\0')
2342
            break;
2343
        if (nb_args >= MAX_ARGS)
2344
            break;
2345
        ret = get_str(buf, sizeof(buf), &p);
2346
        args[nb_args] = qemu_strdup(buf);
2347
        nb_args++;
2348
        if (ret < 0)
2349
            break;
2350
    }
2351
    *pnb_args = nb_args;
2352
}
2353

    
2354
void readline_find_completion(const char *cmdline)
2355
{
2356
    const char *cmdname;
2357
    char *args[MAX_ARGS];
2358
    int nb_args, i, len;
2359
    const char *ptype, *str;
2360
    term_cmd_t *cmd;
2361
    const KeyDef *key;
2362

    
2363
    parse_cmdline(cmdline, &nb_args, args);
2364
#ifdef DEBUG_COMPLETION
2365
    for(i = 0; i < nb_args; i++) {
2366
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2367
    }
2368
#endif
2369

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

    
2438
static int term_can_read(void *opaque)
2439
{
2440
    return 128;
2441
}
2442

    
2443
static void term_read(void *opaque, const uint8_t *buf, int size)
2444
{
2445
    int i;
2446
    for(i = 0; i < size; i++)
2447
        readline_handle_byte(buf[i]);
2448
}
2449

    
2450
static void monitor_start_input(void);
2451

    
2452
static void monitor_handle_command1(void *opaque, const char *cmdline)
2453
{
2454
    monitor_handle_command(cmdline);
2455
    monitor_start_input();
2456
}
2457

    
2458
static void monitor_start_input(void)
2459
{
2460
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2461
}
2462

    
2463
static void term_event(void *opaque, int event)
2464
{
2465
    if (event != CHR_EVENT_RESET)
2466
        return;
2467

    
2468
    if (!hide_banner)
2469
            term_printf("QEMU %s monitor - type 'help' for more information\n",
2470
                        QEMU_VERSION);
2471
    monitor_start_input();
2472
}
2473

    
2474
static int is_first_init = 1;
2475

    
2476
void monitor_init(CharDriverState *hd, int show_banner)
2477
{
2478
    int i;
2479

    
2480
    if (is_first_init) {
2481
        for (i = 0; i < MAX_MON; i++) {
2482
            monitor_hd[i] = NULL;
2483
        }
2484
        is_first_init = 0;
2485
    }
2486
    for (i = 0; i < MAX_MON; i++) {
2487
        if (monitor_hd[i] == NULL) {
2488
            monitor_hd[i] = hd;
2489
            break;
2490
        }
2491
    }
2492

    
2493
    hide_banner = !show_banner;
2494

    
2495
    qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2496
}
2497

    
2498
/* XXX: use threads ? */
2499
/* modal monitor readline */
2500
static int monitor_readline_started;
2501
static char *monitor_readline_buf;
2502
static int monitor_readline_buf_size;
2503

    
2504
static void monitor_readline_cb(void *opaque, const char *input)
2505
{
2506
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2507
    monitor_readline_started = 0;
2508
}
2509

    
2510
void monitor_readline(const char *prompt, int is_password,
2511
                      char *buf, int buf_size)
2512
{
2513
    int i;
2514

    
2515
    if (is_password) {
2516
        for (i = 0; i < MAX_MON; i++)
2517
            if (monitor_hd[i] && monitor_hd[i]->focus == 0)
2518
                qemu_chr_send_event(monitor_hd[i], CHR_EVENT_FOCUS);
2519
    }
2520
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
2521
    monitor_readline_buf = buf;
2522
    monitor_readline_buf_size = buf_size;
2523
    monitor_readline_started = 1;
2524
    while (monitor_readline_started) {
2525
        main_loop_wait(10);
2526
    }
2527
}