Statistics
| Branch: | Revision:

root / monitor.c @ e600d1ef

History | View | Annotate | Download (86.3 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 <dirent.h>
25
#include "hw/hw.h"
26
#include "hw/usb.h"
27
#include "hw/pcmcia.h"
28
#include "hw/pc.h"
29
#include "hw/pci.h"
30
#include "gdbstub.h"
31
#include "net.h"
32
#include "qemu-char.h"
33
#include "sysemu.h"
34
#include "monitor.h"
35
#include "readline.h"
36
#include "console.h"
37
#include "block.h"
38
#include "audio/audio.h"
39
#include "disas.h"
40
#include "balloon.h"
41
#include "qemu-timer.h"
42
#include "migration.h"
43
#include "kvm.h"
44
#include "acl.h"
45

    
46
//#define DEBUG
47
//#define DEBUG_COMPLETION
48

    
49
/*
50
 * Supported types:
51
 *
52
 * 'F'          filename
53
 * 'B'          block device name
54
 * 's'          string (accept optional quote)
55
 * 'i'          32 bit integer
56
 * 'l'          target long (32 or 64 bit)
57
 * '/'          optional gdb-like print format (like "/10x")
58
 *
59
 * '?'          optional type (for 'F', 's' and 'i')
60
 *
61
 */
62

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

    
71
struct Monitor {
72
    CharDriverState *chr;
73
    int flags;
74
    int suspend_cnt;
75
    uint8_t outbuf[1024];
76
    int outbuf_index;
77
    ReadLineState *rs;
78
    CPUState *mon_cpu;
79
    BlockDriverCompletionFunc *password_completion_cb;
80
    void *password_opaque;
81
    LIST_ENTRY(Monitor) entry;
82
};
83

    
84
static LIST_HEAD(mon_list, Monitor) mon_list;
85

    
86
static const mon_cmd_t mon_cmds[];
87
static const mon_cmd_t info_cmds[];
88

    
89
Monitor *cur_mon = NULL;
90

    
91
static void monitor_command_cb(Monitor *mon, const char *cmdline,
92
                               void *opaque);
93

    
94
static void monitor_read_command(Monitor *mon, int show_prompt)
95
{
96
    readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
97
    if (show_prompt)
98
        readline_show_prompt(mon->rs);
99
}
100

    
101
static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
102
                                 void *opaque)
103
{
104
    if (mon->rs) {
105
        readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
106
        /* prompt is printed on return from the command handler */
107
        return 0;
108
    } else {
109
        monitor_printf(mon, "terminal does not support password prompting\n");
110
        return -ENOTTY;
111
    }
112
}
113

    
114
void monitor_flush(Monitor *mon)
115
{
116
    if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
117
        qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
118
        mon->outbuf_index = 0;
119
    }
120
}
121

    
122
/* flush at every end of line or if the buffer is full */
123
static void monitor_puts(Monitor *mon, const char *str)
124
{
125
    char c;
126

    
127
    if (!mon)
128
        return;
129

    
130
    for(;;) {
131
        c = *str++;
132
        if (c == '\0')
133
            break;
134
        if (c == '\n')
135
            mon->outbuf[mon->outbuf_index++] = '\r';
136
        mon->outbuf[mon->outbuf_index++] = c;
137
        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
138
            || c == '\n')
139
            monitor_flush(mon);
140
    }
141
}
142

    
143
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
144
{
145
    char buf[4096];
146
    vsnprintf(buf, sizeof(buf), fmt, ap);
147
    monitor_puts(mon, buf);
148
}
149

    
150
void monitor_printf(Monitor *mon, const char *fmt, ...)
151
{
152
    va_list ap;
153
    va_start(ap, fmt);
154
    monitor_vprintf(mon, fmt, ap);
155
    va_end(ap);
156
}
157

    
158
void monitor_print_filename(Monitor *mon, const char *filename)
159
{
160
    int i;
161

    
162
    for (i = 0; filename[i]; i++) {
163
        switch (filename[i]) {
164
        case ' ':
165
        case '"':
166
        case '\\':
167
            monitor_printf(mon, "\\%c", filename[i]);
168
            break;
169
        case '\t':
170
            monitor_printf(mon, "\\t");
171
            break;
172
        case '\r':
173
            monitor_printf(mon, "\\r");
174
            break;
175
        case '\n':
176
            monitor_printf(mon, "\\n");
177
            break;
178
        default:
179
            monitor_printf(mon, "%c", filename[i]);
180
            break;
181
        }
182
    }
183
}
184

    
185
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
186
{
187
    va_list ap;
188
    va_start(ap, fmt);
189
    monitor_vprintf((Monitor *)stream, fmt, ap);
190
    va_end(ap);
191
    return 0;
192
}
193

    
194
static int compare_cmd(const char *name, const char *list)
195
{
196
    const char *p, *pstart;
197
    int len;
198
    len = strlen(name);
199
    p = list;
200
    for(;;) {
201
        pstart = p;
202
        p = strchr(p, '|');
203
        if (!p)
204
            p = pstart + strlen(pstart);
205
        if ((p - pstart) == len && !memcmp(pstart, name, len))
206
            return 1;
207
        if (*p == '\0')
208
            break;
209
        p++;
210
    }
211
    return 0;
212
}
213

    
214
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
215
                          const char *prefix, const char *name)
216
{
217
    const mon_cmd_t *cmd;
218

    
219
    for(cmd = cmds; cmd->name != NULL; cmd++) {
220
        if (!name || !strcmp(name, cmd->name))
221
            monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
222
                           cmd->params, cmd->help);
223
    }
224
}
225

    
226
static void help_cmd(Monitor *mon, const char *name)
227
{
228
    if (name && !strcmp(name, "info")) {
229
        help_cmd_dump(mon, info_cmds, "info ", NULL);
230
    } else {
231
        help_cmd_dump(mon, mon_cmds, "", name);
232
        if (name && !strcmp(name, "log")) {
233
            const CPULogItem *item;
234
            monitor_printf(mon, "Log items (comma separated):\n");
235
            monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
236
            for(item = cpu_log_items; item->mask != 0; item++) {
237
                monitor_printf(mon, "%-10s %s\n", item->name, item->help);
238
            }
239
        }
240
    }
241
}
242

    
243
static void do_commit(Monitor *mon, const char *device)
244
{
245
    int i, all_devices;
246

    
247
    all_devices = !strcmp(device, "all");
248
    for (i = 0; i < nb_drives; i++) {
249
            if (all_devices ||
250
                !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
251
                bdrv_commit(drives_table[i].bdrv);
252
    }
253
}
254

    
255
static void do_info(Monitor *mon, const char *item)
256
{
257
    const mon_cmd_t *cmd;
258
    void (*handler)(Monitor *);
259

    
260
    if (!item)
261
        goto help;
262
    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
263
        if (compare_cmd(item, cmd->name))
264
            goto found;
265
    }
266
 help:
267
    help_cmd(mon, "info");
268
    return;
269
 found:
270
    handler = cmd->handler;
271
    handler(mon);
272
}
273

    
274
static void do_info_version(Monitor *mon)
275
{
276
    monitor_printf(mon, "%s\n", QEMU_VERSION);
277
}
278

    
279
static void do_info_name(Monitor *mon)
280
{
281
    if (qemu_name)
282
        monitor_printf(mon, "%s\n", qemu_name);
283
}
284

    
285
#if defined(TARGET_I386)
286
static void do_info_hpet(Monitor *mon)
287
{
288
    monitor_printf(mon, "HPET is %s by QEMU\n",
289
                   (no_hpet) ? "disabled" : "enabled");
290
}
291
#endif
292

    
293
static void do_info_uuid(Monitor *mon)
294
{
295
    monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
296
                   qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
297
                   qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
298
                   qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
299
                   qemu_uuid[14], qemu_uuid[15]);
300
}
301

    
302
/* get the current CPU defined by the user */
303
static int mon_set_cpu(int cpu_index)
304
{
305
    CPUState *env;
306

    
307
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
308
        if (env->cpu_index == cpu_index) {
309
            cur_mon->mon_cpu = env;
310
            return 0;
311
        }
312
    }
313
    return -1;
314
}
315

    
316
static CPUState *mon_get_cpu(void)
317
{
318
    if (!cur_mon->mon_cpu) {
319
        mon_set_cpu(0);
320
    }
321
    return cur_mon->mon_cpu;
322
}
323

    
324
static void do_info_registers(Monitor *mon)
325
{
326
    CPUState *env;
327
    env = mon_get_cpu();
328
    if (!env)
329
        return;
330
#ifdef TARGET_I386
331
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
332
                   X86_DUMP_FPU);
333
#else
334
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
335
                   0);
336
#endif
337
}
338

    
339
static void do_info_cpus(Monitor *mon)
340
{
341
    CPUState *env;
342

    
343
    /* just to set the default cpu if not already done */
344
    mon_get_cpu();
345

    
346
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
347
        monitor_printf(mon, "%c CPU #%d:",
348
                       (env == mon->mon_cpu) ? '*' : ' ',
349
                       env->cpu_index);
350
#if defined(TARGET_I386)
351
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
352
                       env->eip + env->segs[R_CS].base);
353
#elif defined(TARGET_PPC)
354
        monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
355
#elif defined(TARGET_SPARC)
356
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
357
                       env->pc, env->npc);
358
#elif defined(TARGET_MIPS)
359
        monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
360
#endif
361
        if (env->halted)
362
            monitor_printf(mon, " (halted)");
363
        monitor_printf(mon, "\n");
364
    }
365
}
366

    
367
static void do_cpu_set(Monitor *mon, int index)
368
{
369
    if (mon_set_cpu(index) < 0)
370
        monitor_printf(mon, "Invalid CPU index\n");
371
}
372

    
373
static void do_info_jit(Monitor *mon)
374
{
375
    dump_exec_info((FILE *)mon, monitor_fprintf);
376
}
377

    
378
static void do_info_history(Monitor *mon)
379
{
380
    int i;
381
    const char *str;
382

    
383
    if (!mon->rs)
384
        return;
385
    i = 0;
386
    for(;;) {
387
        str = readline_get_history(mon->rs, i);
388
        if (!str)
389
            break;
390
        monitor_printf(mon, "%d: '%s'\n", i, str);
391
        i++;
392
    }
393
}
394

    
395
#if defined(TARGET_PPC)
396
/* XXX: not implemented in other targets */
397
static void do_info_cpu_stats(Monitor *mon)
398
{
399
    CPUState *env;
400

    
401
    env = mon_get_cpu();
402
    cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
403
}
404
#endif
405

    
406
static void do_quit(Monitor *mon)
407
{
408
    exit(0);
409
}
410

    
411
static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
412
{
413
    if (bdrv_is_inserted(bs)) {
414
        if (!force) {
415
            if (!bdrv_is_removable(bs)) {
416
                monitor_printf(mon, "device is not removable\n");
417
                return -1;
418
            }
419
            if (bdrv_is_locked(bs)) {
420
                monitor_printf(mon, "device is locked\n");
421
                return -1;
422
            }
423
        }
424
        bdrv_close(bs);
425
    }
426
    return 0;
427
}
428

    
429
static void do_eject(Monitor *mon, int force, const char *filename)
430
{
431
    BlockDriverState *bs;
432

    
433
    bs = bdrv_find(filename);
434
    if (!bs) {
435
        monitor_printf(mon, "device not found\n");
436
        return;
437
    }
438
    eject_device(mon, bs, force);
439
}
440

    
441
static void do_change_block(Monitor *mon, const char *device,
442
                            const char *filename, const char *fmt)
443
{
444
    BlockDriverState *bs;
445
    BlockDriver *drv = NULL;
446

    
447
    bs = bdrv_find(device);
448
    if (!bs) {
449
        monitor_printf(mon, "device not found\n");
450
        return;
451
    }
452
    if (fmt) {
453
        drv = bdrv_find_format(fmt);
454
        if (!drv) {
455
            monitor_printf(mon, "invalid format %s\n", fmt);
456
            return;
457
        }
458
    }
459
    if (eject_device(mon, bs, 0) < 0)
460
        return;
461
    bdrv_open2(bs, filename, 0, drv);
462
    monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
463
}
464

    
465
static void change_vnc_password_cb(Monitor *mon, const char *password,
466
                                   void *opaque)
467
{
468
    if (vnc_display_password(NULL, password) < 0)
469
        monitor_printf(mon, "could not set VNC server password\n");
470

    
471
    monitor_read_command(mon, 1);
472
}
473

    
474
static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
475
{
476
    if (strcmp(target, "passwd") == 0 ||
477
        strcmp(target, "password") == 0) {
478
        if (arg) {
479
            char password[9];
480
            strncpy(password, arg, sizeof(password));
481
            password[sizeof(password) - 1] = '\0';
482
            change_vnc_password_cb(mon, password, NULL);
483
        } else {
484
            monitor_read_password(mon, change_vnc_password_cb, NULL);
485
        }
486
    } else {
487
        if (vnc_display_open(NULL, target) < 0)
488
            monitor_printf(mon, "could not start VNC server on %s\n", target);
489
    }
490
}
491

    
492
static void do_change(Monitor *mon, const char *device, const char *target,
493
                      const char *arg)
