Statistics
| Branch: | Revision:

root / monitor.c @ 7fe48483

History | View | Annotate | Download (50.2 kB)

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

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

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

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

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

    
56
static CharDriverState *monitor_hd;
57

    
58
static term_cmd_t term_cmds[];
59
static term_cmd_t info_cmds[];
60

    
61
static char term_outbuf[1024];
62
static int term_outbuf_index;
63

    
64
static void monitor_start_input(void);
65

    
66
void term_flush(void)
67
{
68
    if (term_outbuf_index > 0) {
69
        qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
70
        term_outbuf_index = 0;
71
    }
72
}
73

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

    
89
void term_vprintf(const char *fmt, va_list ap)
90
{
91
    char buf[4096];
92
    vsnprintf(buf, sizeof(buf), fmt, ap);
93
    term_puts(buf);
94
}
95

    
96
void term_printf(const char *fmt, ...)
97
{
98
    va_list ap;
99
    va_start(ap, fmt);
100
    term_vprintf(fmt, ap);
101
    va_end(ap);
102
}
103

    
104
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
105
{
106
    va_list ap;
107
    va_start(ap, fmt);
108
    term_vprintf(fmt, ap);
109
    va_end(ap);
110
    return 0;
111
}
112

    
113
static int compare_cmd(const char *name, const char *list)
114
{
115
    const char *p, *pstart;
116
    int len;
117
    len = strlen(name);
118
    p = list;
119
    for(;;) {
120
        pstart = p;
121
        p = strchr(p, '|');
122
        if (!p)
123
            p = pstart + strlen(pstart);
124
        if ((p - pstart) == len && !memcmp(pstart, name, len))
125
            return 1;
126
        if (*p == '\0')
127
            break;
128
        p++;
129
    }
130
    return 0;
131
}
132

    
133
static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
134
{
135
    term_cmd_t *cmd;
136

    
137
    for(cmd = cmds; cmd->name != NULL; cmd++) {
138
        if (!name || !strcmp(name, cmd->name))
139
            term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
140
    }
141
}
142

    
143
static void help_cmd(const char *name)
144
{
145
    if (name && !strcmp(name, "info")) {
146
        help_cmd1(info_cmds, "info ", NULL);
147
    } else {
148
        help_cmd1(term_cmds, "", name);
149
        if (name && !strcmp(name, "log")) {
150
            CPULogItem *item;
151
            term_printf("Log items (comma separated):\n");
152
            term_printf("%-10s %s\n", "none", "remove all logs");
153
            for(item = cpu_log_items; item->mask != 0; item++) {
154
                term_printf("%-10s %s\n", item->name, item->help);
155
            }
156
        }
157
    }
158
}
159

    
160
static void do_help(const char *name)
161
{
162
    help_cmd(name);
163
}
164

    
165
static void do_commit(void)
166
{
167
    int i;
168

    
169
    for (i = 0; i < MAX_DISKS; i++) {
170
        if (bs_table[i]) {
171
            bdrv_commit(bs_table[i]);
172
        }
173
    }
174
}
175

    
176
static void do_info(const char *item)
177
{
178
    term_cmd_t *cmd;
179

    
180
    if (!item)
181
        goto help;
182
    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
183
        if (compare_cmd(item, cmd->name)) 
184
            goto found;
185
    }
186
 help:
187
    help_cmd("info");
188
    return;
189
 found:
190
    cmd->handler();
191
}
192

    
193
static void do_info_network(void)
194
{
195
    int i, j;
196
    NetDriverState *nd;
197
    
198
    for(i = 0; i < nb_nics; i++) {
199
        nd = &nd_table[i];
200
        term_printf("%d: ifname=%s macaddr=", i, nd->ifname);
201
        for(j = 0; j < 6; j++) {
202
            if (j > 0)
203
                term_printf(":");
204
            term_printf("%02x", nd->macaddr[j]);
205
        }
206
        term_printf("\n");
207
    }
208
}
209
 
210
static void do_info_block(void)
211
{
212
    bdrv_info();
213
}
214

    
215
static void do_info_registers(void)
216
{
217
#ifdef TARGET_I386
218
    cpu_dump_state(cpu_single_env, stdout, monitor_fprintf,
219
                   X86_DUMP_FPU | X86_DUMP_CCOP);
220
#else
221
    cpu_dump_state(cpu_single_env, stdout, monitor_fprintf, 
222
                   0);
223
#endif
224
}
225

    
226
static void do_info_history (void)
227
{
228
    int i;
229
    const char *str;
230
    
231
    i = 0;
232
    for(;;) {
233
        str = readline_get_history(i);
234
        if (!str)
235
            break;
236
        term_printf("%d: '%s'\n", i, str);
237
        i++;
238
    }
239
}
240

    
241
static void do_quit(void)
242
{
243
    exit(0);
244
}
245

    
246
static int eject_device(BlockDriverState *bs, int force)
247
{
248
    if (bdrv_is_inserted(bs)) {
249
        if (!force) {
250
            if (!bdrv_is_removable(bs)) {
251
                term_printf("device is not removable\n");
252
                return -1;
253
            }
254
            if (bdrv_is_locked(bs)) {
255
                term_printf("device is locked\n");
256
                return -1;
257
            }
258
        }
259
        bdrv_close(bs);
260
    }
261
    return 0;
262
}
263

    
264
static void do_eject(int force, const char *filename)
265
{
266
    BlockDriverState *bs;
267

    
268
    bs = bdrv_find(filename);
269
    if (!bs) {
270
        term_printf("device not found\n");
271
        return;
272
    }
273
    eject_device(bs, force);
274
}
275

    
276
static void do_change(const char *device, const char *filename)
277
{
278
    BlockDriverState *bs;
279
    int i;
280
    char password[256];
281

    
282
    bs = bdrv_find(device);
283
    if (!bs) {
284
        term_printf("device not found\n");
285
        return;
286
    }
287
    if (eject_device(bs, 0) < 0)
288
        return;
289
    bdrv_open(bs, filename, 0);
290
    if (bdrv_is_encrypted(bs)) {
291
        term_printf("%s is encrypted.\n", device);
292
        for(i = 0; i < 3; i++) {
293
            monitor_readline("Password: ", 1, password, sizeof(password));
294
            if (bdrv_set_key(bs, password) == 0)
295
                break;
296
            term_printf("invalid password\n");
297
        }
298
    }
299
}
300

    
301
static void do_screen_dump(const char *filename)
302
{
303
    vga_screen_dump(filename);
304
}
305

    
306
static void do_log(const char *items)
307
{
308
    int mask;
309
    
310
    if (!strcmp(items, "none")) {
311
        mask = 0;
312
    } else {
313
        mask = cpu_str_to_log_mask(items);
314
        if (!mask) {
315
            help_cmd("log");
316
            return;
317
        }
318
    }
319
    cpu_set_log(mask);
320
}
321

    
322
static void do_savevm(const char *filename)
323
{
324
    if (qemu_savevm(filename) < 0)
325
        term_printf("I/O error when saving VM to '%s'\n", filename);
326
}
327

    
328
static void do_loadvm(const char *filename)
329
{
330
    if (qemu_loadvm(filename) < 0) 
331
        term_printf("I/O error when loading VM from '%s'\n", filename);
332
}
333

    
334
static void do_stop(void)
335
{
336
    vm_stop(EXCP_INTERRUPT);
337
}
338

    
339
static void do_cont(void)
340
{
341
    vm_start();
342
}
343

    
344
#ifdef CONFIG_GDBSTUB
345
static void do_gdbserver(int has_port, int port)
346
{
347
    if (!has_port)
348
        port = DEFAULT_GDBSTUB_PORT;
349
    if (gdbserver_start(port) < 0) {
350
        qemu_printf("Could not open gdbserver socket on port %d\n", port);
351
    } else {
352
        qemu_printf("Waiting gdb connection on port %d\n", port);
353
    }
354
}
355
#endif
356

    
357
static void term_printc(int c)
358
{
359
    term_printf("'");
360
    switch(c) {
361
    case '\'':
362
        term_printf("\\'");
363
        break;
364
    case '\\':
365
        term_printf("\\\\");
366
        break;
367
    case '\n':
368
        term_printf("\\n");
369
        break;
370
    case '\r':
371
        term_printf("\\r");
372
        break;
373
    default:
374
        if (c >= 32 && c <= 126) {
375
            term_printf("%c", c);
376
        } else {
377
            term_printf("\\x%02x", c);
378
        }
379
        break;
380
    }
381
    term_printf("'");
382
}
383

    
384
static void memory_dump(int count, int format, int wsize, 
385
                        target_ulong addr, int is_physical)
