Statistics
| Branch: | Revision:

root / monitor.c @ 95219897

History | View | Annotate | Download (58.8 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
CPUState *mon_cpu = NULL;
68

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

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

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

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

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

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

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

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

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

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

    
168
static void do_commit(void)
169
{
170
    int i;
171

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

    
179
static void do_info(const char *item)
180
{
181
    term_cmd_t *cmd;
182

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

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

    
201
static void do_info_block(void)
202
{
203
    bdrv_info();
204
}
205

    
206
/* get the current CPU defined by the user */
207
int mon_set_cpu(int cpu_index)
208
{
209
    CPUState *env;
210

    
211
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
212
        if (env->cpu_index == cpu_index) {
213
            mon_cpu = env;
214
            return 0;
215
        }
216
    }
217
    return -1;
218
}
219

    
220
CPUState *mon_get_cpu(void)
221
{
222
    if (!mon_cpu) {
223
        mon_set_cpu(0);
224
    }
225
    return mon_cpu;
226
}
227

    
228
static void do_info_registers(void)
229
{
230
    CPUState *env;
231
    env = mon_get_cpu();
232
    if (!env)
233
        return;
234
#ifdef TARGET_I386
235
    cpu_dump_state(env, NULL, monitor_fprintf,
236
                   X86_DUMP_FPU);
237
#else
238
    cpu_dump_state(env, NULL, monitor_fprintf, 
239
                   0);
240
#endif
241
}
242

    
243
static void do_info_cpus(void)
244
{
245
    CPUState *env;
246

    
247
    /* just to set the default cpu if not already done */
248
    mon_get_cpu();
249

    
250
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
251
        term_printf("%c CPU #%d:", 
252
                    (env == mon_cpu) ? '*' : ' ',
253
                    env->cpu_index);
254
#if defined(TARGET_I386)
255
        term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
256
        if (env->hflags & HF_HALTED_MASK)
257
            term_printf(" (halted)");
258
#elif defined(TARGET_PPC)
259
        term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
260
        if (env->halted)
261
            term_printf(" (halted)");
262
#elif defined(TARGET_SPARC)
263
        term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
264
        if (env->halted)
265
            term_printf(" (halted)");
266
#endif
267
        term_printf("\n");
268
    }
269
}
270

    
271
static void do_cpu_set(int index)
272
{
273
    if (mon_set_cpu(index) < 0)
274
        term_printf("Invalid CPU index\n");
275
}
276

    
277
static void do_info_jit(void)
278
{
279
    dump_exec_info(NULL, monitor_fprintf);
280
}
281

    
282
static void do_info_history (void)
283
{
284
    int i;
285
    const char *str;
286
    
287
    i = 0;
288
    for(;;) {
289
        str = readline_get_history(i);
290
        if (!str)
291
            break;
292
        term_printf("%d: '%s'\n", i, str);
293
        i++;
294
    }
295
}
296

    
297
static void do_quit(void)
298
{
299
    exit(0);
300
}
301

    
302
static int eject_device(BlockDriverState *bs, int force)
303
{
304
    if (bdrv_is_inserted(bs)) {
305
        if (!force) {
306
            if (!bdrv_is_removable(bs)) {
307
                term_printf("device is not removable\n");
308
                return -1;
309
            }
310
            if (bdrv_is_locked(bs)) {
311
                term_printf("device is locked\n");
312
                return -1;
313
            }
314
        }
315
        bdrv_close(bs);
316
    }
317
    return 0;
318
}
319

    
320
static void do_eject(int force, const char *filename)
321
{
322
    BlockDriverState *bs;
323

    
324
    bs = bdrv_find(filename);
325
    if (!bs) {
326
        term_printf("device not found\n");
327
        return;
328
    }
329
    eject_device(bs, force);
330
}
331

    
332
static void do_change(const char *device, const char *filename)
333
{
334
    BlockDriverState *bs;
335
    int i;
336
    char password[256];
337

    
338
    bs = bdrv_find(device);
339
    if (!bs) {
340
        term_printf("device not found\n");
341
        return;
342
    }
343
    if (eject_device(bs, 0) < 0)
344
        return;
345
    bdrv_open(bs, filename, 0);
346
    if (bdrv_is_encrypted(bs)) {
347
        term_printf("%s is encrypted.\n", device);
348
        for(i = 0; i < 3; i++) {
349
            monitor_readline("Password: ", 1, password, sizeof(password));
350
            if (bdrv_set_key(bs, password) == 0)
351
                break;
352
            term_printf("invalid password\n");
353
        }
354
    }
355
}
356

    
357
static void do_screen_dump(const char *filename)
358
{
359
    vga_hw_screen_dump(filename);
360
}
361

    
362
static void do_log(const char *items)
363
{
364
    int mask;
365
    
366
    if (!strcmp(items, "none")) {
367
        mask = 0;
368
    } else {
369
        mask = cpu_str_to_log_mask(items);
370
        if (!mask) {
371
            help_cmd("log");
372
            return;
373
        }
374
    }
375
    cpu_set_log(mask);
376
}
377

    
378
static void do_savevm(const char *filename)
379
{
380
    if (qemu_savevm(filename) < 0)
381
        term_printf("I/O error when saving VM to '%s'\n", filename);
382
}
383

    
384
static void do_loadvm(const char *filename)
385
{
386
    if (qemu_loadvm(filename) < 0) 
387
        term_printf("I/O error when loading VM from '%s'\n", filename);
388
}
389

    
390
static void do_stop(void)
391
{
392
    vm_stop(EXCP_INTERRUPT);
393
}
394

    
395
static void do_cont(void)
396
{
397
    vm_start();
398
}
399

    
400
#ifdef CONFIG_GDBSTUB
401
static void do_gdbserver(int has_port, int port)
402
{
403
    if (!has_port)
404
        port = DEFAULT_GDBSTUB_PORT;
405
    if (gdbserver_start(port) < 0) {
406
        qemu_printf("Could not open gdbserver socket on port %d\n", port);
407
    } else {
408
        qemu_printf("Waiting gdb connection on port %d\n", port);
409
    }
410
}
411
#endif
412

    
413
static void term_printc(int c)
414
{
415
    term_printf("'");
416
    switch(c) {
417
    case '\'':
418
        term_printf("\\'");
419
        break;
420
    case '\\':
421
        term_printf("\\\\");
422
        break;
423
    case '\n':
424
        term_printf("\\n");
425
        break;
426
    case '\r':
427
        term_printf("\\r");
428
        break;
429
    default:
430
        if (c >= 32 && c <= 126) {
431
            term_printf("%c", c);
432
        } else {
433
            term_printf("\\x%02x", c);
434
        }
435
        break;
436
    }
437
    term_printf("'");
438
}
439

    
440
static void memory_dump(int count, int format, int wsize, 
441
                        target_ulong addr, int is_physical)
442
{
443
    CPUState *env;
444
    int nb_per_line, l, line_size, i, max_digits, len;
445
    uint8_t buf[16];
446
    uint64_t v;
447

    
448
    if (format == 'i') {
449
        int flags;
450
        flags = 0;
451
        env = mon_get_cpu();
452
        if (!env && !is_physical)
453
            return;
454
#ifdef TARGET_I386
455
        if (wsize == 2) {
456
            flags = 1;
457
        } else if (wsize == 4) {
458
            flags = 0;
459
        } else {
460
                /* as default we use the current CS size */
461
            flags = 0;
462
            if (env && !(env->segs[R_CS].flags & DESC_B_MASK))
463
                flags = 1;
464
        }
465
#endif
466
        monitor_disas(env, addr, count, is_physical, flags);
467
        return;
468
    }
469

    
470
    len = wsize * count;
471
    if (wsize == 1)
472
        line_size = 8;
473
    else
474
        line_size = 16;
475
    nb_per_line = line_size / wsize;
476
    max_digits = 0;
477

    
478
    switch(format) {
479
    case 'o':
480
        max_digits = (wsize * 8 + 2) / 3;
481
        break;
482
    default:
483
    case 'x':
484
        max_digits = (wsize * 8) / 4;
485
        break;
486
    case 'u':
487
    case 'd':
488
        max_digits = (wsize * 8 * 10 + 32) / 33;
489
        break;
490
    case 'c':
491
        wsize = 1;
492
        break;
493
    }
494

    
495
    while (len > 0) {
496
        term_printf(TARGET_FMT_lx ":", addr);
497
        l = len;
498
        if (l > line_size)
499
            l = line_size;
500
        if (is_physical) {
501
            cpu_physical_memory_rw(addr, buf, l, 0);
502
        } else {
503
            env = mon_get_cpu();
504
            if (!env)
505
                break;
506
            cpu_memory_rw_debug(env, addr, buf, l, 0);
507
        }
508
        i = 0; 
509
        while (i < l) {
510
            switch(wsize) {
511
            default:
512
            case 1:
513
                v = ldub_raw(buf + i);
514
                break;
515
            case 2:
516
                v = lduw_raw(buf + i);
517
                break;
518
            case 4:
519
                v = (uint32_t)ldl_raw(buf + i);
520
                break;
521
            case 8:
522
                v = ldq_raw(buf + i);
523
                break;
524
            }
525
            term_printf(" ");
526
            switch(format) {
527
            case 'o':
528
                term_printf("%#*llo", max_digits, v);
529
                break;
530
            case 'x':
531
                term_printf("0x%0*llx", max_digits, v);
532
                break;
533
            case 'u':
534
                term_printf("%*llu", max_digits, v);
535
                break;
536
            case 'd':
537
                term_printf("%*lld", max_digits, v);
538
                break;
539
            case 'c':
540
                term_printc(v);
541
                break;
542
            }
543
            i += wsize;
544
        }
545
        term_printf("\n");
546
        addr += l;
547
        len -= l;
548
    }
549
}
550

    
551
#if TARGET_LONG_BITS == 64
552
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
553
#else
554
#define GET_TLONG(h, l) (l)
555
#endif
556

    
557
static void do_memory_dump(int count, int format, int size, 
558
                           uint32_t addrh, uint32_t addrl)
559
{
560
    target_long addr = GET_TLONG(addrh, addrl);
561
    memory_dump(count, format, size, addr, 0);
562
}
563

    
564
static void do_physical_memory_dump(int count, int format, int size,
565
                                    uint32_t addrh, uint32_t addrl)
566

    
567
{
568
    target_long addr = GET_TLONG(addrh, addrl);
569
    memory_dump(count, format, size, addr, 1);
570
}
571

    
572
static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
573
{
574
    target_long val = GET_TLONG(valh, vall);
575
#if TARGET_LONG_BITS == 32
576
    switch(format) {
577
    case 'o':
578
        term_printf("%#o", val);
579
        break;
580
    case 'x':
581
        term_printf("%#x", val);
582
        break;
583
    case 'u':
584
        term_printf("%u", val);
585
        break;
586
    default:
587
    case 'd':
588
        term_printf("%d", val);
589
        break;
590
    case 'c':
591
        term_printc(val);
592
        break;
593
    }
594
#else
595
    switch(format) {
596
    case 'o':
597
        term_printf("%#llo", val);
598
        break;
599
    case 'x':
600
        term_printf("%#llx", val);
601
        break;
602
    case 'u':
603
        term_printf("%llu", val);
604
        break;
605
    default:
606
    case 'd':
607
        term_printf("%lld", val);
608
        break;
609
    case 'c':
610
        term_printc(val);
611
        break;
612
    }
613
#endif
614
    term_printf("\n");
615
}
616

    
617
static void do_sum(uint32_t start, uint32_t size)
618
{
619
    uint32_t addr;
620
    uint8_t buf[1];
621
    uint16_t sum;
622

    
623
    sum = 0;
624
    for(addr = start; addr < (start + size); addr++) {
625
        cpu_physical_memory_rw(addr, buf, 1, 0);
626
        /* BSD sum algorithm ('sum' Unix command) */
627
        sum = (sum >> 1) | (sum << 15);
628
        sum += buf[0];
629
    }
630
    term_printf("%05d\n", sum);
631
}
632

    
633
typedef struct {
634
    int keycode;
635
    const char *name;
636
} KeyDef;
637

    
638
static const KeyDef key_defs[] = {
639
    { 0x2a, "shift" },
640
    { 0x36, "shift_r" },
641
    
642
    { 0x38, "alt" },
643
    { 0xb8, "alt_r" },
644
    { 0x1d, "ctrl" },
645
    { 0x9d, "ctrl_r" },
646

    
647
    { 0xdd, "menu" },
648

    
649
    { 0x01, "esc" },
650

    
651
    { 0x02, "1" },
652
    { 0x03, "2" },
653
    { 0x04, "3" },
654
    { 0x05, "4" },
655
    { 0x06, "5" },
656
    { 0x07, "6" },
657
    { 0x08, "7" },
658
    { 0x09, "8" },
659
    { 0x0a, "9" },
660
    { 0x0b, "0" },
661
    { 0x0e, "backspace" },
662

    
663
    { 0x0f, "tab" },
664
    { 0x10, "q" },
665
    { 0x11, "w" },
666
    { 0x12, "e" },
667
    { 0x13, "r" },
668
    { 0x14, "t" },
669
    { 0x15, "y" },
670
    { 0x16, "u" },
671
    { 0x17, "i" },
672
    { 0x18, "o" },
673
    { 0x19, "p" },
674

    
675
    { 0x1c, "ret" },
676

    
677
    { 0x1e, "a" },
678
    { 0x1f, "s" },
679
    { 0x20, "d" },
680
    { 0x21, "f" },
681
    { 0x22, "g" },
682
    { 0x23, "h" },
683
    { 0x24, "j" },
684
    { 0x25, "k" },
685
    { 0x26, "l" },
686

    
687
    { 0x2c, "z" },
688
    { 0x2d, "x" },
689
    { 0x2e, "c" },
690
    { 0x2f, "v" },
691
    { 0x30, "b" },
692
    { 0x31, "n" },
693
    { 0x32, "m" },
694
    
695
    { 0x39, "spc" },
696
    { 0x3a, "caps_lock" },
697
    { 0x3b, "f1" },
698
    { 0x3c, "f2" },
699
    { 0x3d, "f3" },
700
    { 0x3e, "f4" },
701
    { 0x3f, "f5" },
702
    { 0x40, "f6" },
703
    { 0x41, "f7" },
704
    { 0x42, "f8" },
705
    { 0x43, "f9" },
706
    { 0x44, "f10" },
707
    { 0x45, "num_lock" },
708
    { 0x46, "scroll_lock" },
709

    
710
    { 0x56, "<" },
711

    
712
    { 0x57, "f11" },
713
    { 0x58, "f12" },
714

    
715
    { 0xb7, "print" },
716

    
717
    { 0xc7, "home" },
718
    { 0xc9, "pgup" },
719
    { 0xd1, "pgdn" },
720
    { 0xcf, "end" },
721

    
722
    { 0xcb, "left" },
723
    { 0xc8, "up" },
724
    { 0xd0, "down" },
725
    { 0xcd, "right" },
726

    
727
    { 0xd2, "insert" },
728
    { 0xd3, "delete" },
729
    { 0, NULL },
730
};
731

    
732
static int get_keycode(const char *key)
733
{
734
    const KeyDef *p;
735

    
736
    for(p = key_defs; p->name != NULL; p++) {
737
        if (!strcmp(key, p->name))
738
            return p->keycode;
739
    }
740
    return -1;
741
}
742

    
743
static void do_send_key(const char *string)
744
{
745
    char keybuf[16], *q;
746
    uint8_t keycodes[16];
747
    const char *p;
748
    int nb_keycodes, keycode, i;
749
    
750
    nb_keycodes = 0;
751
    p = string;
752
    while (*p != '\0') {
753
        q = keybuf;
754
        while (*p != '\0' && *p != '-') {
755
            if ((q - keybuf) < sizeof(keybuf) - 1) {
756
                *q++ = *p;
757
            }
758
            p++;
759
        }
760
        *q = '\0';
761
        keycode = get_keycode(keybuf);
762
        if (keycode < 0) {
763
            term_printf("unknown key: '%s'\n", keybuf);
764
            return;
765
        }
766
        keycodes[nb_keycodes++] = keycode;
767
        if (*p == '\0')
768
            break;
769
        p++;
770
    }
771
    /* key down events */
772
    for(i = 0; i < nb_keycodes; i++) {
773
        keycode = keycodes[i];
774
        if (keycode & 0x80)
775
            kbd_put_keycode(0xe0);
776
        kbd_put_keycode(keycode & 0x7f);
777
    }
778
    /* key up events */
779
    for(i = nb_keycodes - 1; i >= 0; i--) {
780
        keycode = keycodes[i];
781
        if (keycode & 0x80)
782
            kbd_put_keycode(0xe0);
783
        kbd_put_keycode(keycode | 0x80);
784
    }
785
}
786

    
787
static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
788
{
789
    uint32_t val;
790
    int suffix;
791

    
792
    if (has_index) {
793
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
794
        addr++;
795
    }
796
    addr &= 0xffff;
797

    
798
    switch(size) {
799
    default:
800
    case 1:
801
        val = cpu_inb(NULL, addr);
802
        suffix = 'b';
803
        break;
804
    case 2:
805
        val = cpu_inw(NULL, addr);
806
        suffix = 'w';
807
        break;
808
    case 4:
809
        val = cpu_inl(NULL, addr);
810
        suffix = 'l';
811
        break;
812
    }
813
    term_printf("port%c[0x%04x] = %#0*x\n",
814
                suffix, addr, size * 2, val);
815
}
816

    
817
static void do_system_reset(void)
818
{
819
    qemu_system_reset_request();
820
}
821

    
822
static void do_system_powerdown(void)
823
{
824
    qemu_system_powerdown_request();
825
}
826

    
827
#if defined(TARGET_I386)
828
static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
829
{
830
    term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n", 
831
                addr,
832
                pte & mask,
833
                pte & PG_GLOBAL_MASK ? 'G' : '-',
834
                pte & PG_PSE_MASK ? 'P' : '-',
835
                pte & PG_DIRTY_MASK ? 'D' : '-',
836
                pte & PG_ACCESSED_MASK ? 'A' : '-',
837
                pte & PG_PCD_MASK ? 'C' : '-',
838
                pte & PG_PWT_MASK ? 'T' : '-',
839
                pte & PG_USER_MASK ? 'U' : '-',
840
                pte & PG_RW_MASK ? 'W' : '-');
841
}
842

    
843
static void tlb_info(void)
844
{
845
    CPUState *env;
846
    int l1, l2;
847
    uint32_t pgd, pde, pte;
848

    
849
    env = mon_get_cpu();
850
    if (!env)
851
        return;
852

    
853
    if (!(env->cr[0] & CR0_PG_MASK)) {
854
        term_printf("PG disabled\n");
855
        return;
856
    }
857
    pgd = env->cr[3] & ~0xfff;
858
    for(l1 = 0; l1 < 1024; l1++) {
859
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
860
        pde = le32_to_cpu(pde);
861
        if (pde & PG_PRESENT_MASK) {
862
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
863
                print_pte((l1 << 22), pde, ~((1 << 20) - 1));
864
            } else {
865
                for(l2 = 0; l2 < 1024; l2++) {
866
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
867
                                             (uint8_t *)&pte, 4);
868
                    pte = le32_to_cpu(pte);
869
                    if (pte & PG_PRESENT_MASK) {
870
                        print_pte((l1 << 22) + (l2 << 12), 
871
                                  pte & ~PG_PSE_MASK, 
872
                                  ~0xfff);
873
                    }
874
                }
875
            }
876
        }
