Statistics
| Branch: | Revision:

root / monitor.c @ a541f297

History | View | Annotate | Download (37.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

    
27
//#define DEBUG
28

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

    
33
#define TERM_CMD_BUF_SIZE 4095
34
#define TERM_MAX_CMDS 64
35

    
36
#define IS_NORM 0
37
#define IS_ESC  1
38
#define IS_CSI  2
39

    
40
#define printf do_not_use_printf
41

    
42
static char term_cmd_buf[TERM_CMD_BUF_SIZE + 1];
43
static int term_cmd_buf_index;
44
static int term_cmd_buf_size;
45
static int term_esc_state;
46
static int term_esc_param;
47

    
48
static char *term_history[TERM_MAX_CMDS];
49
static int term_hist_entry;
50

    
51
/*
52
 * Supported types:
53
 * 
54
 * 'F'          filename
55
 * 's'          string (accept optional quote)
56
 * 'i'          integer
57
 * '/'          optional gdb-like print format (like "/10x")
58
 *
59
 * '?'          optional type (for 'F', 's' and 'i')
60
 *
61
 */
62

    
63
typedef struct term_cmd_t {
64
    const char *name;
65
    const char *args_type;
66
    void (*handler)();
67
    const char *params;
68
    const char *help;
69
} term_cmd_t;
70

    
71
static term_cmd_t term_cmds[];
72
static term_cmd_t info_cmds[];
73

    
74
void term_printf(const char *fmt, ...)
75
{
76
    va_list ap;
77
    va_start(ap, fmt);
78
    vprintf(fmt, ap);
79
    va_end(ap);
80
}
81

    
82
void term_flush(void)
83
{
84
    fflush(stdout);
85
}
86

    
87
static int compare_cmd(const char *name, const char *list)
88
{
89
    const char *p, *pstart;
90
    int len;
91
    len = strlen(name);
92
    p = list;
93
    for(;;) {
94
        pstart = p;
95
        p = strchr(p, '|');
96
        if (!p)
97
            p = pstart + strlen(pstart);
98
        if ((p - pstart) == len && !memcmp(pstart, name, len))
99
            return 1;
100
        if (*p == '\0')
101
            break;
102
        p++;
103
    }
104
    return 0;
105
}
106

    
107
static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
108
{
109
    term_cmd_t *cmd;
110

    
111
    for(cmd = cmds; cmd->name != NULL; cmd++) {
112
        if (!name || !strcmp(name, cmd->name))
113
            term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
114
    }
115
}
116

    
117
static void help_cmd(const char *name)
118
{
119
    if (name && !strcmp(name, "info")) {
120
        help_cmd1(info_cmds, "info ", NULL);
121
    } else {
122
        help_cmd1(term_cmds, "", name);
123
        if (name && !strcmp(name, "log")) {
124
            CPULogItem *item;
125
            term_printf("Log items (comma separated):\n");
126
            term_printf("%-10s %s\n", "none", "remove all logs");
127
            for(item = cpu_log_items; item->mask != 0; item++) {
128
                term_printf("%-10s %s\n", item->name, item->help);
129
            }
130
        }
131
    }
132
}
133

    
134
static void do_help(const char *name)
135
{
136
    help_cmd(name);
137
}
138

    
139
static void do_commit(void)
140
{
141
    int i;
142

    
143
    for (i = 0; i < MAX_DISKS; i++) {
144
        if (bs_table[i])
145
            bdrv_commit(bs_table[i]);
146
    }
147
}
148

    
149
static void do_info(const char *item)
150
{
151
    term_cmd_t *cmd;
152

    
153
    if (!item)
154
        goto help;
155
    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
156
        if (compare_cmd(item, cmd->name)) 
157
            goto found;
158
    }
159
 help:
160
    help_cmd("info");
161
    return;
162
 found:
163
    cmd->handler();
164
}
165

    
166
static void do_info_network(void)
167
{
168
    int i, j;
169
    NetDriverState *nd;
170
    
171
    for(i = 0; i < nb_nics; i++) {
172
        nd = &nd_table[i];
173
        term_printf("%d: ifname=%s macaddr=", i, nd->ifname);
174
        for(j = 0; j < 6; j++) {
175
            if (j > 0)
176
                term_printf(":");
177
            term_printf("%02x", nd->macaddr[j]);
178
        }
179
        term_printf("\n");
180
    }
181
}
182
 