386
{
387
    int nb_per_line, l, line_size, i, max_digits, len;
388
    uint8_t buf[16];
389
    uint64_t v;
390

    
391
    if (format == 'i') {
392
        int flags;
393
        flags = 0;
394
#ifdef TARGET_I386
395
        if (wsize == 2) {
396
            flags = 1;
397
        } else if (wsize == 4) {
398
            flags = 0;
399
        } else {
400
            /* as default we use the current CS size */
401
            flags = 0;
402
            if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK))
403
                flags = 1;
404
        }
405
#endif
406
        monitor_disas(addr, count, is_physical, flags);
407
        return;
408
    }
409

    
410
    len = wsize * count;
411
    if (wsize == 1)
412
        line_size = 8;
413
    else
414
        line_size = 16;
415
    nb_per_line = line_size / wsize;
416
    max_digits = 0;
417

    
418
    switch(format) {
419
    case 'o':
420
        max_digits = (wsize * 8 + 2) / 3;
421
        break;
422
    default:
423
    case 'x':
424
        max_digits = (wsize * 8) / 4;
425
        break;
426
    case 'u':
427
    case 'd':
428
        max_digits = (wsize * 8 * 10 + 32) / 33;
429
        break;
430
    case 'c':
431
        wsize = 1;
432
        break;
433
    }
434

    
435
    while (len > 0) {
436
        term_printf("0x%08x:", addr);
437
        l = len;
438
        if (l > line_size)
439
            l = line_size;
440
        if (is_physical) {
441
            cpu_physical_memory_rw(addr, buf, l, 0);
442
        } else {
443
            cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0);
444
        }
445
        i = 0; 
446
        while (i < l) {
447
            switch(wsize) {
448
            default:
449
            case 1:
450
                v = ldub_raw(buf + i);
451
                break;
452
            case 2:
453
                v = lduw_raw(buf + i);
454
                break;
455
            case 4:
456
                v = ldl_raw(buf + i);
457
                break;
458
            case 8:
459
                v = ldq_raw(buf + i);
460
                break;
461
            }
462
            term_printf(" ");
463
            switch(format) {
464
            case 'o':
465
                term_printf("%#*llo", max_digits, v);
466
                break;
467
            case 'x':
468
                term_printf("0x%0*llx", max_digits, v);
469
                break;
470
            case 'u':
471
                term_printf("%*llu", max_digits, v);
472
                break;
473
            case 'd':
474
                term_printf("%*lld", max_digits, v);
475
                break;
476
            case 'c':
477
                term_printc(v);
478
                break;
479
            }
480
            i += wsize;
481
        }
482
        term_printf("\n");
483
        addr += l;
484
        len -= l;
485
    }
486
}
487

    
488
static void do_memory_dump(int count, int format, int size, int addr)
489
{
490
    memory_dump(count, format, size, addr, 0);
491
}
492

    
493
static void do_physical_memory_dump(int count, int format, int size, int addr)
494
{
495
    memory_dump(count, format, size, addr, 1);
496
}
497

    
498
static void do_print(int count, int format, int size, int val)
499
{
500
    switch(format) {
501
    case 'o':
502
        term_printf("%#o", val);
503
        break;
504
    case 'x':
505
        term_printf("%#x", val);
506
        break;
507
    case 'u':
508
        term_printf("%u", val);
509
        break;
510
    default:
511
    case 'd':
512
        term_printf("%d", val);
513
        break;
514
    case 'c':
515
        term_printc(val);
516
        break;
517
    }
518
    term_printf("\n");
519
}
520

    
521
typedef struct {
522
    int keycode;
523
    const char *name;
524
} KeyDef;
525

    
526
static const KeyDef key_defs[] = {
527
    { 0x2a, "shift" },
528
    { 0x36, "shift_r" },
529
    
530
    { 0x38, "alt" },
531
    { 0xb8, "alt_r" },
532
    { 0x1d, "ctrl" },
533
    { 0x9d, "ctrl_r" },
534

    
535
    { 0xdd, "menu" },
536

    
537
    { 0x01, "esc" },
538

    
539
    { 0x02, "1" },
540
    { 0x03, "2" },
541
    { 0x04, "3" },
542
    { 0x05, "4" },
543
    { 0x06, "5" },
544
    { 0x07, "6" },
545
    { 0x08, "7" },
546
    { 0x09, "8" },
547
    { 0x0a, "9" },
548
    { 0x0b, "0" },
549
    { 0x0e, "backspace" },
550

    
551
    { 0x0f, "tab" },
552
    { 0x10, "q" },
553
    { 0x11, "w" },
554
    { 0x12, "e" },
555
    { 0x13, "r" },
556
    { 0x14, "t" },
557
    { 0x15, "y" },
558
    { 0x16, "u" },
559
    { 0x17, "i" },
560
    { 0x18, "o" },
561
    { 0x19, "p" },
562

    
563
    { 0x1c, "ret" },
564

    
565
    { 0x1e, "a" },
566
    { 0x1f, "s" },
567
    { 0x20, "d" },
568
    { 0x21, "f" },
569
    { 0x22, "g" },
570
    { 0x23, "h" },
571
    { 0x24, "j" },
572
    { 0x25, "k" },
573
    { 0x26, "l" },
574

    
575
    { 0x2c, "z" },
576
    { 0x2d, "x" },
577
    { 0x2e, "c" },
578
    { 0x2f, "v" },
579
    { 0x30, "b" },
580
    { 0x31, "n" },
581
    { 0x32, "m" },
582
    
583
    { 0x39, "spc" },
584
    { 0x3a, "caps_lock" },
585
    { 0x3b, "f1" },
586
    { 0x3c, "f2" },
587
    { 0x3d, "f3" },
588
    { 0x3e, "f4" },
589
    { 0x3f, "f5" },
590
    { 0x40, "f6" },
591
    { 0x41, "f7" },
592
    { 0x42, "f8" },
593
    { 0x43, "f9" },
594
    { 0x44, "f10" },
595
    { 0x45, "num_lock" },
596
    { 0x46, "scroll_lock" },
597

    
598
    { 0x56, "<" },
599

    
600
    { 0x57, "f11" },
601
    { 0x58, "f12" },
602

    
603
    { 0xb7, "print" },
604

    
605
    { 0xc7, "home" },
606
    { 0xc9, "pgup" },
607
    { 0xd1, "pgdn" },
608
    { 0xcf, "end" },
609

    
610
    { 0xcb, "left" },
611
    { 0xc8, "up" },
612
    { 0xd0, "down" },
613
    { 0xcd, "right" },
614

    
615
    { 0xd2, "insert" },
616
    { 0xd3, "delete" },
617
    { 0, NULL },
618
};
619

    
620
static int get_keycode(const char *key)
621
{
622
    const KeyDef *p;
623

    
624
    for(p = key_defs; p->name != NULL; p++) {
625
        if (!strcmp(key, p->name))
626
            return p->keycode;
627
    }
628
    return -1;
629
}
630

    
631
static void do_send_key(const char *string)
632
{
633
    char keybuf[16], *q;
634
    uint8_t keycodes[16];
635
    const char *p;
636
    int nb_keycodes, keycode, i;
637
    
638
    nb_keycodes = 0;
639
    p = string;
640
    while (*p != '\0') {
641
        q = keybuf;
642
        while (*p != '\0' && *p != '-') {
643
            if ((q - keybuf) < sizeof(keybuf) - 1) {
644
                *q++ = *p;
645
            }
646
            p++;
647
        }
648
        *q = '\0';
649
        keycode = get_keycode(keybuf);
650
        if (keycode < 0) {
651
            term_printf("unknown key: '%s'\n", keybuf);
652
            return;
653
        }
654
        keycodes[nb_keycodes++] = keycode;
655
        if (*p == '\0')
656
            break;
657
        p++;
658
    }
659
    /* key down events */
660
    for(i = 0; i < nb_keycodes; i++) {
661
        keycode = keycodes[i];
662
        if (keycode & 0x80)
663
            kbd_put_keycode(0xe0);
664
        kbd_put_keycode(keycode & 0x7f);
665
    }
666
    /* key up events */
667
    for(i = nb_keycodes - 1; i >= 0; i--) {
668
        keycode = keycodes[i];
669
        if (keycode & 0x80)
670
            kbd_put_keycode(0xe0);
671
        kbd_put_keycode(keycode | 0x80);
672
    }
673
}
674

    
675
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
676
{
677
    uint32_t val;
678
    int suffix;
679

    
680
    if (has_index) {
681
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
682
        addr++;
683
    }
684
    addr &= 0xffff;
685

    
686
    switch(size) {
687
    default:
688
    case 1:
689
        val = cpu_inb(NULL, addr);
690
        suffix = 'b';
691
        break;
692
    case 2:
693
        val = cpu_inw(NULL, addr);
694
        suffix = 'w';
695
        break;
696
    case 4:
697
        val = cpu_inl(NULL, addr);
698
        suffix = 'l';
699
        break;
700
    }
701
    term_printf("port%c[0x%04x] = %#0*x\n",
702
                suffix, addr, size * 2, val);
703
}
704

    
705
static void do_system_reset(void)
706
{
707
    qemu_system_reset_request();
708
}
709

    
710
#if defined(TARGET_I386)
711
static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
712
{
713
    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n", 
714
                addr,
715
                pte & mask,
716
                pte & PG_GLOBAL_MASK ? 'G' : '-',
717
                pte & PG_PSE_MASK ? 'P' : '-',
718
                pte & PG_DIRTY_MASK ? 'D' : '-',
719
                pte & PG_ACCESSED_MASK ? 'A' : '-',
720
                pte & PG_PCD_MASK ? 'C' : '-',
721
                pte & PG_PWT_MASK ? 'T' : '-',
722
                pte & PG_USER_MASK ? 'U' : '-',
723
                pte & PG_RW_MASK ? 'W' : '-');
