Statistics
| Branch: | Revision:

root / monitor.c @ e5b0bc44

History | View | Annotate | Download (64.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
static CharDriverState *monitor_hd;
58
static int hide_banner;
59

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

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

    
66
static void monitor_start_input(void);
67

    
68
CPUState *mon_cpu = NULL;
69

    
70
void term_flush(void)
71
{
72
    if (term_outbuf_index > 0) {
73
        qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
74
        term_outbuf_index = 0;
75
    }
76
}
77

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

    
95
void term_vprintf(const char *fmt, va_list ap)
96
{
97
    char buf[4096];
98
    vsnprintf(buf, sizeof(buf), fmt, ap);
99
    term_puts(buf);
100
}
101

    
102
void term_printf(const char *fmt, ...)
103
{
104
    va_list ap;
105
    va_start(ap, fmt);
106
    term_vprintf(fmt, ap);
107
    va_end(ap);
108
}
109

    
110
void term_print_filename(const char *filename)
111
{
112
    int i;
113

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

    
137
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
138
{
139
    va_list ap;
140
    va_start(ap, fmt);
141
    term_vprintf(fmt, ap);
142
    va_end(ap);
143
    return 0;
144
}
145

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

    
166
static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
167
{
168
    term_cmd_t *cmd;
169

    
170
    for(cmd = cmds; cmd->name != NULL; cmd++) {
171
        if (!name || !strcmp(name, cmd->name))
172
            term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
173
    }
174
}
175

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

    
193
static void do_help(const char *name)
194
{
195
    help_cmd(name);
196
}
197

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

    
212
static void do_info(const char *item)
213
{
214
    term_cmd_t *cmd;
215

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

    
229
static void do_info_version(void)
230
{
231
  term_printf("%s\n", QEMU_VERSION);
232
}
233

    
234
static void do_info_block(void)
235
{
236
    bdrv_info();
237
}
238

    
239
/* get the current CPU defined by the user */
240
int mon_set_cpu(int cpu_index)
241
{
242
    CPUState *env;
243

    
244
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
245
        if (env->cpu_index == cpu_index) {
246
            mon_cpu = env;
247
            return 0;
248
        }
249
    }
250
    return -1;
251
}
252

    
253
CPUState *mon_get_cpu(void)
254
{
255
    if (!mon_cpu) {
256
        mon_set_cpu(0);
257
    }
258
    return mon_cpu;
259
}
260

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

    
276
static void do_info_cpus(void)
277
{
278
    CPUState *env;
279

    
280
    /* just to set the default cpu if not already done */
281
    mon_get_cpu();
282

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

    
304
static void do_cpu_set(int index)
305
{
306
    if (mon_set_cpu(index) < 0)
307
        term_printf("Invalid CPU index\n");
308
}
309

    
310
static void do_info_jit(void)
311
{
312
    dump_exec_info(NULL, monitor_fprintf);
313
}
314

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

    
330
static void do_quit(void)
331
{
332
    exit(0);
333
}
334

    
335
static int eject_device(BlockDriverState *bs, int force)
336
{
337
    if (bdrv_is_inserted(bs)) {
338
        if (!force) {
339
            if (!bdrv_is_removable(bs)) {
340
                term_printf("device is not removable\n");
341
                return -1;
342
            }
343
            if (bdrv_is_locked(bs)) {
344
                term_printf("device is locked\n");
345
                return -1;
346
            }
347
        }
348
        bdrv_close(bs);
349
    }
350
    return 0;
351
}
352

    
353
static void do_eject(int force, const char *filename)
354
{
355
    BlockDriverState *bs;
356

    
357
    bs = bdrv_find(filename);
358
    if (!bs) {
359
        term_printf("device not found\n");
360
        return;
361
    }
362
    eject_device(bs, force);
363
}
364

    
365
static void do_change(const char *device, const char *filename)
366
{
367
    BlockDriverState *bs;
368
    int i;
369
    char password[256];
370

    
371
    bs = bdrv_find(device);
372
    if (!bs) {
373
        term_printf("device not found\n");
374
        return;
375
    }
376
    if (eject_device(bs, 0) < 0)
377
        return;
378
    bdrv_open(bs, filename, 0);
379
    if (bdrv_is_encrypted(bs)) {
380
        term_printf("%s is encrypted.\n", device);
381
        for(i = 0; i < 3; i++) {
382
            monitor_readline("Password: ", 1, password, sizeof(password));
383
            if (bdrv_set_key(bs, password) == 0)
384
                break;
385
            term_printf("invalid password\n");
386
        }
387
    }
388
}
389

    
390
static void do_screen_dump(const char *filename)
391
{
392
    vga_hw_screen_dump(filename);
393
}
394

    
395
static void do_log(const char *items)
396
{
397
    int mask;
398
    
399
    if (!strcmp(items, "none")) {
400
        mask = 0;
401
    } else {
402
        mask = cpu_str_to_log_mask(items);
403
        if (!mask) {
404
            help_cmd("log");
405
            return;
406
        }
407
    }
408
    cpu_set_log(mask);
409
}
410

    
411
static void do_stop(void)
412
{
413
    vm_stop(EXCP_INTERRUPT);
414
}
415

    
416
static void do_cont(void)
417
{
418
    vm_start();
419
}
420

    
421
#ifdef CONFIG_GDBSTUB
422
static void do_gdbserver(int has_port, int port)
423
{
424
    if (!has_port)
425
        port = DEFAULT_GDBSTUB_PORT;
426
    if (gdbserver_start(port) < 0) {
427
        qemu_printf("Could not open gdbserver socket on port %d\n", port);
428
    } else {
429
        qemu_printf("Waiting gdb connection on port %d\n", port);
430
    }
431
}
432
#endif
433

    
434
static void term_printc(int c)
435
{
436
    term_printf("'");
437
    switch(c) {
438
    case '\'':
439
        term_printf("\\'");
440
        break;
441
    case '\\':
442
        term_printf("\\\\");
443
        break;
444
    case '\n':
445
        term_printf("\\n");
446
        break;
447
    case '\r':
448
        term_printf("\\r");
449
        break;
450
    default:
451
        if (c >= 32 && c <= 126) {
452
            term_printf("%c", c);
453
        } else {
454
            term_printf("\\x%02x", c);
455
        }
456
        break;
457
    }
458
    term_printf("'");
459
}
460

    
461
static void memory_dump(int count, int format, int wsize, 
462
                        target_ulong addr, int is_physical)
463
{
464
    CPUState *env;
465
    int nb_per_line, l, line_size, i, max_digits, len;
466
    uint8_t buf[16];
467
    uint64_t v;
468

    
469
    if (format == 'i') {
470
        int flags;
471
        flags = 0;
472
        env = mon_get_cpu();
473
        if (!env && !is_physical)
474
            return;
475
#ifdef TARGET_I386
476
        if (wsize == 2) {
477
            flags = 1;
478
        } else if (wsize == 4) {
479
            flags = 0;
480
        } else {
481
            /* as default we use the current CS size */
482
            flags = 0;
483
            if (env) {
484
#ifdef TARGET_X86_64
485
                if ((env->efer & MSR_EFER_LMA) && 
486
                    (env->segs[R_CS].flags & DESC_L_MASK))
487
                    flags = 2;
488
                else
489
#endif
490
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
491
                    flags = 1;
492
            }
493
        }
494
#endif
495
        monitor_disas(env, addr, count, is_physical, flags);
496
        return;
497
    }
498

    
499
    len = wsize * count;
500
    if (wsize == 1)
501
        line_size = 8;
502
    else
503
        line_size = 16;
504
    nb_per_line = line_size / wsize;
505
    max_digits = 0;
506

    
507
    switch(format) {
508
    case 'o':
509
        max_digits = (wsize * 8 + 2) / 3;
510
        break;
511
    default:
512
    case 'x':
513
        max_digits = (wsize * 8) / 4;
514
        break;
515
    case 'u':
516
    case 'd':
517
        max_digits = (wsize * 8 * 10 + 32) / 33;
518
        break;
519
    case 'c':
520
        wsize = 1;
521
        break;
522
    }
523

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

    
580
#if TARGET_LONG_BITS == 64
581
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
582
#else
583
#define GET_TLONG(h, l) (l)
584
#endif
585

    
586
static void do_memory_dump(int count, int format, int size, 
587
                           uint32_t addrh, uint32_t addrl)
588
{
589
    target_long addr = GET_TLONG(addrh, addrl);
590
    memory_dump(count, format, size, addr, 0);
591
}
592

    
593
static void do_physical_memory_dump(int count, int format, int size,
594
                                    uint32_t addrh, uint32_t addrl)
595

    
596
{
597
    target_long addr = GET_TLONG(addrh, addrl);
598
    memory_dump(count, format, size, addr, 1);
599
}
600

    
601
static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
602
{
603
    target_long val = GET_TLONG(valh, vall);
604
#if TARGET_LONG_BITS == 32
605
    switch(format) {
606
    case 'o':
607
        term_printf("%#o", val);
608
        break;
609
    case 'x':
610
        term_printf("%#x", val);
611
        break;
612
    case 'u':
613
        term_printf("%u", val);
614
        break;
615
    default:
616
    case 'd':
617
        term_printf("%d", val);
618
        break;
619
    case 'c':
620
        term_printc(val);
621
        break;
622
    }
623
#else
624
    switch(format) {
625
    case 'o':
626
        term_printf("%#" PRIo64, val);
627
        break;
628
    case 'x':
629
        term_printf("%#" PRIx64, val);
630
        break;
631
    case 'u':
632
        term_printf("%" PRIu64, val);
633
        break;
634
    default:
635
    case 'd':
636
        term_printf("%" PRId64, val);
637
        break;
638
    case 'c':
639
        term_printc(val);
640
        break;
641
    }
642
#endif
643
    term_printf("\n");
644
}
645

    
646
static void do_memory_save(unsigned int valh, unsigned int vall, 
647
                           uint32_t size, const char *filename)
648
{
649
    FILE *f;
650
    target_long addr = GET_TLONG(valh, vall);
651
    uint32_t l;
652
    CPUState *env;
653
    uint8_t buf[1024];
654

    
655
    env = mon_get_cpu();
656
    if (!env)
657
        return;
658

    
659
    f = fopen(filename, "wb");
660
    if (!f) {
661
        term_printf("could not open '%s'\n", filename);
662
        return;
663
    }
664
    while (size != 0) {
665
        l = sizeof(buf);
666
        if (l > size)
667
            l = size;
668
        cpu_memory_rw_debug(env, addr, buf, l, 0);
669
        fwrite(buf, 1, l, f);
670
        addr += l;
671
        size -= l;
672
    }
673
    fclose(f);
674
}
675

    
676
static void do_sum(uint32_t start, uint32_t size)
677
{
678
    uint32_t addr;
679
    uint8_t buf[1];
680
    uint16_t sum;
681

    
682
    sum = 0;
683
    for(addr = start; addr < (start + size); addr++) {
684
        cpu_physical_memory_rw(addr, buf, 1, 0);
685
        /* BSD sum algorithm ('sum' Unix command) */
686
        sum = (sum >> 1) | (sum << 15);
687
        sum += buf[0];
688
    }
689
    term_printf("%05d\n", sum);
690
}
691

    
692
typedef struct {
693
    int keycode;
694
    const char *name;
695
} KeyDef;
696

    
697
static const KeyDef key_defs[] = {
698
    { 0x2a, "shift" },
699
    { 0x36, "shift_r" },
700
    
701
    { 0x38, "alt" },
702
    { 0xb8, "alt_r" },
703
    { 0x1d, "ctrl" },
704
    { 0x9d, "ctrl_r" },
705

    
706
    { 0xdd, "menu" },
707

    
708
    { 0x01, "esc" },
709

    
710
    { 0x02, "1" },
711
    { 0x03, "2" },
712
    { 0x04, "3" },
713
    { 0x05, "4" },
714
    { 0x06, "5" },
715
    { 0x07, "6" },
716
    { 0x08, "7" },
717
    { 0x09, "8" },
718
    { 0x0a, "9" },
719
    { 0x0b, "0" },
720
    { 0x0c, "minus" },
721
    { 0x0d, "equal" },
722
    { 0x0e, "backspace" },
723

    
724
    { 0x0f, "tab" },
725
    { 0x10, "q" },
726
    { 0x11, "w" },
727
    { 0x12, "e" },
728
    { 0x13, "r" },
729
    { 0x14, "t" },
730
    { 0x15, "y" },
731
    { 0x16, "u" },
732
    { 0x17, "i" },
733
    { 0x18, "o" },
734
    { 0x19, "p" },
735

    
736
    { 0x1c, "ret" },
737

    
738
    { 0x1e, "a" },
739
    { 0x1f, "s" },
740
    { 0x20, "d" },
741
    { 0x21, "f" },
742
    { 0x22, "g" },
743
    { 0x23, "h" },
744
    { 0x24, "j" },
745
    { 0x25, "k" },
746
    { 0x26, "l" },
747

    
748
    { 0x2c, "z" },
749
    { 0x2d, "x" },
750
    { 0x2e, "c" },
751
    { 0x2f, "v" },
752
    { 0x30, "b" },
753
    { 0x31, "n" },
754
    { 0x32, "m" },
755
    
756
    { 0x39, "spc" },
757
    { 0x3a, "caps_lock" },
758
    { 0x3b, "f1" },
759
    { 0x3c, "f2" },
760
    { 0x3d, "f3" },
761
    { 0x3e, "f4" },
762
    { 0x3f, "f5" },
763
    { 0x40, "f6" },
764
    { 0x41, "f7" },
765
    { 0x42, "f8" },
766
    { 0x43, "f9" },
767
    { 0x44, "f10" },
768
    { 0x45, "num_lock" },
769
    { 0x46, "scroll_lock" },
770

    
771
    { 0xb5, "kp_divide" },
772
    { 0x37, "kp_multiply" },
773
    { 0x4a, "kp_substract" },
774
    { 0x4e, "kp_add" },
775
    { 0x9c, "kp_enter" },
776
    { 0x53, "kp_decimal" },
777

    
778
    { 0x52, "kp_0" },
779
    { 0x4f, "kp_1" },
780
    { 0x50, "kp_2" },
781
    { 0x51, "kp_3" },
782
    { 0x4b, "kp_4" },
783
    { 0x4c, "kp_5" },
784
    { 0x4d, "kp_6" },
785
    { 0x47, "kp_7" },
786
    { 0x48, "kp_8" },
787
    { 0x49, "kp_9" },
788
    
789
    { 0x56, "<" },
790

    
791
    { 0x57, "f11" },
792
    { 0x58, "f12" },
793

    
794
    { 0xb7, "print" },
795

    
796
    { 0xc7, "home" },
797
    { 0xc9, "pgup" },
798
    { 0xd1, "pgdn" },
799
    { 0xcf, "end" },
800

    
801
    { 0xcb, "left" },
802
    { 0xc8, "up" },
803
    { 0xd0, "down" },
804
    { 0xcd, "right" },
805

    
806
    { 0xd2, "insert" },
807
    { 0xd3, "delete" },
808
    { 0, NULL },
809
};
810

    
811
static int get_keycode(const char *key)
812
{
813
    const KeyDef *p;
814
    char *endp;
815
    int ret;
816

    
817
    for(p = key_defs; p->name != NULL; p++) {
818
        if (!strcmp(key, p->name))
819
            return p->keycode;
820
    }
821
    if (strstart(key, "0x", NULL)) {
822
        ret = strtoul(key, &endp, 0);
823
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
824
            return ret;
825
    }
826
    return -1;
827
}
828

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

    
873
static int mouse_button_state;
874

    
875
static void do_mouse_move(const char *dx_str, const char *dy_str, 
876
                          const char *dz_str)
877
{
878
    int dx, dy, dz;
879
    dx = strtol(dx_str, NULL, 0);
880
    dy = strtol(dy_str, NULL, 0);
881
    dz = 0;
882
    if (dz_str) 
883
        dz = strtol(dz_str, NULL, 0);
884
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
885
}
886

    
887
static void do_mouse_button(int button_state)
888
{
889
    mouse_button_state = button_state;
890
    kbd_mouse_event(0, 0, 0, mouse_button_state);
891
}
892

    
893
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
894
{
895
    uint32_t val;
896
    int suffix;
897

    
898
    if (has_index) {
899
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
900
        addr++;
901
    }
902
    addr &= 0xffff;
903

    
904
    switch(size) {
905
    default:
906
    case 1:
907
        val = cpu_inb(NULL, addr);
908
        suffix = 'b';
909
        break;
910
    case 2:
911
        val = cpu_inw(NULL, addr);
912
        suffix = 'w';
913
        break;
914
    case 4:
915
        val = cpu_inl(NULL, addr);
916
        suffix = 'l';
917
        break;
918
    }
919
    term_printf("port%c[0x%04x] = %#0*x\n",
920
                suffix, addr, size * 2, val);
921
}
922

    
923
static void do_system_reset(void)
924
{
925
    qemu_system_reset_request();
926
}
927

    
928
static void do_system_powerdown(void)
929
{
930
    qemu_system_powerdown_request();
931
}
932

    
933
#if defined(TARGET_I386)
934
static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
935
{
936
    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n", 
937
                addr,
938
                pte & mask,
939
                pte & PG_GLOBAL_MASK ? 'G' : '-',
940
                pte & PG_PSE_MASK ? 'P' : '-',
941
                pte & PG_DIRTY_MASK ? 'D' : '-',
942
                pte & PG_ACCESSED_MASK ? 'A' : '-',
943
                pte & PG_PCD_MASK ? 'C' : '-',
944
                pte & PG_PWT_MASK ? 'T' : '-',
945
                pte & PG_USER_MASK ? 'U' : '-',
946
                pte & PG_RW_MASK ? 'W' : '-');
947
}
948

    
949
static void tlb_info(void)
950
{
951
    CPUState *env;
952
    int l1, l2;
953
    uint32_t pgd, pde, pte;
954

    
955
    env = mon_get_cpu();
956
    if (!env)
957
        return;
958

    
959
    if (!(env->cr[0] & CR0_PG_MASK)) {
960
        term_printf("PG disabled\n");
961
        return;
962
    }
963
    pgd = env->cr[3] & ~0xfff;
964
    for(l1 = 0; l1 < 1024; l1++) {
965
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
966
        pde = le32_to_cpu(pde);
967
        if (pde & PG_PRESENT_MASK) {
968
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
969
                print_pte((l1 << 22), pde, ~((1 << 20) - 1));
970
            } else {
971
                for(l2 = 0; l2 < 1024; l2++) {
972
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
973
                                             (uint8_t *)&pte, 4);
974
                    pte = le32_to_cpu(pte);
975
                    if (pte & PG_PRESENT_MASK) {
976
                        print_pte((l1 << 22) + (l2 << 12), 
977
                                  pte & ~PG_PSE_MASK, 
978
                                  ~0xfff);
979
                    }
980
                }
981
            }
982
        }
983
    }
984
}
985

    
986
static void mem_print(uint32_t *pstart, int *plast_prot, 
987
                      uint32_t end, int prot)
988
{
989
    int prot1;
990
    prot1 = *plast_prot;
991
    if (prot != prot1) {
992
        if (*pstart != -1) {
993
            term_printf("%08x-%08x %08x %c%c%c\n",
994
                        *pstart, end, end - *pstart, 
995
                        prot1 & PG_USER_MASK ? 'u' : '-',
996
                        'r',
997
                        prot1 & PG_RW_MASK ? 'w' : '-');
998
        }
999
        if (prot != 0)
1000
            *pstart = end;
1001
        else
1002
            *pstart = -1;
1003
        *plast_prot = prot;
1004
    }
1005
}
1006

    
1007
static void mem_info(void)
1008
{
1009
    CPUState *env;
1010
    int l1, l2, prot, last_prot;
1011
    uint32_t pgd, pde, pte, start, end;
1012

    
1013
    env = mon_get_cpu();
1014
    if (!env)
1015
        return;
1016

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

    
1054
static void do_info_kqemu(void)
1055
{
1056
#ifdef USE_KQEMU
1057
    CPUState *env;
1058
    int val;
1059
    val = 0;
1060
    env = mon_get_cpu();
1061
    if (!env) {
1062
        term_printf("No cpu initialized yet");
1063
        return;
1064
    }
1065
    val = env->kqemu_enabled;
1066
    term_printf("kqemu support: ");
1067
    switch(val) {
1068
    default:
1069
    case 0:
1070
        term_printf("disabled\n");
1071
        break;
1072
    case 1:
1073
        term_printf("enabled for user code\n");
1074
        break;
1075
    case 2:
1076
        term_printf("enabled for user and kernel code\n");
1077
        break;
1078
    }
1079
#else
1080
    term_printf("kqemu support: not compiled\n");
1081
#endif
1082
} 
1083

    
1084
#ifdef CONFIG_PROFILER
1085

    
1086
int64_t kqemu_time;
1087
int64_t qemu_time;
1088
int64_t kqemu_exec_count;
1089
int64_t dev_time;
1090
int64_t kqemu_ret_int_count;
1091
int64_t kqemu_ret_excp_count;
1092
int64_t kqemu_ret_intr_count;
1093

    
1094
static void do_info_profile(void)
1095
{
1096
    int64_t total;
1097
    total = qemu_time;
1098
    if (total == 0)
1099
        total = 1;
1100
    term_printf("async time  %" PRId64 " (%0.3f)\n",
1101
                dev_time, dev_time / (double)ticks_per_sec);
1102
    term_printf("qemu time   %" PRId64 " (%0.3f)\n",
1103
                qemu_time, qemu_time / (double)ticks_per_sec);
1104
    term_printf("kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1105
                kqemu_time, kqemu_time / (double)ticks_per_sec,
1106
                kqemu_time / (double)total * 100.0,
1107
                kqemu_exec_count,
1108
                kqemu_ret_int_count,
1109
                kqemu_ret_excp_count,
1110
                kqemu_ret_intr_count);
1111
    qemu_time = 0;
1112
    kqemu_time = 0;
1113
    kqemu_exec_count = 0;
1114
    dev_time = 0;
1115
    kqemu_ret_int_count = 0;
1116
    kqemu_ret_excp_count = 0;
1117
    kqemu_ret_intr_count = 0;
1118
#ifdef USE_KQEMU
1119
    kqemu_record_dump();
1120
#endif
1121
}
1122
#else
1123
static void do_info_profile(void)
1124
{
1125
    term_printf("Internal profiler not compiled\n");
1126
}
1127
#endif
1128

    
1129
/* Capture support */
1130
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1131

    
1132
static void do_info_capture (void)
1133
{
1134
    int i;
1135
    CaptureState *s;
1136

    
1137
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1138
        term_printf ("[%d]: ", i);
1139
        s->ops.info (s->opaque);
1140
    }
1141
}
1142

    
1143
static void do_stop_capture (int n)
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
        if (i == n) {
1150
            s->ops.destroy (s->opaque);
1151
            LIST_REMOVE (s, entries);
1152
            qemu_free (s);
1153
            return;
1154
        }
1155
    }
