Statistics
| Branch: | Revision:

root / monitor.c @ 76a66253

History | View | Annotate | Download (65.3 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_block(void)
239
{
240
    bdrv_info();
241
}
242

    
243
/* get the current CPU defined by the user */
244
int mon_set_cpu(int cpu_index)
245
{
246
    CPUState *env;
247

    
248
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
249
        if (env->cpu_index == cpu_index) {
250
            mon_cpu = env;
251
            return 0;
252
        }
253
    }
254
    return -1;
255
}
256

    
257
CPUState *mon_get_cpu(void)
258
{
259
    if (!mon_cpu) {
260
        mon_set_cpu(0);
261
    }
262
    return mon_cpu;
263
}
264

    
265
static void do_info_registers(void)
266
{
267
    CPUState *env;
268
    env = mon_get_cpu();
269
    if (!env)
270
        return;
271
#ifdef TARGET_I386
272
    cpu_dump_state(env, NULL, monitor_fprintf,
273
                   X86_DUMP_FPU);
274
#else
275
    cpu_dump_state(env, NULL, monitor_fprintf, 
276
                   0);
277
#endif
278
}
279

    
280
static void do_info_cpus(void)
281
{
282
    CPUState *env;
283

    
284
    /* just to set the default cpu if not already done */
285
    mon_get_cpu();
286

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

    
308
static void do_cpu_set(int index)
309
{
310
    if (mon_set_cpu(index) < 0)
311
        term_printf("Invalid CPU index\n");
312
}
313

    
314
static void do_info_jit(void)
315
{
316
    dump_exec_info(NULL, monitor_fprintf);
317
}
318

    
319
static void do_info_history (void)
320
{
321
    int i;
322
    const char *str;
323
    
324
    i = 0;
325
    for(;;) {
326
        str = readline_get_history(i);
327
        if (!str)
328
            break;
329
        term_printf("%d: '%s'\n", i, str);
330
        i++;
331
    }
332
}
333

    
334
#if defined(TARGET_PPC)
335
/* XXX: not implemented in other targets */
336
static void do_info_cpu_stats (void)
337
{
338
    CPUState *env;
339

    
340
    env = mon_get_cpu();
341
    cpu_dump_statistics(env, NULL, &monitor_fprintf, 0);
342
}
343
#endif
344

    
345
static void do_quit(void)
346
{
347
    exit(0);
348
}
349

    
350
static int eject_device(BlockDriverState *bs, int force)
351
{
352
    if (bdrv_is_inserted(bs)) {
353
        if (!force) {
354
            if (!bdrv_is_removable(bs)) {
355
                term_printf("device is not removable\n");
356
                return -1;
357
            }
358
            if (bdrv_is_locked(bs)) {
359
                term_printf("device is locked\n");
360
                return -1;
361
            }
362
        }
363
        bdrv_close(bs);
364
    }
365
    return 0;
366
}
367

    
368
static void do_eject(int force, const char *filename)
369
{
370
    BlockDriverState *bs;
371

    
372
    bs = bdrv_find(filename);
373
    if (!bs) {
374
        term_printf("device not found\n");
375
        return;
376
    }
377
    eject_device(bs, force);
378
}
379

    
380
static void do_change(const char *device, const char *filename)
381
{
382
    BlockDriverState *bs;
383
    int i;
384
    char password[256];
385

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

    
405
static void do_screen_dump(const char *filename)
406
{
407
    vga_hw_screen_dump(filename);
408
}
409

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

    
426
static void do_stop(void)
427
{
428
    vm_stop(EXCP_INTERRUPT);
429
}
430

    
431
static void do_cont(void)
432
{
433
    vm_start();
434
}
435

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

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

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

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

    
514
    len = wsize * count;
515
    if (wsize == 1)
516
        line_size = 8;
517
    else
518
        line_size = 16;
519
    nb_per_line = line_size / wsize;
520
    max_digits = 0;
521

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

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

    
595
#if TARGET_LONG_BITS == 64
596
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
597
#else
598
#define GET_TLONG(h, l) (l)
599
#endif
600

    
601
static void do_memory_dump(int count, int format, int size, 
602
                           uint32_t addrh, uint32_t addrl)
603
{
604
    target_long addr = GET_TLONG(addrh, addrl);
605
    memory_dump(count, format, size, addr, 0);
606
}
607

    
608
static void do_physical_memory_dump(int count, int format, int size,
609
                                    uint32_t addrh, uint32_t addrl)
610

    
611
{
612
    target_long addr = GET_TLONG(addrh, addrl);
613
    memory_dump(count, format, size, addr, 1);
614
}
615

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

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

    
670
    env = mon_get_cpu();
671
    if (!env)
672
        return;
673

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

    
691
static void do_sum(uint32_t start, uint32_t size)
692
{
693
    uint32_t addr;
694
    uint8_t buf[1];
695
    uint16_t sum;
696

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

    
707
typedef struct {
708
    int keycode;
709
    const char *name;
710
} KeyDef;
711

    
712
static const KeyDef key_defs[] = {
713
    { 0x2a, "shift" },
714
    { 0x36, "shift_r" },
715
    
716
    { 0x38, "alt" },
717
    { 0xb8, "alt_r" },
718
    { 0x1d, "ctrl" },
719
    { 0x9d, "ctrl_r" },
720

    
721
    { 0xdd, "menu" },
722

    
723
    { 0x01, "esc" },
724

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

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

    
751
    { 0x1c, "ret" },
752

    
753
    { 0x1e, "a" },
754
    { 0x1f, "s" },
755
    { 0x20, "d" },
756
    { 0x21, "f" },
757
    { 0x22, "g" },
758
    { 0x23, "h" },
759
    { 0x24, "j" },
760
    { 0x25, "k" },
761
    { 0x26, "l" },
762

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

    
786
    { 0xb5, "kp_divide" },
787
    { 0x37, "kp_multiply" },
788
    { 0x4a, "kp_substract" },
789
    { 0x4e, "kp_add" },
790
    { 0x9c, "kp_enter" },
791
    { 0x53, "kp_decimal" },
792

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

    
806
    { 0x57, "f11" },
807
    { 0x58, "f12" },
808

    
809
    { 0xb7, "print" },
810

    
811
    { 0xc7, "home" },
812
    { 0xc9, "pgup" },
813
    { 0xd1, "pgdn" },
814
    { 0xcf, "end" },
815

    
816
    { 0xcb, "left" },
817
    { 0xc8, "up" },
818
    { 0xd0, "down" },
819
    { 0xcd, "right" },
820

    
821
    { 0xd2, "insert" },
822
    { 0xd3, "delete" },
823
    { 0, NULL },
824
};
825

    
826
static int get_keycode(const char *key)
827
{
828
    const KeyDef *p;
829
    char *endp;
830
    int ret;
831

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

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

    
888
static int mouse_button_state;
889

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

    
902
static void do_mouse_button(int button_state)
903
{
904
    mouse_button_state = button_state;
905
    kbd_mouse_event(0, 0, 0, mouse_button_state);
906
}
907

    
908
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
909
{
910
    uint32_t val;
911
    int suffix;
912

    
913
    if (has_index) {
914
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
915
        addr++;
916
    }
917
    addr &= 0xffff;
918

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

    
938
static void do_system_reset(void)
939
{
940
    qemu_system_reset_request();
941
}
942

    
943
static void do_system_powerdown(void)
944
{
945
    qemu_system_powerdown_request();
946
}
947

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

    
964
static void tlb_info(void)
965
{
966
    CPUState *env;
967
    int l1, l2;
968
    uint32_t pgd, pde, pte;
969

    
970
    env = mon_get_cpu();
971
    if (!env)
972
        return;
973

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

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

    
1022
static void mem_info(void)
1023
{
1024
    CPUState *env;
1025
    int l1, l2, prot, last_prot;
1026
    uint32_t pgd, pde, pte, start, end;
1027

    
1028
    env = mon_get_cpu();
1029
    if (!env)
1030
        return;
1031

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

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

    
1099
#ifdef CONFIG_PROFILER
1100

    
1101
int64_t kqemu_time;
1102
int64_t qemu_time;
1103
int64_t kqemu_exec_count;
1104
int64_t dev_time;
1105
int64_t kqemu_ret_int_count;
1106
int64_t kqemu_ret_excp_count;
1107
int64_t kqemu_ret_intr_count;
1108

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

    
1144
/* Capture support */
1145
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1146

    
1147
static void do_info_capture (void)
1148
{
1149
    int i;
1150
    CaptureState *s;
1151

    
1152
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1153
        term_printf ("[%d]: ", i);
1154
        s->ops.info (s->opaque);
1155
    }
1156
}
1157

    
1158
static void do_stop_capture (int n)
1159
{
1160
    int i;
1161
    CaptureState *s;
1162

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

    
1173
#ifdef HAS_AUDIO
1174
int wav_start_capture (CaptureState *s, const char *path, int freq,
1175
                       int bits, int nchannels);
1176

    
1177
static void do_wav_capture (const char *path,
1178
                            int has_freq, int freq,
1179
                            int has_bits, int bits,
1180
                            int has_channels, int nchannels)
1181
{
1182
    CaptureState *s;
1183

    
1184
    s = qemu_mallocz (sizeof (*s));
1185
    if (!s) {
1186
        term_printf ("Not enough memory to add wave capture\n");
1187
        return;
1188
    }
1189

    
1190
    freq = has_freq ? freq : 44100;
1191
    bits = has_bits ? bits : 16;
1192
    nchannels = has_channels ? nchannels : 2;
1193

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

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

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

    
1274
static term_cmd_t info_cmds[] = {
1275
    { "version", "", do_info_version,
1276
      "", "show the version of qemu" },
1277
    { "network", "", do_info_network,
1278
      "", "show the network state" },
1279
    { "block", "", do_info_block,
1280
      "", "show the block devices" },
1281
    { "registers", "", do_info_registers,
1282
      "", "show the cpu registers" },
1283
    { "cpus", "", do_info_cpus,
1284
      "", "show infos for each CPU" },
1285
    { "history", "", do_info_history,
1286
      "", "show the command line history", },
1287
    { "irq", "", irq_info,
1288
      "", "show the interrupts statistics (if available)", },
1289
    { "pic", "", pic_info,
1290
      "", "show i8259 (PIC) state", },
1291
    { "pci", "", pci_info,
1292
      "", "show PCI info", },
1293
#if defined(TARGET_I386)
1294
    { "tlb", "", tlb_info,
1295
      "", "show virtual to physical memory mappings", },
1296
    { "mem", "", mem_info,
1297
      "", "show the active virtual memory mappings", },
1298
#endif
1299
    { "jit", "", do_info_jit,
1300
      "", "show dynamic compiler info", },
1301
    { "kqemu", "", do_info_kqemu,
1302
      "", "show kqemu information", },
1303
    { "usb", "", usb_info,
1304
      "", "show guest USB devices", },
1305
    { "usbhost", "", usb_host_info,
1306
      "", "show host USB devices", },
1307
    { "profile", "", do_info_profile,
1308
      "", "show profiling information", },
1309
    { "capture", "", do_info_capture,
1310
      "", "show capture information" },
1311
    { "snapshots", "", do_info_snapshots,
1312
      "", "show the currently saved VM snapshots" },
1313
    { "mice", "", do_info_mice,
1314
      "", "show which guest mouse is receiving events" },
1315
    { "vnc", "", do_info_vnc,
1316
      "", "show the vnc server status"},
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*)count;
2112
                args[nb_args++] = (void*)format;
2113
                args[nb_args++] = (void*)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 *)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 *)(int)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 *)(int)((val >> 32) & 0xffffffff);
2161
#else
2162
                    args[nb_args++] = (void *)0;
2163
#endif
2164
                    args[nb_args++] = (void *)(int)(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 *)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
}