Statistics
| Branch: | Revision:

root / monitor.c @ c27004ec

History | View | Annotate | Download (50.4 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_version(void)
194
{
195
  term_printf("%s\n", QEMU_VERSION);
196
}
197

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

    
220
static void do_info_registers(void)
221
{
222
#ifdef TARGET_I386
223
    cpu_dump_state(cpu_single_env, stdout, monitor_fprintf,
224
                   X86_DUMP_FPU | X86_DUMP_CCOP);
225
#else
226
    cpu_dump_state(cpu_single_env, stdout, monitor_fprintf, 
227
                   0);
228
#endif
229
}
230

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

    
246
static void do_quit(void)
247
{
248
    exit(0);
249
}
250

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

    
269
static void do_eject(int force, const char *filename)
270
{
271
    BlockDriverState *bs;
272

    
273
    bs = bdrv_find(filename);
274
    if (!bs) {
275
        term_printf("device not found\n");
276
        return;
277
    }
278
    eject_device(bs, force);
279
}
280

    
281
static void do_change(const char *device, const char *filename)
282
{
283
    BlockDriverState *bs;
284
    int i;
285
    char password[256];
286

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

    
306
static void do_screen_dump(const char *filename)
307
{
308
    vga_screen_dump(filename);
309
}
310

    
311
static void do_log(const char *items)
312
{
313
    int mask;
314
    
315
    if (!strcmp(items, "none")) {
316
        mask = 0;
317
    } else {
318
        mask = cpu_str_to_log_mask(items);
319
        if (!mask) {
320
            help_cmd("log");
321
            return;
322
        }
323
    }
324
    cpu_set_log(mask);
325
}
326

    
327
static void do_savevm(const char *filename)
328
{
329
    if (qemu_savevm(filename) < 0)
330
        term_printf("I/O error when saving VM to '%s'\n", filename);
331
}
332

    
333
static void do_loadvm(const char *filename)
334
{
335
    if (qemu_loadvm(filename) < 0) 
336
        term_printf("I/O error when loading VM from '%s'\n", filename);
337
}
338

    
339
static void do_stop(void)
340
{
341
    vm_stop(EXCP_INTERRUPT);
342
}
343

    
344
static void do_cont(void)
345
{
346
    vm_start();
347
}
348

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

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

    
389
static void memory_dump(int count, int format, int wsize, 
390
                        target_ulong addr, int is_physical)
391
{
392
    int nb_per_line, l, line_size, i, max_digits, len;
393
    uint8_t buf[16];
394
    uint64_t v;
395

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

    
415
    len = wsize * count;
416
    if (wsize == 1)
417
        line_size = 8;
418
    else
419
        line_size = 16;
420
    nb_per_line = line_size / wsize;
421
    max_digits = 0;
422

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

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

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

    
498
static void do_physical_memory_dump(int count, int format, int size, int addr)
499
{
500
    memory_dump(count, format, size, addr, 1);
501
}
502

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

    
526
typedef struct {
527
    int keycode;
528
    const char *name;
529
} KeyDef;
530

    
531
static const KeyDef key_defs[] = {
532
    { 0x2a, "shift" },
533
    { 0x36, "shift_r" },
534
    
535
    { 0x38, "alt" },
536
    { 0xb8, "alt_r" },
537
    { 0x1d, "ctrl" },
538
    { 0x9d, "ctrl_r" },
539

    
540
    { 0xdd, "menu" },
541

    
542
    { 0x01, "esc" },
543

    
544
    { 0x02, "1" },
545
    { 0x03, "2" },
546
    { 0x04, "3" },
547
    { 0x05, "4" },
548
    { 0x06, "5" },
549
    { 0x07, "6" },
550
    { 0x08, "7" },
551
    { 0x09, "8" },
552
    { 0x0a, "9" },
553
    { 0x0b, "0" },
554
    { 0x0e, "backspace" },
555

    
556
    { 0x0f, "tab" },
557
    { 0x10, "q" },
558
    { 0x11, "w" },
559
    { 0x12, "e" },
560
    { 0x13, "r" },
561
    { 0x14, "t" },
562
    { 0x15, "y" },
563
    { 0x16, "u" },
564
    { 0x17, "i" },
565
    { 0x18, "o" },
566
    { 0x19, "p" },
567

    
568
    { 0x1c, "ret" },
569

    
570
    { 0x1e, "a" },
571
    { 0x1f, "s" },
572
    { 0x20, "d" },
573
    { 0x21, "f" },
574
    { 0x22, "g" },
575
    { 0x23, "h" },
576
    { 0x24, "j" },
577
    { 0x25, "k" },
578
    { 0x26, "l" },
579

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

    
603
    { 0x56, "<" },
604

    
605
    { 0x57, "f11" },
606
    { 0x58, "f12" },
607

    
608
    { 0xb7, "print" },
609

    
610
    { 0xc7, "home" },
611
    { 0xc9, "pgup" },
612
    { 0xd1, "pgdn" },
613
    { 0xcf, "end" },
614

    
615
    { 0xcb, "left" },
616
    { 0xc8, "up" },
617
    { 0xd0, "down" },
618
    { 0xcd, "right" },
619

    
620
    { 0xd2, "insert" },
621
    { 0xd3, "delete" },
622
    { 0, NULL },
623
};
624

    
625
static int get_keycode(const char *key)
626
{
627
    const KeyDef *p;
628

    
629
    for(p = key_defs; p->name != NULL; p++) {
630
        if (!strcmp(key, p->name))
631
            return p->keycode;
632
    }
633
    return -1;
634
}
635

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

    
680
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
681
{
682
    uint32_t val;
683
    int suffix;
684

    
685
    if (has_index) {
686
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
687
        addr++;
688
    }
689
    addr &= 0xffff;
690

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

    
710
static void do_system_reset(void)
711
{
712
    qemu_system_reset_request();
713
}
714

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

    
731
static void tlb_info(void)
732
{
733
    CPUState *env = cpu_single_env;
734
    int l1, l2;
735
    uint32_t pgd, pde, pte;
736

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

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

    
785
static void mem_info(void)
786
{
787
    CPUState *env = cpu_single_env;
788
    int l1, l2, prot, last_prot;
789
    uint32_t pgd, pde, pte, start, end;
790

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

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

    
866
    { "sendkey", "s", do_send_key, 
867
      "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
868
    { "system_reset", "", do_system_reset, 
869
      "", "reset the system" },
870
    { NULL, NULL, }, 
871
};
872

    
873
static term_cmd_t info_cmds[] = {
874
    { "version", "", do_info_version,
875
      "", "show the version of qemu" },
876
    { "network", "", do_info_network,
877
      "", "show the network state" },
878
    { "block", "", do_info_block,
879
      "", "show the block devices" },
880
    { "registers", "", do_info_registers,
881
      "", "show the cpu registers" },
882
    { "history", "", do_info_history,
883
      "", "show the command line history", },
884
    { "irq", "", irq_info,
885
      "", "show the interrupts statistics (if available)", },
886
    { "pic", "", pic_info,
887
      "", "show i8259 (PIC) state", },
888
    { "pci", "", pci_info,
889
      "", "show PCI info", },
890
#if defined(TARGET_I386)
891
    { "tlb", "", tlb_info,
892
      "", "show virtual to physical memory mappings", },
893
    { "mem", "", mem_info,
894
      "", "show the active virtual memory mappings", },
895
#endif
896
    { NULL, NULL, },
897
};
898

    
899
/*******************************************************************/
900

    
901
static const char *pch;
902
static jmp_buf expr_env;
903

    
904
typedef struct MonitorDef {
905
    const char *name;
906
    int offset;
907
    int (*get_value)(struct MonitorDef *md, int val);
908
} MonitorDef;
909

    
910
#if defined(TARGET_I386)
911
static int monitor_get_pc (struct MonitorDef *md, int val)
912
{
913
    return cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base;
914
}
915
#endif
916

    
917
#if defined(TARGET_PPC)
918
static int monitor_get_ccr (struct MonitorDef *md, int val)
919
{
920
    unsigned int u;
921
    int i;
922

    
923
    u = 0;
924
    for (i = 0; i < 8; i++)
925
        u |= cpu_single_env->crf[i] << (32 - (4 * i));
926

    
927
    return u;
928
}
929

    
930
static int monitor_get_msr (struct MonitorDef *md, int val)
931
{
932
    return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
933
        (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
934
        (cpu_single_env->msr[MSR_EE] << MSR_EE) |
935
        (cpu_single_env->msr[MSR_PR] << MSR_PR) |
936
        (cpu_single_env->msr[MSR_FP] << MSR_FP) |
937
        (cpu_single_env->msr[MSR_ME] << MSR_ME) |
938
        (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
939
        (cpu_single_env->msr[MSR_SE] << MSR_SE) |
940
        (cpu_single_env->msr[MSR_BE] << MSR_BE) |
941
        (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
942
        (cpu_single_env->msr[MSR_IP] << MSR_IP) |
943
        (cpu_single_env->msr[MSR_IR] << MSR_IR) |
944
        (cpu_single_env->msr[MSR_DR] << MSR_DR) |
945
        (cpu_single_env->msr[MSR_RI] << MSR_RI) |
946
        (cpu_single_env->msr[MSR_LE] << MSR_LE);
947
}
948

    
949
static int monitor_get_xer (struct MonitorDef *md, int val)
950
{
951
    return (cpu_single_env->xer[XER_SO] << XER_SO) |
952
        (cpu_single_env->xer[XER_OV] << XER_OV) |
953
        (cpu_single_env->xer[XER_CA] << XER_CA) |
954
        (cpu_single_env->xer[XER_BC] << XER_BC);
955
}
956

    
957
static int monitor_get_decr (struct MonitorDef *md, int val)
958
{
959
    return cpu_ppc_load_decr(cpu_single_env);
960
}
961

    
962
static int monitor_get_tbu (struct MonitorDef *md, int val)
963
{
964
    return cpu_ppc_load_tbu(cpu_single_env);
965
}
966

    
967
static int monitor_get_tbl (struct MonitorDef *md, int val)
968
{
969
    return cpu_ppc_load_tbl(cpu_single_env);
970
}
971
#endif
972

    
973
#if defined(TARGET_SPARC)
974
static int monitor_get_psr (struct MonitorDef *md, int val)
975
{
976
    return GET_PSR(cpu_single_env);
977
}
978

    
979
static int monitor_get_reg(struct MonitorDef *md, int val)
980
{
981
    return cpu_single_env->regwptr[val];
982
}
983
#endif
984

    
985
static MonitorDef monitor_defs[] = {
986
#ifdef TARGET_I386
987

    
988
#define SEG(name, seg) \
989
    { name, offsetof(CPUState, segs[seg].selector) },\
990
    { name ".base", offsetof(CPUState, segs[seg].base) },\
991
    { name ".limit", offsetof(CPUState, segs[seg].limit) },
992

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

    
1146
static void expr_error(const char *fmt) 
1147
{
1148
    term_printf(fmt);
1149
    term_printf("\n");
1150
    longjmp(expr_env, 1);
1151
}
1152

    
1153
static int get_monitor_def(int *pval, const char *name)
1154
{
1155
    MonitorDef *md;
1156
    for(md = monitor_defs; md->name != NULL; md++) {
1157
        if (compare_cmd(name, md->name)) {
1158
            if (md->get_value) {
1159
                *pval = md->get_value(md, md->offset);
1160
            } else {
1161
                *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset);
1162
            }
1163
            return 0;
1164
        }
1165
    }
1166
    return -1;
1167
}
1168

    
1169
static void next(void)
1170
{
1171
    if (pch != '\0') {
1172
        pch++;
1173
        while (isspace(*pch))
1174
            pch++;
1175
    }
1176
}
1177

    
1178
static int expr_sum(void);
1179

    
1180
static int expr_unary(void)
1181
{
1182
    int n;
1183
    char *p;
1184

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

    
1254

    
1255
static int expr_prod(void)
1256
{
1257
    int val, val2, op;
1258

    
1259
    val = expr_unary();
1260
    for(;;) {
1261
        op = *pch;
1262
        if (op != '*' && op != '/' && op != '%')
1263
            break;
1264
        next();
1265
        val2 = expr_unary();
1266
        switch(op) {
1267
        default:
1268
        case '*':
1269
            val *= val2;
1270
            break;
1271
        case '/':
1272
        case '%':
1273
            if (val2 == 0) 
1274
                expr_error("division by zero");
1275
            if (op == '/')
1276
                val /= val2;
1277
            else
1278
                val %= val2;
1279
            break;
1280
        }
1281
    }
1282
    return val;
1283
}
1284

    
1285
static int expr_logic(void)
1286
{
1287
    int val, val2, op;
1288

    
1289
    val = expr_prod();
1290
    for(;;) {
1291
        op = *pch;
1292
        if (op != '&' && op != '|' && op != '^')
1293
            break;
1294
        next();
1295
        val2 = expr_prod();
1296
        switch(op) {
1297
        default:
1298
        case '&':
1299
            val &= val2;
1300
            break;
1301
        case '|':
1302
            val |= val2;
1303
            break;
1304
        case '^':
1305
            val ^= val2;
1306
            break;
1307
        }
1308
    }
1309
    return val;
1310
}
1311

    
1312
static int expr_sum(void)
1313
{
1314
    int val, val2, op;
1315

    
1316
    val = expr_logic();
1317
    for(;;) {
1318
        op = *pch;
1319
        if (op != '+' && op != '-')
1320
            break;
1321
        next();
1322
        val2 = expr_logic();
1323
        if (op == '+')
1324
            val += val2;
1325
        else
1326
            val -= val2;
1327
    }
1328
    return val;
1329
}
1330

    
1331
static int get_expr(int *pval, const char **pp)
1332
{
1333
    pch = *pp;
1334
    if (setjmp(expr_env)) {
1335
        *pp = pch;
1336
        return -1;
1337
    }
1338
    while (isspace(*pch))
1339
        pch++;
1340
    *pval = expr_sum();
1341
    *pp = pch;
1342
    return 0;
1343
}
1344

    
1345
static int get_str(char *buf, int buf_size, const char **pp)
1346
{
1347
    const char *p;
1348
    char *q;
1349
    int c;
1350

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

    
1410
static int default_fmt_format = 'x';
1411
static int default_fmt_size = 4;
1412

    
1413
#define MAX_ARGS 16
1414

    
1415
static void monitor_handle_command(const char *cmdline)
1416
{
1417
    const char *p, *pstart, *typestr;
1418
    char *q;
1419
    int c, nb_args, len, i, has_arg;
1420
    term_cmd_t *cmd;
1421
    char cmdname[256];
1422
    char buf[1024];
1423
    void *str_allocated[MAX_ARGS];
1424
    void *args[MAX_ARGS];
1425

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

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

    
1672
    switch(nb_args) {
1673
    case 0:
1674
        cmd->handler();
1675
        break;
1676
    case 1:
1677
        cmd->handler(args[0]);
1678
        break;
1679
    case 2:
1680
        cmd->handler(args[0], args[1]);
1681
        break;
1682
    case 3:
1683
        cmd->handler(args[0], args[1], args[2]);
1684
        break;
1685
    case 4:
1686
        cmd->handler(args[0], args[1], args[2], args[3]);
1687
        break;
1688
    case 5:
1689
        cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1690
        break;
1691
    case 6:
1692
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1693
        break;
1694
    default:
1695
        term_printf("unsupported number of arguments: %d\n", nb_args);
1696
        goto fail;
1697
    }
1698
 fail:
1699
    for(i = 0; i < MAX_ARGS; i++)
1700
        qemu_free(str_allocated[i]);
1701
    return;
1702
}
1703

    
1704
static void cmd_completion(const char *name, const char *list)
1705
{
1706
    const char *p, *pstart;
1707
    char cmd[128];
1708
    int len;
1709

    
1710
    p = list;
1711
    for(;;) {
1712
        pstart = p;
1713
        p = strchr(p, '|');
1714
        if (!p)
1715
            p = pstart + strlen(pstart);
1716
        len = p - pstart;
1717
        if (len > sizeof(cmd) - 2)
1718
            len = sizeof(cmd) - 2;
1719
        memcpy(cmd, pstart, len);
1720
        cmd[len] = '\0';
1721
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1722
            add_completion(cmd);
1723
        }
1724
        if (*p == '\0')
1725
            break;
1726
        p++;
1727
    }
1728
}
1729

    
1730
static void file_completion(const char *input)
1731
{
1732
    DIR *ffs;
1733
    struct dirent *d;
1734
    char path[1024];
1735
    char file[1024], file_prefix[1024];
1736
    int input_path_len;
1737
    const char *p;
1738

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

    
1778
static void block_completion_it(void *opaque, const char *name)
1779
{
1780
    const char *input = opaque;
1781

    
1782
    if (input[0] == '\0' ||
1783
        !strncmp(name, (char *)input, strlen(input))) {
1784
        add_completion(name);
1785
    }
1786
}
1787

    
1788
/* NOTE: this parser is an approximate form of the real command parser */
1789
static void parse_cmdline(const char *cmdline,
1790
                         int *pnb_args, char **args)
1791
{
1792
    const char *p;
1793
    int nb_args, ret;
1794
    char buf[1024];
1795

    
1796
    p = cmdline;
1797
    nb_args = 0;
1798
    for(;;) {
1799
        while (isspace(*p))
1800
            p++;
1801
        if (*p == '\0')
1802
            break;
1803
        if (nb_args >= MAX_ARGS)
1804
            break;
1805
        ret = get_str(buf, sizeof(buf), &p);
1806
        args[nb_args] = qemu_strdup(buf);
1807
        nb_args++;
1808
        if (ret < 0)
1809
            break;
1810
    }
1811
    *pnb_args = nb_args;
1812
}
1813

    
1814
void readline_find_completion(const char *cmdline)
1815
{
1816
    const char *cmdname;
1817
    char *args[MAX_ARGS];
1818
    int nb_args, i, len;
1819
    const char *ptype, *str;
1820
    term_cmd_t *cmd;
1821

    
1822
    parse_cmdline(cmdline, &nb_args, args);
1823
#ifdef DEBUG_COMPLETION
1824
    for(i = 0; i < nb_args; i++) {
1825
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
1826
    }
1827
#endif
1828

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

    
1892
static int term_can_read(void *opaque)
1893
{
1894
    return 128;
1895
}
1896

    
1897
static void term_read(void *opaque, const uint8_t *buf, int size)
1898
{
1899
    int i;
1900
    for(i = 0; i < size; i++)
1901
        readline_handle_byte(buf[i]);
1902
}
1903

    
1904
static void monitor_start_input(void);
1905

    
1906
static void monitor_handle_command1(void *opaque, const char *cmdline)
1907
{
1908
    monitor_handle_command(cmdline);
1909
    monitor_start_input();
1910
}
1911

    
1912
static void monitor_start_input(void)
1913
{
1914
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
1915
}
1916

    
1917
void monitor_init(CharDriverState *hd, int show_banner)
1918
{
1919
    monitor_hd = hd;
1920
    if (show_banner) {
1921
        term_printf("QEMU %s monitor - type 'help' for more information\n",
1922
                    QEMU_VERSION);
1923
    }
1924
    qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
1925
    monitor_start_input();
1926
}
1927

    
1928
/* XXX: use threads ? */
1929
/* modal monitor readline */
1930
static int monitor_readline_started;
1931
static char *monitor_readline_buf;
1932
static int monitor_readline_buf_size;
1933

    
1934
static void monitor_readline_cb(void *opaque, const char *input)
1935
{
1936
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
1937
    monitor_readline_started = 0;
1938
}
1939

    
1940
void monitor_readline(const char *prompt, int is_password,
1941
                      char *buf, int buf_size)
1942
{
1943
    if (is_password) {
1944
        qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
1945
    }
1946
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
1947
    monitor_readline_buf = buf;
1948
    monitor_readline_buf_size = buf_size;
1949
    monitor_readline_started = 1;
1950
    while (monitor_readline_started) {
1951
        main_loop_wait(10);
1952
    }
1953
}