1156
}
1157

    
1158
#ifdef HAS_AUDIO
1159
int wav_start_capture (CaptureState *s, const char *path, int freq,
1160
                       int bits, int nchannels);
1161

    
1162
static void do_wav_capture (const char *path,
1163
                            int has_freq, int freq,
1164
                            int has_bits, int bits,
1165
                            int has_channels, int nchannels)
1166
{
1167
    CaptureState *s;
1168

    
1169
    s = qemu_mallocz (sizeof (*s));
1170
    if (!s) {
1171
        term_printf ("Not enough memory to add wave capture\n");
1172
        return;
1173
    }
1174

    
1175
    freq = has_freq ? freq : 44100;
1176
    bits = has_bits ? bits : 16;
1177
    nchannels = has_channels ? nchannels : 2;
1178

    
1179
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1180
        term_printf ("Faied to add wave capture\n");
1181
        qemu_free (s);
1182
    }
1183
    LIST_INSERT_HEAD (&capture_head, s, entries);
1184
}
1185
#endif
1186

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

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

    
1259
static term_cmd_t info_cmds[] = {
1260
    { "version", "", do_info_version,
1261
      "", "show the version of qemu" },
1262
    { "network", "", do_info_network,
1263
      "", "show the network state" },
1264
    { "block", "", do_info_block,
1265
      "", "show the block devices" },
1266
    { "registers", "", do_info_registers,
1267
      "", "show the cpu registers" },
1268
    { "cpus", "", do_info_cpus,
1269
      "", "show infos for each CPU" },
1270
    { "history", "", do_info_history,
1271
      "", "show the command line history", },
1272
    { "irq", "", irq_info,
1273
      "", "show the interrupts statistics (if available)", },
1274
    { "pic", "", pic_info,
1275
      "", "show i8259 (PIC) state", },
1276
    { "pci", "", pci_info,
1277
      "", "show PCI info", },
1278
#if defined(TARGET_I386)
1279
    { "tlb", "", tlb_info,
1280
      "", "show virtual to physical memory mappings", },
1281
    { "mem", "", mem_info,
1282
      "", "show the active virtual memory mappings", },
1283
#endif
1284
    { "jit", "", do_info_jit,
1285
      "", "show dynamic compiler info", },
1286
    { "kqemu", "", do_info_kqemu,
1287
      "", "show kqemu information", },
1288
    { "usb", "", usb_info,
1289
      "", "show guest USB devices", },
1290
    { "usbhost", "", usb_host_info,
1291
      "", "show host USB devices", },
1292
    { "profile", "", do_info_profile,
1293
      "", "show profiling information", },
1294
    { "capture", "", do_info_capture,
1295
      "", "show capture information" },
1296
    { "snapshots", "", do_info_snapshots,
1297
      "", "show the currently saved VM snapshots" },
1298
    { "mice", "", do_info_mice,
1299
      "", "show which guest mouse is receiving events" },
1300
    { NULL, NULL, },
1301
};
1302

    
1303
/*******************************************************************/
1304

    
1305
static const char *pch;
1306
static jmp_buf expr_env;
1307

    
1308
#define MD_TLONG 0
1309
#define MD_I32   1
1310

    
1311
typedef struct MonitorDef {
1312
    const char *name;
1313
    int offset;
1314
    target_long (*get_value)(struct MonitorDef *md, int val);
1315
    int type;
1316
} MonitorDef;
1317

    
1318
#if defined(TARGET_I386)
1319
static target_long monitor_get_pc (struct MonitorDef *md, int val)
1320
{
1321
    CPUState *env = mon_get_cpu();
1322
    if (!env)
1323
        return 0;
1324
    return env->eip + env->segs[R_CS].base;
1325
}
1326
#endif
1327

    
1328
#if defined(TARGET_PPC)
1329
static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1330
{
1331
    CPUState *env = mon_get_cpu();
1332
    unsigned int u;
1333
    int i;
1334

    
1335
    if (!env)
1336
        return 0;
1337

    
1338
    u = 0;
1339
    for (i = 0; i < 8; i++)
1340
        u |= env->crf[i] << (32 - (4 * i));
1341

    
1342
    return u;
1343
}
1344

    
1345
static target_long monitor_get_msr (struct MonitorDef *md, int val)
1346
{
1347
    CPUState *env = mon_get_cpu();
1348
    if (!env)
1349
        return 0;
1350
    return (env->msr[MSR_POW] << MSR_POW) |
1351
        (env->msr[MSR_ILE] << MSR_ILE) |
1352
        (env->msr[MSR_EE] << MSR_EE) |
1353
        (env->msr[MSR_PR] << MSR_PR) |
1354
        (env->msr[MSR_FP] << MSR_FP) |
1355
        (env->msr[MSR_ME] << MSR_ME) |
1356
        (env->msr[MSR_FE0] << MSR_FE0) |
1357
        (env->msr[MSR_SE] << MSR_SE) |
1358
        (env->msr[MSR_BE] << MSR_BE) |
1359
        (env->msr[MSR_FE1] << MSR_FE1) |
1360
        (env->msr[MSR_IP] << MSR_IP) |
1361
        (env->msr[MSR_IR] << MSR_IR) |
1362
        (env->msr[MSR_DR] << MSR_DR) |
1363
        (env->msr[MSR_RI] << MSR_RI) |
1364
        (env->msr[MSR_LE] << MSR_LE);
1365
}
1366

    
1367
static target_long monitor_get_xer (struct MonitorDef *md, int val)
1368
{
1369
    CPUState *env = mon_get_cpu();
1370
    if (!env)
1371
        return 0;
1372
    return (env->xer[XER_SO] << XER_SO) |
1373
        (env->xer[XER_OV] << XER_OV) |
1374
        (env->xer[XER_CA] << XER_CA) |
1375
        (env->xer[XER_BC] << XER_BC);
1376
}
1377

    
1378
static target_long monitor_get_decr (struct MonitorDef *md, int val)
1379
{
1380
    CPUState *env = mon_get_cpu();
1381
    if (!env)
1382
        return 0;
1383
    return cpu_ppc_load_decr(env);
1384
}
1385

    
1386
static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1387
{
1388
    CPUState *env = mon_get_cpu();
1389
    if (!env)
1390
        return 0;
1391
    return cpu_ppc_load_tbu(env);
1392
}
1393

    
1394
static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1395
{
1396
    CPUState *env = mon_get_cpu();
1397
    if (!env)
1398
        return 0;
1399
    return cpu_ppc_load_tbl(env);
1400
}
1401
#endif
1402

    
1403
#if defined(TARGET_SPARC)
1404
#ifndef TARGET_SPARC64
1405
static target_long monitor_get_psr (struct MonitorDef *md, int val)
1406
{
1407
    CPUState *env = mon_get_cpu();
1408
    if (!env)
1409
        return 0;
1410
    return GET_PSR(env);
1411
}
1412
#endif
1413

    
1414
static target_long monitor_get_reg(struct MonitorDef *md, int val)
1415
{
1416
    CPUState *env = mon_get_cpu();
1417
    if (!env)
1418
        return 0;
1419
    return env->regwptr[val];
1420
}
1421
#endif
1422

    
1423
static MonitorDef monitor_defs[] = {
1424
#ifdef TARGET_I386
1425

    
1426
#define SEG(name, seg) \
1427
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1428
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1429
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1430

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

    
1622
static void expr_error(const char *fmt) 
1623
{
1624
    term_printf(fmt);
1625
    term_printf("\n");
1626
    longjmp(expr_env, 1);
1627
}
1628

    
1629
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
1630
static int get_monitor_def(target_long *pval, const char *name)
1631
{
1632
    MonitorDef *md;
1633
    void *ptr;
1634

    
1635
    for(md = monitor_defs; md->name != NULL; md++) {
1636
        if (compare_cmd(name, md->name)) {
1637
            if (md->get_value) {
1638
                *pval = md->get_value(md, md->offset);
1639
            } else {
1640
                CPUState *env = mon_get_cpu();
1641
                if (!env)
1642
                    return -2;
1643
                ptr = (uint8_t *)env + md->offset;
1644
                switch(md->type) {
1645
                case MD_I32:
1646
                    *pval = *(int32_t *)ptr;
1647
                    break;
1648
                case MD_TLONG:
1649
                    *pval = *(target_long *)ptr;
1650
                    break;
1651
                default:
1652
                    *pval = 0;
1653
                    break;
1654
                }
1655
            }
1656
            return 0;
1657
        }
1658
    }
1659
    return -1;
1660
}
1661

    
1662
static void next(void)
1663
{
1664
    if (pch != '\0') {
1665
        pch++;
1666
        while (isspace(*pch))
1667
            pch++;
1668
    }
1669
}
1670

    
1671
static target_long expr_sum(void);
1672

    
1673
static target_long expr_unary(void)
1674
{
1675
    target_long n;
1676
    char *p;
1677
    int ret;
1678

    
1679
    switch(*pch) {
1680
    case '+':
1681
        next();
1682
        n = expr_unary();
1683
        break;
1684
    case '-':
1685
        next();
1686
        n = -expr_unary();
1687
        break;
1688
    case '~':
1689
        next();
1690
        n = ~expr_unary();
1691
        break;
1692
    case '(':
1693
        next();
1694
        n = expr_sum();
1695
        if (*pch != ')') {
1696
            expr_error("')' expected");
1697
        }
1698
        next();
1699
        break;
1700
    case '\'':
1701
        pch++;
1702
        if (*pch == '\0')
1703
            expr_error("character constant expected");
1704
        n = *pch;
1705
        pch++;
1706
        if (*pch != '\'')
1707
            expr_error("missing terminating \' character");
1708
        next();
1709
        break;
1710
    case '$':
1711
        {
1712
            char buf[128], *q;
1713
            
1714
            pch++;
1715
            q = buf;
1716
            while ((*pch >= 'a' && *pch <= 'z') ||
1717
                   (*pch >= 'A' && *pch <= 'Z') ||
1718
                   (*pch >= '0' && *pch <= '9') ||
1719
                   *pch == '_' || *pch == '.') {
1720
                if ((q - buf) < sizeof(buf) - 1)
1721
                    *q++ = *pch;
1722
                pch++;
1723
            }
1724
            while (isspace(*pch))
1725
                pch++;
1726
            *q = 0;
1727
            ret = get_monitor_def(&n, buf);
1728
            if (ret == -1)
1729
                expr_error("unknown register");
1730
            else if (ret == -2) 
1731
                expr_error("no cpu defined");
1732
        }
1733
        break;
1734
    case '\0':
1735
        expr_error("unexpected end of expression");
1736
        n = 0;
1737
        break;
1738
    default:
1739
#if TARGET_LONG_BITS == 64
1740
        n = strtoull(pch, &p, 0);
1741
#else
1742
        n = strtoul(pch, &p, 0);
1743
#endif
1744
        if (pch == p) {
1745
            expr_error("invalid char in expression");
1746
        }
1747
        pch = p;
1748
        while (isspace(*pch))
1749
            pch++;
1750
        break;
1751
    }
1752
    return n;
1753
}
1754

    
1755

    
1756
static target_long expr_prod(void)
1757
{
1758
    target_long val, val2;
1759
    int op;
1760
    
1761
    val = expr_unary();
1762
    for(;;) {
1763
        op = *pch;
1764
        if (op != '*' && op != '/' && op != '%')
1765
            break;
1766
        next();
1767
        val2 = expr_unary();
1768
        switch(op) {
1769
        default:
1770
        case '*':
1771
            val *= val2;
1772
            break;
1773
        case '/':
1774
        case '%':
1775
            if (val2 == 0) 
1776
                expr_error("division by zero");
1777
            if (op == '/')
1778
                val /= val2;
1779
            else
1780
                val %= val2;
1781
            break;
1782
        }
1783
    }
1784
    return val;
1785
}
1786

    
1787
static target_long expr_logic(void)
1788
{
1789
    target_long val, val2;
1790
    int op;
1791

    
1792
    val = expr_prod();
1793
    for(;;) {
1794
        op = *pch;
1795
        if (op != '&' && op != '|' && op != '^')
1796
            break;
1797
        next();
1798
        val2 = expr_prod();
1799
        switch(op) {
1800
        default:
1801
        case '&':
1802
            val &= val2;
1803
            break;
1804
        case '|':
1805
            val |= val2;
1806
            break;
1807
        case '^':
1808
            val ^= val2;
1809
            break;
1810
        }
1811
    }
1812
    return val;
1813
}
1814

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

    
1820
    val = expr_logic();
1821
    for(;;) {
1822
        op = *pch;
1823
        if (op != '+' && op != '-')
1824
            break;
1825
        next();
1826
        val2 = expr_logic();
1827
        if (op == '+')
1828
            val += val2;
1829
        else
1830
            val -= val2;
1831
    }
1832
    return val;
1833
}
1834

    
1835
static int get_expr(target_long *pval, const char **pp)
1836
{
1837
    pch = *pp;
1838
    if (setjmp(expr_env)) {
1839
        *pp = pch;
1840
        return -1;
1841
    }
1842
    while (isspace(*pch))
1843
        pch++;
1844
    *pval = expr_sum();
1845
    *pp = pch;
1846
    return 0;
1847
}
1848

    
1849
static int get_str(char *buf, int buf_size, const char **pp)
1850
{
1851
    const char *p;
1852
    char *q;
1853
    int c;
1854

    
1855
    q = buf;
1856
    p = *pp;
1857
    while (isspace(*p))
1858
        p++;
1859
    if (*p == '\0') {
1860
    fail:
1861
        *q = '\0';
1862
        *pp = p;
1863
        return -1;
1864
    }
1865
    if (*p == '\"') {
1866
        p++;
1867
        while (*p != '\0' && *p != '\"') {
1868
            if (*p == '\\') {
1869
                p++;
1870
                c = *p++;
1871
                switch(c) {
1872
                case 'n':
1873
                    c = '\n';
1874
                    break;
1875
                case 'r':
1876
                    c = '\r';
1877
                    break;
1878
                case '\\':
1879
                case '\'':
1880
                case '\"':
1881
                    break;
1882
                default:
1883
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
1884
                    goto fail;
1885
                }
1886
                if ((q - buf) < buf_size - 1) {
1887
                    *q++ = c;
1888
                }
1889
            } else {
1890
                if ((q - buf) < buf_size - 1) {
1891
                    *q++ = *p;
1892
                }
1893
                p++;
1894
            }
1895
        }
1896
        if (*p != '\"') {
1897
            qemu_printf("unterminated string\n");
1898
            goto fail;
1899
        }
1900
        p++;
1901
    } else {
1902
        while (*p != '\0' && !isspace(*p)) {
1903
            if ((q - buf) < buf_size - 1) {
1904
                *q++ = *p;
1905
            }
1906
            p++;
1907
        }
1908
    }
1909
    *q = '\0';
1910
    *pp = p;
1911
    return 0;
1912
}
1913

    
1914
static int default_fmt_format = 'x';
1915
static int default_fmt_size = 4;
1916

    
1917
#define MAX_ARGS 16
1918

    
1919
static void monitor_handle_command(const char *cmdline)
1920
{
1921
    const char *p, *pstart, *typestr;
1922
    char *q;
1923
    int c, nb_args, len, i, has_arg;
1924
    term_cmd_t *cmd;
1925
    char cmdname[256];
1926
    char buf[1024];
1927
    void *str_allocated[MAX_ARGS];
1928
    void *args[MAX_ARGS];
1929

    
1930
#ifdef DEBUG
1931
    term_printf("command='%s'\n", cmdline);
1932
#endif
1933
    
1934
    /* extract the command name */
1935
    p = cmdline;
1936
    q = cmdname;
1937
    while (isspace(*p))
1938
        p++;
1939
    if (*p == '\0')
1940
        return;
1941
    pstart = p;
1942
    while (*p != '\0' && *p != '/' && !isspace(*p))
1943
        p++;
1944
    len = p - pstart;
1945
    if (len > sizeof(cmdname) - 1)
1946
        len = sizeof(cmdname) - 1;
1947
    memcpy(cmdname, pstart, len);
1948
    cmdname[len] = '\0';
1949
    
1950
    /* find the command */
1951
    for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1952
        if (compare_cmd(cmdname, cmd->name)) 
1953
            goto found;
1954
    }
