Statistics
| Branch: | Revision:

root / monitor.c @ 7c9d8e07

History | View | Annotate | Download (54.9 kB)

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

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

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

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

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

    
57
static CharDriverState *monitor_hd;
58

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

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

    
65
static void monitor_start_input(void);
66

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
194
static void do_info_version(void)
195
{
196
  term_printf("%s\n", QEMU_VERSION);
197
}
198

    
199
static void do_info_block(void)
200
{
201
    bdrv_info();
202
}
203

    
204
static void do_info_registers(void)
205
{
206
#ifdef TARGET_I386
207
    cpu_dump_state(cpu_single_env, NULL, monitor_fprintf,
208
                   X86_DUMP_FPU);
209
#else
210
    cpu_dump_state(cpu_single_env, NULL, monitor_fprintf, 
211
                   0);
212
#endif
213
}
214

    
215
static void do_info_jit(void)
216
{
217
    dump_exec_info(NULL, monitor_fprintf);
218
}
219

    
220
static void do_info_history (void)
221
{
222
    int i;
223
    const char *str;
224
    
225
    i = 0;
226
    for(;;) {
227
        str = readline_get_history(i);
228
        if (!str)
229
            break;
230
        term_printf("%d: '%s'\n", i, str);
231
        i++;
232
    }
233
}
234

    
235
static void do_quit(void)
236
{
237
#ifdef USE_KQEMU
238
    kqemu_record_dump();
239
#endif
240
    exit(0);
241
}
242

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

    
261
static void do_eject(int force, const char *filename)
262
{
263
    BlockDriverState *bs;
264

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

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

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

    
298
static void do_screen_dump(const char *filename)
299
{
300
    vga_screen_dump(filename);
301
}
302

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

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

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

    
331
static void do_stop(void)
332
{
333
    vm_stop(EXCP_INTERRUPT);
334
}
335

    
336
static void do_cont(void)
337
{
338
    vm_start();
339
}
340

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

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

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

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

    
407
    len = wsize * count;
408
    if (wsize == 1)
409
        line_size = 8;
410
    else
411
        line_size = 16;
412
    nb_per_line = line_size / wsize;
413
    max_digits = 0;
414

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

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

    
485
#if TARGET_LONG_BITS == 64
486
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
487
#else
488
#define GET_TLONG(h, l) (l)
489
#endif
490

    
491
static void do_memory_dump(int count, int format, int size, 
492
                           uint32_t addrh, uint32_t addrl)
493
{
494
    target_long addr = GET_TLONG(addrh, addrl);
495
    memory_dump(count, format, size, addr, 0);
496
}
497

    
498
static void do_physical_memory_dump(int count, int format, int size,
499
                                    uint32_t addrh, uint32_t addrl)
500

    
501
{
502
    target_long addr = GET_TLONG(addrh, addrl);
503
    memory_dump(count, format, size, addr, 1);
504
}
505

    
506
static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
507
{
508
    target_long val = GET_TLONG(valh, vall);
509
#if TARGET_LONG_BITS == 32
510
    switch(format) {
511
    case 'o':
512
        term_printf("%#o", val);
513
        break;
514
    case 'x':
515
        term_printf("%#x", val);
516
        break;
517
    case 'u':
518
        term_printf("%u", val);
519
        break;
520
    default:
521
    case 'd':
522
        term_printf("%d", val);
523
        break;
524
    case 'c':
525
        term_printc(val);
526
        break;
527
    }
528
#else
529
    switch(format) {
530
    case 'o':
531
        term_printf("%#llo", val);
532
        break;
533
    case 'x':
534
        term_printf("%#llx", val);
535
        break;
536
    case 'u':
537
        term_printf("%llu", val);
538
        break;
539
    default:
540
    case 'd':
541
        term_printf("%lld", val);
542
        break;
543
    case 'c':
544
        term_printc(val);
545
        break;
546
    }
547
#endif
548
    term_printf("\n");
549
}
550

    
551
static void do_sum(uint32_t start, uint32_t size)
552
{
553
    uint32_t addr;
554
    uint8_t buf[1];
555
    uint16_t sum;
556

    
557
    sum = 0;
558
    for(addr = start; addr < (start + size); addr++) {
559
        cpu_physical_memory_rw(addr, buf, 1, 0);
560
        /* BSD sum algorithm ('sum' Unix command) */
561
        sum = (sum >> 1) | (sum << 15);
562
        sum += buf[0];
563
    }
564
    term_printf("%05d\n", sum);
565
}
566

    
567
typedef struct {
568
    int keycode;
569
    const char *name;
570
} KeyDef;
571

    
572
static const KeyDef key_defs[] = {
573
    { 0x2a, "shift" },
574
    { 0x36, "shift_r" },
575
    
576
    { 0x38, "alt" },
577
    { 0xb8, "alt_r" },
578
    { 0x1d, "ctrl" },
579
    { 0x9d, "ctrl_r" },
580

    
581
    { 0xdd, "menu" },
582

    
583
    { 0x01, "esc" },
584

    
585
    { 0x02, "1" },
586
    { 0x03, "2" },
587
    { 0x04, "3" },
588
    { 0x05, "4" },
589
    { 0x06, "5" },
590
    { 0x07, "6" },
591
    { 0x08, "7" },
592
    { 0x09, "8" },
593
    { 0x0a, "9" },
594
    { 0x0b, "0" },
595
    { 0x0e, "backspace" },
596

    
597
    { 0x0f, "tab" },
598
    { 0x10, "q" },
599
    { 0x11, "w" },
600
    { 0x12, "e" },
601
    { 0x13, "r" },
602
    { 0x14, "t" },
603
    { 0x15, "y" },
604
    { 0x16, "u" },
605
    { 0x17, "i" },
606
    { 0x18, "o" },
607
    { 0x19, "p" },
608

    
609
    { 0x1c, "ret" },
610

    
611
    { 0x1e, "a" },
612
    { 0x1f, "s" },
613
    { 0x20, "d" },
614
    { 0x21, "f" },
615
    { 0x22, "g" },
616
    { 0x23, "h" },
617
    { 0x24, "j" },
618
    { 0x25, "k" },
619
    { 0x26, "l" },
620

    
621
    { 0x2c, "z" },
622
    { 0x2d, "x" },
623
    { 0x2e, "c" },
624
    { 0x2f, "v" },
625
    { 0x30, "b" },
626
    { 0x31, "n" },
627
    { 0x32, "m" },
628
    
629
    { 0x39, "spc" },
630
    { 0x3a, "caps_lock" },
631
    { 0x3b, "f1" },
632
    { 0x3c, "f2" },
633
    { 0x3d, "f3" },
634
    { 0x3e, "f4" },
635
    { 0x3f, "f5" },
636
    { 0x40, "f6" },
637
    { 0x41, "f7" },
638
    { 0x42, "f8" },
639
    { 0x43, "f9" },
640
    { 0x44, "f10" },
641
    { 0x45, "num_lock" },
642
    { 0x46, "scroll_lock" },
643

    
644
    { 0x56, "<" },
645

    
646
    { 0x57, "f11" },
647
    { 0x58, "f12" },
648

    
649
    { 0xb7, "print" },
650

    
651
    { 0xc7, "home" },
652
    { 0xc9, "pgup" },
653
    { 0xd1, "pgdn" },
654
    { 0xcf, "end" },
655

    
656
    { 0xcb, "left" },
657
    { 0xc8, "up" },
658
    { 0xd0, "down" },
659
    { 0xcd, "right" },
660

    
661
    { 0xd2, "insert" },
662
    { 0xd3, "delete" },
663
    { 0, NULL },
664
};
665

    
666
static int get_keycode(const char *key)
667
{
668
    const KeyDef *p;
669

    
670
    for(p = key_defs; p->name != NULL; p++) {
671
        if (!strcmp(key, p->name))
672
            return p->keycode;
673
    }
674
    return -1;
675
}
676

    
677
static void do_send_key(const char *string)
678
{
679
    char keybuf[16], *q;
680
    uint8_t keycodes[16];
681
    const char *p;
682
    int nb_keycodes, keycode, i;
683
    
684
    nb_keycodes = 0;
685
    p = string;
686
    while (*p != '\0') {
687
        q = keybuf;
688
        while (*p != '\0' && *p != '-') {
689
            if ((q - keybuf) < sizeof(keybuf) - 1) {
690
                *q++ = *p;
691
            }
692
            p++;
693
        }
694
        *q = '\0';
695
        keycode = get_keycode(keybuf);
696
        if (keycode < 0) {
697
            term_printf("unknown key: '%s'\n", keybuf);
698
            return;
699
        }
700
        keycodes[nb_keycodes++] = keycode;
701
        if (*p == '\0')
702
            break;
703
        p++;
704
    }
705
    /* key down events */
706
    for(i = 0; i < nb_keycodes; i++) {
707
        keycode = keycodes[i];
708
        if (keycode & 0x80)
709
            kbd_put_keycode(0xe0);
710
        kbd_put_keycode(keycode & 0x7f);
711
    }
712
    /* key up events */
713
    for(i = nb_keycodes - 1; i >= 0; i--) {
714
        keycode = keycodes[i];
715
        if (keycode & 0x80)
716
            kbd_put_keycode(0xe0);
717
        kbd_put_keycode(keycode | 0x80);
718
    }
719
}
720

    
721
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
722
{
723
    uint32_t val;
724
    int suffix;
725

    
726
    if (has_index) {
727
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
728
        addr++;
729
    }
730
    addr &= 0xffff;
731

    
732
    switch(size) {
733
    default:
734
    case 1:
735
        val = cpu_inb(NULL, addr);
736
        suffix = 'b';
737
        break;
738
    case 2:
739
        val = cpu_inw(NULL, addr);
740
        suffix = 'w';
741
        break;
742
    case 4:
743
        val = cpu_inl(NULL, addr);
744
        suffix = 'l';
745
        break;
746
    }
747
    term_printf("port%c[0x%04x] = %#0*x\n",
748
                suffix, addr, size * 2, val);
749
}
750

    
751
static void do_system_reset(void)
752
{
753
    qemu_system_reset_request();
754
}
755

    
756
static void do_system_powerdown(void)
757
{
758
    qemu_system_powerdown_request();
759
}
760

    
761
#if defined(TARGET_I386)
762
static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
763
{
764
    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n", 
765
                addr,
766
                pte & mask,
767
                pte & PG_GLOBAL_MASK ? 'G' : '-',
768
                pte & PG_PSE_MASK ? 'P' : '-',
769
                pte & PG_DIRTY_MASK ? 'D' : '-',
770
                pte & PG_ACCESSED_MASK ? 'A' : '-',
771
                pte & PG_PCD_MASK ? 'C' : '-',
772
                pte & PG_PWT_MASK ? 'T' : '-',
773
                pte & PG_USER_MASK ? 'U' : '-',
774
                pte & PG_RW_MASK ? 'W' : '-');
775
}
776

    
777
static void tlb_info(void)
778
{
779
    CPUState *env = cpu_single_env;
780
    int l1, l2;
781
    uint32_t pgd, pde, pte;
782

    
783
    if (!(env->cr[0] & CR0_PG_MASK)) {
784
        term_printf("PG disabled\n");
785
        return;
786
    }
787
    pgd = env->cr[3] & ~0xfff;
788
    for(l1 = 0; l1 < 1024; l1++) {
789
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
790
        pde = le32_to_cpu(pde);
791
        if (pde & PG_PRESENT_MASK) {
792
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
793
                print_pte((l1 << 22), pde, ~((1 << 20) - 1));
794
            } else {
795
                for(l2 = 0; l2 < 1024; l2++) {
796
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
797
                                             (uint8_t *)&pte, 4);
798
                    pte = le32_to_cpu(pte);
799
                    if (pte & PG_PRESENT_MASK) {
800
                        print_pte((l1 << 22) + (l2 << 12), 
801
                                  pte & ~PG_PSE_MASK, 
802
                                  ~0xfff);
803
                    }
804
                }
805
            }
806
        }