494
{
495
    if (strcmp(device, "vnc") == 0) {
496
        do_change_vnc(mon, target, arg);
497
    } else {
498
        do_change_block(mon, device, target, arg);
499
    }
500
}
501

    
502
static void do_screen_dump(Monitor *mon, const char *filename)
503
{
504
    vga_hw_screen_dump(filename);
505
}
506

    
507
static void do_logfile(Monitor *mon, const char *filename)
508
{
509
    cpu_set_log_filename(filename);
510
}
511

    
512
static void do_log(Monitor *mon, const char *items)
513
{
514
    int mask;
515

    
516
    if (!strcmp(items, "none")) {
517
        mask = 0;
518
    } else {
519
        mask = cpu_str_to_log_mask(items);
520
        if (!mask) {
521
            help_cmd(mon, "log");
522
            return;
523
        }
524
    }
525
    cpu_set_log(mask);
526
}
527

    
528
static void do_stop(Monitor *mon)
529
{
530
    vm_stop(EXCP_INTERRUPT);
531
}
532

    
533
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
534

    
535
struct bdrv_iterate_context {
536
    Monitor *mon;
537
    int err;
538
};
539

    
540
static void do_cont(Monitor *mon)
541
{
542
    struct bdrv_iterate_context context = { mon, 0 };
543

    
544
    bdrv_iterate(encrypted_bdrv_it, &context);
545
    /* only resume the vm if all keys are set and valid */
546
    if (!context.err)
547
        vm_start();
548
}
549

    
550
static void bdrv_key_cb(void *opaque, int err)
551
{
552
    Monitor *mon = opaque;
553

    
554
    /* another key was set successfully, retry to continue */
555
    if (!err)
556
        do_cont(mon);
557
}
558

    
559
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
560
{
561
    struct bdrv_iterate_context *context = opaque;
562

    
563
    if (!context->err && bdrv_key_required(bs)) {
564
        context->err = -EBUSY;
565
        monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
566
                                    context->mon);
567
    }
568
}
569

    
570
#ifdef CONFIG_GDBSTUB
571
static void do_gdbserver(Monitor *mon, const char *port)
572
{
573
    if (!port)
574
        port = DEFAULT_GDBSTUB_PORT;
575
    if (gdbserver_start(port) < 0) {
576
        monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
577
                       port);
578
    } else {
579
        monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
580
    }
581
}
582
#endif
583

    
584
static void monitor_printc(Monitor *mon, int c)
585
{
586
    monitor_printf(mon, "'");
587
    switch(c) {
588
    case '\'':
589
        monitor_printf(mon, "\\'");
590
        break;
591
    case '\\':
592
        monitor_printf(mon, "\\\\");
593
        break;
594
    case '\n':
595
        monitor_printf(mon, "\\n");
596
        break;
597
    case '\r':
598
        monitor_printf(mon, "\\r");
599
        break;
600
    default:
601
        if (c >= 32 && c <= 126) {
602
            monitor_printf(mon, "%c", c);
603
        } else {
604
            monitor_printf(mon, "\\x%02x", c);
605
        }
606
        break;
607
    }
608
    monitor_printf(mon, "'");
609
}
610

    
611
static void memory_dump(Monitor *mon, int count, int format, int wsize,
612
                        target_phys_addr_t addr, int is_physical)
613
{
614
    CPUState *env;
615
    int nb_per_line, l, line_size, i, max_digits, len;
616
    uint8_t buf[16];
617
    uint64_t v;
618

    
619
    if (format == 'i') {
620
        int flags;
621
        flags = 0;
622
        env = mon_get_cpu();
623
        if (!env && !is_physical)
624
            return;
625
#ifdef TARGET_I386
626
        if (wsize == 2) {
627
            flags = 1;
628
        } else if (wsize == 4) {
629
            flags = 0;
630
        } else {
631
            /* as default we use the current CS size */
632
            flags = 0;
633
            if (env) {
634
#ifdef TARGET_X86_64
635
                if ((env->efer & MSR_EFER_LMA) &&
636
                    (env->segs[R_CS].flags & DESC_L_MASK))
637
                    flags = 2;
638
                else
639
#endif
640
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
641
                    flags = 1;
642
            }
643
        }
644
#endif
645
        monitor_disas(mon, env, addr, count, is_physical, flags);
646
        return;
647
    }
648

    
649
    len = wsize * count;
650
    if (wsize == 1)
651
        line_size = 8;
652
    else
653
        line_size = 16;
654
    nb_per_line = line_size / wsize;
655
    max_digits = 0;
656

    
657
    switch(format) {
658
    case 'o':
659
        max_digits = (wsize * 8 + 2) / 3;
660
        break;
661
    default:
662
    case 'x':
663
        max_digits = (wsize * 8) / 4;
664
        break;
665
    case 'u':
666
    case 'd':
667
        max_digits = (wsize * 8 * 10 + 32) / 33;
668
        break;
669
    case 'c':
670
        wsize = 1;
671
        break;
672
    }
673

    
674
    while (len > 0) {
675
        if (is_physical)
676
            monitor_printf(mon, TARGET_FMT_plx ":", addr);
677
        else
678
            monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
679
        l = len;
680
        if (l > line_size)
681
            l = line_size;
682
        if (is_physical) {
683
            cpu_physical_memory_rw(addr, buf, l, 0);
684
        } else {
685
            env = mon_get_cpu();
686
            if (!env)
687
                break;
688
            if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
689
                monitor_printf(mon, " Cannot access memory\n");
690
                break;
691
            }
692
        }
693
        i = 0;
694
        while (i < l) {
695
            switch(wsize) {
696
            default:
697
            case 1:
698
                v = ldub_raw(buf + i);
699
                break;
700
            case 2:
701
                v = lduw_raw(buf + i);
702
                break;
703
            case 4:
704
                v = (uint32_t)ldl_raw(buf + i);
705
                break;
706
            case 8:
707
                v = ldq_raw(buf + i);
708
                break;
709
            }
710
            monitor_printf(mon, " ");
711
            switch(format) {
712
            case 'o':
713
                monitor_printf(mon, "%#*" PRIo64, max_digits, v);
714
                break;
715
            case 'x':
716
                monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
717
                break;
718
            case 'u':
719
                monitor_printf(mon, "%*" PRIu64, max_digits, v);
720
                break;
721
            case 'd':
722
                monitor_printf(mon, "%*" PRId64, max_digits, v);
723
                break;
724
            case 'c':
725
                monitor_printc(mon, v);
726
                break;
727
            }
728
            i += wsize;
729
        }
730
        monitor_printf(mon, "\n");
731
        addr += l;
732
        len -= l;
733
    }
734
}
735

    
736
#if TARGET_LONG_BITS == 64
737
#define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
738
#else
739
#define GET_TLONG(h, l) (l)
740
#endif
741

    
742
static void do_memory_dump(Monitor *mon, int count, int format, int size,
743
                           uint32_t addrh, uint32_t addrl)
744
{
745
    target_long addr = GET_TLONG(addrh, addrl);
746
    memory_dump(mon, count, format, size, addr, 0);
747
}
748

    
749
#if TARGET_PHYS_ADDR_BITS > 32
750
#define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
751
#else
752
#define GET_TPHYSADDR(h, l) (l)
753
#endif
754

    
755
static void do_physical_memory_dump(Monitor *mon, int count, int format,
756
                                    int size, uint32_t addrh, uint32_t addrl)
757

    
758
{
759
    target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
760
    memory_dump(mon, count, format, size, addr, 1);
761
}
762

    
763
static void do_print(Monitor *mon, int count, int format, int size,
764
                     unsigned int valh, unsigned int vall)
765
{
766
    target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
767
#if TARGET_PHYS_ADDR_BITS == 32
768
    switch(format) {
769
    case 'o':
770
        monitor_printf(mon, "%#o", val);
771
        break;
772
    case 'x':
773
        monitor_printf(mon, "%#x", val);
774
        break;
775
    case 'u':
776
        monitor_printf(mon, "%u", val);
777
        break;
778
    default:
779
    case 'd':
780
        monitor_printf(mon, "%d", val);
781
        break;
782
    case 'c':
783
        monitor_printc(mon, val);
784
        break;
785
    }
786
#else
787
    switch(format) {
788
    case 'o':
789
        monitor_printf(mon, "%#" PRIo64, val);
790
        break;
791
    case 'x':
792
        monitor_printf(mon, "%#" PRIx64, val);
793
        break;
794
    case 'u':
795
        monitor_printf(mon, "%" PRIu64, val);
796
        break;
797
    default:
798
    case 'd':
799
        monitor_printf(mon, "%" PRId64, val);
800
        break;
801
    case 'c':
802
        monitor_printc(mon, val);
803
        break;
804
    }
805
#endif
806
    monitor_printf(mon, "\n");
807
}
808

    
809
static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
810
                           uint32_t size, const char *filename)
811
{
812
    FILE *f;
813
    target_long addr = GET_TLONG(valh, vall);
814
    uint32_t l;
815
    CPUState *env;
816
    uint8_t buf[1024];
817

    
818
    env = mon_get_cpu();
819
    if (!env)
820
        return;
821

    
822
    f = fopen(filename, "wb");
823
    if (!f) {
824
        monitor_printf(mon, "could not open '%s'\n", filename);
825
        return;
826
    }
827
    while (size != 0) {
828
        l = sizeof(buf);
829
        if (l > size)
830
            l = size;
831
        cpu_memory_rw_debug(env, addr, buf, l, 0);
832
        fwrite(buf, 1, l, f);
833
        addr += l;
834
        size -= l;
835
    }
836
    fclose(f);
837
}
838

    
839
static void do_physical_memory_save(Monitor *mon, unsigned int valh,
840
                                    unsigned int vall, uint32_t size,
841
                                    const char *filename)