724
}
725

    
726
static void tlb_info(void)
727
{
728
    CPUState *env = cpu_single_env;
729
    int l1, l2;
730
    uint32_t pgd, pde, pte;
731

    
732
    if (!(env->cr[0] & CR0_PG_MASK)) {
733
        term_printf("PG disabled\n");
734
        return;
735
    }
736
    pgd = env->cr[3] & ~0xfff;
737
    for(l1 = 0; l1 < 1024; l1++) {
738
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
739
        pde = le32_to_cpu(pde);
740
        if (pde & PG_PRESENT_MASK) {
741
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
742
                print_pte((l1 << 22), pde, ~((1 << 20) - 1));
743
            } else {
744
                for(l2 = 0; l2 < 1024; l2++) {
745
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
746
                                             (uint8_t *)&pte, 4);
747
                    pte = le32_to_cpu(pte);
748
                    if (pte & PG_PRESENT_MASK) {
749
                        print_pte((l1 << 22) + (l2 << 12), 
750
                                  pte & ~PG_PSE_MASK, 
751
                                  ~0xfff);
752
                    }
753
                }
754
            }
755
        }
756
    }
757
}
758

    
759
static void mem_print(uint32_t *pstart, int *plast_prot, 
760
                      uint32_t end, int prot)
761
{
762
    if (prot != *plast_prot) {
763
        if (*pstart != -1) {
764
            term_printf("%08x-%08x %08x %c%c%c\n",
765
                        *pstart, end, end - *pstart, 
766
                        prot & PG_USER_MASK ? 'u' : '-',
767
                        'r',
768
                        prot & PG_RW_MASK ? 'w' : '-');
769
        }
770
        if (prot != 0)
771
            *pstart = end;
772
        else
773
            *pstart = -1;
774
        *plast_prot = prot;
775
    }
776
}
777

    
778
static void mem_info(void)
779
{
780
    CPUState *env = cpu_single_env;
781
    int l1, l2, prot, last_prot;
782
    uint32_t pgd, pde, pte, start, end;
783

    
784
    if (!(env->cr[0] & CR0_PG_MASK)) {
785
        term_printf("PG disabled\n");
786
        return;
787
    }
788
    pgd = env->cr[3] & ~0xfff;
789
    last_prot = 0;
790
    start = -1;
791
    for(l1 = 0; l1 < 1024; l1++) {
792
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
793
        pde = le32_to_cpu(pde);
794
        end = l1 << 22;
795
        if (pde & PG_PRESENT_MASK) {
796
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
797
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
798
                mem_print(&start, &last_prot, end, prot);
799
            } else {
800
                for(l2 = 0; l2 < 1024; l2++) {
801
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
802
                                             (uint8_t *)&pte, 4);
803
                    pte = le32_to_cpu(pte);
804
                    end = (l1 << 22) + (l2 << 12);
805
                    if (pte & PG_PRESENT_MASK) {
806
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
807
                    } else {
808
                        prot = 0;
809
                    }
810
                    mem_print(&start, &last_prot, end, prot);
811
                }
812
            }
813
        } else {
814
            prot = 0;
815
            mem_print(&start, &last_prot, end, prot);
816
        }
817
    }