1955
    term_printf("unknown command: '%s'\n", cmdname);
1956
    return;
1957
 found:
1958

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

    
2188
    switch(nb_args) {
2189
    case 0:
2190
        cmd->handler();
2191
        break;
2192
    case 1:
2193
        cmd->handler(args[0]);
2194
        break;
2195
    case 2:
2196
        cmd->handler(args[0], args[1]);
2197
        break;
2198
    case 3:
2199
        cmd->handler(args[0], args[1], args[2]);
2200
        break;
2201
    case 4:
2202
        cmd->handler(args[0], args[1], args[2], args[3]);
2203
        break;
2204
    case 5:
2205
        cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2206
        break;
2207
    case 6:
2208
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2209
        break;
2210
    case 7:
2211
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
2212
        break;
2213
    default:
2214
        term_printf("unsupported number of arguments: %d\n", nb_args);
2215
        goto fail;
2216
    }
2217
 fail:
2218
    for(i = 0; i < MAX_ARGS; i++)
2219
        qemu_free(str_allocated[i]);
2220
    return;
2221
}
2222

    
2223
static void cmd_completion(const char *name, const char *list)
2224
{
2225
    const char *p, *pstart;
2226
    char cmd[128];
2227
    int len;
2228

    
2229
    p = list;
2230
    for(;;) {
2231
        pstart = p;
2232
        p = strchr(p, '|');
2233
        if (!p)
2234
            p = pstart + strlen(pstart);
2235
        len = p - pstart;
2236
        if (len > sizeof(cmd) - 2)
2237
            len = sizeof(cmd) - 2;
2238
        memcpy(cmd, pstart, len);
2239
        cmd[len] = '\0';
2240
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2241
            add_completion(cmd);
2242
        }
2243
        if (*p == '\0')
2244
            break;
2245
        p++;
2246
    }