842
{
843
    FILE *f;
844
    uint32_t l;
845
    uint8_t buf[1024];
846
    target_phys_addr_t addr = GET_TPHYSADDR(valh, vall); 
847

    
848
    f = fopen(filename, "wb");
849
    if (!f) {
850
        monitor_printf(mon, "could not open '%s'\n", filename);
851
        return;
852
    }
853
    while (size != 0) {
854
        l = sizeof(buf);
855
        if (l > size)
856
            l = size;
857
        cpu_physical_memory_rw(addr, buf, l, 0);
858
        fwrite(buf, 1, l, f);
859
        fflush(f);
860
        addr += l;
861
        size -= l;
862
    }
863
    fclose(f);
864
}
865

    
866
static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
867
{
868
    uint32_t addr;
869
    uint8_t buf[1];
870
    uint16_t sum;
871

    
872
    sum = 0;
873
    for(addr = start; addr < (start + size); addr++) {
874
        cpu_physical_memory_rw(addr, buf, 1, 0);
875
        /* BSD sum algorithm ('sum' Unix command) */
876
        sum = (sum >> 1) | (sum << 15);
877
        sum += buf[0];
878
    }
879
    monitor_printf(mon, "%05d\n", sum);
880
}
881

    
882
typedef struct {
883
    int keycode;
884
    const char *name;
885
} KeyDef;
886

    
887
static const KeyDef key_defs[] = {
888
    { 0x2a, "shift" },
889
    { 0x36, "shift_r" },
890

    
891
    { 0x38, "alt" },
892
    { 0xb8, "alt_r" },
893
    { 0x64, "altgr" },
894
    { 0xe4, "altgr_r" },
895
    { 0x1d, "ctrl" },
896
    { 0x9d, "ctrl_r" },
897

    
898
    { 0xdd, "menu" },
899

    
900
    { 0x01, "esc" },
901

    
902
    { 0x02, "1" },
903
    { 0x03, "2" },
904
    { 0x04, "3" },
905
    { 0x05, "4" },
906
    { 0x06, "5" },
907
    { 0x07, "6" },
908
    { 0x08, "7" },
909
    { 0x09, "8" },
910
    { 0x0a, "9" },
911
    { 0x0b, "0" },
912
    { 0x0c, "minus" },
913
    { 0x0d, "equal" },
914
    { 0x0e, "backspace" },
915

    
916
    { 0x0f, "tab" },
917
    { 0x10, "q" },
918
    { 0x11, "w" },
919
    { 0x12, "e" },
920
    { 0x13, "r" },
921
    { 0x14, "t" },
922
    { 0x15, "y" },
923
    { 0x16, "u" },
924
    { 0x17, "i" },
925
    { 0x18, "o" },
926
    { 0x19, "p" },
927

    
928
    { 0x1c, "ret" },
929

    
930
    { 0x1e, "a" },
931
    { 0x1f, "s" },
932
    { 0x20, "d" },
933
    { 0x21, "f" },
934
    { 0x22, "g" },
935
    { 0x23, "h" },
936
    { 0x24, "j" },
937
    { 0x25, "k" },
938
    { 0x26, "l" },
939

    
940
    { 0x2c, "z" },
941
    { 0x2d, "x" },
942
    { 0x2e, "c" },
943
    { 0x2f, "v" },
944
    { 0x30, "b" },
945
    { 0x31, "n" },
946
    { 0x32, "m" },
947
    { 0x33, "comma" },
948
    { 0x34, "dot" },
949
    { 0x35, "slash" },
950

    
951
    { 0x37, "asterisk" },
952

    
953
    { 0x39, "spc" },
954
    { 0x3a, "caps_lock" },
955
    { 0x3b, "f1" },
956
    { 0x3c, "f2" },
957
    { 0x3d, "f3" },
958
    { 0x3e, "f4" },
959
    { 0x3f, "f5" },
960
    { 0x40, "f6" },
961
    { 0x41, "f7" },
962
    { 0x42, "f8" },
963
    { 0x43, "f9" },
964
    { 0x44, "f10" },
965
    { 0x45, "num_lock" },
966
    { 0x46, "scroll_lock" },
967

    
968
    { 0xb5, "kp_divide" },
969
    { 0x37, "kp_multiply" },
970
    { 0x4a, "kp_subtract" },
971
    { 0x4e, "kp_add" },
972
    { 0x9c, "kp_enter" },
973
    { 0x53, "kp_decimal" },
974
    { 0x54, "sysrq" },
975

    
976
    { 0x52, "kp_0" },
977
    { 0x4f, "kp_1" },
978
    { 0x50, "kp_2" },
979
    { 0x51, "kp_3" },
980
    { 0x4b, "kp_4" },
981
    { 0x4c, "kp_5" },
982
    { 0x4d, "kp_6" },
983
    { 0x47, "kp_7" },
984
    { 0x48, "kp_8" },
985
    { 0x49, "kp_9" },
986

    
987
    { 0x56, "<" },
988

    
989
    { 0x57, "f11" },
990
    { 0x58, "f12" },
991

    
992
    { 0xb7, "print" },
993

    
994
    { 0xc7, "home" },
995
    { 0xc9, "pgup" },
996
    { 0xd1, "pgdn" },
997
    { 0xcf, "end" },
998

    
999
    { 0xcb, "left" },
1000
    { 0xc8, "up" },
1001
    { 0xd0, "down" },
1002
    { 0xcd, "right" },
1003

    
1004
    { 0xd2, "insert" },
1005
    { 0xd3, "delete" },
1006
#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1007
    { 0xf0, "stop" },
1008
    { 0xf1, "again" },
1009
    { 0xf2, "props" },
1010
    { 0xf3, "undo" },
1011
    { 0xf4, "front" },
1012
    { 0xf5, "copy" },
1013
    { 0xf6, "open" },
1014
    { 0xf7, "paste" },
1015
    { 0xf8, "find" },
1016
    { 0xf9, "cut" },
1017
    { 0xfa, "lf" },
1018
    { 0xfb, "help" },
1019
    { 0xfc, "meta_l" },
1020
    { 0xfd, "meta_r" },
1021
    { 0xfe, "compose" },
1022
#endif
1023
    { 0, NULL },
1024
};
1025

    
1026
static int get_keycode(const char *key)
1027
{
1028
    const KeyDef *p;
1029
    char *endp;
1030
    int ret;
1031

    
1032
    for(p = key_defs; p->name != NULL; p++) {
1033
        if (!strcmp(key, p->name))
1034
            return p->keycode;
1035
    }
1036
    if (strstart(key, "0x", NULL)) {
1037
        ret = strtoul(key, &endp, 0);
1038
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1039
            return ret;
1040
    }
1041
    return -1;
1042
}
1043

    
1044
#define MAX_KEYCODES 16
1045
static uint8_t keycodes[MAX_KEYCODES];
1046
static int nb_pending_keycodes;
1047
static QEMUTimer *key_timer;
1048

    
1049
static void release_keys(void *opaque)
1050
{
1051
    int keycode;
1052

    
1053
    while (nb_pending_keycodes > 0) {
1054
        nb_pending_keycodes--;
1055
        keycode = keycodes[nb_pending_keycodes];
1056
        if (keycode & 0x80)
1057
            kbd_put_keycode(0xe0);
1058
        kbd_put_keycode(keycode | 0x80);
1059
    }
1060
}
1061

    
1062
static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1063
                       int hold_time)
1064
{
1065
    char keyname_buf[16];
1066
    char *separator;
1067
    int keyname_len, keycode, i;
1068

    
1069
    if (nb_pending_keycodes > 0) {
1070
        qemu_del_timer(key_timer);
1071
        release_keys(NULL);
1072
    }
1073
    if (!has_hold_time)
1074
        hold_time = 100;
1075
    i = 0;
1076
    while (1) {
1077
        separator = strchr(string, '-');
1078
        keyname_len = separator ? separator - string : strlen(string);
1079
        if (keyname_len > 0) {
1080
            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1081
            if (keyname_len > sizeof(keyname_buf) - 1) {
1082
                monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1083
                return;
1084
            }
1085
            if (i == MAX_KEYCODES) {
1086
                monitor_printf(mon, "too many keys\n");
1087
                return;
1088
            }
1089
            keyname_buf[keyname_len] = 0;
1090
            keycode = get_keycode(keyname_buf);
1091
            if (keycode < 0) {
1092
                monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1093
                return;
1094
            }
1095
            keycodes[i++] = keycode;
1096
        }
1097
        if (!separator)
1098
            break;
1099
        string = separator + 1;
1100
    }
1101
    nb_pending_keycodes = i;
1102
    /* key down events */
1103
    for (i = 0; i < nb_pending_keycodes; i++) {
1104
        keycode = keycodes[i];
1105
        if (keycode & 0x80)
1106
            kbd_put_keycode(0xe0);
1107
        kbd_put_keycode(keycode & 0x7f);
1108
    }
1109
    /* delayed key up events */
1110
    qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1111
                    muldiv64(ticks_per_sec, hold_time, 1000));
1112
}
1113

    
1114
static int mouse_button_state;
1115

    
1116
static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1117
                          const char *dz_str)
1118
{
1119
    int dx, dy, dz;
1120
    dx = strtol(dx_str, NULL, 0);
1121
    dy = strtol(dy_str, NULL, 0);
1122
    dz = 0;
1123
    if (dz_str)
1124
        dz = strtol(dz_str, NULL, 0);
1125
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
1126
}
1127

    
1128
static void do_mouse_button(Monitor *mon, int button_state)
1129
{
1130
    mouse_button_state = button_state;
1131
    kbd_mouse_event(0, 0, 0, mouse_button_state);
1132
}
1133

    
1134
static void do_ioport_read(Monitor *mon, int count, int format, int size,
1135
                           int addr, int has_index, int index)
1136
{
1137
    uint32_t val;
1138
    int suffix;
1139

    
1140
    if (has_index) {
1141
        cpu_outb(NULL, addr & 0xffff, index & 0xff);
1142
        addr++;
1143
    }
1144
    addr &= 0xffff;
1145

    
1146
    switch(size) {
1147
    default:
1148
    case 1:
1149
        val = cpu_inb(NULL, addr);
1150
        suffix = 'b';
1151
        break;
1152
    case 2:
1153
        val = cpu_inw(NULL, addr);
1154
        suffix = 'w';
1155
        break;
1156
    case 4:
1157
        val = cpu_inl(NULL, addr);
1158
        suffix = 'l';
1159
        break;
1160
    }
1161
    monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1162
                   suffix, addr, size * 2, val);
1163
}
1164

    
1165
/* boot_set handler */
1166
static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1167
static void *boot_opaque;
1168

    
1169
void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1170
{
1171
    qemu_boot_set_handler = func;
1172
    boot_opaque = opaque;
1173
}
1174

    
1175
static void do_boot_set(Monitor *mon, const char *bootdevice)
1176
{
1177
    int res;
1178

    
1179
    if (qemu_boot_set_handler)  {
1180
        res = qemu_boot_set_handler(boot_opaque, bootdevice);
1181
        if (res == 0)
1182
            monitor_printf(mon, "boot device list now set to %s\n",
1183
                           bootdevice);
1184
        else
1185
            monitor_printf(mon, "setting boot device list failed with "
1186
                           "error %i\n", res);
1187
    } else {
1188
        monitor_printf(mon, "no function defined to set boot device list for "
1189
                       "this architecture\n");
1190
    }
1191
}
1192

    
1193
static void do_system_reset(Monitor *mon)
1194
{
1195
    qemu_system_reset_request();
1196
}
1197

    
1198
static void do_system_powerdown(Monitor *mon)
1199
{
1200
    qemu_system_powerdown_request();
1201
}
1202

    
1203
#if defined(TARGET_I386)
1204
static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1205
{
1206
    monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1207
                   addr,
1208
                   pte & mask,
1209
                   pte & PG_GLOBAL_MASK ? 'G' : '-',
1210
                   pte & PG_PSE_MASK ? 'P' : '-',
1211
                   pte & PG_DIRTY_MASK ? 'D' : '-',
1212
                   pte & PG_ACCESSED_MASK ? 'A' : '-',
1213
                   pte & PG_PCD_MASK ? 'C' : '-',
1214
                   pte & PG_PWT_MASK ? 'T' : '-',
1215
                   pte & PG_USER_MASK ? 'U' : '-',
1216
                   pte & PG_RW_MASK ? 'W' : '-');
1217
}
1218

    
1219
static void tlb_info(Monitor *mon)
1220
{
1221
    CPUState *env;
1222
    int l1, l2;
1223
    uint32_t pgd, pde, pte;
1224

    
1225
    env = mon_get_cpu();
1226
    if (!env)
1227
        return;
1228

    
1229
    if (!(env->cr[0] & CR0_PG_MASK)) {
1230
        monitor_printf(mon, "PG disabled\n");
1231
        return;
1232
    }
1233
    pgd = env->cr[3] & ~0xfff;
1234
    for(l1 = 0; l1 < 1024; l1++) {
1235
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1236
        pde = le32_to_cpu(pde);
1237
        if (pde & PG_PRESENT_MASK) {
1238
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1239
                print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1240
            } else {
1241
                for(l2 = 0; l2 < 1024; l2++) {
1242
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1243
                                             (uint8_t *)&pte, 4);
1244
                    pte = le32_to_cpu(pte);
1245
                    if (pte & PG_PRESENT_MASK) {
1246
                        print_pte(mon, (l1 << 22) + (l2 << 12),
1247
                                  pte & ~PG_PSE_MASK,
1248
                                  ~0xfff);
1249
                    }
1250
                }
1251
            }
1252
        }
1253
    }
1254
}
1255

    
1256
static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1257
                      uint32_t end, int prot)
1258
{
1259
    int prot1;
1260
    prot1 = *plast_prot;
1261
    if (prot != prot1) {
1262
        if (*pstart != -1) {
1263
            monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1264
                           *pstart, end, end - *pstart,
1265
                           prot1 & PG_USER_MASK ? 'u' : '-',
1266
                           'r',
1267
                           prot1 & PG_RW_MASK ? 'w' : '-');
1268
        }
1269
        if (prot != 0)
1270
            *pstart = end;
1271
        else
1272
            *pstart = -1;
1273
        *plast_prot = prot;
1274
    }
1275
}
1276

    
1277
static void mem_info(Monitor *mon)
1278
{
1279
    CPUState *env;
1280
    int l1, l2, prot, last_prot;
1281
    uint32_t pgd, pde, pte, start, end;
1282

    
1283
    env = mon_get_cpu();
1284
    if (!env)
1285
        return;
1286

    
1287
    if (!(env->cr[0] & CR0_PG_MASK)) {
1288
        monitor_printf(mon, "PG disabled\n");
1289
        return;
1290
    }
1291
    pgd = env->cr[3] & ~0xfff;
1292
    last_prot = 0;
1293
    start = -1;
1294
    for(l1 = 0; l1 < 1024; l1++) {
1295
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1296
        pde = le32_to_cpu(pde);
1297
        end = l1 << 22;
1298
        if (pde & PG_PRESENT_MASK) {
1299
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1300
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1301
                mem_print(mon, &start, &last_prot, end, prot);
1302
            } else {
1303
                for(l2 = 0; l2 < 1024; l2++) {
1304
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1305
                                             (uint8_t *)&pte, 4);
1306
                    pte = le32_to_cpu(pte);
1307
                    end = (l1 << 22) + (l2 << 12);
1308
                    if (pte & PG_PRESENT_MASK) {
1309
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1310
                    } else {
1311
                        prot = 0;
1312
                    }
1313
                    mem_print(mon, &start, &last_prot, end, prot);
1314
                }
1315
            }
1316
        } else {
1317
            prot = 0;
1318
            mem_print(mon, &start, &last_prot, end, prot);
1319
        }
1320
    }