818
}
819
#endif
820

    
821
static term_cmd_t term_cmds[] = {
822
    { "help|?", "s?", do_help, 
823
      "[cmd]", "show the help" },
824
    { "commit", "", do_commit, 
825
      "", "commit changes to the disk images (if -snapshot is used)" },
826
    { "info", "s?", do_info,
827
      "subcommand", "show various information about the system state" },
828
    { "q|quit", "", do_quit,
829
      "", "quit the emulator" },
830
    { "eject", "-fB", do_eject,
831
      "[-f] device", "eject a removable media (use -f to force it)" },
832
    { "change", "BF", do_change,
833
      "device filename", "change a removable media" },
834
    { "screendump", "F", do_screen_dump, 
835
      "filename", "save screen into PPM image 'filename'" },
836
    { "log", "s", do_log,
837
      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, 
838
    { "savevm", "F", do_savevm,
839
      "filename", "save the whole virtual machine state to 'filename'" }, 
840
    { "loadvm", "F", do_loadvm,
841
      "filename", "restore the whole virtual machine state from 'filename'" }, 
842
    { "stop", "", do_stop, 
843
      "", "stop emulation", },
844
    { "c|cont", "", do_cont, 
845
      "", "resume emulation", },
846
#ifdef CONFIG_GDBSTUB
847
    { "gdbserver", "i?", do_gdbserver, 
848
      "[port]", "start gdbserver session (default port=1234)", },
849
#endif
850
    { "x", "/i", do_memory_dump, 
851
      "/fmt addr", "virtual memory dump starting at 'addr'", },
852
    { "xp", "/i", do_physical_memory_dump, 
853
      "/fmt addr", "physical memory dump starting at 'addr'", },
854
    { "p|print", "/i", do_print, 
855
      "/fmt expr", "print expression value (use $reg for CPU register access)", },
856
    { "i", "/ii.", do_ioport_read, 
857
      "/fmt addr", "I/O port read" },
858

    
859
    { "sendkey", "s", do_send_key, 
860
      "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
861
    { "system_reset", "", do_system_reset, 
862
      "", "reset the system" },
863
    { NULL, NULL, }, 
864
};
865

    
866
static term_cmd_t info_cmds[] = {
867
    { "network", "", do_info_network,
868
      "", "show the network state" },
869
    { "block", "", do_info_block,
870
      "", "show the block devices" },
871
    { "registers", "", do_info_registers,
872
      "", "show the cpu registers" },
873
    { "history", "", do_info_history,
874
      "", "show the command line history", },
875
    { "irq", "", irq_info,
876
      "", "show the interrupts statistics (if available)", },
877
    { "pic", "", pic_info,
878
      "", "show i8259 (PIC) state", },
879
    { "pci", "", pci_info,
880
      "", "show PCI info", },
881
#if defined(TARGET_I386)
882
    { "tlb", "", tlb_info,
883
      "", "show virtual to physical memory mappings", },
884
    { "mem", "", mem_info,
885
      "", "show the active virtual memory mappings", },
886
#endif
887
    { NULL, NULL, },
888
};
889

    
890
/*******************************************************************/
891

    
892
static const char *pch;
893
static jmp_buf expr_env;
894

    
895
typedef struct MonitorDef {
896
    const char *name;
897
    int offset;
898
    int (*get_value)(struct MonitorDef *md, int val);
899
} MonitorDef;
900

    
901
#if defined(TARGET_I386)
902
static int monitor_get_pc (struct MonitorDef *md, int val)
903
{
904
    return cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base;
905
}
906
#endif
907

    
908
#if defined(TARGET_PPC)
909
static int monitor_get_ccr (struct MonitorDef *md, int val)
910
{
911
    unsigned int u;
912
    int i;
913

    
914
    u = 0;
915
    for (i = 0; i < 8; i++)
916
        u |= cpu_single_env->crf[i] << (32 - (4 * i));
917

    
918
    return u;
919
}
920

    
921
static int monitor_get_msr (struct MonitorDef *md, int val)
922
{
923
    return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
924
        (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
925
        (cpu_single_env->msr[MSR_EE] << MSR_EE) |
926
        (cpu_single_env->msr[MSR_PR] << MSR_PR) |
927
        (cpu_single_env->msr[MSR_FP] << MSR_FP) |
928
        (cpu_single_env->msr[MSR_ME] << MSR_ME) |
929
        (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
930
        (cpu_single_env->msr[MSR_SE] << MSR_SE) |
931
        (cpu_single_env->msr[MSR_BE] << MSR_BE) |
932
        (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
933
        (cpu_single_env->msr[MSR_IP] << MSR_IP) |
934
        (cpu_single_env->msr[MSR_IR] << MSR_IR) |
935
        (cpu_single_env->msr[MSR_DR] << MSR_DR) |
936
        (cpu_single_env->msr[MSR_RI] << MSR_RI) |
937
        (cpu_single_env->msr[MSR_LE] << MSR_LE);
938
}
939

    
940
static int monitor_get_xer (struct MonitorDef *md, int val)
941
{
942
    return (cpu_single_env->xer[XER_SO] << XER_SO) |
943
        (cpu_single_env->xer[XER_OV] << XER_OV) |
944
        (cpu_single_env->xer[XER_CA] << XER_CA) |
945
        (cpu_single_env->xer[XER_BC] << XER_BC);
946
}
947

    
948
static int monitor_get_decr (struct MonitorDef *md, int val)
949
{
950
    return cpu_ppc_load_decr(cpu_single_env);
951
}
952

    
953
static int monitor_get_tbu (struct MonitorDef *md, int val)
954
{
955
    return cpu_ppc_load_tbu(cpu_single_env);
956
}
957

    
958
static int monitor_get_tbl (struct MonitorDef *md, int val)
959
{
960
    return cpu_ppc_load_tbl(cpu_single_env);
961
}
962
#endif
963

    
964
#if defined(TARGET_SPARC)
965
static int monitor_get_psr (struct MonitorDef *md, int val)
966
{
967
    return GET_PSR(cpu_single_env);
968
}
969

    
970
static int monitor_get_reg(struct MonitorDef *md, int val)
971
{
972
    return cpu_single_env->regwptr[val];
973
}
974
#endif
975

    
976
static MonitorDef monitor_defs[] = {
977
#ifdef TARGET_I386
978

    
979
#define SEG(name, seg) \
980
    { name, offsetof(CPUState, segs[seg].selector) },\
981
    { name ".base", offsetof(CPUState, segs[seg].base) },\
982
    { name ".limit", offsetof(CPUState, segs[seg].limit) },
983

    
984
    { "eax", offsetof(CPUState, regs[0]) },
985
    { "ecx", offsetof(CPUState, regs[1]) },
986
    { "edx", offsetof(CPUState, regs[2]) },
987
    { "ebx", offsetof(CPUState, regs[3]) },
988
    { "esp|sp", offsetof(CPUState, regs[4]) },
989
    { "ebp|fp", offsetof(CPUState, regs[5]) },
990
    { "esi", offsetof(CPUState, regs[6]) },
991
    { "edi", offsetof(CPUState, regs[7]) },
992
    { "eflags", offsetof(CPUState, eflags) },
993
    { "eip", offsetof(CPUState, eip) },
994
    SEG("cs", R_CS)
995
    SEG("ds", R_DS)
996
    SEG("es", R_ES)
997
    SEG("ss", R_SS)
998
    SEG("fs", R_FS)
999
    SEG("gs", R_GS)
1000
    { "pc", 0, monitor_get_pc, },
1001
#elif defined(TARGET_PPC)
1002
    { "r0", offsetof(CPUState, gpr[0]) },
1003
    { "r1", offsetof(CPUState, gpr[1]) },
1004
    { "r2", offsetof(CPUState, gpr[2]) },
1005
    { "r3", offsetof(CPUState, gpr[3]) },
1006
    { "r4", offsetof(CPUState, gpr[4]) },
1007
    { "r5", offsetof(CPUState, gpr[5]) },
1008
    { "r6", offsetof(CPUState, gpr[6]) },
1009
    { "r7", offsetof(CPUState, gpr[7]) },
1010
    { "r8", offsetof(CPUState, gpr[8]) },
1011
    { "r9", offsetof(CPUState, gpr[9]) },
1012
    { "r10", offsetof(CPUState, gpr[10]) },
1013
    { "r11", offsetof(CPUState, gpr[11]) },
1014
    { "r12", offsetof(CPUState, gpr[12]) },
1015
    { "r13", offsetof(CPUState, gpr[13]) },
1016
    { "r14", offsetof(CPUState, gpr[14]) },
1017
    { "r15", offsetof(CPUState, gpr[15]) },
1018
    { "r16", offsetof(CPUState, gpr[16]) },
1019
    { "r17", offsetof(CPUState, gpr[17]) },
1020
    { "r18", offsetof(CPUState, gpr[18]) },
1021
    { "r19", offsetof(CPUState, gpr[19]) },
1022
    { "r20", offsetof(CPUState, gpr[20]) },
1023
    { "r21", offsetof(CPUState, gpr[21]) },
1024
    { "r22", offsetof(CPUState, gpr[22]) },
1025
    { "r23", offsetof(CPUState, gpr[23]) },
1026
    { "r24", offsetof(CPUState, gpr[24]) },
1027
    { "r25", offsetof(CPUState, gpr[25]) },
1028
    { "r26", offsetof(CPUState, gpr[26]) },
1029
    { "r27", offsetof(CPUState, gpr[27]) },
1030
    { "r28", offsetof(CPUState, gpr[28]) },
1031
    { "r29", offsetof(CPUState, gpr[29]) },
1032
    { "r30", offsetof(CPUState, gpr[30]) },
1033
    { "r31", offsetof(CPUState, gpr[31]) },
1034
    { "nip|pc", offsetof(CPUState, nip) },
1035
    { "lr", offsetof(CPUState, lr) },
1036
    { "ctr", offsetof(CPUState, ctr) },
1037
    { "decr", 0, &monitor_get_decr, },
1038
    { "ccr", 0, &monitor_get_ccr, },
1039
    { "msr", 0, &monitor_get_msr, },
1040
    { "xer", 0, &monitor_get_xer, },
1041
    { "tbu", 0, &monitor_get_tbu, },
1042
    { "tbl", 0, &monitor_get_tbl, },
1043
    { "sdr1", offsetof(CPUState, sdr1) },
1044
    { "sr0", offsetof(CPUState, sr[0]) },
1045
    { "sr1", offsetof(CPUState, sr[1]) },
1046
    { "sr2", offsetof(CPUState, sr[2]) },
1047
    { "sr3", offsetof(CPUState, sr[3]) },
1048
    { "sr4", offsetof(CPUState, sr[4]) },
1049
    { "sr5", offsetof(CPUState, sr[5]) },
1050
    { "sr6", offsetof(CPUState, sr[6]) },
1051
    { "sr7", offsetof(CPUState, sr[7]) },
1052
    { "sr8", offsetof(CPUState, sr[8]) },
1053
    { "sr9", offsetof(CPUState, sr[9]) },
1054
    { "sr10", offsetof(CPUState, sr[10]) },
1055
    { "sr11", offsetof(CPUState, sr[11]) },
1056
    { "sr12", offsetof(CPUState, sr[12]) },
1057
    { "sr13", offsetof(CPUState, sr[13]) },
1058
    { "sr14", offsetof(CPUState, sr[14]) },
1059
    { "sr15", offsetof(CPUState, sr[15]) },
1060
    /* Too lazy to put BATs and SPRs ... */
1061
#elif defined(TARGET_SPARC)
1062
    { "g0", offsetof(CPUState, gregs[0]) },
1063
    { "g1", offsetof(CPUState, gregs[1]) },
1064
    { "g2", offsetof(CPUState, gregs[2]) },
1065
    { "g3", offsetof(CPUState, gregs[3]) },
1066
    { "g4", offsetof(CPUState, gregs[4]) },
1067
    { "g5", offsetof(CPUState, gregs[5]) },
1068
    { "g6", offsetof(CPUState, gregs[6]) },
1069
    { "g7", offsetof(CPUState, gregs[7]) },
1070
    { "o0", 0, monitor_get_reg },
1071
    { "o1", 1, monitor_get_reg },
1072
    { "o2", 2, monitor_get_reg },
1073
    { "o3", 3, monitor_get_reg },
1074
    { "o4", 4, monitor_get_reg },
1075
    { "o5", 5, monitor_get_reg },
1076
    { "o6", 6, monitor_get_reg },
1077
    { "o7", 7, monitor_get_reg },
1078
    { "l0", 8, monitor_get_reg },
1079
    { "l1", 9, monitor_get_reg },
1080
    { "l2", 10, monitor_get_reg },
1081
    { "l3", 11, monitor_get_reg },
1082
    { "l4", 12, monitor_get_reg },
1083
    { "l5", 13, monitor_get_reg },
1084
    { "l6", 14, monitor_get_reg },
1085
    { "l7", 15, monitor_get_reg },
1086
    { "i0", 16, monitor_get_reg },
1087
    { "i1", 17, monitor_get_reg },
1088
    { "i2", 18, monitor_get_reg },
1089
    { "i3", 19, monitor_get_reg },
1090
    { "i4", 20, monitor_get_reg },
1091
    { "i5", 21, monitor_get_reg },
1092
    { "i6", 22, monitor_get_reg },
1093
    { "i7", 23, monitor_get_reg },
1094
    { "pc", offsetof(CPUState, pc) },
1095
    { "npc", offsetof(CPUState, npc) },
1096
    { "y", offsetof(CPUState, y) },
1097
    { "psr", 0, &monitor_get_psr, },
1098
    { "wim", offsetof(CPUState, wim) },
1099
    { "tbr", offsetof(CPUState, tbr) },
1100
    { "fsr", offsetof(CPUState, fsr) },
1101
    { "f0", offsetof(CPUState, fpr[0]) },
1102
    { "f1", offsetof(CPUState, fpr[1]) },
1103
    { "f2", offsetof(CPUState, fpr[2]) },
1104
    { "f3", offsetof(CPUState, fpr[3]) },
1105
    { "f4", offsetof(CPUState, fpr[4]) },
1106
    { "f5", offsetof(CPUState, fpr[5]) },
1107
    { "f6", offsetof(CPUState, fpr[6]) },
1108
    { "f7", offsetof(CPUState, fpr[7]) },
1109
    { "f8", offsetof(CPUState, fpr[8]) },
1110
    { "f9", offsetof(CPUState, fpr[9]) },
1111
    { "f10", offsetof(CPUState, fpr[10]) },
1112
    { "f11", offsetof(CPUState, fpr[11]) },
1113
    { "f12", offsetof(CPUState, fpr[12]) },
1114
    { "f13", offsetof(CPUState, fpr[13]) },
1115
    { "f14", offsetof(CPUState, fpr[14]) },
1116
    { "f15", offsetof(CPUState, fpr[15]) },
1117
    { "f16", offsetof(CPUState, fpr[16]) },
1118
    { "f17", offsetof(CPUState, fpr[17]) },
1119
    { "f18", offsetof(CPUState, fpr[18]) },
1120
    { "f19", offsetof(CPUState, fpr[19]) },
1121
    { "f20", offsetof(CPUState, fpr[20]) },
1122
    { "f21", offsetof(CPUState, fpr[21]) },
1123
    { "f22", offsetof(CPUState, fpr[22]) },
1124
    { "f23", offsetof(CPUState, fpr[23]) },
1125
    { "f24", offsetof(CPUState, fpr[24]) },
1126
    { "f25", offsetof(CPUState, fpr[25]) },
1127
    { "f26", offsetof(CPUState, fpr[26]) },
1128
    { "f27", offsetof(CPUState, fpr[27]) },
1129
    { "f28", offsetof(CPUState, fpr[28]) },
1130
    { "f29", offsetof(CPUState, fpr[29]) },
1131
    { "f30", offsetof(CPUState, fpr[30]) },
1132
    { "f31", offsetof(CPUState, fpr[31]) },
1133
#endif
1134
    { NULL },
1135
};
1136

    
1137
static void expr_error(const char *fmt) 
1138
{
1139
    term_printf(fmt);
1140
    term_printf("\n");
1141
    longjmp(expr_env, 1);
1142
}
1143

    
1144
static int get_monitor_def(int *pval, const char *name)
1145
{
1146
    MonitorDef *md;
1147
    for(md = monitor_defs; md->name != NULL; md++) {
1148
        if (compare_cmd(name, md->name)) {
1149
            if (md->get_value) {
1150
                *pval = md->get_value(md, md->offset);
1151
            } else {
1152
                *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset);
1153
            }
1154
            return 0;
1155
        }
1156
    }
1157
    return -1;
1158
}
1159

    
1160
static void next(void)
1161
{
1162
    if (pch != '\0') {
1163
        pch++;
1164
        while (isspace(*pch))
1165
            pch++;
1166
    }
1167
}
1168

    
1169
static int expr_sum(void);
1170

    
1171
static int expr_unary(void)
1172
{
1173
    int n;
1174
    char *p;
1175

    
1176
    switch(*pch) {
1177
    case '+':
1178
        next();
1179
        n = expr_unary();
1180
        break;
1181
    case '-':
1182
        next();
1183
        n = -expr_unary();
1184
        break;
1185
    case '~':
1186
        next();
1187
        n = ~expr_unary();
1188
        break;
1189
    case '(':
1190
        next();
1191
        n = expr_sum();
1192
        if (*pch != ')') {
1193
            expr_error("')' expected");
1194
        }
1195
        next();
1196
        break;
1197
    case '\'':
1198
        pch++;
1199
        if (*pch == '\0')
1200
            expr_error("character constant expected");
1201
        n = *pch;
1202
        pch++;
1203
        if (*pch != '\'')
1204
            expr_error("missing terminating \' character");
1205
        next();
1206
        break;
1207
    case '$':
1208
        {
1209
            char buf[128], *q;
1210
            
1211
            pch++;
1212
            q = buf;
1213
            while ((*pch >= 'a' && *pch <= 'z') ||
1214
                   (*pch >= 'A' && *pch <= 'Z') ||
1215
                   (*pch >= '0' && *pch <= '9') ||
1216
                   *pch == '_' || *pch == '.') {
1217
                if ((q - buf) < sizeof(buf) - 1)
1218
                    *q++ = *pch;
1219
                pch++;
1220
            }
1221
            while (isspace(*pch))
1222
                pch++;
1223
            *q = 0;
1224
            if (get_monitor_def(&n, buf))
1225
                expr_error("unknown register");
1226
        }
1227
        break;
1228
    case '\0':
1229
        expr_error("unexpected end of expression");
1230
        n = 0;
1231
        break;
1232
    default:
1233
        n = strtoul(pch, &p, 0);
1234
        if (pch == p) {
1235
            expr_error("invalid char in expression");
1236
        }
1237
        pch = p;
1238
        while (isspace(*pch))
1239
            pch++;
1240
        break;
1241
    }
1242
    return n;
1243
}
1244

    
1245

    
1246
static int expr_prod(void)
1247
{
1248
    int val, val2, op;
1249

    
1250
    val = expr_unary();
1251
    for(;;) {
1252
        op = *pch;
1253
        if (op != '*' && op != '/' && op != '%')
1254
            break;
1255
        next();
1256
        val2 = expr_unary();
1257
        switch(op) {
1258
        default:
1259
        case '*':
1260
            val *= val2;
1261
            break;
1262
        case '/':
1263
        case '%':
1264
            if (val2 == 0) 
1265
                expr_error("division by zero");
1266
            if (op == '/')
1267
                val /= val2;
1268
            else
1269
                val %= val2;
1270
            break;
1271
        }
1272
    }
1273
    return val;
1274
}
1275

    
1276
static int expr_logic(void)
1277
{
1278
    int val, val2, op;
1279

    
1280
    val = expr_prod();
1281
    for(;;) {
1282
        op = *pch;
1283
        if (op != '&' && op != '|' && op != '^')
1284
            break;
1285
        next();
1286
        val2 = expr_prod();
1287
        switch(op) {
1288
        default:
1289
        case '&':
1290
            val &= val2;
1291
            break;
1292
        case '|':
1293
            val |= val2;
1294
            break;
1295
        case '^':
1296
            val ^= val2;
1297
            break;
1298
        }
1299
    }
1300
    return val;
1301
}
1302

    
1303
static int expr_sum(void)
1304
{
1305
    int val, val2, op;
1306

    
1307
    val = expr_logic();
1308
    for(;;) {
1309
        op = *pch;
1310
        if (op != '+' && op != '-')
1311
            break;
1312
        next();
1313
        val2 = expr_logic();
1314
        if (op == '+')
1315
            val += val2;
1316
        else
1317
            val -= val2;
1318
    }
1319
    return val;
1320
}
1321

    
1322
static int get_expr(int *pval, const char **pp)
1323
{
1324
    pch = *pp;
1325
    if (setjmp(expr_env)) {
1326
        *pp = pch;
1327
        return -1;
1328
    }
1329
    while (isspace(*pch))
1330
        pch++;
1331
    *pval = expr_sum();
1332
    *pp = pch;
1333
    return 0;
1334
}
1335

    
1336
static int get_str(char *buf, int buf_size, const char **pp)
1337
{
1338
    const char *p;
1339
    char *q;
1340
    int c;
1341

    
1342
    q = buf;
1343
    p = *pp;
1344
    while (isspace(*p))
1345
        p++;
1346
    if (*p == '\0') {
1347
    fail:
1348
        *q = '\0';
1349
        *pp = p;
1350
        return -1;
1351
    }
1352
    if (*p == '\"') {
1353
        p++;
1354
        while (*p != '\0' && *p != '\"') {
1355
            if (*p == '\\') {
1356
                p++;
1357
                c = *p++;
1358
                switch(c) {
1359
                case 'n':
1360
                    c = '\n';
1361
                    break;
1362
                case 'r':
1363
                    c = '\r';
1364
                    break;
1365
                case '\\':
1366
                case '\'':
1367
                case '\"':
1368
                    break;
1369
                default:
1370
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
1371
                    goto fail;
1372
                }
1373
                if ((q - buf) < buf_size - 1) {
1374
                    *q++ = c;
1375
                }
1376
            } else {
1377
                if ((q - buf) < buf_size - 1) {
1378
                    *q++ = *p;
1379
                }
1380
                p++;
1381
            }
1382
        }
1383
        if (*p != '\"') {
1384
            qemu_printf("unterminated string\n");
1385
            goto fail;
1386
        }
1387
        p++;
1388
    } else {
1389
        while (*p != '\0' && !isspace(*p)) {
1390
            if ((q - buf) < buf_size - 1) {
1391
                *q++ = *p;
1392
            }
1393
            p++;
1394
        }
1395
    }
1396
    *q = '\0';
1397
    *pp = p;
1398
    return 0;
1399
}
1400

    
1401
static int default_fmt_format = 'x';
1402
static int default_fmt_size = 4;
1403

    
1404
#define MAX_ARGS 16
1405

    
1406
static void monitor_handle_command(const char *cmdline)
1407
{
1408
    const char *p, *pstart, *typestr;
1409
    char *q;
1410
    int c, nb_args, len, i, has_arg;
1411
    term_cmd_t *cmd;
1412
    char cmdname[256];
1413
    char buf[1024];
1414
    void *str_allocated[MAX_ARGS];
1415
    void *args[MAX_ARGS];
1416

    
1417
#ifdef DEBUG
1418
    term_printf("command='%s'\n", cmdline);
1419
#endif
1420
    
1421
    /* extract the command name */
1422
    p = cmdline;
1423
    q = cmdname;
1424
    while (isspace(*p))
1425
        p++;
1426
    if (*p == '\0')
1427
        return;
1428
    pstart = p;
1429
    while (*p != '\0' && *p != '/' && !isspace(*p))
1430
        p++;
1431
    len = p - pstart;
1432
    if (len > sizeof(cmdname) - 1)
1433
        len = sizeof(cmdname) - 1;
1434
    memcpy(cmdname, pstart, len);
1435
    cmdname[len] = '\0';
1436
    
1437
    /* find the command */
1438
    for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1439
        if (compare_cmd(cmdname, cmd->name)) 
1440
            goto found;
1441
    }
1442
    term_printf("unknown command: '%s'\n", cmdname);
1443
    return;
1444
 found:
1445

    
1446
    for(i = 0; i < MAX_ARGS; i++)
1447
        str_allocated[i] = NULL;
1448
    
1449
    /* parse the parameters */
1450
    typestr = cmd->args_type;
1451
    nb_args = 0;
1452
    for(;;) {
1453
        c = *typestr;
1454
        if (c == '\0')
1455
            break;
1456
        typestr++;
1457
        switch(c) {
1458
        case 'F':
1459
        case 'B':
1460
        case 's':
1461
            {
1462
                int ret;
1463
                char *str;
1464
                
1465
                while (isspace(*p)) 
1466
                    p++;
1467
                if (*typestr == '?') {
1468
                    typestr++;
1469
                    if (*p == '\0') {
1470
                        /* no optional string: NULL argument */
1471
                        str = NULL;
1472
                        goto add_str;
1473
                    }
1474
                }
1475
                ret = get_str(buf, sizeof(buf), &p);
1476
                if (ret < 0) {
1477
                    switch(c) {
1478
                    case 'F':
1479
                        term_printf("%s: filename expected\n", cmdname);
1480
                        break;
1481
                    case 'B':
1482
                        term_printf("%s: block device name expected\n", cmdname);
1483
                        break;
1484
                    default:
1485
                        term_printf("%s: string expected\n", cmdname);
1486
                        break;
1487
                    }
1488
                    goto fail;
1489
                }
1490
                str = qemu_malloc(strlen(buf) + 1);
1491
                strcpy(str, buf);
1492
                str_allocated[nb_args] = str;
1493
            add_str:
1494
                if (nb_args >= MAX_ARGS) {
1495
                error_args:
1496
                    term_printf("%s: too many arguments\n", cmdname);
1497
                    goto fail;
1498
                }
1499
                args[nb_args++] = str;
1500
            }
1501
            break;
1502
        case '/':
1503
            {
1504
                int count, format, size;
1505
                
1506
                while (isspace(*p))
1507
                    p++;
1508
                if (*p == '/') {
1509
                    /* format found */
1510
                    p++;
1511
                    count = 1;
1512
                    if (isdigit(*p)) {
1513
                        count = 0;
1514
                        while (isdigit(*p)) {
1515
                            count = count * 10 + (*p - '0');
1516
                            p++;
1517
                        }
1518
                    }
1519
                    size = -1;
1520
                    format = -1;
1521
                    for(;;) {
1522
                        switch(*p) {
1523
                        case 'o':
1524
                        case 'd':
1525
                        case 'u':
1526
                        case 'x':
1527
                        case 'i':
1528
                        case 'c':
1529
                            format = *p++;
1530
                            break;
1531
                        case 'b':
1532
                            size = 1;
1533
                            p++;
1534
                            break;
1535
                        case 'h':
1536
                            size = 2;
1537
                            p++;
1538
                            break;
1539
                        case 'w':
1540
                            size = 4;
1541
                            p++;
1542
                            break;
1543
                        case 'g':
1544
                        case 'L':
1545
                            size = 8;
1546
                            p++;
1547
                            break;
1548
                        default:
1549
                            goto next;
1550
                        }
1551
                    }
1552
                next:
1553
                    if (*p != '\0' && !isspace(*p)) {
1554
                        term_printf("invalid char in format: '%c'\n", *p);
1555
                        goto fail;
1556
                    }
1557
                    if (format < 0)
1558
                        format = default_fmt_format;
1559
                    if (format != 'i') {
1560
                        /* for 'i', not specifying a size gives -1 as size */
1561
                        if (size < 0)
1562
                            size = default_fmt_size;
1563
                    }
1564
                    default_fmt_size = size;
1565
                    default_fmt_format = format;
1566
                } else {
1567
                    count = 1;
1568
                    format = default_fmt_format;
1569
                    if (format != 'i') {
1570
                        size = default_fmt_size;
1571
                    } else {
1572
                        size = -1;
1573
                    }
1574
                }
1575
                if (nb_args + 3 > MAX_ARGS)
1576
                    goto error_args;
1577
                args[nb_args++] = (void*)count;
1578
                args[nb_args++] = (void*)format;
1579
                args[nb_args++] = (void*)size;
1580
            }
1581
            break;
1582
        case 'i':
1583
            {
1584
                int val;
1585
                while (isspace(*p)) 
1586
                    p++;
1587
                if (*typestr == '?' || *typestr == '.') {
1588
                    typestr++;
1589
                    if (*typestr == '?') {
1590
                        if (*p == '\0')
1591
                            has_arg = 0;
1592
                        else
1593
                            has_arg = 1;
1594
                    } else {
1595
                        if (*p == '.') {
1596
                            p++;
1597
                            while (isspace(*p)) 
1598
                                p++;
1599
                            has_arg = 1;
1600
                        } else {
1601
                            has_arg = 0;
1602
                        }
1603
                    }
1604
                    if (nb_args >= MAX_ARGS)
1605
                        goto error_args;
1606
                    args[nb_args++] = (void *)has_arg;
1607
                    if (!has_arg) {
1608
                        if (nb_args >= MAX_ARGS)
1609
                            goto error_args;
1610
                        val = -1;
1611
                        goto add_num;
1612
                    }
1613
                }
1614
                if (get_expr(&val, &p))
1615
                    goto fail;
1616
            add_num:
1617
                if (nb_args >= MAX_ARGS)
1618
                    goto error_args;
1619
                args[nb_args++] = (void *)val;
1620
            }
1621
            break;
1622
        case '-':
1623
            {
1624
                int has_option;
1625
                /* option */
1626
                
1627
                c = *typestr++;
1628
                if (c == '\0')
1629
                    goto bad_type;
1630
                while (isspace(*p)) 
1631
                    p++;
1632
                has_option = 0;
1633
                if (*p == '-') {
1634
                    p++;
1635
                    if (*p != c) {
1636
                        term_printf("%s: unsupported option -%c\n", 
1637
                                    cmdname, *p);
1638
                        goto fail;
1639
                    }
1640
                    p++;
1641
                    has_option = 1;
1642
                }
1643
                if (nb_args >= MAX_ARGS)
1644
                    goto error_args;
1645
                args[nb_args++] = (void *)has_option;
1646
            }
1647
            break;
1648
        default:
1649
        bad_type:
1650
            term_printf("%s: unknown type '%c'\n", cmdname, c);
1651
            goto fail;
1652
        }
1653
    }
1654
    /* check that all arguments were parsed */
1655
    while (isspace(*p))
1656
        p++;
1657
    if (*p != '\0') {
1658
        term_printf("%s: extraneous characters at the end of line\n", 
1659
                    cmdname);
1660
        goto fail;
1661
    }
1662

    
1663
    switch(nb_args) {
1664
    case 0:
1665
        cmd->handler();
1666
        break;
1667
    case 1:
1668
        cmd->handler(args[0]);
1669
        break;
1670
    case 2:
1671
        cmd->handler(args[0], args[1]);
1672
        break;
1673
    case 3:
1674
        cmd->handler(args[0], args[1], args[2]);
1675
        break;
1676
    case 4:
1677
        cmd->handler(args[0], args[1], args[2], args[3]);
1678
        break;
1679
    case 5:
1680
        cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1681
        break;
1682
    case 6:
1683
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1684
        break;
1685
    default:
1686
        term_printf("unsupported number of arguments: %d\n", nb_args);
1687
        goto fail;
1688
    }
1689
 fail:
1690
    for(i = 0; i < MAX_ARGS; i++)
1691
        qemu_free(str_allocated[i]);
1692
    return;
1693
}
1694

    
1695
static void cmd_completion(const char *name, const char *list)
1696
{
1697
    const char *p, *pstart;
1698
    char cmd[128];
1699
    int len;
1700

    
1701
    p = list;
1702
    for(;;) {
1703
        pstart = p;
1704
        p = strchr(p, '|');
1705
        if (!p)
1706
            p = pstart + strlen(pstart);
1707
        len = p - pstart;
1708
        if (len > sizeof(cmd) - 2)
1709
            len = sizeof(cmd) - 2;
1710
        memcpy(cmd, pstart, len);
1711
        cmd[len] = '\0';
1712
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1713
            add_completion(cmd);
1714
        }
1715
        if (*p == '\0')
1716
            break;
1717
        p++;
1718
    }
1719
}
1720

    
1721
static void file_completion(const char *input)
1722
{
1723
    DIR *ffs;
1724
    struct dirent *d;
1725
    char path[1024];
1726
    char file[1024], file_prefix[1024];
1727
    int input_path_len;
1728
    const char *p;
1729

    
1730
    p = strrchr(input, '/'); 
1731
    if (!p) {
1732
        input_path_len = 0;
1733
        pstrcpy(file_prefix, sizeof(file_prefix), input);
1734
        strcpy(path, ".");
1735
    } else {
1736
        input_path_len = p - input + 1;
1737
        memcpy(path, input, input_path_len);
1738
        if (input_path_len > sizeof(path) - 1)
1739
            input_path_len = sizeof(path) - 1;
1740
        path[input_path_len] = '\0';
1741
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1742
    }
1743
#ifdef DEBUG_COMPLETION
1744
    term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
1745
#endif
1746
    ffs = opendir(path);
1747
    if (!ffs)
1748
        return;
1749
    for(;;) {
1750
        struct stat sb;
1751
        d = readdir(ffs);
1752
        if (!d)
1753
            break;
1754
        if (strstart(d->d_name, file_prefix, NULL)) {
1755
            memcpy(file, input, input_path_len);
1756
            strcpy(file + input_path_len, d->d_name);
1757
            /* stat the file to find out if it's a directory.
1758
             * In that case add a slash to speed up typing long paths
1759
             */
1760
            stat(file, &sb);
1761
            if(S_ISDIR(sb.st_mode))
1762
                strcat(file, "/");
1763
            add_completion(file);
1764
        }
1765
    }
1766
    closedir(ffs);
1767
}
1768

    
1769
static void block_completion_it(void *opaque, const char *name)
1770
{
1771
    const char *input = opaque;
1772

    
1773
    if (input[0] == '\0' ||
1774
        !strncmp(name, (char *)input, strlen(input))) {
1775
        add_completion(name);
1776
    }
1777
}
1778

    
1779
/* NOTE: this parser is an approximate form of the real command parser */
1780
static void parse_cmdline(const char *cmdline,
1781
                         int *pnb_args, char **args)
1782
{
1783
    const char *p;
1784
    int nb_args, ret;
1785
    char buf[1024];
1786

    
1787
    p = cmdline;
1788
    nb_args = 0;
1789
    for(;;) {
1790
        while (isspace(*p))
1791
            p++;
1792
        if (*p == '\0')
1793
            break;
1794
        if (nb_args >= MAX_ARGS)
1795
            break;
1796
        ret = get_str(buf, sizeof(buf), &p);
1797
        args[nb_args] = qemu_strdup(buf);
1798
        nb_args++;
1799
        if (ret < 0)
1800
            break;
1801
    }
1802
    *pnb_args = nb_args;
1803
}
1804

    
1805
void readline_find_completion(const char *cmdline)
1806
{
1807
    const char *cmdname;
1808
    char *args[MAX_ARGS];
1809
    int nb_args, i, len;
1810
    const char *ptype, *str;
1811
    term_cmd_t *cmd;
1812

    
1813
    parse_cmdline(cmdline, &nb_args, args);
1814
#ifdef DEBUG_COMPLETION
1815
    for(i = 0; i < nb_args; i++) {
1816
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
1817
    }
1818
#endif
1819

    
1820
    /* if the line ends with a space, it means we want to complete the
1821
       next arg */
1822
    len = strlen(cmdline);
1823
    if (len > 0 && isspace(cmdline[len - 1])) {
1824
        if (nb_args >= MAX_ARGS)
1825
            return;
1826
        args[nb_args++] = qemu_strdup("");
1827
    }
1828
    if (nb_args <= 1) {
1829
        /* command completion */
1830
        if (nb_args == 0)
1831
            cmdname = "";
1832
        else
1833
            cmdname = args[0];
1834
        completion_index = strlen(cmdname);
1835
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1836
            cmd_completion(cmdname, cmd->name);
1837
        }
1838
    } else {
1839
        /* find the command */
1840
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1841
            if (compare_cmd(args[0], cmd->name))
1842
                goto found;
1843
        }
1844
        return;
1845
    found:
1846
        ptype = cmd->args_type;
1847
        for(i = 0; i < nb_args - 2; i++) {
1848
            if (*ptype != '\0') {
1849
                ptype++;
1850
                while (*ptype == '?')
1851
                    ptype++;
1852
            }
1853
        }
1854
        str = args[nb_args - 1];
1855
        switch(*ptype) {
1856
        case 'F':
1857
            /* file completion */
1858
            completion_index = strlen(str);
1859
            file_completion(str);
1860
            break;
1861
        case 'B':
1862
            /* block device name completion */
1863
            completion_index = strlen(str);
1864
            bdrv_iterate(block_completion_it, (void *)str);
1865
            break;
1866
        case 's':
1867
            /* XXX: more generic ? */
1868
            if (!strcmp(cmd->name, "info")) {
1869
                completion_index = strlen(str);
1870
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
1871
                    cmd_completion(str, cmd->name);
1872
                }
1873
            }
1874
            break;
1875
        default:
1876
            break;
1877
        }
1878
    }