877
    }
878
}
879

    
880
static void mem_print(uint32_t *pstart, int *plast_prot, 
881
                      uint32_t end, int prot)
882
{
883
    int prot1;
884
    prot1 = *plast_prot;
885
    if (prot != prot1) {
886
        if (*pstart != -1) {
887
            term_printf("%08x-%08x %08x %c%c%c\n",
888
                        *pstart, end, end - *pstart, 
889
                        prot1 & PG_USER_MASK ? 'u' : '-',
890
                        'r',
891
                        prot1 & PG_RW_MASK ? 'w' : '-');
892
        }
893
        if (prot != 0)
894
            *pstart = end;
895
        else
896
            *pstart = -1;
897
        *plast_prot = prot;
898
    }
899
}
900

    
901
static void mem_info(void)
902
{
903
    CPUState *env;
904
    int l1, l2, prot, last_prot;
905
    uint32_t pgd, pde, pte, start, end;
906

    
907
    env = mon_get_cpu();
908
    if (!env)
909
        return;
910

    
911
    if (!(env->cr[0] & CR0_PG_MASK)) {
912
        term_printf("PG disabled\n");
913
        return;
914
    }
915
    pgd = env->cr[3] & ~0xfff;
916
    last_prot = 0;
917
    start = -1;
918
    for(l1 = 0; l1 < 1024; l1++) {
919
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
920
        pde = le32_to_cpu(pde);
921
        end = l1 << 22;
922
        if (pde & PG_PRESENT_MASK) {
923
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
924
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
925
                mem_print(&start, &last_prot, end, prot);
926
            } else {
927
                for(l2 = 0; l2 < 1024; l2++) {
928
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, 
929
                                             (uint8_t *)&pte, 4);
930
                    pte = le32_to_cpu(pte);
931
                    end = (l1 << 22) + (l2 << 12);
932
                    if (pte & PG_PRESENT_MASK) {
933
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
934
                    } else {
935
                        prot = 0;
936
                    }
937
                    mem_print(&start, &last_prot, end, prot);
938
                }
939
            }
940
        } else {
941
            prot = 0;
942
            mem_print(&start, &last_prot, end, prot);
943
        }