1321
}
1322
#endif
1323

    
1324
#if defined(TARGET_SH4)
1325

    
1326
static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1327
{
1328
    monitor_printf(mon, " tlb%i:\t"
1329
                   "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1330
                   "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1331
                   "dirty=%hhu writethrough=%hhu\n",
1332
                   idx,
1333
                   tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1334
                   tlb->v, tlb->sh, tlb->c, tlb->pr,
1335
                   tlb->d, tlb->wt);
1336
}
1337

    
1338
static void tlb_info(Monitor *mon)
1339
{
1340
    CPUState *env = mon_get_cpu();
1341
    int i;
1342

    
1343
    monitor_printf (mon, "ITLB:\n");
1344
    for (i = 0 ; i < ITLB_SIZE ; i++)
1345
        print_tlb (mon, i, &env->itlb[i]);
1346
    monitor_printf (mon, "UTLB:\n");
1347
    for (i = 0 ; i < UTLB_SIZE ; i++)
1348
        print_tlb (mon, i, &env->utlb[i]);
1349
}
1350

    
1351
#endif
1352

    
1353
static void do_info_kqemu(Monitor *mon)
1354
{
1355
#ifdef USE_KQEMU
1356
    CPUState *env;
1357
    int val;
1358
    val = 0;
1359
    env = mon_get_cpu();
1360
    if (!env) {
1361
        monitor_printf(mon, "No cpu initialized yet");
1362
        return;
1363
    }
1364
    val = env->kqemu_enabled;
1365
    monitor_printf(mon, "kqemu support: ");
1366
    switch(val) {
1367
    default:
1368
    case 0:
1369
        monitor_printf(mon, "disabled\n");
1370
        break;
1371
    case 1:
1372
        monitor_printf(mon, "enabled for user code\n");
1373
        break;
1374
    case 2:
1375
        monitor_printf(mon, "enabled for user and kernel code\n");
1376
        break;
1377
    }
1378
#else
1379
    monitor_printf(mon, "kqemu support: not compiled\n");
1380
#endif
1381
}
1382

    
1383
static void do_info_kvm(Monitor *mon)
1384
{
1385
#ifdef CONFIG_KVM
1386
    monitor_printf(mon, "kvm support: ");
1387
    if (kvm_enabled())
1388
        monitor_printf(mon, "enabled\n");
1389
    else
1390
        monitor_printf(mon, "disabled\n");
1391
#else
1392
    monitor_printf(mon, "kvm support: not compiled\n");
1393
#endif
1394
}
1395

    
1396
#ifdef CONFIG_PROFILER
1397

    
1398
int64_t kqemu_time;
1399
int64_t qemu_time;
1400
int64_t kqemu_exec_count;
1401
int64_t dev_time;
1402
int64_t kqemu_ret_int_count;
1403
int64_t kqemu_ret_excp_count;
1404
int64_t kqemu_ret_intr_count;
1405

    
1406
static void do_info_profile(Monitor *mon)
1407
{
1408
    int64_t total;
1409
    total = qemu_time;
1410
    if (total == 0)
1411
        total = 1;
1412
    monitor_printf(mon, "async time  %" PRId64 " (%0.3f)\n",
1413
                   dev_time, dev_time / (double)ticks_per_sec);
1414
    monitor_printf(mon, "qemu time   %" PRId64 " (%0.3f)\n",
1415
                   qemu_time, qemu_time / (double)ticks_per_sec);
1416
    monitor_printf(mon, "kqemu time  %" PRId64 " (%0.3f %0.1f%%) count=%"
1417
                        PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1418
                        PRId64 "\n",
1419
                   kqemu_time, kqemu_time / (double)ticks_per_sec,
1420
                   kqemu_time / (double)total * 100.0,
1421
                   kqemu_exec_count,
1422
                   kqemu_ret_int_count,
1423
                   kqemu_ret_excp_count,
1424
                   kqemu_ret_intr_count);
1425
    qemu_time = 0;
1426
    kqemu_time = 0;
1427
    kqemu_exec_count = 0;
1428
    dev_time = 0;
1429
    kqemu_ret_int_count = 0;
1430
    kqemu_ret_excp_count = 0;
1431
    kqemu_ret_intr_count = 0;
1432
#ifdef USE_KQEMU
1433
    kqemu_record_dump();
1434
#endif
1435
}
1436
#else
1437
static void do_info_profile(Monitor *mon)
1438
{
1439
    monitor_printf(mon, "Internal profiler not compiled\n");
1440
}
1441
#endif
1442

    
1443
/* Capture support */
1444
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1445

    
1446
static void do_info_capture(Monitor *mon)
1447
{
1448
    int i;
1449
    CaptureState *s;
1450

    
1451
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1452
        monitor_printf(mon, "[%d]: ", i);
1453
        s->ops.info (s->opaque);
1454
    }
1455
}
1456

    
1457
static void do_stop_capture(Monitor *mon, int n)
1458
{
1459
    int i;
1460
    CaptureState *s;
1461

    
1462
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1463
        if (i == n) {
1464
            s->ops.destroy (s->opaque);
1465
            LIST_REMOVE (s, entries);
1466
            qemu_free (s);
1467
            return;
1468
        }
1469
    }
1470
}
1471

    
1472
#ifdef HAS_AUDIO
1473
static void do_wav_capture(Monitor *mon, const char *path,
1474
                           int has_freq, int freq,
1475
                           int has_bits, int bits,
1476
                           int has_channels, int nchannels)
1477
{
1478
    CaptureState *s;
1479

    
1480
    s = qemu_mallocz (sizeof (*s));
1481

    
1482
    freq = has_freq ? freq : 44100;
1483
    bits = has_bits ? bits : 16;
1484
    nchannels = has_channels ? nchannels : 2;
1485

    
1486
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1487
        monitor_printf(mon, "Faied to add wave capture\n");
1488
        qemu_free (s);
1489
    }
1490
    LIST_INSERT_HEAD (&capture_head, s, entries);
1491
}
1492
#endif
1493

    
1494
#if defined(TARGET_I386)
1495
static void do_inject_nmi(Monitor *mon, int cpu_index)
1496
{
1497
    CPUState *env;
1498

    
1499
    for (env = first_cpu; env != NULL; env = env->next_cpu)
1500
        if (env->cpu_index == cpu_index) {
1501
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1502
            break;
1503
        }
1504
}
1505
#endif
1506

    
1507
static void do_info_status(Monitor *mon)
1508
{
1509
    if (vm_running)
1510
       monitor_printf(mon, "VM status: running\n");
1511
    else
1512
       monitor_printf(mon, "VM status: paused\n");
1513
}
1514

    
1515

    
1516
static void do_balloon(Monitor *mon, int value)
1517
{
1518
    ram_addr_t target = value;
1519
    qemu_balloon(target << 20);
1520
}
1521

    
1522
static void do_info_balloon(Monitor *mon)
1523
{
1524
    ram_addr_t actual;
1525

    
1526
    actual = qemu_balloon_status();
1527
    if (kvm_enabled() && !kvm_has_sync_mmu())
1528
        monitor_printf(mon, "Using KVM without synchronous MMU, "
1529
                       "ballooning disabled\n");
1530
    else if (actual == 0)
1531
        monitor_printf(mon, "Ballooning not activated in VM\n");
1532
    else
1533
        monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1534
}
1535

    
1536
static void do_acl(Monitor *mon,
1537
                   const char *command,
1538
                   const char *aclname,
1539
                   const char *match,
1540
                   int has_index,
1541
                   int index)