183
static void do_info_block(void)
184
{
185
    bdrv_info();
186
}
187

    
188
static void do_info_registers(void)
189
{
190
#ifdef TARGET_I386
191
    cpu_dump_state(cpu_single_env, stdout, X86_DUMP_FPU | X86_DUMP_CCOP);
192
#else
193
    cpu_dump_state(cpu_single_env, stdout, 0);
194
#endif
195
}
196

    
197
static void do_info_history (void)
198
{
199
    int i;
200

    
201
    for (i = 0; i < TERM_MAX_CMDS; i++) {
202
        if (term_history[i] == NULL)
203
            break;
204
        term_printf("%d: '%s'\n", i, term_history[i]);
205
    }
206
}
207

    
208
static void do_quit(void)
209
{
210
    exit(0);
211
}
212

    
213
static int eject_device(BlockDriverState *bs, int force)
214
{
215
    if (bdrv_is_inserted(bs)) {
216
        if (!force) {
217
            if (!bdrv_is_removable(bs)) {
218
                term_printf("device is not removable\n");
219
                return -1;
220
            }
221
            if (bdrv_is_locked(bs)) {
222
                term_printf("device is locked\n");
223
                return -1;
224
            }
225
        }
226
        bdrv_close(bs);
227
    }
228
    return 0;
229
}
230

    
231
static void do_eject(int force, const char *filename)
232
{
233
    BlockDriverState *bs;
234

    
235
    term_printf("%d %s\n", force, filename);
236

    
237
    bs = bdrv_find(filename);
238
    if (!bs) {
239
        term_printf("device not found\n");
240
        return;
241
    }
242
    eject_device(bs, force);
243
}
244

    
245
static void do_change(const char *device, const char *filename)
246
{
247
    BlockDriverState *bs;
248

    
249
    bs = bdrv_find(device);
250
    if (!bs) {
251
        term_printf("device not found\n");
252
        return;
253
    }
254
    if (eject_device(bs, 0) < 0)
255
        return;
256
    bdrv_open(bs, filename, 0);
257
}
258

    
259
static void do_screen_dump(const char *filename)
260
{
261
    vga_screen_dump(filename);
262
}
263

    
264
static void do_log(const char *items)
265
{
266
    int mask;
267
    
268
    if (!strcmp(items, "none")) {
269
        mask = 0;
270
    } else {
271
        mask = cpu_str_to_log_mask(items);
272
        if (!mask) {
273
            help_cmd("log");
274
            return;
275
        }
276
    }
277
    cpu_set_log(mask);
278
}
279

    
280
static void do_savevm(const char *filename)
281
{
282
    if (qemu_savevm(filename) < 0)
283
        term_printf("I/O error when saving VM to '%s'\n", filename);
284
}
285

    
286
static void do_loadvm(const char *filename)
287
{
288
    if (qemu_loadvm(filename) < 0) 
289
        term_printf("I/O error when loading VM from '%s'\n", filename);
290
}
291

    
292
static void do_stop(void)
293
{
294
    vm_stop(EXCP_INTERRUPT);
295
}
296

    
297
static void do_cont(void)
298
{
299
    vm_start();
300
}
301

    
302
#ifdef CONFIG_GDBSTUB
303
static void do_gdbserver(int has_port, int port)
304
{
305
    if (!has_port)
306
        port = DEFAULT_GDBSTUB_PORT;
307
    if (gdbserver_start(port) < 0) {
308
        qemu_printf("Could not open gdbserver socket on port %d\n", port);
309
    } else {
310
        qemu_printf("Waiting gdb connection on port %d\n", port);
311
    }
312
}
313
#endif
314

    
315
static void term_printc(int c)
316
{
317
    term_printf("'");
318
    switch(c) {
319
    case '\'':
320
        term_printf("\\'");
321
        break;
322
    case '\\':
323
        term_printf("\\\\");
324
        break;
325
    case '\n':
326
        term_printf("\\n");
327
        break;
328
    case '\r':
329
        term_printf("\\r");
330
        break;
331
    default:
332
        if (c >= 32 && c <= 126) {
333
            term_printf("%c", c);
334
        } else {
335
            term_printf("\\x%02x", c);
336
        }
337
        break;
338
    }
339
    term_printf("'");
340
}
341

    
342
static void memory_dump(int count, int format, int wsize, 
343
                        target_ulong addr, int is_physical)
344
{
345
    int nb_per_line, l, line_size, i, max_digits, len;
346
    uint8_t buf[16];
347
    uint64_t v;
348

    
349
    if (format == 'i') {
350
        int flags;
351
        flags = 0;
352
#ifdef TARGET_I386
353
        /* we use the current CS size */
354
        if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK))
355
            flags = 1;
356
#endif        
357
        monitor_disas(addr, count, is_physical, flags);
358
        return;
359
    }
360

    
361
    len = wsize * count;
362
    if (wsize == 1)
363
        line_size = 8;
364
    else
365
        line_size = 16;
366
    nb_per_line = line_size / wsize;
367
    max_digits = 0;
368

    
369
    switch(format) {
370
    case 'o':
371
        max_digits = (wsize * 8 + 2) / 3;
372
        break;
373
    default:
374
    case 'x':
375
        max_digits = (wsize * 8) / 4;
376
        break;
377
    case 'u':
378
    case 'd':
379
        max_digits = (wsize * 8 * 10 + 32) / 33;
380
        break;
381
    case 'c':
382
        wsize = 1;
383
        break;
384
    }
385

    
386
    while (len > 0) {
387
        term_printf("0x%08x:", addr);
388
        l = len;
389
        if (l > line_size)
390
            l = line_size;
391
        if (is_physical) {
392
            cpu_physical_memory_rw(addr, buf, l, 0);
393
        } else {
394
            cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0);
395
        }
396
        i = 0; 
397
        while (i < l) {
398
            switch(wsize) {
399
            default:
400
            case 1:
401
                v = ldub_raw(buf + i);
402
                break;
403
            case 2:
404
                v = lduw_raw(buf + i);
405
                break;
406
            case 4:
407
                v = ldl_raw(buf + i);
408
                break;
409
            case 8:
410
                v = ldq_raw(buf + i);
411
                break;
412
            }
413
            term_printf(" ");
414
            switch(format) {
415
            case 'o':
416
                term_printf("%#*llo", max_digits, v);
417
                break;
418
            case 'x':
419
                term_printf("0x%0*llx", max_digits, v);
420
                break;
421
            case 'u':
422
                term_printf("%*llu", max_digits, v);
423
                break;
424
            case 'd':
425
                term_printf("%*lld", max_digits, v);
426
                break;
427
            case 'c':
428
                term_printc(v);
429
                break;
430
            }
431
            i += wsize;
432
        }
433
        term_printf("\n");
434
        addr += l;
435
        len -= l;
436
    }