2247
}
2248

    
2249
static void file_completion(const char *input)
2250
{
2251
    DIR *ffs;
2252
    struct dirent *d;
2253
    char path[1024];
2254
    char file[1024], file_prefix[1024];
2255
    int input_path_len;
2256
    const char *p;
2257

    
2258
    p = strrchr(input, '/'); 
2259
    if (!p) {
2260
        input_path_len = 0;
2261
        pstrcpy(file_prefix, sizeof(file_prefix), input);
2262
        strcpy(path, ".");
2263
    } else {
2264
        input_path_len = p - input + 1;
2265
        memcpy(path, input, input_path_len);
2266
        if (input_path_len > sizeof(path) - 1)
2267
            input_path_len = sizeof(path) - 1;
2268
        path[input_path_len] = '\0';
2269
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2270
    }
2271
#ifdef DEBUG_COMPLETION
2272
    term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2273
#endif
2274
    ffs = opendir(path);
2275
    if (!ffs)
2276
        return;
2277
    for(;;) {
2278
        struct stat sb;
2279
        d = readdir(ffs);
2280
        if (!d)
2281
            break;
2282
        if (strstart(d->d_name, file_prefix, NULL)) {
2283
            memcpy(file, input, input_path_len);
2284
            strcpy(file + input_path_len, d->d_name);
2285
            /* stat the file to find out if it's a directory.
2286
             * In that case add a slash to speed up typing long paths
2287
             */
2288
            stat(file, &sb);
2289
            if(S_ISDIR(sb.st_mode))
2290
                strcat(file, "/");
2291
            add_completion(file);
2292
        }
2293
    }
2294
    closedir(ffs);
2295
}
2296

    
2297
static void block_completion_it(void *opaque, const char *name)
2298
{
2299
    const char *input = opaque;
2300

    
2301
    if (input[0] == '\0' ||
2302
        !strncmp(name, (char *)input, strlen(input))) {
2303
        add_completion(name);
2304
    }
2305
}
2306

    
2307
/* NOTE: this parser is an approximate form of the real command parser */
2308
static void parse_cmdline(const char *cmdline,
2309
                         int *pnb_args, char **args)