807
    }
808
}
809

    
810
static void mem_print(uint32_t *pstart, int *plast_prot, 
811
                      uint32_t end, int prot)
812
{
813
    int prot1;
814
    prot1 = *plast_prot;
815
    if (prot != prot1) {
816
        if (*pstart != -1) {
817
            term_printf("%08x-%08x %08x %c%c%c\n",
818
                        *pstart, end, end - *pstart, 
819
                        prot1 & PG_USER_MASK ? 'u' : '-',
820
                        'r',
821
                        prot1 & PG_RW_MASK ? 'w' : '-');
822
        }
823
        if (prot != 0)
824
            *pstart = end;
825
        else
826
            *pstart = -1;
827
        *plast_prot = prot;
828
    }
829
}
830

    
831
static void mem_info(void)
832
{
833
    CPUState *env = cpu_single_env;
834
    int l1, l2, prot, last_prot;
835
    uint32_t pgd, pde, pte, start, end;
836

    
837
    if (!(env->cr[0] & CR0_PG_MASK)) {
838
        term_printf("PG disabled\n");
839
        return;
840
    }
841
    pgd = env->cr[3] & ~0xfff;
842
    last_prot = 0;
843
    start = -1;
844
    for(l1 = 0; l1 < 1024; l1++) {
845
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
846
        pde = le32_to_cpu(pde);
847
        end = l1 << 22;
848
        if (pde & PG_PRESENT_MASK) {
849
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
850
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
851
                mem_print(&start, &last_prot, end, prot);
852
            } else {
853
                for(l2 = 0; l2 < 1024; l2++) {
854
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
855
                                             (uint8_t *)&pte, 4);
856
                    pte = le32_to_cpu(pte);
857
                    end = (l1 << 22) + (l2 << 12);
858
                    if (pte & PG_PRESENT_MASK) {
859
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
860
                    } else {
861
                        prot = 0;
862
                    }
863
                    mem_print(&start, &last_prot, end, prot);
864
                }
865
            }
866
        } else {
867
            prot = 0;
868
            mem_print(&start, &last_prot, end, prot);
869
        }
870
    }