944
    }
945
}
946
#endif
947

    
948
static void do_info_kqemu(void)
949
{
950
#ifdef USE_KQEMU
951
    CPUState *env;
952
    int val;
953
    val = 0;
954
    env = mon_get_cpu();
955
    if (!env) {
956
        term_printf("No cpu initialized yet");
957
        return;
958
    }
959
    val = env->kqemu_enabled;
960
    term_printf("kqemu support: ");
961
    switch(val) {
962
    default:
963
    case 0:
964
        term_printf("disabled\n");
965
        break;
966
    case 1:
967
        term_printf("enabled for user code\n");
968
        break;
969
    case 2:
970
        term_printf("enabled for user and kernel code\n");
971
        break;
972
    }
973
#else
974
    term_printf("kqemu support: not compiled\n");
975
#endif
976
} 
977

    
978
#ifdef CONFIG_PROFILER
979

    
980
int64_t kqemu_time;
981
int64_t qemu_time;
982
int64_t kqemu_exec_count;
983
int64_t dev_time;
984
int64_t kqemu_ret_int_count;
985
int64_t kqemu_ret_excp_count;
986
int64_t kqemu_ret_intr_count;
987

    
988
static void do_info_profile(void)
989
{
990
    int64_t total;
991
    total = qemu_time;
992
    if (total == 0)
993
        total = 1;
994
    term_printf("async time  %lld (%0.3f)\n",
995
                dev_time, dev_time / (double)ticks_per_sec);
996
    term_printf("qemu time   %lld (%0.3f)\n",
997
                qemu_time, qemu_time / (double)ticks_per_sec);
998
    term_printf("kqemu time  %lld (%0.3f %0.1f%%) count=%lld int=%lld excp=%lld intr=%lld\n",
999
                kqemu_time, kqemu_time / (double)ticks_per_sec,
1000
                kqemu_time / (double)total * 100.0,
1001
                kqemu_exec_count,
1002
                kqemu_ret_int_count,
1003
                kqemu_ret_excp_count,
1004
                kqemu_ret_intr_count);
1005
    qemu_time = 0;
1006
    kqemu_time = 0;
1007
    kqemu_exec_count = 0;
1008
    dev_time = 0;
1009
    kqemu_ret_int_count = 0;
1010
    kqemu_ret_excp_count = 0;
1011
    kqemu_ret_intr_count = 0;
1012
#ifdef USE_KQEMU
1013
    kqemu_record_dump();
1014
#endif
1015
}
1016
#else
1017
static void do_info_profile(void)
1018
{
1019
    term_printf("Internal profiler not compiled\n");
1020
}
1021
#endif
1022

    
1023
static term_cmd_t term_cmds[] = {
1024
    { "help|?", "s?", do_help, 
1025
      "[cmd]", "show the help" },
1026
    { "commit", "", do_commit, 
1027
      "", "commit changes to the disk images (if -snapshot is used)" },
1028
    { "info", "s?", do_info,
1029
      "subcommand", "show various information about the system state" },
1030
    { "q|quit", "", do_quit,
1031
      "", "quit the emulator" },
1032
    { "eject", "-fB", do_eject,
1033
      "[-f] device", "eject a removable media (use -f to force it)" },
1034
    { "change", "BF", do_change,
1035
      "device filename", "change a removable media" },
1036
    { "screendump", "F", do_screen_dump, 
1037
      "filename", "save screen into PPM image 'filename'" },
1038
    { "log", "s", do_log,
1039
      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, 
1040
    { "savevm", "F", do_savevm,
1041
      "filename", "save the whole virtual machine state to 'filename'" }, 
1042
    { "loadvm", "F", do_loadvm,
1043
      "filename", "restore the whole virtual machine state from 'filename'" }, 
1044
    { "stop", "", do_stop, 
1045
      "", "stop emulation", },
1046
    { "c|cont", "", do_cont, 
1047
      "", "resume emulation", },
1048
#ifdef CONFIG_GDBSTUB
1049
    { "gdbserver", "i?", do_gdbserver, 
1050
      "[port]", "start gdbserver session (default port=1234)", },
1051
#endif
1052
    { "x", "/l", do_memory_dump, 
1053
      "/fmt addr", "virtual memory dump starting at 'addr'", },
1054
    { "xp", "/l", do_physical_memory_dump, 
1055
      "/fmt addr", "physical memory dump starting at 'addr'", },
1056
    { "p|print", "/l", do_print, 
1057
      "/fmt expr", "print expression value (use $reg for CPU register access)", },
1058
    { "i", "/ii.", do_ioport_read, 
1059
      "/fmt addr", "I/O port read" },
1060

    
1061
    { "sendkey", "s", do_send_key, 
1062
      "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1063
    { "system_reset", "", do_system_reset, 
1064
      "", "reset the system" },
1065
    { "system_powerdown", "", do_system_powerdown, 
1066
      "", "send system power down event" },
1067
    { "sum", "ii", do_sum, 
1068
      "addr size", "compute the checksum of a memory region" },
1069
    { "usb_add", "s", do_usb_add,
1070
      "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1071
    { "usb_del", "s", do_usb_del,
1072
      "device", "remove USB device 'bus.addr'" },
1073
    { "cpu", "i", do_cpu_set, 
1074
      "index", "set the default CPU" },
1075
    { NULL, NULL, }, 
1076
};
1077

    
1078
static term_cmd_t info_cmds[] = {
1079
    { "version", "", do_info_version,
1080
      "", "show the version of qemu" },
1081
    { "network", "", do_info_network,
1082
      "", "show the network state" },
1083
    { "block", "", do_info_block,
1084
      "", "show the block devices" },
1085
    { "registers", "", do_info_registers,
1086
      "", "show the cpu registers" },
1087
    { "cpus", "", do_info_cpus,
1088
      "", "show infos for each CPU" },
1089
    { "history", "", do_info_history,
1090
      "", "show the command line history", },
1091
    { "irq", "", irq_info,
1092
      "", "show the interrupts statistics (if available)", },
1093
    { "pic", "", pic_info,
1094
      "", "show i8259 (PIC) state", },
1095
    { "pci", "", pci_info,
1096
      "", "show PCI info", },
1097
#if defined(TARGET_I386)
1098
    { "tlb", "", tlb_info,
1099
      "", "show virtual to physical memory mappings", },
1100
    { "mem", "", mem_info,
1101
      "", "show the active virtual memory mappings", },
1102
#endif
1103
    { "jit", "", do_info_jit,
1104
      "", "show dynamic compiler info", },
1105
    { "kqemu", "", do_info_kqemu,
1106
      "", "show kqemu information", },
1107
    { "usb", "", usb_info,
1108
      "", "show guest USB devices", },
1109
    { "usbhost", "", usb_host_info,
1110
      "", "show host USB devices", },
1111
    { "profile", "", do_info_profile,
1112
      "", "show profiling information", },
1113
    { NULL, NULL, },
1114
};
1115

    
1116
/*******************************************************************/
1117

    
1118
static const char *pch;
1119
static jmp_buf expr_env;
1120

    
1121
#define MD_TLONG 0
1122
#define MD_I32   1
1123

    
1124
typedef struct MonitorDef {
1125
    const char *name;
1126
    int offset;
1127
    target_long (*get_value)(struct MonitorDef *md, int val);
1128
    int type;
1129
} MonitorDef;
1130

    
1131
#if defined(TARGET_I386)
1132
static target_long monitor_get_pc (struct MonitorDef *md, int val)
1133
{
1134
    CPUState *env = mon_get_cpu();
1135
    if (!env)
1136
        return 0;
1137
    return env->eip + env->segs[R_CS].base;
1138
}
1139
#endif
1140

    
1141
#if defined(TARGET_PPC)
1142
static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1143
{
1144
    CPUState *env = mon_get_cpu();
1145
    unsigned int u;
1146
    int i;
1147

    
1148
    if (!env)
1149
        return 0;
1150

    
1151
    u = 0;
1152
    for (i = 0; i < 8; i++)
1153
        u |= env->crf[i] << (32 - (4 * i));
1154

    
1155
    return u;
1156
}
1157

    
1158
static target_long monitor_get_msr (struct MonitorDef *md, int val)
1159
{
1160
    CPUState *env = mon_get_cpu();
1161
    if (!env)
1162
        return 0;
1163
    return (env->msr[MSR_POW] << MSR_POW) |
1164
        (env->msr[MSR_ILE] << MSR_ILE) |
1165
        (env->msr[MSR_EE] << MSR_EE) |
1166
        (env->msr[MSR_PR] << MSR_PR) |
1167
        (env->msr[MSR_FP] << MSR_FP) |
1168
        (env->msr[MSR_ME] << MSR_ME) |
1169
        (env->msr[MSR_FE0] << MSR_FE0) |
1170
        (env->msr[MSR_SE] << MSR_SE) |
1171
        (env->msr[MSR_BE] << MSR_BE) |
1172
        (env->msr[MSR_FE1] << MSR_FE1) |
1173
        (env->msr[MSR_IP] << MSR_IP) |
1174
        (env->msr[MSR_IR] << MSR_IR) |
1175
        (env->msr[MSR_DR] << MSR_DR) |
1176
        (env->msr[MSR_RI] << MSR_RI) |
1177
        (env->msr[MSR_LE] << MSR_LE);
1178
}
1179

    
1180
static target_long monitor_get_xer (struct MonitorDef *md, int val)
1181
{
1182
    CPUState *env = mon_get_cpu();
1183
    if (!env)
1184
        return 0;
1185
    return (env->xer[XER_SO] << XER_SO) |
1186
        (env->xer[XER_OV] << XER_OV) |
1187
        (env->xer[XER_CA] << XER_CA) |
1188
        (env->xer[XER_BC] << XER_BC);
1189
}
1190

    
1191
static target_long monitor_get_decr (struct MonitorDef *md, int val)
1192
{
1193
    CPUState *env = mon_get_cpu();
1194
    if (!env)
1195
        return 0;
1196
    return cpu_ppc_load_decr(env);
1197
}
1198

    
1199
static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1200
{
1201
    CPUState *env = mon_get_cpu();
1202
    if (!env)
1203
        return 0;
1204
    return cpu_ppc_load_tbu(env);
1205
}
1206

    
1207
static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1208
{
1209
    CPUState *env = mon_get_cpu();
1210
    if (!env)
1211
        return 0;
1212
    return cpu_ppc_load_tbl(env);
1213
}
1214
#endif
1215

    
1216
#if defined(TARGET_SPARC)
1217
#ifndef TARGET_SPARC64
1218
static target_long monitor_get_psr (struct MonitorDef *md, int val)
1219
{
1220
    CPUState *env = mon_get_cpu();
1221
    if (!env)
1222
        return 0;
1223
    return GET_PSR(env);
1224
}
1225
#endif
1226

    
1227
static target_long monitor_get_reg(struct MonitorDef *md, int val)
1228
{
1229
    CPUState *env = mon_get_cpu();
1230
    if (!env)
1231
        return 0;
1232
    return env->regwptr[val];
1233
}
1234
#endif
1235

    
1236
static MonitorDef monitor_defs[] = {
1237
#ifdef TARGET_I386
1238

    
1239
#define SEG(name, seg) \
1240
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1241
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1242
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1243

    
1244
    { "eax", offsetof(CPUState, regs[0]) },
1245
    { "ecx", offsetof(CPUState, regs[1]) },
1246
    { "edx", offsetof(CPUState, regs[2]) },
1247
    { "ebx", offsetof(CPUState, regs[3]) },
1248
    { "esp|sp", offsetof(CPUState, regs[4]) },
1249
    { "ebp|fp", offsetof(CPUState, regs[5]) },
1250
    { "esi", offsetof(CPUState, regs[6]) },
1251
    { "edi", offsetof(CPUState, regs[7]) },
1252
#ifdef TARGET_X86_64
1253
    { "r8", offsetof(CPUState, regs[8]) },
1254
    { "r9", offsetof(CPUState, regs[9]) },
1255
    { "r10", offsetof(CPUState, regs[10]) },
1256
    { "r11", offsetof(CPUState, regs[11]) },
1257
    { "r12", offsetof(CPUState, regs[12]) },
1258
    { "r13", offsetof(CPUState, regs[13]) },
1259
    { "r14", offsetof(CPUState, regs[14]) },
1260
    { "r15", offsetof(CPUState, regs[15]) },
1261
#endif
1262
    { "eflags", offsetof(CPUState, eflags) },
1263
    { "eip", offsetof(CPUState, eip) },
1264
    SEG("cs", R_CS)
1265
    SEG("ds", R_DS)
1266
    SEG("es", R_ES)
1267
    SEG("ss", R_SS)
1268
    SEG("fs", R_FS)
1269
    SEG("gs", R_GS)
1270
    { "pc", 0, monitor_get_pc, },
1271
#elif defined(TARGET_PPC)
1272
    { "r0", offsetof(CPUState, gpr[0]) },
1273
    { "r1", offsetof(CPUState, gpr[1]) },
1274
    { "r2", offsetof(CPUState, gpr[2]) },
1275
    { "r3", offsetof(CPUState, gpr[3]) },
1276
    { "r4", offsetof(CPUState, gpr[4]) },
1277
    { "r5", offsetof(CPUState, gpr[5]) },
1278
    { "r6", offsetof(CPUState, gpr[6]) },
1279
    { "r7", offsetof(CPUState, gpr[7]) },
1280
    { "r8", offsetof(CPUState, gpr[8]) },
1281
    { "r9", offsetof(CPUState, gpr[9]) },
1282
    { "r10", offsetof(CPUState, gpr[10]) },
1283
    { "r11", offsetof(CPUState, gpr[11]) },
1284
    { "r12", offsetof(CPUState, gpr[12]) },
1285
    { "r13", offsetof(CPUState, gpr[13]) },
1286
    { "r14", offsetof(CPUState, gpr[14]) },
1287
    { "r15", offsetof(CPUState, gpr[15]) },
1288
    { "r16", offsetof(CPUState, gpr[16]) },
1289
    { "r17", offsetof(CPUState, gpr[17]) },
1290
    { "r18", offsetof(CPUState, gpr[18]) },
1291
    { "r19", offsetof(CPUState, gpr[19]) },
1292
    { "r20", offsetof(CPUState, gpr[20]) },
1293
    { "r21", offsetof(CPUState, gpr[21]) },
1294
    { "r22", offsetof(CPUState, gpr[22]) },
1295
    { "r23", offsetof(CPUState, gpr[23]) },
1296
    { "r24", offsetof(CPUState, gpr[24]) },
1297
    { "r25", offsetof(CPUState, gpr[25]) },
1298
    { "r26", offsetof(CPUState, gpr[26]) },
1299
    { "r27", offsetof(CPUState, gpr[27]) },
1300
    { "r28", offsetof(CPUState, gpr[28]) },
1301
    { "r29", offsetof(CPUState, gpr[29]) },
1302
    { "r30", offsetof(CPUState, gpr[30]) },
1303
    { "r31", offsetof(CPUState, gpr[31]) },
1304
    { "nip|pc", offsetof(CPUState, nip) },
1305
    { "lr", offsetof(CPUState, lr) },
1306
    { "ctr", offsetof(CPUState, ctr) },
1307
    { "decr", 0, &monitor_get_decr, },
1308
    { "ccr", 0, &monitor_get_ccr, },
1309
    { "msr", 0, &monitor_get_msr, },
1310
    { "xer", 0, &monitor_get_xer, },
1311
    { "tbu", 0, &monitor_get_tbu, },
1312
    { "tbl", 0, &monitor_get_tbl, },
1313
    { "sdr1", offsetof(CPUState, sdr1) },
1314
    { "sr0", offsetof(CPUState, sr[0]) },
1315
    { "sr1", offsetof(CPUState, sr[1]) },
1316
    { "sr2", offsetof(CPUState, sr[2]) },
1317
    { "sr3", offsetof(CPUState, sr[3]) },
1318
    { "sr4", offsetof(CPUState, sr[4]) },
1319
    { "sr5", offsetof(CPUState, sr[5]) },
1320
    { "sr6", offsetof(CPUState, sr[6]) },
1321
    { "sr7", offsetof(CPUState, sr[7]) },
1322
    { "sr8", offsetof(CPUState, sr[8]) },
1323
    { "sr9", offsetof(CPUState, sr[9]) },
1324
    { "sr10", offsetof(CPUState, sr[10]) },
1325
    { "sr11", offsetof(CPUState, sr[11]) },
1326
    { "sr12", offsetof(CPUState, sr[12]) },
1327
    { "sr13", offsetof(CPUState, sr[13]) },
1328
    { "sr14", offsetof(CPUState, sr[14]) },
1329
    { "sr15", offsetof(CPUState, sr[15]) },
1330
    /* Too lazy to put BATs and SPRs ... */
1331
#elif defined(TARGET_SPARC)
1332
    { "g0", offsetof(CPUState, gregs[0]) },
1333
    { "g1", offsetof(CPUState, gregs[1]) },
1334
    { "g2", offsetof(CPUState, gregs[2]) },
1335
    { "g3", offsetof(CPUState, gregs[3]) },
1336
    { "g4", offsetof(CPUState, gregs[4]) },
1337
    { "g5", offsetof(CPUState, gregs[5]) },
1338
    { "g6", offsetof(CPUState, gregs[6]) },
1339
    { "g7", offsetof(CPUState, gregs[7]) },
1340
    { "o0", 0, monitor_get_reg },
1341
    { "o1", 1, monitor_get_reg },
1342
    { "o2", 2, monitor_get_reg },
1343
    { "o3", 3, monitor_get_reg },
1344
    { "o4", 4, monitor_get_reg },
1345
    { "o5", 5, monitor_get_reg },
1346
    { "o6", 6, monitor_get_reg },
1347
    { "o7", 7, monitor_get_reg },
1348
    { "l0", 8, monitor_get_reg },
1349
    { "l1", 9, monitor_get_reg },
1350
    { "l2", 10, monitor_get_reg },
1351
    { "l3", 11, monitor_get_reg },
1352
    { "l4", 12, monitor_get_reg },
1353
    { "l5", 13, monitor_get_reg },
1354
    { "l6", 14, monitor_get_reg },
1355
    { "l7", 15, monitor_get_reg },
1356
    { "i0", 16, monitor_get_reg },
1357
    { "i1", 17, monitor_get_reg },
1358
    { "i2", 18, monitor_get_reg },
1359
    { "i3", 19, monitor_get_reg },
1360
    { "i4", 20, monitor_get_reg },
1361
    { "i5", 21, monitor_get_reg },
1362
    { "i6", 22, monitor_get_reg },
1363
    { "i7", 23, monitor_get_reg },
1364
    { "pc", offsetof(CPUState, pc) },
1365
    { "npc", offsetof(CPUState, npc) },
1366
    { "y", offsetof(CPUState, y) },
1367
#ifndef TARGET_SPARC64
1368
    { "psr", 0, &monitor_get_psr, },
1369
    { "wim", offsetof(CPUState, wim) },
1370
#endif
1371
    { "tbr", offsetof(CPUState, tbr) },
1372
    { "fsr", offsetof(CPUState, fsr) },
1373
    { "f0", offsetof(CPUState, fpr[0]) },
1374
    { "f1", offsetof(CPUState, fpr[1]) },
1375
    { "f2", offsetof(CPUState, fpr[2]) },
1376
    { "f3", offsetof(CPUState, fpr[3]) },
1377
    { "f4", offsetof(CPUState, fpr[4]) },
1378
    { "f5", offsetof(CPUState, fpr[5]) },
1379
    { "f6", offsetof(CPUState, fpr[6]) },
1380
    { "f7", offsetof(CPUState, fpr[7]) },
1381
    { "f8", offsetof(CPUState, fpr[8]) },
1382
    { "f9", offsetof(CPUState, fpr[9]) },
1383
    { "f10", offsetof(CPUState, fpr[10]) },
1384
    { "f11", offsetof(CPUState, fpr[11]) },
1385
    { "f12", offsetof(CPUState, fpr[12]) },
1386
    { "f13", offsetof(CPUState, fpr[13]) },
1387
    { "f14", offsetof(CPUState, fpr[14]) },
1388
    { "f15", offsetof(CPUState, fpr[15]) },
1389
    { "f16", offsetof(CPUState, fpr[16]) },
1390
    { "f17", offsetof(CPUState, fpr[17]) },
1391
    { "f18", offsetof(CPUState, fpr[18]) },
1392
    { "f19", offsetof(CPUState, fpr[19]) },
1393
    { "f20", offsetof(CPUState, fpr[20]) },
1394
    { "f21", offsetof(CPUState, fpr[21]) },
1395
    { "f22", offsetof(CPUState, fpr[22]) },
1396
    { "f23", offsetof(CPUState, fpr[23]) },
1397
    { "f24", offsetof(CPUState, fpr[24]) },
1398
    { "f25", offsetof(CPUState, fpr[25]) },
1399
    { "f26", offsetof(CPUState, fpr[26]) },
1400
    { "f27", offsetof(CPUState, fpr[27]) },
1401
    { "f28", offsetof(CPUState, fpr[28]) },
1402
    { "f29", offsetof(CPUState, fpr[29]) },
1403
    { "f30", offsetof(CPUState, fpr[30]) },
1404
    { "f31", offsetof(CPUState, fpr[31]) },
1405
#ifdef TARGET_SPARC64
1406
    { "f32", offsetof(CPUState, fpr[32]) },
1407
    { "f34", offsetof(CPUState, fpr[34]) },
1408
    { "f36", offsetof(CPUState, fpr[36]) },
1409
    { "f38", offsetof(CPUState, fpr[38]) },
1410
    { "f40", offsetof(CPUState, fpr[40]) },
1411
    { "f42", offsetof(CPUState, fpr[42]) },
1412
    { "f44", offsetof(CPUState, fpr[44]) },
1413
    { "f46", offsetof(CPUState, fpr[46]) },
1414
    { "f48", offsetof(CPUState, fpr[48]) },
1415
    { "f50", offsetof(CPUState, fpr[50]) },
1416
    { "f52", offsetof(CPUState, fpr[52]) },
1417
    { "f54", offsetof(CPUState, fpr[54]) },
1418
    { "f56", offsetof(CPUState, fpr[56]) },
1419
    { "f58", offsetof(CPUState, fpr[58]) },
1420
    { "f60", offsetof(CPUState, fpr[60]) },
1421
    { "f62", offsetof(CPUState, fpr[62]) },
1422
    { "asi", offsetof(CPUState, asi) },
1423
    { "pstate", offsetof(CPUState, pstate) },
1424
    { "cansave", offsetof(CPUState, cansave) },
1425
    { "canrestore", offsetof(CPUState, canrestore) },
1426
    { "otherwin", offsetof(CPUState, otherwin) },
1427
    { "wstate", offsetof(CPUState, wstate) },
1428
    { "cleanwin", offsetof(CPUState, cleanwin) },
1429
    { "fprs", offsetof(CPUState, fprs) },
1430
#endif
1431
#endif
1432
    { NULL },
1433
};
1434

    
1435
static void expr_error(const char *fmt) 
1436
{
1437
    term_printf(fmt);
1438
    term_printf("\n");
1439
    longjmp(expr_env, 1);
1440
}
1441

    
1442
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
1443
static int get_monitor_def(target_long *pval, const char *name)
1444
{
1445
    MonitorDef *md;
1446
    void *ptr;
1447

    
1448
    for(md = monitor_defs; md->name != NULL; md++) {
1449
        if (compare_cmd(name, md->name)) {
1450
            if (md->get_value) {
1451
                *pval = md->get_value(md, md->offset);
1452
            } else {
1453
                CPUState *env = mon_get_cpu();
1454
                if (!env)
1455
                    return -2;
1456
                ptr = (uint8_t *)env + md->offset;
1457
                switch(md->type) {
1458
                case MD_I32:
1459
                    *pval = *(int32_t *)ptr;
1460
                    break;
1461
                case MD_TLONG:
1462
                    *pval = *(target_long *)ptr;
1463
                    break;
1464
                default:
1465
                    *pval = 0;
1466
                    break;
1467
                }
1468
            }
1469
            return 0;
1470
        }
1471
    }
1472
    return -1;
1473
}
1474

    
1475
static void next(void)
1476
{
1477
    if (pch != '\0') {
1478
        pch++;
1479
        while (isspace(*pch))
1480
            pch++;
1481
    }
1482
}
1483

    
1484
static target_long expr_sum(void);
1485

    
1486
static target_long expr_unary(void)
1487
{
1488
    target_long n;
1489
    char *p;
1490
    int ret;
1491

    
1492
    switch(*pch) {
1493
    case '+':
1494
        next();
1495
        n = expr_unary();
1496
        break;
1497
    case '-':
1498
        next();
1499
        n = -expr_unary();
1500
        break;
1501
    case '~':
1502
        next();
1503
        n = ~expr_unary();
1504
        break;
1505
    case '(':
1506
        next();
1507
        n = expr_sum();
1508
        if (*pch != ')') {
1509
            expr_error("')' expected");
1510
        }
1511
        next();
1512
        break;
1513
    case '\'':
1514
        pch++;
1515
        if (*pch == '\0')
1516
            expr_error("character constant expected");
1517
        n = *pch;
1518
        pch++;
1519
        if (*pch != '\'')
1520
            expr_error("missing terminating \' character");
1521
        next();
1522
        break;
1523
    case '$':
1524
        {
1525
            char buf[128], *q;
1526
            
1527
            pch++;
1528
            q = buf;
1529
            while ((*pch >= 'a' && *pch <= 'z') ||
1530
                   (*pch >= 'A' && *pch <= 'Z') ||
1531
                   (*pch >= '0' && *pch <= '9') ||
1532
                   *pch == '_' || *pch == '.') {
1533
                if ((q - buf) < sizeof(buf) - 1)
1534
                    *q++ = *pch;
1535
                pch++;
1536
            }
1537
            while (isspace(*pch))
1538
                pch++;
1539
            *q = 0;
1540
            ret = get_monitor_def(&n, buf);
1541
            if (ret == -1)
1542
                expr_error("unknown register");
1543
            else if (ret == -2) 
1544
                expr_error("no cpu defined");
1545
        }
1546
        break;
1547
    case '\0':
1548
        expr_error("unexpected end of expression");
1549
        n = 0;
1550
        break;
1551
    default:
1552
        n = strtoul(pch, &p, 0);
1553
        if (pch == p) {
1554
            expr_error("invalid char in expression");
1555
        }
1556
        pch = p;
1557
        while (isspace(*pch))
1558
            pch++;
1559
        break;
1560
    }
1561
    return n;
1562
}
1563

    
1564

    
1565
static target_long expr_prod(void)
1566
{
1567
    target_long val, val2;
1568
    int op;
1569
    
1570
    val = expr_unary();
1571
    for(;;) {
1572
        op = *pch;
1573
        if (op != '*' && op != '/' && op != '%')
1574
            break;
1575
        next();
1576
        val2 = expr_unary();
1577
        switch(op) {
1578
        default:
1579
        case '*':
1580
            val *= val2;
1581
            break;
1582
        case '/':
1583
        case '%':
1584
            if (val2 == 0) 
1585
                expr_error("division by zero");
1586
            if (op == '/')
1587
                val /= val2;
1588
            else
1589
                val %= val2;
1590
            break;
1591
        }
1592
    }
1593
    return val;
1594
}
1595

    
1596
static target_long expr_logic(void)
1597
{
1598
    target_long val, val2;
1599
    int op;
1600

    
1601
    val = expr_prod();
1602
    for(;;) {
1603
        op = *pch;
1604
        if (op != '&' && op != '|' && op != '^')
1605
            break;
1606
        next();
1607
        val2 = expr_prod();
1608
        switch(op) {
1609
        default:
1610
        case '&':
1611
            val &= val2;
1612
            break;
1613
        case '|':
1614
            val |= val2;
1615
            break;
1616
        case '^':
1617
            val ^= val2;
1618
            break;
1619
        }
1620
    }
1621
    return val;
1622
}
1623

    
1624
static target_long expr_sum(void)
1625
{
1626
    target_long val, val2;
1627
    int op;
1628

    
1629
    val = expr_logic();
1630
    for(;;) {
1631
        op = *pch;
1632
        if (op != '+' && op != '-')
1633
            break;
1634
        next();
1635
        val2 = expr_logic();
1636
        if (op == '+')
1637
            val += val2;
1638
        else
1639
            val -= val2;
1640
    }
1641
    return val;
1642
}
1643

    
1644
static int get_expr(target_long *pval, const char **pp)
1645
{
1646
    pch = *pp;
1647
    if (setjmp(expr_env)) {
1648
        *pp = pch;
1649
        return -1;
1650
    }
1651
    while (isspace(*pch))
1652
        pch++;
1653
    *pval = expr_sum();
1654
    *pp = pch;
1655
    return 0;
1656
}
1657

    
1658
static int get_str(char *buf, int buf_size, const char **pp)
1659
{
1660
    const char *p;
1661
    char *q;
1662
    int c;
1663

    
1664
    q = buf;
1665
    p = *pp;
1666
    while (isspace(*p))
1667
        p++;
1668
    if (*p == '\0') {
1669
    fail:
1670
        *q = '\0';
1671
        *pp = p;
1672
        return -1;
1673
    }
1674
    if (*p == '\"') {
1675
        p++;
1676
        while (*p != '\0' && *p != '\"') {
1677
            if (*p == '\\') {
1678
                p++;
1679
                c = *p++;
1680
                switch(c) {
1681
                case 'n':
1682
                    c = '\n';
1683
                    break;
1684
                case 'r':
1685
                    c = '\r';
1686
                    break;
1687
                case '\\':
1688
                case '\'':
1689
                case '\"':
1690
                    break;
1691
                default:
1692
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
1693
                    goto fail;
1694
                }
1695
                if ((q - buf) < buf_size - 1) {
1696
                    *q++ = c;
1697
                }
1698
            } else {
1699
                if ((q - buf) < buf_size - 1) {
1700
                    *q++ = *p;
1701
                }
1702
                p++;
1703
            }
1704
        }
1705
        if (*p != '\"') {
1706
            qemu_printf("unterminated string\n");
1707
            goto fail;
1708
        }
1709
        p++;
1710
    } else {
1711
        while (*p != '\0' && !isspace(*p)) {
1712
            if ((q - buf) < buf_size - 1) {
1713
                *q++ = *p;
1714
            }
1715
            p++;
1716
        }
1717
    }
1718
    *q = '\0';
1719
    *pp = p;
1720
    return 0;
1721
}
1722

    
1723
static int default_fmt_format = 'x';
1724
static int default_fmt_size = 4;
1725

    
1726
#define MAX_ARGS 16
1727

    
1728
static void monitor_handle_command(const char *cmdline)
1729
{
1730
    const char *p, *pstart, *typestr;
1731
    char *q;
1732
    int c, nb_args, len, i, has_arg;
1733
    term_cmd_t *cmd;
1734
    char cmdname[256];
1735
    char buf[1024];
1736
    void *str_allocated[MAX_ARGS];
1737
    void *args[MAX_ARGS];
1738

    
1739
#ifdef DEBUG
1740
    term_printf("command='%s'\n", cmdline);
1741
#endif
1742
    
1743
    /* extract the command name */
1744
    p = cmdline;
1745
    q = cmdname;
1746
    while (isspace(*p))
1747
        p++;
1748
    if (*p == '\0')
1749
        return;
1750
    pstart = p;
1751
    while (*p != '\0' && *p != '/' && !isspace(*p))
1752
        p++;
1753
    len = p - pstart;
1754
    if (len > sizeof(cmdname) - 1)
1755
        len = sizeof(cmdname) - 1;
1756
    memcpy(cmdname, pstart, len);
1757
    cmdname[len] = '\0';
1758
    
1759
    /* find the command */
1760
    for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1761
        if (compare_cmd(cmdname, cmd->name)) 
1762
            goto found;
1763
    }
1764
    term_printf("unknown command: '%s'\n", cmdname);
1765
    return;
1766
 found:
1767

    
1768
    for(i = 0; i < MAX_ARGS; i++)
1769
        str_allocated[i] = NULL;
1770
    
1771
    /* parse the parameters */
1772
    typestr = cmd->args_type;
1773
    nb_args = 0;
1774
    for(;;) {
1775
        c = *typestr;
1776
        if (c == '\0')
1777
            break;
1778
        typestr++;
1779
        switch(c) {
1780
        case 'F':
1781
        case 'B':
1782
        case 's':
1783
            {
1784
                int ret;
1785
                char *str;
1786
                
1787
                while (isspace(*p)) 
1788
                    p++;
1789
                if (*typestr == '?') {
1790
                    typestr++;
1791
                    if (*p == '\0') {
1792
                        /* no optional string: NULL argument */
1793
                        str = NULL;
1794
                        goto add_str;
1795
                    }
1796
                }
1797
                ret = get_str(buf, sizeof(buf), &p);
1798
                if (ret < 0) {
1799
                    switch(c) {
1800
                    case 'F':
1801
                        term_printf("%s: filename expected\n", cmdname);
1802
                        break;
1803
                    case 'B':
1804
                        term_printf("%s: block device name expected\n", cmdname);
1805
                        break;
1806
                    default:
1807
                        term_printf("%s: string expected\n", cmdname);
1808
                        break;
1809
                    }
1810
                    goto fail;
1811
                }
1812
                str = qemu_malloc(strlen(buf) + 1);
1813
                strcpy(str, buf);
1814
                str_allocated[nb_args] = str;
1815
            add_str:
1816
                if (nb_args >= MAX_ARGS) {
1817
                error_args:
1818
                    term_printf("%s: too many arguments\n", cmdname);
1819
                    goto fail;
1820
                }
1821
                args[nb_args++] = str;
1822
            }
1823
            break;
1824
        case '/':
1825
            {
1826
                int count, format, size;
1827
                
1828
                while (isspace(*p))
1829
                    p++;
1830
                if (*p == '/') {
1831
                    /* format found */
1832
                    p++;
1833
                    count = 1;
1834
                    if (isdigit(*p)) {
1835
                        count = 0;
1836
                        while (isdigit(*p)) {
1837
                            count = count * 10 + (*p - '0');
1838
                            p++;
1839
                        }
1840
                    }
1841
                    size = -1;
1842
                    format = -1;
1843
                    for(;;) {
1844
                        switch(*p) {
1845
                        case 'o':
1846
                        case 'd':
1847
                        case 'u':
1848
                        case 'x':
1849
                        case 'i':
1850
                        case 'c':
1851
                            format = *p++;
1852
                            break;
1853
                        case 'b':
1854
                            size = 1;
1855
                            p++;
1856
                            break;
1857
                        case 'h':
1858
                            size = 2;
1859
                            p++;
1860
                            break;
1861
                        case 'w':
1862
                            size = 4;
1863
                            p++;
1864
                            break;
1865
                        case 'g':
1866
                        case 'L':
1867
                            size = 8;
1868
                            p++;
1869
                            break;
1870
                        default:
1871
                            goto next;
1872
                        }
1873
                    }
1874
                next:
1875
                    if (*p != '\0' && !isspace(*p)) {
1876
                        term_printf("invalid char in format: '%c'\n", *p);
1877
                        goto fail;
1878
                    }
1879
                    if (format < 0)
1880
                        format = default_fmt_format;
1881
                    if (format != 'i') {
1882
                        /* for 'i', not specifying a size gives -1 as size */
1883
                        if (size < 0)
1884
                            size = default_fmt_size;
1885
                    }
1886
                    default_fmt_size = size;
1887
                    default_fmt_format = format;
1888
                } else {
1889
                    count = 1;
1890
                    format = default_fmt_format;
1891
                    if (format != 'i') {
1892
                        size = default_fmt_size;
1893
                    } else {
1894
                        size = -1;
1895
                    }
1896
                }
1897
                if (nb_args + 3 > MAX_ARGS)
1898
                    goto error_args;
1899
                args[nb_args++] = (void*)count;
1900
                args[nb_args++] = (void*)format;
1901
                args[nb_args++] = (void*)size;
1902
            }
1903
            break;
1904
        case 'i':
1905
        case 'l':
1906
            {
1907
                target_long val;
1908
                while (isspace(*p)) 
1909
                    p++;
1910
                if (*typestr == '?' || *typestr == '.') {
1911
                    typestr++;
1912
                    if (*typestr == '?') {
1913
                        if (*p == '\0')
1914
                            has_arg = 0;
1915
                        else
1916
                            has_arg = 1;
1917
                    } else {
1918
                        if (*p == '.') {
1919
                            p++;
1920
                            while (isspace(*p)) 
1921
                                p++;
1922
                            has_arg = 1;
1923
                        } else {
1924
                            has_arg = 0;
1925
                        }
1926
                    }
1927
                    if (nb_args >= MAX_ARGS)
1928
                        goto error_args;
1929
                    args[nb_args++] = (void *)has_arg;
1930
                    if (!has_arg) {
1931
                        if (nb_args >= MAX_ARGS)
1932
                            goto error_args;
1933
                        val = -1;
1934
                        goto add_num;
1935
                    }
1936
                }
1937
                if (get_expr(&val, &p))
1938
                    goto fail;
1939
            add_num:
1940
                if (c == 'i') {
1941
                    if (nb_args >= MAX_ARGS)
1942
                        goto error_args;
1943
                    args[nb_args++] = (void *)(int)val;
1944
                } else {
1945
                    if ((nb_args + 1) >= MAX_ARGS)
1946
                        goto error_args;
1947
#if TARGET_LONG_BITS == 64
1948
                    args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
1949
#else
1950
                    args[nb_args++] = (void *)0;
1951
#endif
1952
                    args[nb_args++] = (void *)(int)(val & 0xffffffff);
1953
                }
1954
            }
1955
            break;
1956
        case '-':
1957
            {
1958
                int has_option;
1959
                /* option */
1960
                
1961
                c = *typestr++;
1962
                if (c == '\0')
1963
                    goto bad_type;
1964
                while (isspace(*p)) 
1965
                    p++;
1966
                has_option = 0;
1967
                if (*p == '-') {
1968
                    p++;
1969
                    if (*p != c) {
1970
                        term_printf("%s: unsupported option -%c\n", 
1971
                                    cmdname, *p);
1972
                        goto fail;
1973
                    }
1974
                    p++;
1975
                    has_option = 1;
1976
                }
1977
                if (nb_args >= MAX_ARGS)
1978
                    goto error_args;
1979
                args[nb_args++] = (void *)has_option;
1980
            }
1981
            break;
1982
        default:
1983
        bad_type:
1984
            term_printf("%s: unknown type '%c'\n", cmdname, c);
1985
            goto fail;
1986
        }
1987
    }
1988
    /* check that all arguments were parsed */
1989
    while (isspace(*p))
1990
        p++;
1991
    if (*p != '\0') {
1992
        term_printf("%s: extraneous characters at the end of line\n", 
1993
                    cmdname);
1994
        goto fail;
1995
    }
1996

    
1997
    switch(nb_args) {
1998
    case 0:
1999
        cmd->handler();
2000
        break;
2001
    case 1:
2002
        cmd->handler(args[0]);
2003
        break;
2004
    case 2:
2005
        cmd->handler(args[0], args[1]);
2006
        break;
2007
    case 3:
2008
        cmd->handler(args[0], args[1], args[2]);
2009
        break;
2010
    case 4:
2011
        cmd->handler(args[0], args[1], args[2], args[3]);
2012
        break;
2013
    case 5:
2014
        cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2015
        break;
2016
    case 6:
2017
        cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2018
        break;
2019
    default:
2020
        term_printf("unsupported number of arguments: %d\n", nb_args);
2021
        goto fail;
2022
    }
2023
 fail:
2024
    for(i = 0; i < MAX_ARGS; i++)
2025
        qemu_free(str_allocated[i]);
2026
    return;
2027
}
2028

    
2029
static void cmd_completion(const char *name, const char *list)
2030
{
2031
    const char *p, *pstart;
2032
    char cmd[128];
2033
    int len;
2034

    
2035
    p = list;
2036
    for(;;) {
2037
        pstart = p;
2038
        p = strchr(p, '|');
2039
        if (!p)
2040
            p = pstart + strlen(pstart);
2041
        len = p - pstart;
2042
        if (len > sizeof(cmd) - 2)
2043
            len = sizeof(cmd) - 2;
2044
        memcpy(cmd, pstart, len);
2045
        cmd[len] = '\0';
2046
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2047
            add_completion(cmd);
2048
        }
2049
        if (*p == '\0')
2050
            break;
2051
        p++;
2052
    }
2053
}
2054

    
2055
static void file_completion(const char *input)
2056
{
2057
    DIR *ffs;
2058
    struct dirent *d;
2059
    char path[1024];
2060
    char file[1024], file_prefix[1024];
2061
    int input_path_len;
2062
    const char *p;
2063

    
2064
    p = strrchr(input, '/'); 
2065
    if (!p) {
2066
        input_path_len = 0;
2067
        pstrcpy(file_prefix, sizeof(file_prefix), input);
2068
        strcpy(path, ".");
2069
    } else {
2070
        input_path_len = p - input + 1;
2071
        memcpy(path, input, input_path_len);
2072
        if (input_path_len > sizeof(path) - 1)
2073
            input_path_len = sizeof(path) - 1;
2074
        path[input_path_len] = '\0';
2075
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2076
    }
2077
#ifdef DEBUG_COMPLETION
2078
    term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2079
#endif
2080
    ffs = opendir(path);
2081
    if (!ffs)
2082
        return;
2083
    for(;;) {
2084
        struct stat sb;
2085
        d = readdir(ffs);
2086
        if (!d)
2087
            break;
2088
        if (strstart(d->d_name, file_prefix, NULL)) {
2089
            memcpy(file, input, input_path_len);
2090
            strcpy(file + input_path_len, d->d_name);
2091
            /* stat the file to find out if it's a directory.
2092
             * In that case add a slash to speed up typing long paths
2093
             */
2094
            stat(file, &sb);
2095
            if(S_ISDIR(sb.st_mode))
2096
                strcat(file, "/");
2097
            add_completion(file);
2098
        }
2099
    }
2100
    closedir(ffs);
2101
}
2102

    
2103
static void block_completion_it(void *opaque, const char *name)
2104
{
2105
    const char *input = opaque;
2106

    
2107
    if (input[0] == '\0' ||
2108
        !strncmp(name, (char *)input, strlen(input))) {
2109
        add_completion(name);
2110
    }
2111
}
2112

    
2113
/* NOTE: this parser is an approximate form of the real command parser */
2114
static void parse_cmdline(const char *cmdline,
2115
                         int *pnb_args, char **args)
2116
{
2117
    const char *p;
2118
    int nb_args, ret;
2119
    char buf[1024];
2120

    
2121
    p = cmdline;
2122
    nb_args = 0;
2123
    for(;;) {
2124
        while (isspace(*p))
2125
            p++;
2126
        if (*p == '\0')
2127
            break;
2128
        if (nb_args >= MAX_ARGS)
2129
            break;
2130
        ret = get_str(buf, sizeof(buf), &p);
2131
        args[nb_args] = qemu_strdup(buf);
2132
        nb_args++;
2133
        if (ret < 0)
2134
            break;
2135
    }
2136
    *pnb_args = nb_args;
2137
}
2138

    
2139
void readline_find_completion(const char *cmdline)
2140
{
2141
    const char *cmdname;
2142
    char *args[MAX_ARGS];
2143
    int nb_args, i, len;
2144
    const char *ptype, *str;
2145
    term_cmd_t *cmd;
2146

    
2147
    parse_cmdline(cmdline, &nb_args, args);
2148
#ifdef DEBUG_COMPLETION
2149
    for(i = 0; i < nb_args; i++) {
2150
        term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2151
    }
2152
#endif
2153

    
2154
    /* if the line ends with a space, it means we want to complete the
2155
       next arg */
2156
    len = strlen(cmdline);
2157
    if (len > 0 && isspace(cmdline[len - 1])) {
2158
        if (nb_args >= MAX_ARGS)
2159
            return;
2160
        args[nb_args++] = qemu_strdup("");
2161
    }
2162
    if (nb_args <= 1) {
2163
        /* command completion */
2164
        if (nb_args == 0)
2165
            cmdname = "";
2166
        else
2167
            cmdname = args[0];
2168
        completion_index = strlen(cmdname);
2169
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2170
            cmd_completion(cmdname, cmd->name);
2171
        }
2172
    } else {
2173
        /* find the command */
2174
        for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2175
            if (compare_cmd(args[0], cmd->name))
2176
                goto found;
2177
        }
2178
        return;
2179
    found:
2180
        ptype = cmd->args_type;
2181
        for(i = 0; i < nb_args - 2; i++) {
2182
            if (*ptype != '\0') {
2183
                ptype++;
2184
                while (*ptype == '?')
2185
                    ptype++;
2186
            }
2187
        }
2188
        str = args[nb_args - 1];
2189
        switch(*ptype) {
2190
        case 'F':
2191
            /* file completion */
2192
            completion_index = strlen(str);
2193
            file_completion(str);
2194
            break;
2195
        case 'B':
2196
            /* block device name completion */
2197
            completion_index = strlen(str);
2198
            bdrv_iterate(block_completion_it, (void *)str);
2199
            break;
2200
        case 's':
2201
            /* XXX: more generic ? */
2202
            if (!strcmp(cmd->name, "info")) {
2203
                completion_index = strlen(str);
2204
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2205
                    cmd_completion(str, cmd->name);
2206
                }
2207
            }
2208
            break;
2209
        default:
2210
            break;
2211
        }