2310
{
2311
    const char *p;
2312
    int nb_args, ret;
2313
    char buf[1024];
2314

    
2315
    p = cmdline;
2316
    nb_args = 0;
2317
    for(;;) {
2318
        while (isspace(*p))
2319
            p++;
2320
        if (*p == '\0')
2321
            break;
2322
        if (nb_args >= MAX_ARGS)
2323
            break;
2324
        ret = get_str(buf, sizeof(buf), &p);
2325
        args[nb_args] = qemu_strdup(buf);
2326
        nb_args++;
2327
        if (ret < 0)
2328
            break;
2329
    }
2330
    *pnb_args = nb_args;
2331
}
2332

    
2333
void readline_find_completion(const char *cmdline)
2334
{
2335
    const char *cmdname;
2336
    char *args[MAX_ARGS];
2337
    int nb_args, i, len;
2338
    const char *ptype, *str;
2339
    term_cmd_t *cmd;
2340
    const KeyDef *key;
2341

    
2342
    parse_cmdline(cmdline, &nb_args, args);
2343
#ifdef DEBUG_COMPLETION
2344
    for(i = 0; i < nb_args; i++) {
2345
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2346
    }
2347
#endif
2348

    
2349
    /* if the line ends with a space, it means we want to complete the
2350
       next arg */
2351
    len = strlen(cmdline);
2352
    if (len > 0 && isspace(cmdline[len - 1])) {
2353
        if (nb_args >= MAX_ARGS)
2354
            return;
2355
        args[nb_args++] = qemu_strdup("");
2356
    }
2357
    if (nb_args <= 1) {
2358
        /* command completion */
2359
        if (nb_args == 0)
2360
            cmdname = "";
2361
        else
2362
            cmdname = args[0];
2363
        completion_index = strlen(cmdname);
2364
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2365
            cmd_completion(cmdname, cmd->name);
2366
        }
2367
    } else {
2368
        /* find the command */
2369
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2370
            if (compare_cmd(args[0], cmd->name))
2371
                goto found;
2372
        }