437
}
438

    
439
static void do_memory_dump(int count, int format, int size, int addr)
440
{
441
    memory_dump(count, format, size, addr, 0);
442
}
443

    
444
static void do_physical_memory_dump(int count, int format, int size, int addr)
445
{
446
    memory_dump(count, format, size, addr, 1);
447
}
448

    
449
static void do_print(int count, int format, int size, int val)
450
{
451
    switch(format) {
452
    case 'o':
453
        term_printf("%#o", val);
454
        break;
455
    case 'x':
456
        term_printf("%#x", val);
457
        break;
458
    case 'u':
459
        term_printf("%u", val);
460
        break;
461
    default:
462
    case 'd':
463
        term_printf("%d", val);
464
        break;
465
    case 'c':
466
        term_printc(val);
467
        break;
468
    }
469
    term_printf("\n");
470
}
471

    
472
static term_cmd_t term_cmds[] = {
473
    { "help|?", "s?", do_help, 
474
      "[cmd]", "show the help" },
475
    { "commit", "", do_commit, 
476
      "", "commit changes to the disk images (if -snapshot is used)" },
477
    { "info", "s?", do_info,
478
      "subcommand", "show various information about the system state" },
479
    { "q|quit", "", do_quit,
480
      "", "quit the emulator" },
481
    { "eject", "-fs", do_eject,
482
      "[-f] device", "eject a removable media (use -f to force it)" },
483
    { "change", "sF", do_change,
484
      "device filename", "change a removable media" },
485
    { "screendump", "F", do_screen_dump, 
486
      "filename", "save screen into PPM image 'filename'" },
487
    { "log", "s", do_log,
488
      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" }, 
489
    { "savevm", "F", do_savevm,
490
      "filename", "save the whole virtual machine state to 'filename'" }, 
491
    { "loadvm", "F", do_loadvm,
492
      "filename", "restore the whole virtual machine state from 'filename'" }, 
493
    { "stop", "", do_stop, 
494
      "", "stop emulation", },
495
    { "c|cont", "", do_cont, 
496
      "", "resume emulation", },
497
#ifdef CONFIG_GDBSTUB
498
    { "gdbserver", "i?", do_gdbserver, 
499
      "[port]", "start gdbserver session (default port=1234)", },
500
#endif
501
    { "x", "/i", do_memory_dump, 
502
      "/fmt addr", "virtual memory dump starting at 'addr'", },
503
    { "xp", "/i", do_physical_memory_dump, 
504
      "/fmt addr", "physical memory dump starting at 'addr'", },
505
    { "p|print", "/i", do_print, 
506
      "/fmt expr", "print expression value (use $reg for CPU register access)", },
507
    { NULL, NULL, }, 
508
};
509

    
510
static term_cmd_t info_cmds[] = {
511
    { "network", "", do_info_network,
512
      "", "show the network state" },
513
    { "block", "", do_info_block,
514
      "", "show the block devices" },
515
    { "registers", "", do_info_registers,
516
      "", "show the cpu registers" },
517
    { "history", "", do_info_history,
518
      "", "show the command line history", },
519
    { NULL, NULL, },
520
};
521

    
522
/*******************************************************************/
523

    
524
static const char *pch;
525
static jmp_buf expr_env;
526

    
527
typedef struct MonitorDef {
528
    const char *name;
529
    int offset;
530
    int (*get_value)(struct MonitorDef *md);
531
} MonitorDef;
532

    
533
#if defined(TARGET_PPC)
534
static int monitor_get_ccr (struct MonitorDef *md)
535
{
536
    unsigned int u;
537
    int i;
538

    
539
    u = 0;
540
    for (i = 0; i < 8; i++)
541
        u |= cpu_single_env->crf[i] << (32 - (4 * i));
542

    
543
    return u;
544
}
545

    
546
static int monitor_get_msr (struct MonitorDef *md)
547
{
548
    return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
549
        (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
550
        (cpu_single_env->msr[MSR_EE] << MSR_EE) |
551
        (cpu_single_env->msr[MSR_PR] << MSR_PR) |
552
        (cpu_single_env->msr[MSR_FP] << MSR_FP) |
553
        (cpu_single_env->msr[MSR_ME] << MSR_ME) |
554
        (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
555
        (cpu_single_env->msr[MSR_SE] << MSR_SE) |
556
        (cpu_single_env->msr[MSR_BE] << MSR_BE) |
557
        (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
558
        (cpu_single_env->msr[MSR_IP] << MSR_IP) |
559
        (cpu_single_env->msr[MSR_IR] << MSR_IR) |
560
        (cpu_single_env->msr[MSR_DR] << MSR_DR) |
561
        (cpu_single_env->msr[MSR_RI] << MSR_RI) |
562
        (cpu_single_env->msr[MSR_LE] << MSR_LE);
563
}
564

    
565
static int monitor_get_xer (struct MonitorDef *md)
566
{
567
    return (cpu_single_env->xer[XER_SO] << XER_SO) |
568
        (cpu_single_env->xer[XER_OV] << XER_OV) |
569
        (cpu_single_env->xer[XER_CA] << XER_CA) |
570
        (cpu_single_env->xer[XER_BC] << XER_BC);
571
}
572
#endif
573

    
574
static MonitorDef monitor_defs[] = {
575
#ifdef TARGET_I386
576
    { "eax", offsetof(CPUState, regs[0]) },
577
    { "ecx", offsetof(CPUState, regs[1]) },
578
    { "edx", offsetof(CPUState, regs[2]) },
579
    { "ebx", offsetof(CPUState, regs[3]) },
580
    { "esp|sp", offsetof(CPUState, regs[4]) },
581
    { "ebp|fp", offsetof(CPUState, regs[5]) },
582
    { "esi", offsetof(CPUState, regs[6]) },
583
    { "esi", offsetof(CPUState, regs[7]) },
584
    { "eflags", offsetof(CPUState, eflags) },
585
    { "eip|pc", offsetof(CPUState, eip) },
586
#elif defined(TARGET_PPC)
587
    { "r0", offsetof(CPUState, gpr[0]) },
588
    { "r1", offsetof(CPUState, gpr[1]) },
589
    { "r2", offsetof(CPUState, gpr[2]) },
590
    { "r3", offsetof(CPUState, gpr[3]) },
591
    { "r4", offsetof(CPUState, gpr[4]) },
592
    { "r5", offsetof(CPUState, gpr[5]) },
593
    { "r6", offsetof(CPUState, gpr[6]) },
594
    { "r7", offsetof(CPUState, gpr[7]) },
595
    { "r8", offsetof(CPUState, gpr[8]) },
596
    { "r9", offsetof(CPUState, gpr[9]) },
597
    { "r10", offsetof(CPUState, gpr[10]) },
598
    { "r11", offsetof(CPUState, gpr[11]) },
599
    { "r12", offsetof(CPUState, gpr[12]) },
600
    { "r13", offsetof(CPUState, gpr[13]) },
601
    { "r14", offsetof(CPUState, gpr[14]) },
602
    { "r15", offsetof(CPUState, gpr[15]) },
603
    { "r16", offsetof(CPUState, gpr[16]) },
604
    { "r17", offsetof(CPUState, gpr[17]) },
605
    { "r18", offsetof(CPUState, gpr[18]) },
606
    { "r19", offsetof(CPUState, gpr[19]) },
607
    { "r20", offsetof(CPUState, gpr[20]) },
608
    { "r21", offsetof(CPUState, gpr[21]) },
609
    { "r22", offsetof(CPUState, gpr[22]) },
610
    { "r23", offsetof(CPUState, gpr[23]) },
611
    { "r24", offsetof(CPUState, gpr[24]) },
612
    { "r25", offsetof(CPUState, gpr[25]) },
613
    { "r26", offsetof(CPUState, gpr[26]) },
614
    { "r27", offsetof(CPUState, gpr[27]) },
615
    { "r28", offsetof(CPUState, gpr[28]) },
616
    { "r29", offsetof(CPUState, gpr[29]) },
617
    { "r30", offsetof(CPUState, gpr[30]) },
618
    { "r31", offsetof(CPUState, gpr[31]) },
619
    { "lr", offsetof(CPUState, lr) },
620
    { "ctr", offsetof(CPUState, ctr) },
621
    { "decr", offsetof(CPUState, decr) },
622
    { "ccr", 0, &monitor_get_ccr, },
623
    { "msr", 0, &monitor_get_msr, },
624
    { "xer", 0, &monitor_get_xer, },
625
    { "tbu", offsetof(CPUState, tb[0]) },
626
    { "tbl", offsetof(CPUState, tb[1]) },
627
    { "sdr1", offsetof(CPUState, sdr1) },
628
    { "sr0", offsetof(CPUState, sr[0]) },
629
    { "sr1", offsetof(CPUState, sr[1]) },
630
    { "sr2", offsetof(CPUState, sr[2]) },
631
    { "sr3", offsetof(CPUState, sr[3]) },
632
    { "sr4", offsetof(CPUState, sr[4]) },
633
    { "sr5", offsetof(CPUState, sr[5]) },
634
    { "sr6", offsetof(CPUState, sr[6]) },
635
    { "sr7", offsetof(CPUState, sr[7]) },
636
    { "sr8", offsetof(CPUState, sr[8]) },
637
    { "sr9", offsetof(CPUState, sr[9]) },
638
    { "sr10", offsetof(CPUState, sr[10]) },
639
    { "sr11", offsetof(CPUState, sr[11]) },
640
    { "sr12", offsetof(CPUState, sr[12]) },
641
    { "sr13", offsetof(CPUState, sr[13]) },
642
    { "sr14", offsetof(CPUState, sr[14]) },
643
    { "sr15", offsetof(CPUState, sr[15]) },
644
    /* Too lazy to put BATs and SPRs ... */
645
#endif
646
    { NULL },
647
};
648

    
649
static void expr_error(const char *fmt) 
650
{
651
    term_printf(fmt);
652
    term_printf("\n");
653
    longjmp(expr_env, 1);
654
}
655

    
656
static int get_monitor_def(int *pval, const char *name)
657
{
658
    MonitorDef *md;
659
    for(md = monitor_defs; md->name != NULL; md++) {
660
        if (compare_cmd(name, md->name)) {
661
            if (md->get_value) {
662
                *pval = md->get_value(md);
663
            } else {
664
                *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset);
665
            }
666
            return 0;
667
        }
668
    }
669
    return -1;
670
}
671

    
672
static void next(void)
673
{
674
    if (pch != '\0') {
675
        pch++;
676
        while (isspace(*pch))
677
            pch++;
678
    }
679
}
680

    
681
static int expr_sum(void);
682

    
683
static int expr_unary(void)
684
{
685
    int n;
686
    char *p;
687

    
688
    switch(*pch) {
689
    case '+':
690
        next();
691
        n = expr_unary();
692
        break;
693
    case '-':
694
        next();
695
        n = -expr_unary();
696
        break;
697
    case '~':
698
        next();
699
        n = ~expr_unary();
700
        break;
701
    case '(':
702
        next();
703
        n = expr_sum();
704
        if (*pch != ')') {
705
            expr_error("')' expected");
706
        }
707
        next();
708
        break;
709
    case '$':
710
        {
711
            char buf[128], *q;
712
            
713
            pch++;
714
            q = buf;
715
            while ((*pch >= 'a' && *pch <= 'z') ||
716
                   (*pch >= 'A' && *pch <= 'Z') ||
717
                   (*pch >= '0' && *pch <= '9') ||
718
                   *pch == '_') {
719
                if ((q - buf) < sizeof(buf) - 1)
720
                    *q++ = *pch;
721
                pch++;
722
            }
723
            while (isspace(*pch))
724
                pch++;
725
            *q = 0;
726
            if (get_monitor_def(&n, buf))
727
                expr_error("unknown register");
728
        }
729
        break;
730
    case '\0':
731
        expr_error("unexpected end of expression");
732
        n = 0;
733
        break;
734
    default:
735
        n = strtoul(pch, &p, 0);
736
        if (pch == p) {
737
            expr_error("invalid char in expression");
738
        }
739
        pch = p;
740
        while (isspace(*pch))
741
            pch++;
742
        break;
743
    }
744
    return n;
745
}
746

    
747

    
748
static int expr_prod(void)
749
{
750
    int val, val2, op;
751

    
752
    val = expr_unary();
753
    for(;;) {
754
        op = *pch;
755
        if (op != '*' && op != '/' && op != '%')
756
            break;
757
        next();
758
        val2 = expr_unary();
759
        switch(op) {
760
        default:
761
        case '*':
762
            val *= val2;
763
            break;
764
        case '/':
765
        case '%':
766
            if (val2 == 0) 
767
                expr_error("divison by zero");
768
            if (op == '/')
769
                val /= val2;
770
            else
771
                val %= val2;
772
            break;
773
        }
774
    }
775
    return val;
776
}
777

    
778
static int expr_logic(void)
779
{
780
    int val, val2, op;
781

    
782
    val = expr_prod();
783
    for(;;) {
784
        op = *pch;
785
        if (op != '&' && op != '|' && op != '^')
786
            break;
787
        next();
788
        val2 = expr_prod();
789
        switch(op) {
790
        default:
791
        case '&':
792
            val &= val2;
793
            break;
794
        case '|':
795
            val |= val2;
796
            break;
797
        case '^':
798
            val ^= val2;
799
            break;
800
        }
801
    }
802
    return val;
803
}
804

    
805
static int expr_sum(void)
806
{
807
    int val, val2, op;
808

    
809
    val = expr_logic();
810
    for(;;) {
811
        op = *pch;
812
        if (op != '+' && op != '-')
813
            break;
814
        next();
815
        val2 = expr_logic();
816
        if (op == '+')
817
            val += val2;
818
        else
819
            val -= val2;
820
    }
821
    return val;
822
}
823

    
824
static int get_expr(int *pval, const char **pp)
825
{
826
    pch = *pp;
827
    if (setjmp(expr_env)) {
828
        *pp = pch;
829
        return -1;
830
    }
831
    while (isspace(*pch))
832
        pch++;
833
    *pval = expr_sum();
834
    *pp = pch;
835
    return 0;
836
}
837

    
838
static int get_str(char *buf, int buf_size, const char **pp)
839
{
840
    const char *p;
841
    char *q;
842
    int c;
843

    
844
    p = *pp;
845
    while (isspace(*p))
846
        p++;
847
    if (*p == '\0') {
848
    fail:
849
        *pp = p;
850
        return -1;
851
    }
852
    q = buf;
853
    if (*p == '\"') {
854
        p++;
855
        while (*p != '\0' && *p != '\"') {
856
            if (*p == '\\') {
857
                p++;
858
                c = *p++;
859
                switch(c) {
860
                case 'n':
861
                    c = '\n';
862
                    break;
863
                case 'r':
864
                    c = '\r';
865
                    break;
866
                case '\\':
867
                case '\'':
868
                case '\"':
869
                    break;
870
                default:
871
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
872
                    goto fail;
873
                }
874
                if ((q - buf) < buf_size - 1) {
875
                    *q++ = c;
876
                }
877
            } else {
878
                if ((q - buf) < buf_size - 1) {
879
                    *q++ = *p;
880
                }
881
                p++;
882
            }
883
        }
884
        if (*p != '\"') {
885
            qemu_printf("untermintated string\n");
886
            goto fail;
887
        }
888
        p++;
889
    } else {
890
        while (*p != '\0' && !isspace(*p)) {
891
            if ((q - buf) < buf_size - 1) {
892
                *q++ = *p;
893
            }
894
            p++;
895
        }
896
        *q = '\0';
897
    }
898
    *pp = p;
899
    return 0;
900
}
901

    
902
static int default_fmt_format = 'x';
903
static int default_fmt_size = 4;
904

    
905
#define MAX_ARGS 16
906

    
907
static void term_handle_command(const char *cmdline)
908
{
909
    const char *p, *pstart, *typestr;
910
    char *q;
911
    int c, nb_args, len, i, has_arg;
912
    term_cmd_t *cmd;
913
    char cmdname[256];
914
    char buf[1024];
915
    void *str_allocated[MAX_ARGS];
916
    void *args[MAX_ARGS];
917

    
918
#ifdef DEBUG
919
    term_printf("command='%s'\n", cmdline);
920
#endif
921
    
922
    /* extract the command name */
923
    p = cmdline;
924
    q = cmdname;
925
    while (isspace(*p))
926
        p++;
927
    if (*p == '\0')
928
        return;
929
    pstart = p;
930
    while (*p != '\0' && *p != '/' && !isspace(*p))
931
        p++;
932
    len = p - pstart;
933
    if (len > sizeof(cmdname) - 1)
934
        len = sizeof(cmdname) - 1;
935
    memcpy(cmdname, pstart, len);
936
    cmdname[len] = '\0';
937
    
938
    /* find the command */
939
    for(cmd = term_cmds; cmd->name != NULL; cmd++) {
940
        if (compare_cmd(cmdname, cmd->name)) 
941
            goto found;
942
    }
943
    term_printf("unknown command: '%s'\n", cmdname);
944
    return;
945
 found:
946

    
947
    for(i = 0; i < MAX_ARGS; i++)
948
        str_allocated[i] = NULL;
949
    
950
    /* parse the parameters */
951
    typestr = cmd->args_type;
952
    nb_args = 0;
953
    for(;;) {
954
        c = *typestr;
955
        if (c == '\0')
956
            break;
957
        typestr++;
958
        switch(c) {
959
        case 'F':
960
        case 's':
961
            {
962
                int ret;
963
                char *str;
964
                
965
                while (isspace(*p)) 
966
                    p++;
967
                if (*typestr == '?') {
968
                    typestr++;
969
                    if (*p == '\0') {
970
                        /* no optional string: NULL argument */
971
                        str = NULL;
972
                        goto add_str;
973
                    }
974
                }
975
                ret = get_str(buf, sizeof(buf), &p);
976
                if (ret < 0) {
977
                    if (c == 'F')
978
                        term_printf("%s: filename expected\n", cmdname);
979
                    else
980
                        term_printf("%s: string expected\n", cmdname);
981
                    goto fail;
982
                }
983
                str = qemu_malloc(strlen(buf) + 1);
984
                strcpy(str, buf);
985
                str_allocated[nb_args] = str;
986
            add_str:
987
                if (nb_args >= MAX_ARGS) {
988
                error_args:
989
                    term_printf("%s: too many arguments\n", cmdname);
990
                    goto fail;
991
                }
992
                args[nb_args++] = str;
993
            }
994
            break;
995
        case '/':
996
            {
997
                int count, format, size;
998
                
999
                while (isspace(*p))
1000
                    p++;
1001
                if (*p == '/') {
1002
                    /* format found */
1003
                    p++;
1004
                    count = 1;
1005
                    if (isdigit(*p)) {
1006
                        count = 0;
1007
                        while (isdigit(*p)) {
1008
                            count = count * 10 + (*p - '0');
1009
                            p++;
1010
                        }
1011
                    }
1012
                    size = -1;
1013
                    format = -1;
1014
                    for(;;) {
1015
                        switch(*p) {
1016
                        case 'o':
1017
                        case 'd':
1018
                        case 'u':
1019
                        case 'x':
1020
                        case 'i':
1021
                        case 'c':
1022
                            format = *p++;
1023
                            break;
1024
                        case 'b':
1025
                            size = 1;
1026
                            p++;
1027
                            break;
1028
                        case 'h':
1029
                            size = 2;
1030
                            p++;
1031
                            break;
1032
                        case 'w':
1033
                            size = 4;
1034
                            p++;
1035
                            break;
1036
                        case 'g':
1037
                        case 'L':
1038
                            size = 8;
1039
                            p++;
1040
                            break;
1041
                        default:
1042
                            goto next;
1043
                        }
1044
                    }
1045
                next:
1046
                    if (*p != '\0' && !isspace(*p)) {
1047
                        term_printf("invalid char in format: '%c'\n", *p);
1048
                        goto fail;
1049
                    }
1050
                    if (size < 0)
1051
                        size = default_fmt_size;
1052
                    if (format < 0)
1053
                        format = default_fmt_format;
1054
                    default_fmt_size = size;
1055
                    default_fmt_format = format;
1056
                } else {
1057
                    count = 1;
1058
                    format = default_fmt_format;
1059
                    size = default_fmt_size;
1060
                }
1061
                if (nb_args + 3 > MAX_ARGS)
1062
                    goto error_args;
1063
                args[nb_args++] = (void*)count;
1064
                args[nb_args++] = (void*)format;
1065
                args[nb_args++] = (void*)size;
1066
            }
1067
            break;
1068
        case 'i':
1069
            {
1070
                int val;
1071
                while (isspace(*p)) 
1072
                    p++;
1073
                if (*typestr == '?') {
1074
                    typestr++;
1075
                    if (*p == '\0')
1076
                        has_arg = 0;
1077
                    else
1078
                        has_arg = 1;
1079
                    if (nb_args >= MAX_ARGS)
1080
                        goto error_args;
1081
                    args[nb_args++] = (void *)has_arg;
1082
                    if (!has_arg) {
1083
                        if (nb_args >= MAX_ARGS)
1084
                            goto error_args;
1085
                        val = -1;
1086
                        goto add_num;
1087
                    }
1088
                }
1089
                if (get_expr(&val, &p))
1090
                    goto fail;
1091
            add_num:
1092
                if (nb_args >= MAX_ARGS)
1093
                    goto error_args;
1094
                args[nb_args++] = (void *)val;
1095
            }
1096
            break;
1097
        case '-':
1098
            {
1099
                int has_option;
1100
                /* option */
1101
                
1102
                c = *typestr++;
1103
                if (c == '\0')
1104
                    goto bad_type;
1105
                while (isspace(*p)) 
1106
                    p++;
1107
                has_option = 0;
1108
                if (*p == '-') {
1109
                    p++;
1110
                    if (*p != c) {
1111
                        term_printf("%s: unsupported option -%c\n", 
1112
                                    cmdname, *p);
1113
                        goto fail;
1114
                    }
1115
                    p++;
1116
                    has_option = 1;
1117
                }
1118
                if (nb_args >= MAX_ARGS)
1119
                    goto error_args;
1120
                args[nb_args++] = (void *)has_option;
1121
            }
1122
            break;
1123
        default:
1124
        bad_type:
1125
            term_printf("%s: unknown type '%c'\n", cmdname, c);
1126
            goto fail;
1127
        }
1128
    }
1129
    /* check that all arguments were parsed */
1130
    while (isspace(*p))
1131
        p++;
1132
    if (*p != '\0') {
1133
        term_printf("%s: extraneous characters at the end of line\n", 
1134
                    cmdname);
1135
        goto fail;
1136
    }
1137

    
1138
    switch(nb_args) {
1139
    case 0:
1140
        cmd->handler();
1141
        break;
1142
    case 1:
1143
        cmd->handler(args[0]);
1144
        break;
1145
    case 2:
1146
        cmd->handler(args[0], args[1]);
1147
        break;
1148
    case 3:
1149
        cmd->handler(args[0], args[1], args[2]);
1150
        break;
1151
    case 4:
1152
        cmd->handler(args[0], args[1], args[2], args[3]);
1153
        break;
1154
    case 5:
1155
        cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1156
        break;
1157
    default:
1158
        term_printf("unsupported number of arguments: %d\n", nb_args);
1159
        goto fail;
1160
    }
1161
 fail:
1162
    for(i = 0; i < MAX_ARGS; i++)
1163
        qemu_free(str_allocated[i]);
1164
    return;
1165
}
1166

    
1167
static void term_show_prompt(void)
1168
{
1169
    term_printf("(qemu) ");
1170
    fflush(stdout);
1171
    term_cmd_buf_index = 0;
1172
    term_cmd_buf_size = 0;
1173
    term_esc_state = IS_NORM;
1174
}
1175

    
1176
static void term_print_cmdline (const char *cmdline)
1177
{
1178
    term_show_prompt();
1179
    term_printf(cmdline);
1180
    term_flush();
1181
}
1182

    
1183
static void term_insert_char(int ch)
1184
{
1185
    if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) {
1186
        memmove(term_cmd_buf + term_cmd_buf_index + 1,
1187
                term_cmd_buf + term_cmd_buf_index,
1188
                term_cmd_buf_size - term_cmd_buf_index);
1189
        term_cmd_buf[term_cmd_buf_index] = ch;
1190
        term_cmd_buf_size++;
1191
        term_printf("\033[@%c", ch);
1192
        term_cmd_buf_index++;
1193
        term_flush();
1194
    }
1195
}
1196

    
1197
static void term_backward_char(void)
1198
{
1199
    if (term_cmd_buf_index > 0) {
1200
        term_cmd_buf_index--;
1201
        term_printf("\033[D");
1202
        term_flush();
1203
    }
1204
}
1205

    
1206
static void term_forward_char(void)
1207
{
1208
    if (term_cmd_buf_index < term_cmd_buf_size) {
1209
        term_cmd_buf_index++;
1210
        term_printf("\033[C");
1211
        term_flush();
1212
    }
1213
}
1214

    
1215
static void term_delete_char(void)
1216
{
1217
    if (term_cmd_buf_index < term_cmd_buf_size) {
1218
        memmove(term_cmd_buf + term_cmd_buf_index,
1219
                term_cmd_buf + term_cmd_buf_index + 1,
1220
                term_cmd_buf_size - term_cmd_buf_index - 1);
1221
        term_printf("\033[P");
1222
        term_cmd_buf_size--;
1223
        term_flush();
1224
    }
1225
}
1226

    
1227
static void term_backspace(void)
1228
{
1229
    if (term_cmd_buf_index > 0) {
1230
        term_backward_char();
1231
        term_delete_char();
1232
    }
1233
}
1234

    
1235
static void term_bol(void)
1236
{
1237
    while (term_cmd_buf_index > 0)
1238
        term_backward_char();
1239
}
1240

    
1241
static void term_eol(void)
1242
{
1243
    while (term_cmd_buf_index < term_cmd_buf_size)
1244
        term_forward_char();
1245
}
1246

    
1247
static void term_up_char(void)
1248
{
1249
    int idx;
1250

    
1251
    if (term_hist_entry == 0)
1252
        return;
1253
    if (term_hist_entry == -1) {
1254
        /* Find latest entry */
1255
        for (idx = 0; idx < TERM_MAX_CMDS; idx++) {
1256
            if (term_history[idx] == NULL)
1257
                break;
1258
        }
1259
        term_hist_entry = idx;
1260
    }
1261
    term_hist_entry--;
1262
    if (term_hist_entry >= 0) {
1263
        strcpy(term_cmd_buf, term_history[term_hist_entry]);
1264
        term_printf("\n");
1265
        term_print_cmdline(term_cmd_buf);
1266
        term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
1267
    }
1268
}
1269

    
1270
static void term_down_char(void)
1271
{
1272
    if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1)
1273
        return;
1274
    if (term_history[++term_hist_entry] != NULL) {
1275
        strcpy(term_cmd_buf, term_history[term_hist_entry]);
1276
    } else {
1277
        term_hist_entry = -1;
1278
    }
1279
    term_printf("\n");
1280
    term_print_cmdline(term_cmd_buf);
1281
    term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
1282
}
1283

    
1284
static void term_hist_add(const char *cmdline)
1285
{
1286
    char *hist_entry, *new_entry;
1287
    int idx;
1288

    
1289
    if (cmdline[0] == '\0')
1290
        return;
1291
    new_entry = NULL;
1292
    if (term_hist_entry != -1) {
1293
        /* We were editing an existing history entry: replace it */
1294
        hist_entry = term_history[term_hist_entry];
1295
        idx = term_hist_entry;
1296
        if (strcmp(hist_entry, cmdline) == 0) {
1297
            goto same_entry;
1298
        }
1299
    }
1300
    /* Search cmdline in history buffers */
1301
    for (idx = 0; idx < TERM_MAX_CMDS; idx++) {
1302
        hist_entry = term_history[idx];
1303
        if (hist_entry == NULL)
1304
            break;
1305
        if (strcmp(hist_entry, cmdline) == 0) {
1306
        same_entry:
1307
            new_entry = hist_entry;
1308
            /* Put this entry at the end of history */
1309
            memmove(&term_history[idx], &term_history[idx + 1],
1310
                    &term_history[TERM_MAX_CMDS] - &term_history[idx + 1]);
1311
            term_history[TERM_MAX_CMDS - 1] = NULL;
1312
            for (; idx < TERM_MAX_CMDS; idx++) {
1313
                if (term_history[idx] == NULL)
1314
                    break;
1315
            }
1316
            break;
1317
        }
1318
    }
1319
    if (idx == TERM_MAX_CMDS) {
1320
        /* Need to get one free slot */
1321
        free(term_history[0]);
1322
        memcpy(term_history, &term_history[1],
1323
               &term_history[TERM_MAX_CMDS] - &term_history[1]);
1324
        term_history[TERM_MAX_CMDS - 1] = NULL;
1325
        idx = TERM_MAX_CMDS - 1;
1326
    }
1327
    if (new_entry == NULL)
1328
        new_entry = strdup(cmdline);
1329
    term_history[idx] = new_entry;
1330
    term_hist_entry = -1;
1331
}
1332

    
1333
/* return true if command handled */
1334
static void term_handle_byte(int ch)
1335
{
1336
    switch(term_esc_state) {
1337
    case IS_NORM:
1338
        switch(ch) {
1339
        case 1:
1340
            term_bol();
1341
            break;
1342
        case 5:
1343
            term_eol();
1344
            break;
1345
        case 10:
1346
        case 13:
1347
            term_cmd_buf[term_cmd_buf_size] = '\0';
1348
            term_hist_add(term_cmd_buf);
1349
            term_printf("\n");
1350
            term_handle_command(term_cmd_buf);
1351
            term_show_prompt();
1352
            break;
1353
        case 27:
1354
            term_esc_state = IS_ESC;
1355
            break;
1356
        case 127:
1357
        case 8:
1358
            term_backspace();
1359
            break;
1360
        case 155:
1361
            term_esc_state = IS_CSI;
1362
            break;
1363
        default:
1364
            if (ch >= 32) {
1365
                term_insert_char(ch);
1366
            }
1367
            break;
1368
        }
1369
        break;
1370
    case IS_ESC:
1371
        if (ch == '[') {
1372
            term_esc_state = IS_CSI;
1373
            term_esc_param = 0;
1374
        } else {
1375
            term_esc_state = IS_NORM;
1376
        }
1377
        break;
1378
    case IS_CSI:
1379
        switch(ch) {
1380
        case 'A':
1381
        case 'F':
1382
            term_up_char();
1383
            break;
1384
        case 'B':
1385
        case 'E':
1386
            term_down_char();
1387
            break;
1388
        case 'D':
1389
            term_backward_char();
1390
            break;
1391
        case 'C':
1392
            term_forward_char();
1393
            break;
1394
        case '0' ... '9':
1395
            term_esc_param = term_esc_param * 10 + (ch - '0');
1396
            goto the_end;
1397
        case '~':
1398
            switch(term_esc_param) {
1399
            case 1:
1400
                term_bol();
1401
                break;
1402
            case 3:
1403
                term_delete_char();
1404
                break;
1405
            case 4:
1406
                term_eol();
1407
                break;
1408
            }
1409
            break;
1410
        default:
1411
            break;
1412
        }
1413
        term_esc_state = IS_NORM;
1414
    the_end:
1415
        break;
1416
    }
1417
}
1418

    
1419
/*************************************************************/
1420
/* serial console support */
1421

    
1422
#define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1423

    
1424
static int term_got_escape, term_command;
1425

    
1426
void term_print_help(void)
1427
{
1428
    term_printf("\n"
1429
                "C-a h    print this help\n"
1430
                "C-a x    exit emulatior\n"
1431
                "C-a s    save disk data back to file (if -snapshot)\n"
1432
                "C-a b    send break (magic sysrq)\n"
1433
                "C-a c    switch between console and monitor\n"
1434
                "C-a C-a  send C-a\n"
1435
                );
1436
}
1437

    
1438
/* called when a char is received */
1439
static void term_received_byte(int ch)
1440
{
1441
    if (!serial_console) {
1442
        /* if no serial console, handle every command */
1443
        term_handle_byte(ch);
1444
    } else {
1445
        if (term_got_escape) {
1446
            term_got_escape = 0;
1447
            switch(ch) {
1448
            case 'h':
1449
                term_print_help();
1450
                break;
1451
            case 'x':
1452
                exit(0);
1453
                break;
1454
            case 's': 
1455
                {
1456
                    int i;
1457
                    for (i = 0; i < MAX_DISKS; i++) {
1458
                        if (bs_table[i])
1459
                            bdrv_commit(bs_table[i]);
1460
                    }
1461
                }
1462
                break;
1463
            case 'b':
1464
                if (serial_console)
1465
                    serial_receive_break(serial_console);
1466
                break;
1467
            case 'c':
1468
                if (!term_command) {
1469
                    term_show_prompt();
1470
                    term_command = 1;
1471
                } else {
1472
                    term_command = 0;
1473
                }
1474
                break;
1475
            case TERM_ESCAPE:
1476
                goto send_char;
1477
            }
1478
        } else if (ch == TERM_ESCAPE) {
1479
            term_got_escape = 1;
1480
        } else {
1481
        send_char:
1482
            if (term_command) {
1483
                term_handle_byte(ch);
1484
            } else {
1485
                if (serial_console)
1486
                    serial_receive_byte(serial_console, ch);
1487
            }
1488
        }
1489
    }
1490
}
1491

    
1492
static int term_can_read(void *opaque)
1493
{
1494
    if (serial_console) {
1495
        return serial_can_receive(serial_console);
1496
    } else {
1497
        return 128;
1498
    }
1499
}
1500

    
1501
static void term_read(void *opaque, const uint8_t *buf, int size)
1502
{
1503
    int i;
1504
    for(i = 0; i < size; i++)
1505
        term_received_byte(buf[i]);
1506
}
1507

    
1508
void monitor_init(void)
1509
{
1510
    if (!serial_console) {
1511
        term_printf("QEMU %s monitor - type 'help' for more information\n",
1512
                    QEMU_VERSION);
1513
        term_show_prompt();
1514
    }
1515
    term_hist_entry = -1;
1516
    qemu_add_fd_read_handler(0, term_can_read, term_read, NULL);
1517
}