2212
    }
2213
    for(i = 0; i < nb_args; i++)
2214
        qemu_free(args[i]);
2215
}
2216

    
2217
static int term_can_read(void *opaque)
2218
{
2219
    return 128;
2220
}
2221

    
2222
static void term_read(void *opaque, const uint8_t *buf, int size)
2223
{
2224
    int i;
2225
    for(i = 0; i < size; i++)
2226
        readline_handle_byte(buf[i]);
2227
}
2228

    
2229
static void monitor_start_input(void);
2230

    
2231
static void monitor_handle_command1(void *opaque, const char *cmdline)
2232
{
2233
    monitor_handle_command(cmdline);
2234
    monitor_start_input();
2235
}
2236

    
2237
static void monitor_start_input(void)
2238
{
2239
    readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2240
}
2241

    
2242
void monitor_init(CharDriverState *hd, int show_banner)
2243
{
2244
    monitor_hd = hd;
2245
    if (show_banner) {
2246
        term_printf("QEMU %s monitor - type 'help' for more information\n",
2247
                    QEMU_VERSION);
2248
    }
2249
    qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2250
    monitor_start_input();
2251
}
2252

    
2253
/* XXX: use threads ? */
2254
/* modal monitor readline */
2255
static int monitor_readline_started;
2256
static char *monitor_readline_buf;
2257
static int monitor_readline_buf_size;
2258

    
2259
static void monitor_readline_cb(void *opaque, const char *input)
2260
{
2261
    pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2262
    monitor_readline_started = 0;
2263
}
2264

    
2265
void monitor_readline(const char *prompt, int is_password,
2266
                      char *buf, int buf_size)
2267
{
2268
    if (is_password) {
2269
        qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2270
    }
2271
    readline_start(prompt, is_password, monitor_readline_cb, NULL);
2272
    monitor_readline_buf = buf;
2273
    monitor_readline_buf_size = buf_size;
2274
    monitor_readline_started = 1;
2275
    while (monitor_readline_started) {
2276
        main_loop_wait(10);
2277
    }
2278
}