2373
        return;
2374
    found:
2375
        ptype = cmd->args_type;
2376
        for(i = 0; i < nb_args - 2; i++) {
2377
            if (*ptype != '\0') {
2378
                ptype++;
2379
                while (*ptype == '?')
2380
                    ptype++;
2381
            }
2382
        }
2383
        str = args[nb_args - 1];
2384
        switch(*ptype) {
2385
        case 'F':
2386
            /* file completion */
2387
            completion_index = strlen(str);
2388
            file_completion(str);
2389
            break;
2390
        case 'B':
2391
            /* block device name completion */
2392
            completion_index = strlen(str);
2393
            bdrv_iterate(block_completion_it, (void *)str);
2394
            break;
2395
        case 's':
2396
            /* XXX: more generic ? */
2397
            if (!strcmp(cmd->name, "info")) {
2398
                completion_index = strlen(str);
2399
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2400
                    cmd_completion(str, cmd->name);
2401
                }
2402
            } else if (!strcmp(cmd->name, "sendkey")) {
2403
                completion_index = strlen(str);
2404
                for(key = key_defs; key->name != NULL; key++) {
2405
                    cmd_completion(str, key->name);
2406
                }
2407
            }
2408
            break;
2409
        default:
2410
            break;
2411
        }
2412
    }