1542
{
1543
    qemu_acl *acl;
1544

    
1545
    acl = qemu_acl_find(aclname);
1546
    if (!acl) {
1547
        monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1548
        return;
1549
    }
1550

    
1551
    if (strcmp(command, "show") == 0) {
1552
        int i = 0;
1553
        qemu_acl_entry *entry;
1554
        monitor_printf(mon, "policy: %s\n",
1555
                       acl->defaultDeny ? "deny" : "allow");
1556
        TAILQ_FOREACH(entry, &acl->entries, next) {
1557
            i++;
1558
            monitor_printf(mon, "%d: %s %s\n", i,
1559
                           entry->deny ? "deny" : "allow",
1560
                           entry->match);
1561
        }
1562
    } else if (strcmp(command, "reset") == 0) {
1563
        qemu_acl_reset(acl);
1564
        monitor_printf(mon, "acl: removed all rules\n");
1565
    } else if (strcmp(command, "policy") == 0) {
1566
        if (!match) {
1567
            monitor_printf(mon, "acl: missing policy parameter\n");
1568
            return;
1569
        }
1570

    
1571
        if (strcmp(match, "allow") == 0) {
1572
            acl->defaultDeny = 0;
1573
            monitor_printf(mon, "acl: policy set to 'allow'\n");
1574
        } else if (strcmp(match, "deny") == 0) {
1575
            acl->defaultDeny = 1;
1576
            monitor_printf(mon, "acl: policy set to 'deny'\n");
1577
        } else {
1578
            monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1579
        }
1580
    } else if ((strcmp(command, "allow") == 0) ||
1581
               (strcmp(command, "deny") == 0)) {
1582
        int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1583
        int ret;
1584

    
1585
        if (!match) {
1586
            monitor_printf(mon, "acl: missing match parameter\n");
1587
            return;
1588
        }
1589

    
1590
        if (has_index)
1591
            ret = qemu_acl_insert(acl, deny, match, index);
1592
        else
1593
            ret = qemu_acl_append(acl, deny, match);
1594
        if (ret < 0)
1595
            monitor_printf(mon, "acl: unable to add acl entry\n");
1596
        else
1597
            monitor_printf(mon, "acl: added rule at position %d\n", ret);
1598
    } else if (strcmp(command, "remove") == 0) {
1599
        int ret;
1600

    
1601
        if (!match) {
1602
            monitor_printf(mon, "acl: missing match parameter\n");
1603
            return;
1604
        }
1605

    
1606
        ret = qemu_acl_remove(acl, match);
1607
        if (ret < 0)
1608
            monitor_printf(mon, "acl: no matching acl entry\n");
1609
        else
1610
            monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1611
    } else {
1612
        monitor_printf(mon, "acl: unknown command '%s'\n", command);
1613
    }
1614
}
1615

    
1616
/* Please update qemu-doc.texi when adding or changing commands */
1617
static const mon_cmd_t mon_cmds[] = {
1618
    { "help|?", "s?", help_cmd,
1619
      "[cmd]", "show the help" },
1620
    { "commit", "s", do_commit,
1621
      "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1622
    { "info", "s?", do_info,
1623
      "subcommand", "show various information about the system state" },
1624
    { "q|quit", "", do_quit,
1625
      "", "quit the emulator" },
1626
    { "eject", "-fB", do_eject,
1627
      "[-f] device", "eject a removable medium (use -f to force it)" },
1628
    { "change", "BFs?", do_change,
1629
      "device filename [format]", "change a removable medium, optional format" },
1630
    { "screendump", "F", do_screen_dump,
1631
      "filename", "save screen into PPM image 'filename'" },
1632
    { "logfile", "F", do_logfile,
1633
      "filename", "output logs to 'filename'" },
1634
    { "log", "s", do_log,
1635
      "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1636
    { "savevm", "s?", do_savevm,
1637
      "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1638
    { "loadvm", "s", do_loadvm,
1639
      "tag|id", "restore a VM snapshot from its tag or id" },
1640
    { "delvm", "s", do_delvm,
1641
      "tag|id", "delete a VM snapshot from its tag or id" },
1642
    { "stop", "", do_stop,
1643
      "", "stop emulation", },
1644
    { "c|cont", "", do_cont,
1645
      "", "resume emulation", },
1646
#ifdef CONFIG_GDBSTUB
1647
    { "gdbserver", "s?", do_gdbserver,
1648
      "[port]", "start gdbserver session (default port=1234)", },
1649
#endif
1650
    { "x", "/l", do_memory_dump,
1651
      "/fmt addr", "virtual memory dump starting at 'addr'", },
1652
    { "xp", "/l", do_physical_memory_dump,
1653
      "/fmt addr", "physical memory dump starting at 'addr'", },
1654
    { "p|print", "/l", do_print,
1655
      "/fmt expr", "print expression value (use $reg for CPU register access)", },
1656
    { "i", "/ii.", do_ioport_read,
1657
      "/fmt addr", "I/O port read" },
1658

    
1659
    { "sendkey", "si?", do_sendkey,
1660
      "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1661
    { "system_reset", "", do_system_reset,
1662
      "", "reset the system" },
1663
    { "system_powerdown", "", do_system_powerdown,
1664
      "", "send system power down event" },
1665
    { "sum", "ii", do_sum,
1666
      "addr size", "compute the checksum of a memory region" },
1667
    { "usb_add", "s", do_usb_add,
1668
      "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1669
    { "usb_del", "s", do_usb_del,
1670
      "device", "remove USB device 'bus.addr'" },
1671
    { "cpu", "i", do_cpu_set,
1672
      "index", "set the default CPU" },
1673
    { "mouse_move", "sss?", do_mouse_move,
1674
      "dx dy [dz]", "send mouse move events" },
1675
    { "mouse_button", "i", do_mouse_button,
1676
      "state", "change mouse button state (1=L, 2=M, 4=R)" },
1677
    { "mouse_set", "i", do_mouse_set,
1678
      "index", "set which mouse device receives events" },
1679
#ifdef HAS_AUDIO
1680
    { "wavcapture", "si?i?i?", do_wav_capture,
1681
      "path [frequency bits channels]",
1682
      "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1683
#endif
1684
    { "stopcapture", "i", do_stop_capture,
1685
      "capture index", "stop capture" },
1686
    { "memsave", "lis", do_memory_save,
1687
      "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1688
    { "pmemsave", "lis", do_physical_memory_save,
1689
      "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1690
    { "boot_set", "s", do_boot_set,
1691
      "bootdevice", "define new values for the boot device list" },
1692
#if defined(TARGET_I386)
1693
    { "nmi", "i", do_inject_nmi,
1694
      "cpu", "inject an NMI on the given CPU", },
1695
#endif
1696
    { "migrate", "-ds", do_migrate,
1697
      "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1698
    { "migrate_cancel", "", do_migrate_cancel,
1699
      "", "cancel the current VM migration" },
1700
    { "migrate_set_speed", "s", do_migrate_set_speed,
1701
      "value", "set maximum speed (in bytes) for migrations" },
1702
#if defined(TARGET_I386)
1703
    { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1704
                                         "[file=file][,if=type][,bus=n]\n"
1705
                                        "[,unit=m][,media=d][index=i]\n"
1706
                                        "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1707
                                        "[snapshot=on|off][,cache=on|off]",
1708
                                        "add drive to PCI storage controller" },
1709
    { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1710
    { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1711
    { "host_net_add", "ss", net_host_device_add,
1712
      "[tap,user,socket,vde] options", "add host VLAN client" },
1713
    { "host_net_remove", "is", net_host_device_remove,
1714
      "vlan_id name", "remove host VLAN client" },
1715
#endif
1716
    { "balloon", "i", do_balloon,
1717
      "target", "request VM to change it's memory allocation (in MB)" },
1718
    { "set_link", "ss", do_set_link,
1719
      "name [up|down]", "change the link status of a network adapter" },
1720
    { "acl", "sss?i?", do_acl, "<command> <aclname> [<match>] [<index>]\n",
1721
                               "acl show vnc.username\n"
1722
                               "acl policy vnc.username deny\n"
1723
                               "acl allow vnc.username fred\n"
1724
                               "acl deny vnc.username bob\n"
1725
                               "acl reset vnc.username\n" },
1726
    { NULL, NULL, },
1727
};
1728

    
1729
/* Please update qemu-doc.texi when adding or changing commands */
1730
static const mon_cmd_t info_cmds[] = {
1731
    { "version", "", do_info_version,
1732
      "", "show the version of QEMU" },
1733
    { "network", "", do_info_network,
1734
      "", "show the network state" },
1735
    { "chardev", "", qemu_chr_info,
1736
      "", "show the character devices" },
1737
    { "block", "", bdrv_info,
1738
      "", "show the block devices" },
1739
    { "blockstats", "", bdrv_info_stats,
1740
      "", "show block device statistics" },
1741
    { "registers", "", do_info_registers,
1742
      "", "show the cpu registers" },
1743
    { "cpus", "", do_info_cpus,
1744
      "", "show infos for each CPU" },
1745
    { "history", "", do_info_history,
1746
      "", "show the command line history", },
1747
    { "irq", "", irq_info,
1748
      "", "show the interrupts statistics (if available)", },
1749
    { "pic", "", pic_info,
1750
      "", "show i8259 (PIC) state", },
1751
    { "pci", "", pci_info,
1752
      "", "show PCI info", },
1753
#if defined(TARGET_I386) || defined(TARGET_SH4)
1754
    { "tlb", "", tlb_info,
1755
      "", "show virtual to physical memory mappings", },
1756
#endif
1757
#if defined(TARGET_I386)
1758
    { "mem", "", mem_info,
1759
      "", "show the active virtual memory mappings", },
1760
    { "hpet", "", do_info_hpet,
1761
      "", "show state of HPET", },
1762
#endif
1763
    { "jit", "", do_info_jit,
1764
      "", "show dynamic compiler info", },
1765
    { "kqemu", "", do_info_kqemu,
1766
      "", "show KQEMU information", },
1767
    { "kvm", "", do_info_kvm,
1768
      "", "show KVM information", },
1769
    { "usb", "", usb_info,
1770
      "", "show guest USB devices", },
1771
    { "usbhost", "", usb_host_info,
1772
      "", "show host USB devices", },
1773
    { "profile", "", do_info_profile,
1774
      "", "show profiling information", },
1775
    { "capture", "", do_info_capture,
1776
      "", "show capture information" },
1777
    { "snapshots", "", do_info_snapshots,
1778
      "", "show the currently saved VM snapshots" },
1779
    { "status", "", do_info_status,
1780
      "", "show the current VM status (running|paused)" },
1781
    { "pcmcia", "", pcmcia_info,
1782
      "", "show guest PCMCIA status" },
1783
    { "mice", "", do_info_mice,
1784
      "", "show which guest mouse is receiving events" },
1785
    { "vnc", "", do_info_vnc,
1786
      "", "show the vnc server status"},
1787
    { "name", "", do_info_name,
1788
      "", "show the current VM name" },
1789
    { "uuid", "", do_info_uuid,
1790
      "", "show the current VM UUID" },
1791
#if defined(TARGET_PPC)
1792
    { "cpustats", "", do_info_cpu_stats,
1793
      "", "show CPU statistics", },
1794
#endif
1795
#if defined(CONFIG_SLIRP)
1796
    { "slirp", "", do_info_slirp,
1797
      "", "show SLIRP statistics", },
1798
#endif
1799
    { "migrate", "", do_info_migrate, "", "show migration status" },
1800
    { "balloon", "", do_info_balloon,
1801
      "", "show balloon information" },
1802
    { NULL, NULL, },
1803
};
1804

    
1805
/*******************************************************************/
1806

    
1807
static const char *pch;
1808
static jmp_buf expr_env;
1809

    
1810
#define MD_TLONG 0
1811
#define MD_I32   1
1812

    
1813
typedef struct MonitorDef {
1814
    const char *name;
1815
    int offset;
1816
    target_long (*get_value)(const struct MonitorDef *md, int val);
1817
    int type;
1818
} MonitorDef;
1819

    
1820
#if defined(TARGET_I386)
1821
static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1822
{
1823
    CPUState *env = mon_get_cpu();
1824
    if (!env)
1825
        return 0;
1826
    return env->eip + env->segs[R_CS].base;
1827
}
1828
#endif
1829

    
1830
#if defined(TARGET_PPC)
1831
static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1832
{
1833
    CPUState *env = mon_get_cpu();
1834
    unsigned int u;
1835
    int i;
1836

    
1837
    if (!env)
1838
        return 0;
1839

    
1840
    u = 0;
1841
    for (i = 0; i < 8; i++)
1842
        u |= env->crf[i] << (32 - (4 * i));
1843

    
1844
    return u;
1845
}
1846

    
1847
static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1848
{
1849
    CPUState *env = mon_get_cpu();
1850
    if (!env)
1851
        return 0;
1852
    return env->msr;
1853
}
1854

    
1855
static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1856
{
1857
    CPUState *env = mon_get_cpu();
1858
    if (!env)
1859
        return 0;
1860
    return env->xer;
1861
}
1862

    
1863
static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1864
{
1865
    CPUState *env = mon_get_cpu();
1866
    if (!env)
1867
        return 0;
1868
    return cpu_ppc_load_decr(env);
1869
}
1870

    
1871
static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1872
{
1873
    CPUState *env = mon_get_cpu();
1874
    if (!env)
1875
        return 0;
1876
    return cpu_ppc_load_tbu(env);
1877
}
1878

    
1879
static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1880
{
1881
    CPUState *env = mon_get_cpu();
1882
    if (!env)
1883
        return 0;
1884
    return cpu_ppc_load_tbl(env);
1885
}
1886
#endif
1887

    
1888
#if defined(TARGET_SPARC)
1889
#ifndef TARGET_SPARC64
1890
static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1891
{
1892
    CPUState *env = mon_get_cpu();
1893
    if (!env)
1894
        return 0;
1895
    return GET_PSR(env);
1896
}
1897
#endif
1898

    
1899
static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1900
{
1901
    CPUState *env = mon_get_cpu();
1902
    if (!env)
1903
        return 0;
1904
    return env->regwptr[val];
1905
}
1906
#endif
1907

    
1908
static const MonitorDef monitor_defs[] = {
1909
#ifdef TARGET_I386
1910

    
1911
#define SEG(name, seg) \
1912
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1913
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1914
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1915

    
1916
    { "eax", offsetof(CPUState, regs[0]) },
1917
    { "ecx", offsetof(CPUState, regs[1]) },
1918
    { "edx", offsetof(CPUState, regs[2]) },
1919
    { "ebx", offsetof(CPUState, regs[3]) },
1920
    { "esp|sp", offsetof(CPUState, regs[4]) },
1921
    { "ebp|fp", offsetof(CPUState, regs[5]) },
1922
    { "esi", offsetof(CPUState, regs[6]) },
1923
    { "edi", offsetof(CPUState, regs[7]) },
1924
#ifdef TARGET_X86_64
1925
    { "r8", offsetof(CPUState, regs[8]) },
1926
    { "r9", offsetof(CPUState, regs[9]) },
1927
    { "r10", offsetof(CPUState, regs[10]) },
1928
    { "r11", offsetof(CPUState, regs[11]) },
1929
    { "r12", offsetof(CPUState, regs[12]) },
1930
    { "r13", offsetof(CPUState, regs[13]) },
1931
    { "r14", offsetof(CPUState, regs[14]) },
1932
    { "r15", offsetof(CPUState, regs[15]) },
1933
#endif
1934
    { "eflags", offsetof(CPUState, eflags) },
1935
    { "eip", offsetof(CPUState, eip) },
1936
    SEG("cs", R_CS)
1937
    SEG("ds", R_DS)
1938
    SEG("es", R_ES)
1939
    SEG("ss", R_SS)
1940
    SEG("fs", R_FS)
1941
    SEG("gs", R_GS)
1942
    { "pc", 0, monitor_get_pc, },
1943
#elif defined(TARGET_PPC)
1944
    /* General purpose registers */
1945
    { "r0", offsetof(CPUState, gpr[0]) },
1946
    { "r1", offsetof(CPUState, gpr[1]) },
1947
    { "r2", offsetof(CPUState, gpr[2]) },
1948
    { "r3", offsetof(CPUState, gpr[3]) },
1949
    { "r4", offsetof(CPUState, gpr[4]) },
1950
    { "r5", offsetof(CPUState, gpr[5]) },
1951
    { "r6", offsetof(CPUState, gpr[6]) },
1952
    { "r7", offsetof(CPUState, gpr[7]) },
1953
    { "r8", offsetof(CPUState, gpr[8]) },
1954
    { "r9", offsetof(CPUState, gpr[9]) },
1955
    { "r10", offsetof(CPUState, gpr[10]) },
1956
    { "r11", offsetof(CPUState, gpr[11]) },
1957
    { "r12", offsetof(CPUState, gpr[12]) },
1958
    { "r13", offsetof(CPUState, gpr[13]) },
1959
    { "r14", offsetof(CPUState, gpr[14]) },
1960
    { "r15", offsetof(CPUState, gpr[15]) },
1961
    { "r16", offsetof(CPUState, gpr[16]) },
1962
    { "r17", offsetof(CPUState, gpr[17]) },
1963
    { "r18", offsetof(CPUState, gpr[18]) },
1964
    { "r19", offsetof(CPUState, gpr[19]) },
1965
    { "r20", offsetof(CPUState, gpr[20]) },
1966
    { "r21", offsetof(CPUState, gpr[21]) },
1967
    { "r22", offsetof(CPUState, gpr[22]) },
1968
    { "r23", offsetof(CPUState, gpr[23]) },
1969
    { "r24", offsetof(CPUState, gpr[24]) },
1970
    { "r25", offsetof(CPUState, gpr[25]) },
1971
    { "r26", offsetof(CPUState, gpr[26]) },
1972
    { "r27", offsetof(CPUState, gpr[27]) },
1973
    { "r28", offsetof(CPUState, gpr[28]) },
1974
    { "r29", offsetof(CPUState, gpr[29]) },
1975
    { "r30", offsetof(CPUState, gpr[30]) },
1976
    { "r31", offsetof(CPUState, gpr[31]) },
1977
    /* Floating point registers */
1978
    { "f0", offsetof(CPUState, fpr[0]) },
1979
    { "f1", offsetof(CPUState, fpr[1]) },
1980
    { "f2", offsetof(CPUState, fpr[2]) },
1981
    { "f3", offsetof(CPUState, fpr[3]) },
1982
    { "f4", offsetof(CPUState, fpr[4]) },
1983
    { "f5", offsetof(CPUState, fpr[5]) },
1984
    { "f6", offsetof(CPUState, fpr[6]) },
1985
    { "f7", offsetof(CPUState, fpr[7]) },
1986
    { "f8", offsetof(CPUState, fpr[8]) },
1987
    { "f9", offsetof(CPUState, fpr[9]) },
1988
    { "f10", offsetof(CPUState, fpr[10]) },
1989
    { "f11", offsetof(CPUState, fpr[11]) },
1990
    { "f12", offsetof(CPUState, fpr[12]) },
1991
    { "f13", offsetof(CPUState, fpr[13]) },
1992
    { "f14", offsetof(CPUState, fpr[14]) },
1993
    { "f15", offsetof(CPUState, fpr[15]) },
1994
    { "f16", offsetof(CPUState, fpr[16]) },
1995
    { "f17", offsetof(CPUState, fpr[17]) },
1996
    { "f18", offsetof(CPUState, fpr[18]) },
1997
    { "f19", offsetof(CPUState, fpr[19]) },
1998
    { "f20", offsetof(CPUState, fpr[20]) },
1999
    { "f21", offsetof(CPUState, fpr[21]) },
2000
    { "f22", offsetof(CPUState, fpr[22]) },
2001
    { "f23", offsetof(CPUState, fpr[23]) },
2002
    { "f24", offsetof(CPUState, fpr[24]) },
2003
    { "f25", offsetof(CPUState, fpr[25]) },
2004
    { "f26", offsetof(CPUState, fpr[26]) },
2005
    { "f27", offsetof(CPUState, fpr[27]) },
2006
    { "f28", offsetof(CPUState, fpr[28]) },
2007
    { "f29", offsetof(CPUState, fpr[29]) },
2008
    { "f30", offsetof(CPUState, fpr[30]) },
2009
    { "f31", offsetof(CPUState, fpr[31]) },
2010
    { "fpscr", offsetof(CPUState, fpscr) },
2011
    /* Next instruction pointer */
2012
    { "nip|pc", offsetof(CPUState, nip) },
2013
    { "lr", offsetof(CPUState, lr) },
2014
    { "ctr", offsetof(CPUState, ctr) },
2015
    { "decr", 0, &monitor_get_decr, },
2016
    { "ccr", 0, &monitor_get_ccr, },
2017
    /* Machine state register */
2018
    { "msr", 0, &monitor_get_msr, },
2019
    { "xer", 0, &monitor_get_xer, },
2020
    { "tbu", 0, &monitor_get_tbu, },
2021
    { "tbl", 0, &monitor_get_tbl, },
2022
#if defined(TARGET_PPC64)
2023
    /* Address space register */
2024
    { "asr", offsetof(CPUState, asr) },
2025
#endif
2026
    /* Segment registers */
2027
    { "sdr1", offsetof(CPUState, sdr1) },
2028
    { "sr0", offsetof(CPUState, sr[0]) },
2029
    { "sr1", offsetof(CPUState, sr[1]) },
2030
    { "sr2", offsetof(CPUState, sr[2]) },
2031
    { "sr3", offsetof(CPUState, sr[3]) },
2032
    { "sr4", offsetof(CPUState, sr[4]) },
2033
    { "sr5", offsetof(CPUState, sr[5]) },
2034
    { "sr6", offsetof(CPUState, sr[6]) },
2035
    { "sr7", offsetof(CPUState, sr[7]) },
2036
    { "sr8", offsetof(CPUState, sr[8]) },
2037
    { "sr9", offsetof(CPUState, sr[9]) },
2038
    { "sr10", offsetof(CPUState, sr[10]) },
2039
    { "sr11", offsetof(CPUState, sr[11]) },
2040
    { "sr12", offsetof(CPUState, sr[12]) },
2041
    { "sr13", offsetof(CPUState, sr[13]) },
2042
    { "sr14", offsetof(CPUState, sr[14]) },
2043
    { "sr15", offsetof(CPUState, sr[15]) },
2044
    /* Too lazy to put BATs and SPRs ... */
2045
#elif defined(TARGET_SPARC)
2046
    { "g0", offsetof(CPUState, gregs[0]) },
2047
    { "g1", offsetof(CPUState, gregs[1]) },
2048
    { "g2", offsetof(CPUState, gregs[2]) },
2049
    { "g3", offsetof(CPUState, gregs[3]) },
2050
    { "g4", offsetof(CPUState, gregs[4]) },
2051
    { "g5", offsetof(CPUState, gregs[5]) },
2052
    { "g6", offsetof(CPUState, gregs[6]) },
2053
    { "g7", offsetof(CPUState, gregs[7]) },
2054
    { "o0", 0, monitor_get_reg },
2055
    { "o1", 1, monitor_get_reg },
2056
    { "o2", 2, monitor_get_reg },
2057
    { "o3", 3, monitor_get_reg },
2058
    { "o4", 4, monitor_get_reg },
2059
    { "o5", 5, monitor_get_reg },
2060
    { "o6", 6, monitor_get_reg },
2061
    { "o7", 7, monitor_get_reg },
2062
    { "l0", 8, monitor_get_reg },
2063
    { "l1", 9, monitor_get_reg },
2064
    { "l2", 10, monitor_get_reg },
2065
    { "l3", 11, monitor_get_reg },
2066
    { "l4", 12, monitor_get_reg },
2067
    { "l5", 13, monitor_get_reg },
2068
    { "l6", 14, monitor_get_reg },
2069
    { "l7", 15, monitor_get_reg },
2070
    { "i0", 16, monitor_get_reg },
2071
    { "i1", 17, monitor_get_reg },
2072
    { "i2", 18, monitor_get_reg },
2073
    { "i3", 19, monitor_get_reg },
2074
    { "i4", 20, monitor_get_reg },
2075
    { "i5", 21, monitor_get_reg },
2076
    { "i6", 22, monitor_get_reg },
2077
    { "i7", 23, monitor_get_reg },
2078
    { "pc", offsetof(CPUState, pc) },
2079
    { "npc", offsetof(CPUState, npc) },
2080
    { "y", offsetof(CPUState, y) },
2081
#ifndef TARGET_SPARC64
2082
    { "psr", 0, &monitor_get_psr, },
2083
    { "wim", offsetof(CPUState, wim) },
2084
#endif
2085
    { "tbr", offsetof(CPUState, tbr) },
2086
    { "fsr", offsetof(CPUState, fsr) },
2087
    { "f0", offsetof(CPUState, fpr[0]) },
2088
    { "f1", offsetof(CPUState, fpr[1]) },
2089
    { "f2", offsetof(CPUState, fpr[2]) },
2090
    { "f3", offsetof(CPUState, fpr[3]) },
2091
    { "f4", offsetof(CPUState, fpr[4]) },
2092
    { "f5", offsetof(CPUState, fpr[5]) },
2093
    { "f6", offsetof(CPUState, fpr[6]) },
2094
    { "f7", offsetof(CPUState, fpr[7]) },
2095
    { "f8", offsetof(CPUState, fpr[8]) },
2096
    { "f9", offsetof(CPUState, fpr[9]) },
2097
    { "f10", offsetof(CPUState, fpr[10]) },
2098
    { "f11", offsetof(CPUState, fpr[11]) },
2099
    { "f12", offsetof(CPUState, fpr[12]) },
2100
    { "f13", offsetof(CPUState, fpr[13]) },
2101
    { "f14", offsetof(CPUState, fpr[14]) },
2102
    { "f15", offsetof(CPUState, fpr[15]) },
2103
    { "f16", offsetof(CPUState, fpr[16]) },
2104
    { "f17", offsetof(CPUState, fpr[17]) },
2105
    { "f18", offsetof(CPUState, fpr[18]) },
2106
    { "f19", offsetof(CPUState, fpr[19]) },
2107
    { "f20", offsetof(CPUState, fpr[20]) },
2108
    { "f21", offsetof(CPUState, fpr[21]) },
2109
    { "f22", offsetof(CPUState, fpr[22]) },
2110
    { "f23", offsetof(CPUState, fpr[23]) },
2111
    { "f24", offsetof(CPUState, fpr[24]) },
2112
    { "f25", offsetof(CPUState, fpr[25]) },
2113
    { "f26", offsetof(CPUState, fpr[26]) },
2114
    { "f27", offsetof(CPUState, fpr[27]) },
2115
    { "f28", offsetof(CPUState, fpr[28]) },
2116
    { "f29", offsetof(CPUState, fpr[29]) },
2117
    { "f30", offsetof(CPUState, fpr[30]) },
2118
    { "f31", offsetof(CPUState, fpr[31]) },
2119
#ifdef TARGET_SPARC64
2120
    { "f32", offsetof(CPUState, fpr[32]) },
2121
    { "f34", offsetof(CPUState, fpr[34]) },
2122
    { "f36", offsetof(CPUState, fpr[36]) },
2123
    { "f38", offsetof(CPUState, fpr[38]) },
2124
    { "f40", offsetof(CPUState, fpr[40]) },
2125
    { "f42", offsetof(CPUState, fpr[42]) },
2126
    { "f44", offsetof(CPUState, fpr[44]) },
2127
    { "f46", offsetof(CPUState, fpr[46]) },
2128
    { "f48", offsetof(CPUState, fpr[48]) },
2129
    { "f50", offsetof(CPUState, fpr[50]) },
2130
    { "f52", offsetof(CPUState, fpr[52]) },
2131
    { "f54", offsetof(CPUState, fpr[54]) },
2132
    { "f56", offsetof(CPUState, fpr[56]) },
2133
    { "f58", offsetof(CPUState, fpr[58]) },
2134
    { "f60", offsetof(CPUState, fpr[60]) },
2135
    { "f62", offsetof(CPUState, fpr[62]) },
2136
    { "asi", offsetof(CPUState, asi) },
2137
    { "pstate", offsetof(CPUState, pstate) },
2138
    { "cansave", offsetof(CPUState, cansave) },
2139
    { "canrestore", offsetof(CPUState, canrestore) },
2140
    { "otherwin", offsetof(CPUState, otherwin) },
2141
    { "wstate", offsetof(CPUState, wstate) },
2142
    { "cleanwin", offsetof(CPUState, cleanwin) },
2143
    { "fprs", offsetof(CPUState, fprs) },
2144
#endif
2145
#endif
2146
    { NULL },
2147
};
2148

    
2149
static void expr_error(Monitor *mon, const char *msg)
2150
{
2151
    monitor_printf(mon, "%s\n", msg);
2152
    longjmp(expr_env, 1);
2153
}
2154

    
2155
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
2156
static int get_monitor_def(target_long *pval, const char *name)
2157
{
2158
    const MonitorDef *md;
2159
    void *ptr;
2160

    
2161
    for(md = monitor_defs; md->name != NULL; md++) {
2162
        if (compare_cmd(name, md->name)) {
2163
            if (md->get_value) {
2164
                *pval = md->get_value(md, md->offset);
2165
            } else {
2166
                CPUState *env = mon_get_cpu();
2167
                if (!env)
2168
                    return -2;
2169
                ptr = (uint8_t *)env + md->offset;
2170
                switch(md->type) {
2171
                case MD_I32:
2172
                    *pval = *(int32_t *)ptr;
2173
                    break;
2174
                case MD_TLONG:
2175
                    *pval = *(target_long *)ptr;
2176
                    break;
2177
                default:
2178
                    *pval = 0;
2179
                    break;
2180
                }
2181
            }
2182
            return 0;
2183
        }
2184
    }
2185
    return -1;
2186
}
2187

    
2188
static void next(void)
2189
{
2190
    if (pch != '\0') {
2191
        pch++;
2192
        while (qemu_isspace(*pch))
2193
            pch++;
2194
    }
2195
}
2196

    
2197
static int64_t expr_sum(Monitor *mon);
2198

    
2199
static int64_t expr_unary(Monitor *mon)
2200
{
2201
    int64_t n;
2202
    char *p;
2203
    int ret;
2204

    
2205
    switch(*pch) {
2206
    case '+':
2207
        next();
2208
        n = expr_unary(mon);
2209
        break;
2210
    case '-':
2211
        next();
2212
        n = -expr_unary(mon);
2213
        break;
2214
    case '~':
2215
        next();
2216
        n = ~expr_unary(mon);
2217
        break;
2218
    case '(':
2219
        next();
2220
        n = expr_sum(mon);
2221
        if (*pch != ')') {
2222
            expr_error(mon, "')' expected");
2223
        }
2224
        next();
2225
        break;
2226
    case '\'':
2227
        pch++;
2228
        if (*pch == '\0')
2229
            expr_error(mon, "character constant expected");
2230
        n = *pch;
2231
        pch++;
2232
        if (*pch != '\'')
2233
            expr_error(mon, "missing terminating \' character");
2234
        next();
2235
        break;
2236
    case '$':
2237
        {
2238
            char buf[128], *q;
2239
            target_long reg=0;
2240

    
2241
            pch++;
2242
            q = buf;
2243
            while ((*pch >= 'a' && *pch <= 'z') ||
2244
                   (*pch >= 'A' && *pch <= 'Z') ||
2245
                   (*pch >= '0' && *pch <= '9') ||
2246
                   *pch == '_' || *pch == '.') {
2247
                if ((q - buf) < sizeof(buf) - 1)
2248
                    *q++ = *pch;
2249
                pch++;
2250
            }
2251
            while (qemu_isspace(*pch))
2252
                pch++;
2253
            *q = 0;
2254
            ret = get_monitor_def(&reg, buf);
2255
            if (ret == -1)
2256
                expr_error(mon, "unknown register");
2257
            else if (ret == -2)
2258
                expr_error(mon, "no cpu defined");
2259
            n = reg;
2260
        }
2261
        break;
2262
    case '\0':
2263
        expr_error(mon, "unexpected end of expression");
2264
        n = 0;
2265
        break;
2266
    default:
2267
#if TARGET_PHYS_ADDR_BITS > 32
2268
        n = strtoull(pch, &p, 0);
2269
#else
2270
        n = strtoul(pch, &p, 0);
2271
#endif
2272
        if (pch == p) {
2273
            expr_error(mon, "invalid char in expression");
2274
        }
2275
        pch = p;
2276
        while (qemu_isspace(*pch))
2277
            pch++;
2278
        break;
2279
    }
2280
    return n;
2281
}
2282

    
2283

    
2284
static int64_t expr_prod(Monitor *mon)
2285
{
2286
    int64_t val, val2;
2287
    int op;
2288

    
2289
    val = expr_unary(mon);
2290
    for(;;) {
2291
        op = *pch;
2292
        if (op != '*' && op != '/' && op != '%')
2293
            break;
2294
        next();
2295
        val2 = expr_unary(mon);
2296
        switch(op) {
2297
        default:
2298
        case '*':
2299
            val *= val2;
2300
            break;
2301
        case '/':
2302
        case '%':
2303
            if (val2 == 0)
2304
                expr_error(mon, "division by zero");
2305
            if (op == '/')
2306
                val /= val2;
2307
            else
2308
                val %= val2;
2309
            break;
2310
        }
2311
    }
2312
    return val;
2313
}
2314

    
2315
static int64_t expr_logic(Monitor *mon)
2316
{
2317
    int64_t val, val2;
2318
    int op;
2319

    
2320
    val = expr_prod(mon);
2321
    for(;;) {
2322
        op = *pch;
2323
        if (op != '&' && op != '|' && op != '^')
2324
            break;
2325
        next();
2326
        val2 = expr_prod(mon);
2327
        switch(op) {
2328
        default:
2329
        case '&':
2330
            val &= val2;
2331
            break;
2332
        case '|':
2333
            val |= val2;
2334
            break;
2335
        case '^':
2336
            val ^= val2;
2337
            break;
2338
        }
2339
    }
2340
    return val;
2341
}
2342

    
2343
static int64_t expr_sum(Monitor *mon)
2344
{
2345
    int64_t val, val2;
2346
    int op;
2347

    
2348
    val = expr_logic(mon);
2349
    for(;;) {
2350
        op = *pch;
2351
        if (op != '+' && op != '-')
2352
            break;
2353
        next();
2354
        val2 = expr_logic(mon);
2355
        if (op == '+')
2356
            val += val2;
2357
        else
2358
            val -= val2;
2359
    }
2360
    return val;
2361
}
2362

    
2363
static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2364
{
2365
    pch = *pp;
2366
    if (setjmp(expr_env)) {
2367
        *pp = pch;
2368
        return -1;
2369
    }
2370
    while (qemu_isspace(*pch))
2371
        pch++;
2372
    *pval = expr_sum(mon);
2373
    *pp = pch;
2374
    return 0;
2375
}
2376

    
2377
static int get_str(char *buf, int buf_size, const char **pp)
2378
{
2379
    const char *p;
2380
    char *q;
2381
    int c;
2382

    
2383
    q = buf;
2384
    p = *pp;
2385
    while (qemu_isspace(*p))
2386
        p++;
2387
    if (*p == '\0') {
2388
    fail:
2389
        *q = '\0';
2390
        *pp = p;
2391
        return -1;
2392
    }
2393
    if (*p == '\"') {
2394
        p++;
2395
        while (*p != '\0' && *p != '\"') {
2396
            if (*p == '\\') {
2397
                p++;
2398
                c = *p++;
2399
                switch(c) {
2400
                case 'n':
2401
                    c = '\n';
2402
                    break;
2403
                case 'r':
2404
                    c = '\r';
2405
                    break;
2406
                case '\\':
2407
                case '\'':
2408
                case '\"':
2409
                    break;
2410
                default:
2411
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
2412
                    goto fail;
2413
                }
2414
                if ((q - buf) < buf_size - 1) {
2415
                    *q++ = c;
2416
                }
2417
            } else {
2418
                if ((q - buf) < buf_size - 1) {
2419
                    *q++ = *p;
2420
                }
2421
                p++;
2422
            }
2423
        }
2424
        if (*p != '\"') {
2425
            qemu_printf("unterminated string\n");
2426
            goto fail;
2427
        }
2428
        p++;
2429
    } else {
2430
        while (*p != '\0' && !qemu_isspace(*p)) {
2431
            if ((q - buf) < buf_size - 1) {
2432
                *q++ = *p;
2433
            }
2434
            p++;
2435
        }
2436
    }
2437
    *q = '\0';
2438
    *pp = p;
2439
    return 0;
2440
}
2441

    
2442
static int default_fmt_format = 'x';
2443
static int default_fmt_size = 4;
2444

    
2445
#define MAX_ARGS 16
2446

    
2447
static void monitor_handle_command(Monitor *mon, const char *cmdline)
2448
{
2449
    const char *p, *pstart, *typestr;
2450
    char *q;
2451
    int c, nb_args, len, i, has_arg;
2452
    const mon_cmd_t *cmd;
2453
    char cmdname[256];
2454
    char buf[1024];
2455
    void *str_allocated[MAX_ARGS];
2456
    void *args[MAX_ARGS];
2457
    void (*handler_0)(Monitor *mon);
2458
    void (*handler_1)(Monitor *mon, void *arg0);
2459
    void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2460
    void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2461
    void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2462
                      void *arg3);
2463
    void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2464
                      void *arg3, void *arg4);
2465
    void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2466
                      void *arg3, void *arg4, void *arg5);
2467
    void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2468
                      void *arg3, void *arg4, void *arg5, void *arg6);
2469

    
2470
#ifdef DEBUG
2471
    monitor_printf(mon, "command='%s'\n", cmdline);
2472
#endif
2473

    
2474
    /* extract the command name */
2475
    p = cmdline;
2476
    q = cmdname;
2477
    while (qemu_isspace(*p))
2478
        p++;
2479
    if (*p == '\0')
2480
        return;
2481
    pstart = p;
2482
    while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2483
        p++;
2484
    len = p - pstart;
2485
    if (len > sizeof(cmdname) - 1)
2486
        len = sizeof(cmdname) - 1;
2487
    memcpy(cmdname, pstart, len);
2488
    cmdname[len] = '\0';
2489

    
2490
    /* find the command */
2491
    for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2492
        if (compare_cmd(cmdname, cmd->name))
2493
            goto found;
2494
    }
2495
    monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2496
    return;
2497
 found:
2498

    
2499
    for(i = 0; i < MAX_ARGS; i++)
2500
        str_allocated[i] = NULL;
2501

    
2502
    /* parse the parameters */
2503
    typestr = cmd->args_type;
2504
    nb_args = 0;
2505
    for(;;) {
2506
        c = *typestr;
2507
        if (c == '\0')
2508
            break;
2509
        typestr++;
2510
        switch(c) {
2511
        case 'F':
2512
        case 'B':
2513
        case 's':
2514
            {
2515
                int ret;
2516
                char *str;
2517

    
2518
                while (qemu_isspace(*p))
2519
                    p++;
2520
                if (*typestr == '?') {
2521
                    typestr++;
2522
                    if (*p == '\0') {
2523
                        /* no optional string: NULL argument */
2524
                        str = NULL;
2525
                        goto add_str;
2526
                    }
2527
                }
2528
                ret = get_str(buf, sizeof(buf), &p);
2529
                if (ret < 0) {
2530
                    switch(c) {
2531
                    case 'F':
2532
                        monitor_printf(mon, "%s: filename expected\n",
2533
                                       cmdname);
2534
                        break;
2535
                    case 'B':
2536
                        monitor_printf(mon, "%s: block device name expected\n",
2537
                                       cmdname);
2538
                        break;
2539
                    default:
2540
                        monitor_printf(mon, "%s: string expected\n", cmdname);
2541
                        break;
2542
                    }
2543
                    goto fail;
2544
                }
2545
                str = qemu_malloc(strlen(buf) + 1);
2546
                pstrcpy(str, sizeof(buf), buf);
2547
                str_allocated[nb_args] = str;
2548
            add_str:
2549
                if (nb_args >= MAX_ARGS) {
2550
                error_args:
2551
                    monitor_printf(mon, "%s: too many arguments\n", cmdname);
2552
                    goto fail;
2553
                }
2554
                args[nb_args++] = str;
2555
            }
2556
            break;
2557
        case '/':
2558
            {
2559
                int count, format, size;
2560

    
2561
                while (qemu_isspace(*p))
2562
                    p++;
2563
                if (*p == '/') {
2564
                    /* format found */
2565
                    p++;
2566
                    count = 1;
2567
                    if (qemu_isdigit(*p)) {
2568
                        count = 0;
2569
                        while (qemu_isdigit(*p)) {
2570
                            count = count * 10 + (*p - '0');
2571
                            p++;
2572
                        }
2573
                    }
2574
                    size = -1;
2575
                    format = -1;
2576
                    for(;;) {
2577
                        switch(*p) {
2578
                        case 'o':
2579
                        case 'd':
2580
                        case 'u':
2581
                        case 'x':
2582
                        case 'i':
2583
                        case 'c':
2584
                            format = *p++;
2585
                            break;
2586
                        case 'b':
2587
                            size = 1;
2588
                            p++;
2589
                            break;
2590
                        case 'h':
2591
                            size = 2;
2592
                            p++;
2593
                            break;
2594
                        case 'w':
2595
                            size = 4;
2596
                            p++;
2597
                            break;
2598
                        case 'g':
2599
                        case 'L':
2600
                            size = 8;
2601
                            p++;
2602
                            break;
2603
                        default:
2604
                            goto next;
2605
                        }
2606
                    }
2607
                next:
2608
                    if (*p != '\0' && !qemu_isspace(*p)) {
2609
                        monitor_printf(mon, "invalid char in format: '%c'\n",
2610
                                       *p);
2611
                        goto fail;
2612
                    }
2613
                    if (format < 0)
2614
                        format = default_fmt_format;
2615
                    if (format != 'i') {
2616
                        /* for 'i', not specifying a size gives -1 as size */
2617
                        if (size < 0)
2618
                            size = default_fmt_size;
2619
                        default_fmt_size = size;
2620
                    }
2621
                    default_fmt_format = format;
2622
                } else {
2623
                    count = 1;
2624
                    format = default_fmt_format;
2625
                    if (format != 'i') {
2626
                        size = default_fmt_size;
2627
                    } else {
2628
                        size = -1;
2629
                    }
2630
                }
2631
                if (nb_args + 3 > MAX_ARGS)
2632
                    goto error_args;
2633
                args[nb_args++] = (void*)(long)count;
2634
                args[nb_args++] = (void*)(long)format;
2635
                args[nb_args++] = (void*)(long)size;
2636
            }
2637
            break;
2638
        case 'i':
2639
        case 'l':
2640
            {
2641
                int64_t val;
2642

    
2643
                while (qemu_isspace(*p))
2644
                    p++;
2645
                if (*typestr == '?' || *typestr == '.') {
2646
                    if (*typestr == '?') {
2647
                        if (*p == '\0')
2648
                            has_arg = 0;
2649
                        else
2650
                            has_arg = 1;
2651
                    } else {
2652
                        if (*p == '.') {
2653
                            p++;
2654
                            while (qemu_isspace(*p))
2655
                                p++;
2656
                            has_arg = 1;
2657
                        } else {
2658
                            has_arg = 0;
2659
                        }
2660
                    }
2661
                    typestr++;
2662
                    if (nb_args >= MAX_ARGS)
2663
                        goto error_args;
2664
                    args[nb_args++] = (void *)(long)has_arg;
2665
                    if (!has_arg) {
2666
                        if (nb_args >= MAX_ARGS)
2667
                            goto error_args;
2668
                        val = -1;
2669
                        goto add_num;
2670
                    }
2671
                }
2672
                if (get_expr(mon, &val, &p))
2673
                    goto fail;
2674
            add_num:
2675
                if (c == 'i') {
2676
                    if (nb_args >= MAX_ARGS)
2677
                        goto error_args;
2678
                    args[nb_args++] = (void *)(long)val;
2679
                } else {
2680
                    if ((nb_args + 1) >= MAX_ARGS)
2681
                        goto error_args;
2682
#if TARGET_PHYS_ADDR_BITS > 32
2683
                    args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2684
#else
2685
                    args[nb_args++] = (void *)0;
2686
#endif
2687
                    args[nb_args++] = (void *)(long)(val & 0xffffffff);
2688
                }
2689
            }
2690
            break;
2691
        case '-':
2692
            {
2693
                int has_option;
2694
                /* option */
2695

    
2696
                c = *typestr++;
2697
                if (c == '\0')
2698
                    goto bad_type;
2699
                while (qemu_isspace(*p))
2700
                    p++;
2701
                has_option = 0;
2702
                if (*p == '-') {
2703
                    p++;
2704
                    if (*p != c) {
2705
                        monitor_printf(mon, "%s: unsupported option -%c\n",
2706
                                       cmdname, *p);
2707
                        goto fail;
2708
                    }
2709
                    p++;
2710
                    has_option = 1;
2711
                }
2712
                if (nb_args >= MAX_ARGS)
2713
                    goto error_args;
2714
                args[nb_args++] = (void *)(long)has_option;
2715
            }
2716
            break;
2717
        default:
2718
        bad_type:
2719
            monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2720
            goto fail;
2721
        }
2722
    }
2723
    /* check that all arguments were parsed */
2724
    while (qemu_isspace(*p))
2725
        p++;
2726
    if (*p != '\0') {
2727
        monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2728
                       cmdname);
2729
        goto fail;
2730
    }
2731

    
2732
    switch(nb_args) {
2733
    case 0:
2734
        handler_0 = cmd->handler;
2735
        handler_0(mon);
2736
        break;
2737
    case 1:
2738
        handler_1 = cmd->handler;
2739
        handler_1(mon, args[0]);
2740
        break;
2741
    case 2:
2742
        handler_2 = cmd->handler;
2743
        handler_2(mon, args[0], args[1]);
2744
        break;
2745
    case 3:
2746
        handler_3 = cmd->handler;
2747
        handler_3(mon, args[0], args[1], args[2]);
2748
        break;
2749
    case 4:
2750
        handler_4 = cmd->handler;
2751
        handler_4(mon, args[0], args[1], args[2], args[3]);
2752
        break;
2753
    case 5:
2754
        handler_5 = cmd->handler;
2755
        handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2756
        break;
2757
    case 6:
2758
        handler_6 = cmd->handler;
2759
        handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2760
        break;
2761
    case 7:
2762
        handler_7 = cmd->handler;
2763
        handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2764
                  args[6]);
2765
        break;
2766
    default:
2767
        monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2768
        goto fail;
2769
    }
2770
 fail:
2771
    for(i = 0; i < MAX_ARGS; i++)
2772
        qemu_free(str_allocated[i]);
2773
    return;
2774
}
2775

    
2776
static void cmd_completion(const char *name, const char *list)
2777
{
2778
    const char *p, *pstart;
2779
    char cmd[128];
2780
    int len;
2781

    
2782
    p = list;
2783
    for(;;) {
2784
        pstart = p;
2785
        p = strchr(p, '|');
2786
        if (!p)
2787
            p = pstart + strlen(pstart);
2788
        len = p - pstart;
2789
        if (len > sizeof(cmd) - 2)
2790
            len = sizeof(cmd) - 2;
2791
        memcpy(cmd, pstart, len);
2792
        cmd[len] = '\0';
2793
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2794
            readline_add_completion(cur_mon->rs, cmd);
2795
        }
2796
        if (*p == '\0')
2797
            break;
2798
        p++;
2799
    }
2800
}
2801

    
2802
static void file_completion(const char *input)
2803
{
2804
    DIR *ffs;
2805
    struct dirent *d;
2806
    char path[1024];
2807
    char file[1024], file_prefix[1024];
2808
    int input_path_len;
2809
    const char *p;
2810

    
2811
    p = strrchr(input, '/');
2812
    if (!p) {
2813
        input_path_len = 0;
2814
        pstrcpy(file_prefix, sizeof(file_prefix), input);
2815
        pstrcpy(path, sizeof(path), ".");
2816
    } else {
2817
        input_path_len = p - input + 1;
2818
        memcpy(path, input, input_path_len);
2819
        if (input_path_len > sizeof(path) - 1)
2820
            input_path_len = sizeof(path) - 1;
2821
        path[input_path_len] = '\0';
2822
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2823
    }
2824
#ifdef DEBUG_COMPLETION
2825
    monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2826
                   input, path, file_prefix);
2827
#endif
2828
    ffs = opendir(path);
2829
    if (!ffs)
2830
        return;
2831
    for(;;) {
2832
        struct stat sb;
2833
        d = readdir(ffs);
2834
        if (!d)
2835
            break;
2836
        if (strstart(d->d_name, file_prefix, NULL)) {
2837
            memcpy(file, input, input_path_len);
2838
            if (input_path_len < sizeof(file))
2839
                pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2840
                        d->d_name);
2841
            /* stat the file to find out if it's a directory.
2842
             * In that case add a slash to speed up typing long paths
2843
             */
2844
            stat(file, &sb);
2845
            if(S_ISDIR(sb.st_mode))
2846
                pstrcat(file, sizeof(file), "/");
2847
            readline_add_completion(cur_mon->rs, file);
2848
        }
2849
    }
2850
    closedir(ffs);
2851
}
2852

    
2853
static void block_completion_it(void *opaque, BlockDriverState *bs)
2854
{
2855
    const char *name = bdrv_get_device_name(bs);
2856
    const char *input = opaque;
2857

    
2858
    if (input[0] == '\0' ||
2859
        !strncmp(name, (char *)input, strlen(input))) {
2860
        readline_add_completion(cur_mon->rs, name);
2861
    }
2862
}
2863

    
2864
/* NOTE: this parser is an approximate form of the real command parser */
2865
static void parse_cmdline(const char *cmdline,
2866
                         int *pnb_args, char **args)
2867
{
2868
    const char *p;
2869
    int nb_args, ret;
2870
    char buf[1024];
2871

    
2872
    p = cmdline;
2873
    nb_args = 0;
2874
    for(;;) {
2875
        while (qemu_isspace(*p))
2876
            p++;
2877
        if (*p == '\0')
2878
            break;
2879
        if (nb_args >= MAX_ARGS)
2880
            break;
2881
        ret = get_str(buf, sizeof(buf), &p);
2882
        args[nb_args] = qemu_strdup(buf);
2883
        nb_args++;
2884
        if (ret < 0)
2885
            break;
2886
    }
2887
    *pnb_args = nb_args;
2888
}
2889

    
2890
static void monitor_find_completion(const char *cmdline)
2891
{
2892
    const char *cmdname;
2893
    char *args[MAX_ARGS];
2894
    int nb_args, i, len;
2895
    const char *ptype, *str;
2896
    const mon_cmd_t *cmd;
2897
    const KeyDef *key;
2898

    
2899
    parse_cmdline(cmdline, &nb_args, args);
2900
#ifdef DEBUG_COMPLETION
2901
    for(i = 0; i < nb_args; i++) {
2902
        monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2903
    }
2904
#endif
2905

    
2906
    /* if the line ends with a space, it means we want to complete the
2907
       next arg */
2908
    len = strlen(cmdline);
2909
    if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2910
        if (nb_args >= MAX_ARGS)
2911
            return;
2912
        args[nb_args++] = qemu_strdup("");
2913
    }
2914
    if (nb_args <= 1) {
2915
        /* command completion */
2916
        if (nb_args == 0)
2917
            cmdname = "";
2918
        else
2919
            cmdname = args[0];
2920
        readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2921
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2922
            cmd_completion(cmdname, cmd->name);
2923
        }
2924
    } else {
2925
        /* find the command */
2926
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2927
            if (compare_cmd(args[0], cmd->name))
2928
                goto found;
2929
        }