871
}
872
#endif
873

    
874
static void do_info_kqemu(void)
875
{
876
#ifdef USE_KQEMU
877
    int val;
878
    val = 0;
879
    if (cpu_single_env)
880
        val = cpu_single_env->kqemu_enabled;
881
    term_printf("kqemu is %s\n", val ? "enabled" : "disabled");
882
#else
883
    term_printf("kqemu support is not compiled\n");
884
#endif
885
} 
886

    
887
static term_cmd_t term_cmds[] = {
888
    { "help|?", "s?", do_help, 
889
      "[cmd]", "show the help" },
890
    { "commit", "", do_commit, 
891
      "", "commit changes to the disk images (if -snapshot is used)" },
892
    { "info", "s?", do_info,
893
      "subcommand", "show various information about the system state" },
894
    { "q|quit", "", do_quit,
895
      "", "quit the emulator" },
896
    { "eject", "-fB", do_eject,
897
      "[-f] device", "eject a removable media (use -f to force it)" },
898
    { "change", "BF", do_change,
899
      "device filename", "change a removable media" },
900
    { "screendump", "F", do_screen_dump, 
901
      "filename", "save screen into PPM image 'filename'" },
902
    { "log", "s", do_log,
903
      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, 
904
    { "savevm", "F", do_savevm,
905
      "filename", "save the whole virtual machine state to 'filename'" }, 
906
    { "loadvm", "F", do_loadvm,
907
      "filename", "restore the whole virtual machine state from 'filename'" }, 
908
    { "stop", "", do_stop, 
909
      "", "stop emulation", },
910
    { "c|cont", "", do_cont, 
911
      "", "resume emulation", },
912
#ifdef CONFIG_GDBSTUB
913
    { "gdbserver", "i?", do_gdbserver, 
914
      "[port]", "start gdbserver session (default port=1234)", },
915
#endif
916
    { "x", "/l", do_memory_dump, 
917
      "/fmt addr", "virtual memory dump starting at 'addr'", },
918
    { "xp", "/l", do_physical_memory_dump, 
919
      "/fmt addr", "physical memory dump starting at 'addr'", },
920
    { "p|print", "/l", do_print, 
921
      "/fmt expr", "print expression value (use $reg for CPU register access)", },
922
    { "i", "/ii.", do_ioport_read, 
923
      "/fmt addr", "I/O port read" },
924

    
925
    { "sendkey", "s", do_send_key, 
926
      "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
927
    { "system_reset", "", do_system_reset, 
928
      "", "reset the system" },
929
    { "system_powerdown", "", do_system_powerdown, 
930
      "", "send system power down event" },
931
    { "sum", "ii", do_sum, 
932
      "addr size", "compute the checksum of a memory region" },
933
    { "usb_add", "s", do_usb_add,
934
      "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
935
    { "usb_del", "s", do_usb_del,
936
      "device", "remove USB device 'bus.addr'" },
937
    { NULL, NULL, }, 
938
};
939

    
940
static term_cmd_t info_cmds[] = {
941
    { "version", "", do_info_version,
942
      "", "show the version of qemu" },
943
    { "network", "", do_info_network,
944
      "", "show the network state" },
945
    { "block", "", do_info_block,
946
      "", "show the block devices" },
947
    { "registers", "", do_info_registers,
948
      "", "show the cpu registers" },
949
    { "history", "", do_info_history,
950
      "", "show the command line history", },
951
    { "irq", "", irq_info,
952
      "", "show the interrupts statistics (if available)", },
953
    { "pic", "", pic_info,
954
      "", "show i8259 (PIC) state", },
955
    { "pci", "", pci_info,
956
      "", "show PCI info", },
957
#if defined(TARGET_I386)
958
    { "tlb", "", tlb_info,
959
      "", "show virtual to physical memory mappings", },
960
    { "mem", "", mem_info,
961
      "", "show the active virtual memory mappings", },
962
#endif
963
    { "jit", "", do_info_jit,
964
      "", "show dynamic compiler info", },
965
    { "kqemu", "", do_info_kqemu,
966
      "", "show kqemu information", },
967
    { "usb", "", usb_info,
968
      "", "show guest USB devices", },
969
    { "usbhost", "", usb_host_info,
970
      "", "show host USB devices", },
971
    { NULL, NULL, },
972
};
973

    
974
/*******************************************************************/
975

    
976
static const char *pch;
977
static jmp_buf expr_env;
978

    
979
#define MD_TLONG 0
980
#define MD_I32   1
981

    
982
typedef struct MonitorDef {
983
    const char *name;
984
    int offset;
985
    target_long (*get_value)(struct MonitorDef *md, int val);
986
    int type;
987
} MonitorDef;
988

    
989
#if defined(TARGET_I386)
990
static target_long monitor_get_pc (struct MonitorDef *md, int val)
991
{
992
    return cpu_single_env->eip + cpu_single_env->segs[R_CS].base;
993
}
994
#endif
995

    
996
#if defined(TARGET_PPC)
997
static target_long monitor_get_ccr (struct MonitorDef *md, int val)
998
{
999
    unsigned int u;
1000
    int i;
1001

    
1002
    u = 0;
1003
    for (i = 0; i < 8; i++)
1004
        u |= cpu_single_env->crf[i] << (32 - (4 * i));
1005

    
1006
    return u;
1007
}
1008

    
1009
static target_long monitor_get_msr (struct MonitorDef *md, int val)
1010
{
1011
    return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
1012
        (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
1013
        (cpu_single_env->msr[MSR_EE] << MSR_EE) |
1014
        (cpu_single_env->msr[MSR_PR] << MSR_PR) |
1015
        (cpu_single_env->msr[MSR_FP] << MSR_FP) |
1016
        (cpu_single_env->msr[MSR_ME] << MSR_ME) |
1017
        (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
1018
        (cpu_single_env->msr[MSR_SE] << MSR_SE) |
1019
        (cpu_single_env->msr[MSR_BE] << MSR_BE) |
1020
        (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
1021
        (cpu_single_env->msr[MSR_IP] << MSR_IP) |
1022
        (cpu_single_env->msr[MSR_IR] << MSR_IR) |
1023
        (cpu_single_env->msr[MSR_DR] << MSR_DR) |
1024
        (cpu_single_env->msr[MSR_RI] << MSR_RI) |
1025
        (cpu_single_env->msr[MSR_LE] << MSR_LE);
1026
}
1027

    
1028
static target_long monitor_get_xer (struct MonitorDef *md, int val)
1029
{
1030
    return (cpu_single_env->xer[XER_SO] << XER_SO) |
1031
        (cpu_single_env->xer[XER_OV] << XER_OV) |
1032
        (cpu_single_env->xer[XER_CA] << XER_CA) |
1033
        (cpu_single_env->xer[XER_BC] << XER_BC);
1034
}
1035

    
1036
static target_long monitor_get_decr (struct MonitorDef *md, int val)
1037
{
1038
    return cpu_ppc_load_decr(cpu_single_env);
1039
}
1040

    
1041
static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1042
{
1043
    return cpu_ppc_load_tbu(cpu_single_env);
1044
}
1045

    
1046
static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1047
{
1048
    return cpu_ppc_load_tbl(cpu_single_env);
1049
}
1050
#endif
1051

    
1052
#if defined(TARGET_SPARC)
1053
#ifndef TARGET_SPARC64
1054
static target_long monitor_get_psr (struct MonitorDef *md, int val)
1055
{
1056
    return GET_PSR(cpu_single_env);
1057
}
1058
#endif
1059

    
1060
static target_long monitor_get_reg(struct MonitorDef *md, int val)
1061
{
1062
    return cpu_single_env->regwptr[val];
1063
}
1064
#endif
1065

    
1066
static MonitorDef monitor_defs[] = {
1067
#ifdef TARGET_I386
1068

    
1069
#define SEG(name, seg) \
1070
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1071
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1072
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1073

    
1074
    { "eax", offsetof(CPUState, regs[0]) },
1075
    { "ecx", offsetof(CPUState, regs[1]) },
1076
    { "edx", offsetof(CPUState, regs[2]) },
1077
    { "ebx", offsetof(CPUState, regs[3]) },
1078
    { "esp|sp", offsetof(CPUState, regs[4]) },
1079
    { "ebp|fp", offsetof(CPUState, regs[5]) },
1080
    { "esi", offsetof(CPUState, regs[6]) },
1081
    { "edi", offsetof(CPUState, regs[7]) },
1082
#ifdef TARGET_X86_64
1083
    { "r8", offsetof(CPUState, regs[8]) },
1084
    { "r9", offsetof(CPUState, regs[9]) },
1085
    { "r10", offsetof(CPUState, regs[10]) },
1086
    { "r11", offsetof(CPUState, regs[11]) },
1087
    { "r12", offsetof(CPUState, regs[12]) },
1088
    { "r13", offsetof(CPUState, regs[13]) },
1089
    { "r14", offsetof(CPUState, regs[14]) },
1090
    { "r15", offsetof(CPUState, regs[15]) },
1091
#endif
1092
    { "eflags", offsetof(CPUState, eflags) },
1093
    { "eip", offsetof(CPUState, eip) },
1094
    SEG("cs", R_CS)
1095
    SEG("ds", R_DS)
1096
    SEG("es", R_ES)
1097
    SEG("ss", R_SS)
1098
    SEG("fs", R_FS)
1099
    SEG("gs", R_GS)
1100
    { "pc", 0, monitor_get_pc, },
1101
#elif defined(TARGET_PPC)
1102
    { "r0", offsetof(CPUState, gpr[0]) },
1103
    { "r1", offsetof(CPUState, gpr[1]) },
1104
    { "r2", offsetof(CPUState, gpr[2]) },
1105
    { "r3", offsetof(CPUState, gpr[3]) },
1106
    { "r4", offsetof(CPUState, gpr[4]) },
1107
    { "r5", offsetof(CPUState, gpr[5]) },
1108
    { "r6", offsetof(CPUState, gpr[6]) },
1109
    { "r7", offsetof(CPUState, gpr[7]) },
1110
    { "r8", offsetof(CPUState, gpr[8]) },
1111
    { "r9", offsetof(CPUState, gpr[9]) },
1112
    { "r10", offsetof(CPUState, gpr[10]) },
1113
    { "r11", offsetof(CPUState, gpr[11]) },
1114
    { "r12", offsetof(CPUState, gpr[12]) },
1115
    { "r13", offsetof(CPUState, gpr[13]) },
1116
    { "r14", offsetof(CPUState, gpr[14]) },
1117
    { "r15", offsetof(CPUState, gpr[15]) },
1118
    { "r16", offsetof(CPUState, gpr[16]) },
1119
    { "r17", offsetof(CPUState, gpr[17]) },
1120
    { "r18", offsetof(CPUState, gpr[18]) },
1121
    { "r19", offsetof(CPUState, gpr[19]) },
1122
    { "r20", offsetof(CPUState, gpr[20]) },
1123
    { "r21", offsetof(CPUState, gpr[21]) },
1124
    { "r22", offsetof(CPUState, gpr[22]) },
1125
    { "r23", offsetof(CPUState, gpr[23]) },
1126
    { "r24", offsetof(CPUState, gpr[24]) },
1127
    { "r25", offsetof(CPUState, gpr[25]) },
1128
    { "r26", offsetof(CPUState, gpr[26]) },
1129
    { "r27", offsetof(CPUState, gpr[27]) },
1130
    { "r28", offsetof(CPUState, gpr[28]) },
1131
    { "r29", offsetof(CPUState, gpr[29]) },
1132
    { "r30", offsetof(CPUState, gpr[30]) },
1133
    { "r31", offsetof(CPUState, gpr[31]) },
1134
    { "nip|pc", offsetof(CPUState, nip) },
1135
    { "lr", offsetof(CPUState, lr) },
1136
    { "ctr", offsetof(CPUState, ctr) },
1137
    { "decr", 0, &monitor_get_decr, },
1138
    { "ccr", 0, &monitor_get_ccr, },
1139
    { "msr", 0, &monitor_get_msr, },
1140
    { "xer", 0, &monitor_get_xer, },
1141
    { "tbu", 0, &monitor_get_tbu, },
1142
    { "tbl", 0, &monitor_get_tbl, },
1143
    { "sdr1", offsetof(CPUState, sdr1) },
1144
    { "sr0", offsetof(CPUState, sr[0]) },
1145
    { "sr1", offsetof(CPUState, sr[1]) },
1146
    { "sr2", offsetof(CPUState, sr[2]) },
1147
    { "sr3", offsetof(CPUState, sr[3]) },
1148
    { "sr4", offsetof(CPUState, sr[4]) },
1149
    { "sr5", offsetof(CPUState, sr[5]) },
1150
    { "sr6", offsetof(CPUState, sr[6]) },
1151
    { "sr7", offsetof(CPUState, sr[7]) },
1152
    { "sr8", offsetof(CPUState, sr[8]) },
1153
    { "sr9", offsetof(CPUState, sr[9]) },
1154
    { "sr10", offsetof(CPUState, sr[10]) },
1155
    { "sr11", offsetof(CPUState, sr[11]) },
1156
    { "sr12", offsetof(CPUState, sr[12]) },
1157
    { "sr13", offsetof(CPUState, sr[13]) },
1158
    { "sr14", offsetof(CPUState, sr[14]) },
1159
    { "sr15", offsetof(CPUState, sr[15]) },
1160
    /* Too lazy to put BATs and SPRs ... */
1161
#elif defined(TARGET_SPARC)
1162
    { "g0", offsetof(CPUState, gregs[0]) },
1163
    { "g1", offsetof(CPUState, gregs[1]) },
1164
    { "g2", offsetof(CPUState, gregs[2]) },
1165
    { "g3", offsetof(CPUState, gregs[3]) },
1166
    { "g4", offsetof(CPUState, gregs[4]) },
1167
    { "g5", offsetof(CPUState, gregs[5]) },
1168
    { "g6", offsetof(CPUState, gregs[6]) },
1169
    { "g7", offsetof(CPUState, gregs[7]) },
1170
    { "o0", 0, monitor_get_reg },
1171
    { "o1", 1, monitor_get_reg },
1172
    { "o2", 2, monitor_get_reg },
1173
    { "o3", 3, monitor_get_reg },
1174
    { "o4", 4, monitor_get_reg },
1175
    { "o5", 5, monitor_get_reg },
1176
    { "o6", 6, monitor_get_reg },
1177
    { "o7", 7, monitor_get_reg },
1178
    { "l0", 8, monitor_get_reg },
1179
    { "l1", 9, monitor_get_reg },
1180
    { "l2", 10, monitor_get_reg },
1181
    { "l3", 11, monitor_get_reg },
1182
    { "l4", 12, monitor_get_reg },
1183
    { "l5", 13, monitor_get_reg },
1184
    { "l6", 14, monitor_get_reg },
1185
    { "l7", 15, monitor_get_reg },
1186
    { "i0", 16, monitor_get_reg },
1187
    { "i1", 17, monitor_get_reg },
1188
    { "i2", 18, monitor_get_reg },
1189
    { "i3", 19, monitor_get_reg },
1190
    { "i4", 20, monitor_get_reg },
1191
    { "i5", 21, monitor_get_reg },
1192
    { "i6", 22, monitor_get_reg },
1193
    { "i7", 23, monitor_get_reg },
1194
    { "pc", offsetof(CPUState, pc) },
1195
    { "npc", offsetof(CPUState, npc) },
1196
    { "y", offsetof(CPUState, y) },
1197
#ifndef TARGET_SPARC64
1198
    { "psr", 0, &monitor_get_psr, },
1199
    { "wim", offsetof(CPUState, wim) },
1200
#endif
1201
    { "tbr", offsetof(CPUState, tbr) },
1202
    { "fsr", offsetof(CPUState, fsr) },
1203
    { "f0", offsetof(CPUState, fpr[0]) },
1204
    { "f1", offsetof(CPUState, fpr[1]) },
1205
    { "f2", offsetof(CPUState, fpr[2]) },
1206
    { "f3", offsetof(CPUState, fpr[3]) },
1207
    { "f4", offsetof(CPUState, fpr[4]) },
1208
    { "f5", offsetof(CPUState, fpr[5]) },
1209
    { "f6", offsetof(CPUState, fpr[6]) },
1210
    { "f7", offsetof(CPUState, fpr[7]) },
1211
    { "f8", offsetof(CPUState, fpr[8]) },
1212
    { "f9", offsetof(CPUState, fpr[9]) },
1213
    { "f10", offsetof(CPUState, fpr[10]) },
1214
    { "f11", offsetof(CPUState, fpr[11]) },
1215
    { "f12", offsetof(CPUState, fpr[12]) },
1216
    { "f13", offsetof(CPUState, fpr[13]) },
1217
    { "f14", offsetof(CPUState, fpr[14]) },
1218
    { "f15", offsetof(CPUState, fpr[15]) },
1219
    { "f16", offsetof(CPUState, fpr[16]) },
1220
    { "f17", offsetof(CPUState, fpr[17]) },
1221
    { "f18", offsetof(CPUState, fpr[18]) },
1222
    { "f19", offsetof(CPUState, fpr[19]) },
1223
    { "f20", offsetof(CPUState, fpr[20]) },
1224
    { "f21", offsetof(CPUState, fpr[21]) },
1225
    { "f22", offsetof(CPUState, fpr[22]) },
1226
    { "f23", offsetof(CPUState, fpr[23]) },
1227
    { "f24", offsetof(CPUState, fpr[24]) },
1228
    { "f25", offsetof(CPUState, fpr[25]) },
1229
    { "f26", offsetof(CPUState, fpr[26]) },
1230
    { "f27", offsetof(CPUState, fpr[27]) },
1231
    { "f28", offsetof(CPUState, fpr[28]) },
1232
    { "f29", offsetof(CPUState, fpr[29]) },
1233
    { "f30", offsetof(CPUState, fpr[30]) },
1234
    { "f31", offsetof(CPUState, fpr[31]) },
1235
#ifdef TARGET_SPARC64
1236
    { "f32", offsetof(CPUState, fpr[32]) },
1237
    { "f34", offsetof(CPUState, fpr[34]) },
1238
    { "f36", offsetof(CPUState, fpr[36]) },
1239
    { "f38", offsetof(CPUState, fpr[38]) },
1240
    { "f40", offsetof(CPUState, fpr[40]) },
1241
    { "f42", offsetof(CPUState, fpr[42]) },
1242
    { "f44", offsetof(CPUState, fpr[44]) },
1243
    { "f46", offsetof(CPUState, fpr[46]) },
1244
    { "f48", offsetof(CPUState, fpr[48]) },
1245
    { "f50", offsetof(CPUState, fpr[50]) },
1246
    { "f52", offsetof(CPUState, fpr[52]) },
1247
    { "f54", offsetof(CPUState, fpr[54]) },
1248
    { "f56", offsetof(CPUState, fpr[56]) },
1249
    { "f58", offsetof(CPUState, fpr[58]) },
1250
    { "f60", offsetof(CPUState, fpr[60]) },
1251
    { "f62", offsetof(CPUState, fpr[62]) },
1252
    { "asi", offsetof(CPUState, asi) },
1253
    { "pstate", offsetof(CPUState, pstate) },
1254
    { "cansave", offsetof(CPUState, cansave) },
1255
    { "canrestore", offsetof(CPUState, canrestore) },
1256
    { "otherwin", offsetof(CPUState, otherwin) },
1257
    { "wstate", offsetof(CPUState, wstate) },
1258
    { "cleanwin", offsetof(CPUState, cleanwin) },
1259
    { "fprs", offsetof(CPUState, fprs) },
1260
#endif
1261
#endif
1262
    { NULL },
1263
};
1264

    
1265
static void expr_error(const char *fmt) 
1266
{
1267
    term_printf(fmt);
1268
    term_printf("\n");
1269
    longjmp(expr_env, 1);
1270
}
1271

    
1272
static int get_monitor_def(target_long *pval, const char *name)
1273
{
1274
    MonitorDef *md;
1275
    void *ptr;
1276

    
1277
    for(md = monitor_defs; md->name != NULL; md++) {
1278
        if (compare_cmd(name, md->name)) {
1279
            if (md->get_value) {
1280
                *pval = md->get_value(md, md->offset);
1281
            } else {
1282
                ptr = (uint8_t *)cpu_single_env + md->offset;
1283
                switch(md->type) {
1284
                case MD_I32:
1285
                    *pval = *(int32_t *)ptr;
1286
                    break;
1287
                case MD_TLONG:
1288
                    *pval = *(target_long *)ptr;
1289
                    break;
1290
                default:
1291
                    *pval = 0;
1292
                    break;
1293
                }
1294
            }
1295
            return 0;
1296
        }
1297
    }
1298
    return -1;
1299
}
1300

    
1301
static void next(void)
1302
{
1303
    if (pch != '\0') {
1304
        pch++;
1305
        while (isspace(*pch))
1306
            pch++;
1307
    }
1308
}
1309

    
1310
static target_long expr_sum(void);
1311

    
1312
static target_long expr_unary(void)
1313
{
1314
    target_long n;
1315
    char *p;
1316

    
1317
    switch(*pch) {
1318
    case '+':
1319
        next();
1320
        n = expr_unary();
1321
        break;
1322
    case '-':
1323
        next();
1324
        n = -expr_unary();
1325
        break;
1326
    case '~':
1327
        next();
1328
        n = ~expr_unary();
1329
        break;
1330
    case '(':
1331
        next();
1332
        n = expr_sum();
1333
        if (*pch != ')') {
1334
            expr_error("')' expected");
1335
        }
1336
        next();
1337
        break;
1338
    case '\'':
1339
        pch++;
1340
        if (*pch == '\0')
1341
            expr_error("character constant expected");
1342
        n = *pch;
1343
        pch++;
1344
        if (*pch != '\'')
1345
            expr_error("missing terminating \' character");
1346
        next();
1347
        break;
1348
    case '$':
1349
        {
1350
            char buf[128], *q;
1351
            
1352
            pch++;
1353
            q = buf;
1354
            while ((*pch >= 'a' && *pch <= 'z') ||
1355
                   (*pch >= 'A' && *pch <= 'Z') ||
1356
                   (*pch >= '0' && *pch <= '9') ||
1357
                   *pch == '_' || *pch == '.') {
1358
                if ((q - buf) < sizeof(buf) - 1)
1359
                    *q++ = *pch;
1360
                pch++;
1361
            }
1362
            while (isspace(*pch))
1363
                pch++;
1364
            *q = 0;
1365
            if (get_monitor_def(&n, buf))
1366
                expr_error("unknown register");
1367
        }
1368
        break;
1369
    case '\0':
1370
        expr_error("unexpected end of expression");
1371
        n = 0;
1372
        break;
1373
    default:
1374
        n = strtoul(pch, &p, 0);
1375
        if (pch == p) {
1376
            expr_error("invalid char in expression");
1377
        }
1378
        pch = p;
1379
        while (isspace(*pch))
1380
            pch++;
1381
        break;
1382
    }
1383
    return n;
1384
}
1385

    
1386

    
1387
static target_long expr_prod(void)
1388
{
1389
    target_long val, val2;
1390
    int op;
1391
    
1392
    val = expr_unary();
1393
    for(;;) {
1394
        op = *pch;
1395
        if (op != '*' && op != '/' && op != '%')
1396
            break;
1397
        next();
1398
        val2 = expr_unary();
1399
        switch(op) {
1400
        default:
1401
        case '*':
1402
            val *= val2;
1403
            break;
1404
        case '/':
1405
        case '%':
1406
            if (val2 == 0) 
1407
                expr_error("division by zero");
1408
            if (op == '/')
1409
                val /= val2;
1410
            else
1411
                val %= val2;
1412
            break;
1413
        }
1414
    }
1415
    return val;
1416
}
1417

    
1418
static target_long expr_logic(void)
1419
{
1420
    target_long val, val2;
1421
    int op;
1422

    
1423
    val = expr_prod();
1424
    for(;;) {
1425
        op = *pch;
1426
        if (op != '&' && op != '|' && op != '^')
1427
            break;
1428
        next();
1429
        val2 = expr_prod();
1430
        switch(op) {
1431
        default:
1432
        case '&':
1433
            val &= val2;
1434
            break;
1435
        case '|':
1436
            val |= val2;
1437
            break;
1438
        case '^':
1439
            val ^= val2;
1440
            break;
1441
        }
1442
    }
1443
    return val;
1444
}
1445

    
1446
static target_long expr_sum(void)
1447
{
1448
    target_long val, val2;
1449
    int op;
1450

    
1451
    val = expr_logic();
1452
    for(;;) {
1453
        op = *pch;
1454
        if (op != '+' && op != '-')
1455
            break;
1456
        next();
1457
        val2 = expr_logic();
1458
        if (op == '+')
1459
            val += val2;
1460
        else
1461
            val -= val2;
1462
    }
1463
    return val;
1464
}
1465

    
1466
static int get_expr(target_long *pval, const char **pp)
1467
{
1468
    pch = *pp;
1469
    if (setjmp(expr_env)) {
1470
        *pp = pch;
1471
        return -1;
1472
    }
1473
    while (isspace(*pch))
1474
        pch++;
1475
    *pval = expr_sum();
1476
    *pp = pch;
1477
    return 0;
1478
}
1479

    
1480
static int get_str(char *buf, int buf_size, const char **pp)
1481
{
1482
    const char *p;
1483
    char *q;
1484
    int c;
1485

    
1486
    q = buf;
1487
    p = *pp;
1488
    while (isspace(*p))
1489
        p++;
1490
    if (*p == '\0') {
1491
    fail:
1492
        *q = '\0';
1493
        *pp = p;
1494
        return -1;
1495
    }
1496
    if (*p == '\"') {
1497
        p++;
1498
        while (*p != '\0' && *p != '\"') {
1499
            if (*p == '\\') {
1500
                p++;
1501
                c = *p++;
1502
                switch(c) {
1503
                case 'n':
1504
                    c = '\n';
1505
                    break;
1506
                case 'r':
1507
                    c = '\r';
1508
                    break;
1509
                case '\\':
1510
                case '\'':
1511
                case '\"':
1512
                    break;
1513
                default:
1514
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
1515
                    goto fail;
1516
                }
1517
                if ((q - buf) < buf_size - 1) {
1518
                    *q++ = c;
1519
                }
1520
            } else {
1521
                if ((q - buf) < buf_size - 1) {
1522
                    *q++ = *p;
1523
                }
1524
                p++;
1525
            }
1526
        }
1527
        if (*p != '\"') {
1528
            qemu_printf("unterminated string\n");
1529
            goto fail;
1530
        }
1531
        p++;
1532
    } else {
1533
        while (*p != '\0' && !isspace(*p)) {
1534
            if ((q - buf) < buf_size - 1) {
1535
                *q++ = *p;
1536
            }
1537
            p++;
1538
        }
1539
    }
1540
    *q = '\0';
1541
    *pp = p;
1542
    return 0;
1543
}
1544

    
1545
static int default_fmt_format = 'x';
1546
static int default_fmt_size = 4;
1547

    
1548
#define MAX_ARGS 16
1549

    
1550
static void monitor_handle_command(const char *cmdline)
1551
{
1552
    const char *p, *pstart, *typestr;
1553
    char *q;
1554
    int c, nb_args, len, i, has_arg;
1555
    term_cmd_t *cmd;
1556
    char cmdname[256];
1557
    char buf[1024];
1558
    void *str_allocated[MAX_ARGS];
1559
    void *args[MAX_ARGS];
1560

    
1561
#ifdef DEBUG
1562
    term_printf("command='%s'\n", cmdline);
1563
#endif
1564
    
1565
    /* extract the command name */
1566
    p = cmdline;
1567
    q = cmdname;
1568
    while (isspace(*p))
1569
        p++;
1570
    if (*p == '\0')
1571
        return;
1572
    pstart = p;
1573
    while (*p != '\0' && *p != '/' && !isspace(*p))
1574
        p++;
1575
    len = p - pstart;
1576
    if (len > sizeof(cmdname) - 1)
1577
        len = sizeof(cmdname) - 1;
1578
    memcpy(cmdname, pstart, len);
1579
    cmdname[len] = '\0';
1580
    
1581
    /* find the command */
1582
    for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1583
        if (compare_cmd(cmdname, cmd->name)) 
1584
            goto found;
1585
    }
1586
    term_printf("unknown command: '%s'\n", cmdname);
1587
    return;
1588
 found:
1589

    
1590
    for(i = 0; i < MAX_ARGS; i++)
1591
        str_allocated[i] = NULL;
1592
    
1593
    /* parse the parameters */
1594
    typestr = cmd->args_type;
1595
    nb_args = 0;
1596
    for(;;) {
1597
        c = *typestr;
1598
        if (c == '\0')
1599
            break;
1600
        typestr++;
1601
        switch(c) {
1602
        case 'F':
1603
        case 'B':
1604
        case 's':
1605
            {
1606
                int ret;
1607
                char *str;
1608
                
1609
                while (isspace(*p)) 
1610
                    p++;
1611
                if (*typestr == '?') {
1612
                    typestr++;
1613
                    if (*p == '\0') {
1614
                        /* no optional string: NULL argument */
1615
                        str = NULL;
1616
                        goto add_str;
1617
                    }
1618
                }
1619
                ret = get_str(buf, sizeof(buf), &p);
1620
                if (ret < 0) {
1621
                    switch(c) {
1622
                    case 'F':
1623
                        term_printf("%s: filename expected\n", cmdname);
1624
                        break;
1625
                    case 'B':
1626
                        term_printf("%s: block device name expected\n", cmdname);
1627
                        break;
1628
                    default:
1629
                        term_printf("%s: string expected\n", cmdname);
1630
                        break;
1631
                    }
1632
                    goto fail;
1633
                }
1634
                str = qemu_malloc(strlen(buf) + 1);
1635
                strcpy(str, buf);
1636
                str_allocated[nb_args] = str;
1637
            add_str:
1638
                if (nb_args >= MAX_ARGS) {
1639
                error_args:
1640
                    term_printf("%s: too many arguments\n", cmdname);
1641
                    goto fail;
1642
                }
1643
                args[nb_args++] = str;
1644
            }
1645
            break;
1646
        case '/':
1647
            {
1648
                int count, format, size;
1649
                
1650
                while (isspace(*p))
1651
                    p++;
1652
                if (*p == '/') {
1653
                    /* format found */
1654
                    p++;
1655
                    count = 1;
1656
                    if (isdigit(*p)) {
1657
                        count = 0;
1658
                        while (isdigit(*p)) {
1659
                            count = count * 10 + (*p - '0');
1660
                            p++;
1661
                        }
1662
                    }
1663
                    size = -1;
1664
                    format = -1;
1665
                    for(;;) {
1666
                        switch(*p) {
1667
                        case 'o':
1668
                        case 'd':
1669
                        case 'u':
1670
                        case 'x':
1671
                        case 'i':
1672
                        case 'c':
1673
                            format = *p++;
1674
                            break;
1675
                        case 'b':
1676
                            size = 1;
1677
                            p++;
1678
                            break;
1679
                        case 'h':
1680
                            size = 2;
1681
                            p++;
1682
                            break;
1683
                        case 'w':
1684
                            size = 4;
1685
                            p++;
1686
                            break;
1687
                        case 'g':
1688
                        case 'L':
1689
                            size = 8;
1690
                            p++;
1691
                            break;
1692
                        default:
1693
                            goto next;
1694
                        }
1695
                    }
1696
                next:
1697
                    if (*p != '\0' && !isspace(*p)) {
1698
                        term_printf("invalid char in format: '%c'\n", *p);
1699
                        goto fail;
1700
                    }
1701
                    if (format < 0)
1702
                        format = default_fmt_format;
1703
                    if (format != 'i') {
1704
                        /* for 'i', not specifying a size gives -1 as size */
1705
                        if (size < 0)
1706
                            size = default_fmt_size;
1707
                    }
1708
                    default_fmt_size = size;
1709
                    default_fmt_format = format;
1710
                } else {
1711
                    count = 1;
1712
                    format = default_fmt_format;
1713
                    if (format != 'i') {
1714
                        size = default_fmt_size;
1715
                    } else {
1716
                        size = -1;
1717
                    }
1718
                }
1719
                if (nb_args + 3 > MAX_ARGS)
1720
                    goto error_args;
1721
                args[nb_args++] = (void*)count;
1722
                args[nb_args++] = (void*)format;
1723
                args[nb_args++] = (void*)size;
1724
            }
1725
            break;
1726
        case 'i':
1727
        case 'l':
1728
            {
1729
                target_long val;
1730
                while (isspace(*p)) 
1731
                    p++;
1732
                if (*typestr == '?' || *typestr == '.') {
1733
                    typestr++;
1734
                    if (*typestr == '?') {
1735
                        if (*p == '\0')
1736
                            has_arg = 0;
1737
                        else
1738
                            has_arg = 1;
1739
                    } else {
1740
                        if (*p == '.') {
1741
                            p++;
1742
                            while (isspace(*p)) 
1743
                                p++;
1744
                            has_arg = 1;
1745
                        } else {
1746
                            has_arg = 0;
1747
                        }
1748
                    }
1749
                    if (nb_args >= MAX_ARGS)
1750
                        goto error_args;
1751
                    args[nb_args++] = (void *)has_arg;
1752
                    if (!has_arg) {
1753
                        if (nb_args >= MAX_ARGS)
1754
                            goto error_args;
1755
                        val = -1;
1756
                        goto add_num;
1757
                    }
1758
                }
1759
                if (get_expr(&val, &p))
1760
                    goto fail;
1761
            add_num:
1762
                if (c == 'i') {
1763
                    if (nb_args >= MAX_ARGS)
1764
                        goto error_args;
1765
                    args[nb_args++] = (void *)(int)val;
1766
                } else {
1767
                    if ((nb_args + 1) >= MAX_ARGS)
1768
                        goto error_args;
1769
#if TARGET_LONG_BITS == 64
1770
                    args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
1771
#else
1772
                    args[nb_args++] = (void *)0;
1773
#endif
1774
                    args[nb_args++] = (void *)(int)(val & 0xffffffff);
1775
                }
1776
            }
1777
            break;
1778
        case '-':
1779
            {
1780
                int has_option;
1781
                /* option */
1782
                
1783
                c = *typestr++;
1784
                if (c == '\0')
1785
                    goto bad_type;
1786
                while (isspace(*p)) 
1787
                    p++;
1788
                has_option = 0;
1789
                if (*p == '-') {
1790
                    p++;
1791
                    if (*p != c) {
1792
                        term_printf("%s: unsupported option -%c\n", 
1793
                                    cmdname, *p);
1794
                        goto fail;
1795
                    }
1796
                    p++;
1797
                    has_option = 1;
1798
                }
1799
                if (nb_args >= MAX_ARGS)
1800
                    goto error_args;
1801
                args[nb_args++] = (void *)has_option;
1802
            }
1803
            break;
1804
        default:
1805
        bad_type:
1806
            term_printf("%s: unknown type '%c'\n", cmdname, c);
1807
            goto fail;
1808
        }
1809
    }
1810
    /* check that all arguments were parsed */
1811
    while (isspace(*p))
1812
        p++;
1813
    if (*p != '\0') {
1814
        term_printf("%s: extraneous characters at the end of line\n", 
1815
                    cmdname);
1816
        goto fail;
1817
    }
1818

    
1819
    switch(nb_args) {
1820
    case 0:
1821
        cmd->handler();
1822
        break;
1823
    case 1:
1824
        cmd->handler(args[0]);
1825
        break;
1826
    case 2:
1827
        cmd->handler(args[0], args[1]);
1828
        break;
1829
    case 3:
1830
        cmd->handler(args[0], args[1], args[2]);
1831
        break;
1832
    case 4:
1833
        cmd->handler(args[0], args[1], args[2], args[3]);
1834
        break;
1835
    case 5:
1836
        cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1837
        break;
1838
    case 6:
1839
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1840
        break;
1841
    default:
1842
        term_printf("unsupported number of arguments: %d\n", nb_args);
1843
        goto fail;
1844
    }
1845
 fail:
1846
    for(i = 0; i < MAX_ARGS; i++)
1847
        qemu_free(str_allocated[i]);
1848
    return;
1849
}
1850

    
1851
static void cmd_completion(const char *name, const char *list)
1852
{
1853
    const char *p, *pstart;
1854
    char cmd[128];
1855
    int len;
1856

    
1857
    p = list;
1858
    for(;;) {
1859
        pstart = p;
1860
        p = strchr(p, '|');
1861
        if (!p)
1862
            p = pstart + strlen(pstart);
1863
        len = p - pstart;
1864
        if (len > sizeof(cmd) - 2)
1865
            len = sizeof(cmd) - 2;
1866
        memcpy(cmd, pstart, len);
1867
        cmd[len] = '\0';
1868
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1869
            add_completion(cmd);
1870
        }
1871
        if (*p == '\0')
1872
            break;
1873
        p++;
1874
    }
1875
}
1876

    
1877
static void file_completion(const char *input)
1878
{
1879
    DIR *ffs;
1880
    struct dirent *d;
1881
    char path[1024];
1882
    char file[1024], file_prefix[1024];
1883
    int input_path_len;
1884
    const char *p;
1885

    
1886
    p = strrchr(input, '/'); 
1887
    if (!p) {
1888
        input_path_len = 0;
1889
        pstrcpy(file_prefix, sizeof(file_prefix), input);
1890
        strcpy(path, ".");
1891
    } else {
1892
        input_path_len = p - input + 1;
1893
        memcpy(path, input, input_path_len);
1894
        if (input_path_len > sizeof(path) - 1)
1895
            input_path_len = sizeof(path) - 1;
1896
        path[input_path_len] = '\0';
1897
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1898
    }
1899
#ifdef DEBUG_COMPLETION
1900
    term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
1901
#endif
1902
    ffs = opendir(path);
1903
    if (!ffs)
1904
        return;
1905
    for(;;) {
1906
        struct stat sb;
1907
        d = readdir(ffs);
1908
        if (!d)
1909
            break;
1910
        if (strstart(d->d_name, file_prefix, NULL)) {
1911
            memcpy(file, input, input_path_len);
1912
            strcpy(file + input_path_len, d->d_name);
1913
            /* stat the file to find out if it's a directory.
1914
             * In that case add a slash to speed up typing long paths
1915
             */
1916
            stat(file, &sb);
1917
            if(S_ISDIR(sb.st_mode))
1918
                strcat(file, "/");
1919
            add_completion(file);
1920
        }
1921
    }
1922
    closedir(ffs);
1923
}
1924

    
1925
static void block_completion_it(void *opaque, const char *name)
1926
{
1927
    const char *input = opaque;
1928

    
1929
    if (input[0] == '\0' ||
1930
        !strncmp(name, (char *)input, strlen(input))) {
1931
        add_completion(name);
1932
    }
1933
}
1934

    
1935
/* NOTE: this parser is an approximate form of the real command parser */
1936
static void parse_cmdline(const char *cmdline,
1937
                         int *pnb_args, char **args)
1938
{
1939
    const char *p;
1940
    int nb_args, ret;
1941
    char buf[1024];
1942

    
1943
    p = cmdline;
1944
    nb_args = 0;
1945
    for(;;) {
1946
        while (isspace(*p))
1947
            p++;
1948
        if (*p == '\0')
1949
            break;
1950
        if (nb_args >= MAX_ARGS)
1951
            break;
1952
        ret = get_str(buf, sizeof(buf), &p);
1953
        args[nb_args] = qemu_strdup(buf);
1954
        nb_args++;
1955
        if (ret < 0)
1956
            break;
1957
    }
1958
    *pnb_args = nb_args;
1959
}
1960

    
1961
void readline_find_completion(const char *cmdline)
1962
{
1963
    const char *cmdname;
1964
    char *args[MAX_ARGS];
1965
    int nb_args, i, len;
1966
    const char *ptype, *str;
1967
    term_cmd_t *cmd;
1968

    
1969
    parse_cmdline(cmdline, &nb_args, args);
1970
#ifdef DEBUG_COMPLETION
1971
    for(i = 0; i < nb_args; i++) {
1972
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
1973
    }
1974
#endif
1975

    
1976
    /* if the line ends with a space, it means we want to complete the
1977
       next arg */
1978
    len = strlen(cmdline);
1979
    if (len > 0 && isspace(cmdline[len - 1])) {
1980
        if (nb_args >= MAX_ARGS)
1981
            return;
1982
        args[nb_args++] = qemu_strdup("");
1983
    }
1984
    if (nb_args <= 1) {
1985
        /* command completion */
1986
        if (nb_args == 0)
1987
            cmdname = "";
1988
        else
1989
            cmdname = args[0];
1990
        completion_index = strlen(cmdname);
1991
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1992
            cmd_completion(cmdname, cmd->name);
1993
        }
1994
    } else {
1995
        /* find the command */
1996
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1997
            if (compare_cmd(args[0], cmd->name))
1998
                goto found;
1999
        }
2000
        return;
2001
    found:
2002
        ptype = cmd->args_type;
2003
        for(i = 0; i < nb_args - 2; i++) {
2004
            if (*ptype != '\0') {
2005
                ptype++;
2006
                while (*ptype == '?')
2007
                    ptype++;
2008
            }
2009
        }
2010
        str = args[nb_args - 1];
2011
        switch(*ptype) {
2012
        case 'F':
2013
            /* file completion */
2014
            completion_index = strlen(str);
2015
            file_completion(str);
2016
            break;
2017
        case 'B':
2018
            /* block device name completion */
2019
            completion_index = strlen(str);
2020
            bdrv_iterate(block_completion_it, (void *)str);
2021
            break;
2022
        case 's':
2023
            /* XXX: more generic ? */
2024
            if (!strcmp(cmd->name, "info")) {
2025
                completion_index = strlen(str);
2026
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2027
                    cmd_completion(str, cmd->name);
2028
                }
2029
            }
2030
            break;
2031
        default:
2032
            break;
2033
        }
2034
    }
2035
    for(i = 0; i < nb_args; i++)
2036
        qemu_free(args[i]);
2037
}
2038

    
2039
static int term_can_read(void *opaque)
2040
{
2041
    return 128;
2042
}
2043

    
2044
static void term_read(void *opaque, const uint8_t *buf, int size)
2045
{
2046
    int i;
2047
    for(i = 0; i < size; i++)
2048
        readline_handle_byte(buf[i]);
2049
}
2050

    
2051
static void monitor_start_input(void);
2052

    
2053
static void monitor_handle_command1(void *opaque, const char *cmdline)
2054
{
2055
    monitor_handle_command(cmdline);
2056
    monitor_start_input();
2057
}
2058

    
2059
static void monitor_start_input(void)
2060
{
2061
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2062
}
2063

    
2064
void monitor_init(CharDriverState *hd, int show_banner)
2065
{
2066
    monitor_hd = hd;
2067
    if (show_banner) {
2068
        term_printf("QEMU %s monitor - type 'help' for more information\n",
2069
                    QEMU_VERSION);
2070
    }
2071
    qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2072
    monitor_start_input();
2073
}
2074

    
2075
/* XXX: use threads ? */
2076
/* modal monitor readline */
2077
static int monitor_readline_started;
2078
static char *monitor_readline_buf;
2079
static int monitor_readline_buf_size;
2080

    
2081
static void monitor_readline_cb(void *opaque, const char *input)
2082
{
2083
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2084
    monitor_readline_started = 0;
2085
}
2086

    
2087
void monitor_readline(const char *prompt, int is_password,
2088
                      char *buf, int buf_size)
2089
{
2090
    if (is_password) {
2091
        qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2092
    }
2093
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
2094
    monitor_readline_buf = buf;
2095
    monitor_readline_buf_size = buf_size;
2096
    monitor_readline_started = 1;
2097
    while (monitor_readline_started) {
2098
        main_loop_wait(10);
2099
    }
2100
}