2413
    for(i = 0; i < nb_args; i++)
2414
        qemu_free(args[i]);
2415
}
2416

    
2417
static int term_can_read(void *opaque)
2418
{
2419
    return 128;
2420
}
2421

    
2422
static void term_read(void *opaque, const uint8_t *buf, int size)
2423
{
2424
    int i;
2425
    for(i = 0; i < size; i++)
2426
        readline_handle_byte(buf[i]);
2427
}
2428

    
2429
static void monitor_start_input(void);
2430

    
2431
static void monitor_handle_command1(void *opaque, const char *cmdline)
2432
{
2433
    monitor_handle_command(cmdline);
2434
    monitor_start_input();
2435
}
2436

    
2437
static void monitor_start_input(void)
2438
{
2439
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2440
}
2441

    
2442
static void term_event(void *opaque, int event)
2443
{
2444
    if (event != CHR_EVENT_RESET)
2445
        return;
2446

    
2447
    if (!hide_banner)
2448
            term_printf("QEMU %s monitor - type 'help' for more information\n",
2449
                        QEMU_VERSION);
2450
    monitor_start_input();
2451
}
2452

    
2453
void monitor_init(CharDriverState *hd, int show_banner)
2454
{
2455
    monitor_hd = hd;
2456
    hide_banner = !show_banner;
2457

    
2458
    qemu_chr_add_handlers(hd, term_can_read, term_read, term_event, NULL);
2459
}
2460

    
2461
/* XXX: use threads ? */
2462
/* modal monitor readline */
2463
static int monitor_readline_started;
2464
static char *monitor_readline_buf;
2465
static int monitor_readline_buf_size;
2466

    
2467
static void monitor_readline_cb(void *opaque, const char *input)
2468
{
2469
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2470
    monitor_readline_started = 0;
2471
}
2472

    
2473
void monitor_readline(const char *prompt, int is_password,
2474
                      char *buf, int buf_size)
2475
{
2476
    if (is_password) {
2477
        qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2478
    }
2479
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
2480
    monitor_readline_buf = buf;
2481
    monitor_readline_buf_size = buf_size;
2482
    monitor_readline_started = 1;
2483
    while (monitor_readline_started) {
2484
        main_loop_wait(10);
2485
    }
2486
}