2930
        return;
2931
    found:
2932
        ptype = cmd->args_type;
2933
        for(i = 0; i < nb_args - 2; i++) {
2934
            if (*ptype != '\0') {
2935
                ptype++;
2936
                while (*ptype == '?')
2937
                    ptype++;
2938
            }
2939
        }
2940
        str = args[nb_args - 1];
2941
        switch(*ptype) {
2942
        case 'F':
2943
            /* file completion */
2944
            readline_set_completion_index(cur_mon->rs, strlen(str));
2945
            file_completion(str);
2946
            break;
2947
        case 'B':
2948
            /* block device name completion */
2949
            readline_set_completion_index(cur_mon->rs, strlen(str));
2950
            bdrv_iterate(block_completion_it, (void *)str);
2951
            break;
2952
        case 's':
2953
            /* XXX: more generic ? */
2954
            if (!strcmp(cmd->name, "info")) {
2955
                readline_set_completion_index(cur_mon->rs, strlen(str));
2956
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2957
                    cmd_completion(str, cmd->name);
2958
                }
2959
            } else if (!strcmp(cmd->name, "sendkey")) {
2960
                char *sep = strrchr(str, '-');
2961
                if (sep)
2962
                    str = sep + 1;
2963
                readline_set_completion_index(cur_mon->rs, strlen(str));
2964
                for(key = key_defs; key->name != NULL; key++) {
2965
                    cmd_completion(str, key->name);
2966
                }
2967
            }