1879
    for(i = 0; i < nb_args; i++)
1880
        qemu_free(args[i]);
1881
}
1882

    
1883
static int term_can_read(void *opaque)
1884
{
1885
    return 128;
1886
}
1887

    
1888
static void term_read(void *opaque, const uint8_t *buf, int size)
1889
{
1890
    int i;
1891
    for(i = 0; i < size; i++)
1892
        readline_handle_byte(buf[i]);
1893
}
1894

    
1895
static void monitor_start_input(void);
1896

    
1897
static void monitor_handle_command1(void *opaque, const char *cmdline)
1898
{
1899
    monitor_handle_command(cmdline);
1900
    monitor_start_input();
1901
}
1902

    
1903
static void monitor_start_input(void)
1904
{
1905
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
1906
}
1907

    
1908
void monitor_init(CharDriverState *hd, int show_banner)
1909
{
1910
    monitor_hd = hd;
1911
    if (show_banner) {
1912
        term_printf("QEMU %s monitor - type 'help' for more information\n",
1913
                    QEMU_VERSION);
1914
    }
1915
    qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
1916
    monitor_start_input();
1917
}
1918

    
1919
/* XXX: use threads ? */
1920
/* modal monitor readline */
1921
static int monitor_readline_started;
1922
static char *monitor_readline_buf;
1923
static int monitor_readline_buf_size;
1924

    
1925
static void monitor_readline_cb(void *opaque, const char *input)
1926
{
1927
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
1928
    monitor_readline_started = 0;
1929
}
1930

    
1931
void monitor_readline(const char *prompt, int is_password,
1932
                      char *buf, int buf_size)
1933
{
1934
    if (is_password) {
1935
        qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
1936
    }
1937
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
1938
    monitor_readline_buf = buf;
1939
    monitor_readline_buf_size = buf_size;
1940
    monitor_readline_started = 1;
1941
    while (monitor_readline_started) {
1942
        main_loop_wait(10);
1943
    }
1944
}