2968
            break;
2969
        default:
2970
            break;
2971
        }
2972
    }
2973
    for(i = 0; i < nb_args; i++)
2974
        qemu_free(args[i]);
2975
}
2976

    
2977
static int monitor_can_read(void *opaque)
2978
{
2979
    Monitor *mon = opaque;
2980

    
2981
    return (mon->suspend_cnt == 0) ? 128 : 0;
2982
}
2983

    
2984
static void monitor_read(void *opaque, const uint8_t *buf, int size)
2985
{
2986
    Monitor *old_mon = cur_mon;
2987
    int i;
2988

    
2989
    cur_mon = opaque;
2990

    
2991
    if (cur_mon->rs) {
2992
        for (i = 0; i < size; i++)
2993
            readline_handle_byte(cur_mon->rs, buf[i]);
2994
    } else {
2995
        if (size == 0 || buf[size - 1] != 0)
2996
            monitor_printf(cur_mon, "corrupted command\n");
2997
        else
2998
            monitor_handle_command(cur_mon, (char *)buf);
2999
    }
3000

    
3001
    cur_mon = old_mon;
3002
}
3003

    
3004
static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3005
{
3006
    monitor_suspend(mon);
3007
    monitor_handle_command(mon, cmdline);
3008
    monitor_resume(mon);
3009
}
3010

    
3011
int monitor_suspend(Monitor *mon)
3012
{
3013
    if (!mon->rs)
3014
        return -ENOTTY;
3015
    mon->suspend_cnt++;
3016
    return 0;
3017
}
3018

    
3019
void monitor_resume(Monitor *mon)
3020
{
3021
    if (!mon->rs)
3022
        return;
3023
    if (--mon->suspend_cnt == 0)
3024
        readline_show_prompt(mon->rs);
3025
}
3026

    
3027
static void monitor_event(void *opaque, int event)
3028
{
3029
    Monitor *mon = opaque;
3030

    
3031
    switch (event) {
3032
    case CHR_EVENT_MUX_IN:
3033
        readline_restart(mon->rs);
3034
        monitor_resume(mon);
3035
        monitor_flush(mon);
3036
        break;
3037

    
3038
    case CHR_EVENT_MUX_OUT:
3039
        if (mon->suspend_cnt == 0)
3040
            monitor_printf(mon, "\n");
3041
        monitor_flush(mon);
3042
        monitor_suspend(mon);
3043
        break;
3044

    
3045
    case CHR_EVENT_RESET:
3046
        monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3047
                       "information\n", QEMU_VERSION);
3048
        if (mon->chr->focus == 0)
3049
            readline_show_prompt(mon->rs);
3050
        break;
3051
    }
3052
}
3053

    
3054

    
3055
/*
3056
 * Local variables:
3057
 *  c-indent-level: 4
3058
 *  c-basic-offset: 4
3059
 *  tab-width: 8
3060
 * End:
3061
 */
3062

    
3063
void monitor_init(CharDriverState *chr, int flags)
3064
{
3065
    static int is_first_init = 1;
3066
    Monitor *mon;
3067

    
3068
    if (is_first_init) {
3069
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3070
        is_first_init = 0;
3071
    }
3072

    
3073
    mon = qemu_mallocz(sizeof(*mon));
3074

    
3075
    mon->chr = chr;
3076
    mon->flags = flags;
3077
    if (mon->chr->focus != 0)
3078
        mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3079
    if (flags & MONITOR_USE_READLINE) {
3080
        mon->rs = readline_init(mon, monitor_find_completion);
3081
        monitor_read_command(mon, 0);
3082
    }
3083

    
3084
    qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3085
                          mon);
3086

    
3087
    LIST_INSERT_HEAD(&mon_list, mon, entry);
3088
    if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3089
        cur_mon = mon;
3090
}
3091

    
3092
static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3093
{
3094
    BlockDriverState *bs = opaque;
3095
    int ret = 0;
3096

    
3097
    if (bdrv_set_key(bs, password) != 0) {
3098
        monitor_printf(mon, "invalid password\n");
3099
        ret = -EPERM;
3100
    }
3101
    if (mon->password_completion_cb)
3102
        mon->password_completion_cb(mon->password_opaque, ret);
3103

    
3104
    monitor_read_command(mon, 1);
3105
}
3106

    
3107
void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3108
                                 BlockDriverCompletionFunc *completion_cb,
3109
                                 void *opaque)
3110
{
3111
    int err;
3112

    
3113
    if (!bdrv_key_required(bs)) {
3114
        if (completion_cb)
3115
            completion_cb(opaque, 0);
3116
        return;
3117
    }
3118

    
3119
    monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3120
                   bdrv_get_encrypted_filename(bs));
3121

    
3122
    mon->password_completion_cb = completion_cb;
3123
    mon->password_opaque = opaque;
3124

    
3125
    err = monitor_read_password(mon, bdrv_password_cb, bs);
3126

    
3127
    if (err && completion_cb)
3128
        completion_cb(opaque, err);
3129
}