Statistics
| Branch: | Revision:

root / monitor.c @ 590fb3b7

History | View | Annotate | Download (88.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/qdev.h"
27
#include "hw/usb.h"
28
#include "hw/pcmcia.h"
29
#include "hw/pc.h"
30
#include "hw/pci.h"
31
#include "hw/watchdog.h"
32
#include "gdbstub.h"
33
#include "net.h"
34
#include "qemu-char.h"
35
#include "sysemu.h"
36
#include "monitor.h"
37
#include "readline.h"
38
#include "console.h"
39
#include "block.h"
40
#include "audio/audio.h"
41
#include "disas.h"
42
#include "balloon.h"
43
#include "qemu-timer.h"
44
#include "migration.h"
45
#include "kvm.h"
46
#include "acl.h"
47
#include "qint.h"
48
#include "qdict.h"
49
#include "qstring.h"
50

    
51
//#define DEBUG
52
//#define DEBUG_COMPLETION
53

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

    
68
typedef struct mon_cmd_t {
69
    const char *name;
70
    const char *args_type;
71
    void *handler;
72
    const char *params;
73
    const char *help;
74
} mon_cmd_t;
75

    
76
/* file descriptors passed via SCM_RIGHTS */
77
typedef struct mon_fd_t mon_fd_t;
78
struct mon_fd_t {
79
    char *name;
80
    int fd;
81
    LIST_ENTRY(mon_fd_t) next;
82
};
83

    
84
struct Monitor {
85
    CharDriverState *chr;
86
    int flags;
87
    int suspend_cnt;
88
    uint8_t outbuf[1024];
89
    int outbuf_index;
90
    ReadLineState *rs;
91
    CPUState *mon_cpu;
92
    BlockDriverCompletionFunc *password_completion_cb;
93
    void *password_opaque;
94
    LIST_HEAD(,mon_fd_t) fds;
95
    LIST_ENTRY(Monitor) entry;
96
};
97

    
98
static LIST_HEAD(mon_list, Monitor) mon_list;
99

    
100
static const mon_cmd_t mon_cmds[];
101
static const mon_cmd_t info_cmds[];
102

    
103
Monitor *cur_mon = NULL;
104

    
105
static void monitor_command_cb(Monitor *mon, const char *cmdline,
106
                               void *opaque);
107

    
108
static void monitor_read_command(Monitor *mon, int show_prompt)
109
{
110
    readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
111
    if (show_prompt)
112
        readline_show_prompt(mon->rs);
113
}
114

    
115
static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
116
                                 void *opaque)
117
{
118
    if (mon->rs) {
119
        readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
120
        /* prompt is printed on return from the command handler */
121
        return 0;
122
    } else {
123
        monitor_printf(mon, "terminal does not support password prompting\n");
124
        return -ENOTTY;
125
    }
126
}
127

    
128
void monitor_flush(Monitor *mon)
129
{
130
    if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
131
        qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
132
        mon->outbuf_index = 0;
133
    }
134
}
135

    
136
/* flush at every end of line or if the buffer is full */
137
static void monitor_puts(Monitor *mon, const char *str)
138
{
139
    char c;
140

    
141
    if (!mon)
142
        return;
143

    
144
    for(;;) {
145
        c = *str++;
146
        if (c == '\0')
147
            break;
148
        if (c == '\n')
149
            mon->outbuf[mon->outbuf_index++] = '\r';
150
        mon->outbuf[mon->outbuf_index++] = c;
151
        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
152
            || c == '\n')
153
            monitor_flush(mon);
154
    }
155
}
156

    
157
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
158
{
159
    char buf[4096];
160
    vsnprintf(buf, sizeof(buf), fmt, ap);
161
    monitor_puts(mon, buf);
162
}
163

    
164
void monitor_printf(Monitor *mon, const char *fmt, ...)
165
{
166
    va_list ap;
167
    va_start(ap, fmt);
168
    monitor_vprintf(mon, fmt, ap);
169
    va_end(ap);
170
}
171

    
172
void monitor_print_filename(Monitor *mon, const char *filename)
173
{
174
    int i;
175

    
176
    for (i = 0; filename[i]; i++) {
177
        switch (filename[i]) {
178
        case ' ':
179
        case '"':
180
        case '\\':
181
            monitor_printf(mon, "\\%c", filename[i]);
182
            break;
183
        case '\t':
184
            monitor_printf(mon, "\\t");
185
            break;
186
        case '\r':
187
            monitor_printf(mon, "\\r");
188
            break;
189
        case '\n':
190
            monitor_printf(mon, "\\n");
191
            break;
192
        default:
193
            monitor_printf(mon, "%c", filename[i]);
194
            break;
195
        }
196
    }
197
}
198

    
199
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
200
{
201
    va_list ap;
202
    va_start(ap, fmt);
203
    monitor_vprintf((Monitor *)stream, fmt, ap);
204
    va_end(ap);
205
    return 0;
206
}
207

    
208
static int compare_cmd(const char *name, const char *list)
209
{
210
    const char *p, *pstart;
211
    int len;
212
    len = strlen(name);
213
    p = list;
214
    for(;;) {
215
        pstart = p;
216
        p = strchr(p, '|');
217
        if (!p)
218
            p = pstart + strlen(pstart);
219
        if ((p - pstart) == len && !memcmp(pstart, name, len))
220
            return 1;
221
        if (*p == '\0')
222
            break;
223
        p++;
224
    }
225
    return 0;
226
}
227

    
228
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
229
                          const char *prefix, const char *name)
230
{
231
    const mon_cmd_t *cmd;
232

    
233
    for(cmd = cmds; cmd->name != NULL; cmd++) {
234
        if (!name || !strcmp(name, cmd->name))
235
            monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
236
                           cmd->params, cmd->help);
237
    }
238
}
239

    
240
static void help_cmd(Monitor *mon, const char *name)
241
{
242
    if (name && !strcmp(name, "info")) {
243
        help_cmd_dump(mon, info_cmds, "info ", NULL);
244
    } else {
245
        help_cmd_dump(mon, mon_cmds, "", name);
246
        if (name && !strcmp(name, "log")) {
247
            const CPULogItem *item;
248
            monitor_printf(mon, "Log items (comma separated):\n");
249
            monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
250
            for(item = cpu_log_items; item->mask != 0; item++) {
251
                monitor_printf(mon, "%-10s %s\n", item->name, item->help);
252
            }
253
        }
254
    }
255
}
256

    
257
static void do_help_cmd(Monitor *mon, const QDict *qdict)
258
{
259
    help_cmd(mon, qdict_get_try_str(qdict, "name"));
260
}
261

    
262
static void do_commit(Monitor *mon, const QDict *qdict)
263
{
264
    int all_devices;
265
    DriveInfo *dinfo;
266
    const char *device = qdict_get_str(qdict, "device");
267

    
268
    all_devices = !strcmp(device, "all");
269
    TAILQ_FOREACH(dinfo, &drives, next) {
270
        if (!all_devices)
271
            if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
272
                continue;
273
        bdrv_commit(dinfo->bdrv);
274
    }
275
}
276

    
277
static void do_info(Monitor *mon, const QDict *qdict)
278
{
279
    const mon_cmd_t *cmd;
280
    const char *item = qdict_get_try_str(qdict, "item");
281
    void (*handler)(Monitor *);
282

    
283
    if (!item)
284
        goto help;
285
    for(cmd = info_cmds; cmd->name != NULL; cmd++) {
286
        if (compare_cmd(item, cmd->name))
287
            goto found;
288
    }
289
 help:
290
    help_cmd(mon, "info");
291
    return;
292
 found:
293
    handler = cmd->handler;
294
    handler(mon);
295
}
296

    
297
static void do_info_version(Monitor *mon)
298
{
299
    monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
300
}
301

    
302
static void do_info_name(Monitor *mon)
303
{
304
    if (qemu_name)
305
        monitor_printf(mon, "%s\n", qemu_name);
306
}
307

    
308
#if defined(TARGET_I386)
309
static void do_info_hpet(Monitor *mon)
310
{
311
    monitor_printf(mon, "HPET is %s by QEMU\n",
312
                   (no_hpet) ? "disabled" : "enabled");
313
}
314
#endif
315

    
316
static void do_info_uuid(Monitor *mon)
317
{
318
    monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
319
                   qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
320
                   qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
321
                   qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
322
                   qemu_uuid[14], qemu_uuid[15]);
323
}
324

    
325
/* get the current CPU defined by the user */
326
static int mon_set_cpu(int cpu_index)
327
{
328
    CPUState *env;
329

    
330
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
331
        if (env->cpu_index == cpu_index) {
332
            cur_mon->mon_cpu = env;
333
            return 0;
334
        }
335
    }
336
    return -1;
337
}
338

    
339
static CPUState *mon_get_cpu(void)
340
{
341
    if (!cur_mon->mon_cpu) {
342
        mon_set_cpu(0);
343
    }
344
    cpu_synchronize_state(cur_mon->mon_cpu);
345
    return cur_mon->mon_cpu;
346
}
347

    
348
static void do_info_registers(Monitor *mon)
349
{
350
    CPUState *env;
351
    env = mon_get_cpu();
352
    if (!env)
353
        return;
354
#ifdef TARGET_I386
355
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
356
                   X86_DUMP_FPU);
357
#else
358
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
359
                   0);
360
#endif
361
}
362

    
363
static void do_info_cpus(Monitor *mon)
364
{
365
    CPUState *env;
366

    
367
    /* just to set the default cpu if not already done */
368
    mon_get_cpu();
369

    
370
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
371
        cpu_synchronize_state(env);
372
        monitor_printf(mon, "%c CPU #%d:",
373
                       (env == mon->mon_cpu) ? '*' : ' ',
374
                       env->cpu_index);
375
#if defined(TARGET_I386)
376
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
377
                       env->eip + env->segs[R_CS].base);
378
#elif defined(TARGET_PPC)
379
        monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
380
#elif defined(TARGET_SPARC)
381
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
382
                       env->pc, env->npc);
383
#elif defined(TARGET_MIPS)
384
        monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
385
#endif
386
        if (env->halted)
387
            monitor_printf(mon, " (halted)");
388
        monitor_printf(mon, "\n");
389
    }
390
}
391

    
392
static void do_cpu_set(Monitor *mon, const QDict *qdict)
393
{
394
    int index = qdict_get_int(qdict, "index");
395
    if (mon_set_cpu(index) < 0)
396
        monitor_printf(mon, "Invalid CPU index\n");
397
}
398

    
399
static void do_info_jit(Monitor *mon)
400
{
401
    dump_exec_info((FILE *)mon, monitor_fprintf);
402
}
403

    
404
static void do_info_history(Monitor *mon)
405
{
406
    int i;
407
    const char *str;
408

    
409
    if (!mon->rs)
410
        return;
411
    i = 0;
412
    for(;;) {
413
        str = readline_get_history(mon->rs, i);
414
        if (!str)
415
            break;
416
        monitor_printf(mon, "%d: '%s'\n", i, str);
417
        i++;
418
    }
419
}
420

    
421
#if defined(TARGET_PPC)
422
/* XXX: not implemented in other targets */
423
static void do_info_cpu_stats(Monitor *mon)
424
{
425
    CPUState *env;
426

    
427
    env = mon_get_cpu();
428
    cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
429
}
430
#endif
431

    
432
static void do_quit(Monitor *mon, const QDict *qdict)
433
{
434
    exit(0);
435
}
436

    
437
static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
438
{
439
    if (bdrv_is_inserted(bs)) {
440
        if (!force) {
441
            if (!bdrv_is_removable(bs)) {
442
                monitor_printf(mon, "device is not removable\n");
443
                return -1;
444
            }
445
            if (bdrv_is_locked(bs)) {
446
                monitor_printf(mon, "device is locked\n");
447
                return -1;
448
            }
449
        }
450
        bdrv_close(bs);
451
    }
452
    return 0;
453
}
454

    
455
static void do_eject(Monitor *mon, const QDict *qdict)
456
{
457
    BlockDriverState *bs;
458
    int force = qdict_get_int(qdict, "force");
459
    const char *filename = qdict_get_str(qdict, "filename");
460

    
461
    bs = bdrv_find(filename);
462
    if (!bs) {
463
        monitor_printf(mon, "device not found\n");
464
        return;
465
    }
466
    eject_device(mon, bs, force);
467
}
468

    
469
static void do_change_block(Monitor *mon, const char *device,
470
                            const char *filename, const char *fmt)
471
{
472
    BlockDriverState *bs;
473
    BlockDriver *drv = NULL;
474

    
475
    bs = bdrv_find(device);
476
    if (!bs) {
477
        monitor_printf(mon, "device not found\n");
478
        return;
479
    }
480
    if (fmt) {
481
        drv = bdrv_find_format(fmt);
482
        if (!drv) {
483
            monitor_printf(mon, "invalid format %s\n", fmt);
484
            return;
485
        }
486
    }
487
    if (eject_device(mon, bs, 0) < 0)
488
        return;
489
    bdrv_open2(bs, filename, 0, drv);
490
    monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
491
}
492

    
493
static void change_vnc_password_cb(Monitor *mon, const char *password,
494
                                   void *opaque)
495
{
496
    if (vnc_display_password(NULL, password) < 0)
497
        monitor_printf(mon, "could not set VNC server password\n");
498

    
499
    monitor_read_command(mon, 1);
500
}
501

    
502
static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
503
{
504
    if (strcmp(target, "passwd") == 0 ||
505
        strcmp(target, "password") == 0) {
506
        if (arg) {
507
            char password[9];
508
            strncpy(password, arg, sizeof(password));
509
            password[sizeof(password) - 1] = '\0';
510
            change_vnc_password_cb(mon, password, NULL);
511
        } else {
512
            monitor_read_password(mon, change_vnc_password_cb, NULL);
513
        }
514
    } else {
515
        if (vnc_display_open(NULL, target) < 0)
516
            monitor_printf(mon, "could not start VNC server on %s\n", target);
517
    }
518
}
519

    
520
static void do_change(Monitor *mon, const QDict *qdict)
521
{
522
    const char *device = qdict_get_str(qdict, "device");
523
    const char *target = qdict_get_str(qdict, "target");
524
    const char *arg = qdict_get_try_str(qdict, "arg");
525
    if (strcmp(device, "vnc") == 0) {
526
        do_change_vnc(mon, target, arg);
527
    } else {
528
        do_change_block(mon, device, target, arg);
529
    }
530
}
531

    
532
static void do_screen_dump(Monitor *mon, const QDict *qdict)
533
{
534
    vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
535
}
536

    
537
static void do_logfile(Monitor *mon, const QDict *qdict)
538
{
539
    cpu_set_log_filename(qdict_get_str(qdict, "filename"));
540
}
541

    
542
static void do_log(Monitor *mon, const QDict *qdict)
543
{
544
    int mask;
545
    const char *items = qdict_get_str(qdict, "items");
546

    
547
    if (!strcmp(items, "none")) {
548
        mask = 0;
549
    } else {
550
        mask = cpu_str_to_log_mask(items);
551
        if (!mask) {
552
            help_cmd(mon, "log");
553
            return;
554
        }
555
    }
556
    cpu_set_log(mask);
557
}
558

    
559
static void do_singlestep(Monitor *mon, const QDict *qdict)
560
{
561
    const char *option = qdict_get_try_str(qdict, "option");
562
    if (!option || !strcmp(option, "on")) {
563
        singlestep = 1;
564
    } else if (!strcmp(option, "off")) {
565
        singlestep = 0;
566
    } else {
567
        monitor_printf(mon, "unexpected option %s\n", option);
568
    }
569
}
570

    
571
static void do_stop(Monitor *mon, const QDict *qdict)
572
{
573
    vm_stop(EXCP_INTERRUPT);
574
}
575

    
576
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
577

    
578
struct bdrv_iterate_context {
579
    Monitor *mon;
580
    int err;
581
};
582

    
583
static void do_cont(Monitor *mon, const QDict *qdict)
584
{
585
    struct bdrv_iterate_context context = { mon, 0 };
586

    
587
    bdrv_iterate(encrypted_bdrv_it, &context);
588
    /* only resume the vm if all keys are set and valid */
589
    if (!context.err)
590
        vm_start();
591
}
592

    
593
static void bdrv_key_cb(void *opaque, int err)
594
{
595
    Monitor *mon = opaque;
596

    
597
    /* another key was set successfully, retry to continue */
598
    if (!err)
599
        do_cont(mon, NULL);
600
}
601

    
602
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
603
{
604
    struct bdrv_iterate_context *context = opaque;
605

    
606
    if (!context->err && bdrv_key_required(bs)) {
607
        context->err = -EBUSY;
608
        monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
609
                                    context->mon);
610
    }
611
}
612

    
613
static void do_gdbserver(Monitor *mon, const QDict *qdict)
614
{
615
    const char *device = qdict_get_try_str(qdict, "device");
616
    if (!device)
617
        device = "tcp::" DEFAULT_GDBSTUB_PORT;
618
    if (gdbserver_start(device) < 0) {
619
        monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
620
                       device);
621
    } else if (strcmp(device, "none") == 0) {
622
        monitor_printf(mon, "Disabled gdbserver\n");
623
    } else {
624
        monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
625
                       device);
626
    }
627
}
628

    
629
static void do_watchdog_action(Monitor *mon, const QDict *qdict)
630
{
631
    const char *action = qdict_get_str(qdict, "action");
632
    if (select_watchdog_action(action) == -1) {
633
        monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
634
    }
635
}
636

    
637
static void monitor_printc(Monitor *mon, int c)
638
{
639
    monitor_printf(mon, "'");
640
    switch(c) {
641
    case '\'':
642
        monitor_printf(mon, "\\'");
643
        break;
644
    case '\\':
645
        monitor_printf(mon, "\\\\");
646
        break;
647
    case '\n':
648
        monitor_printf(mon, "\\n");
649
        break;
650
    case '\r':
651
        monitor_printf(mon, "\\r");
652
        break;
653
    default:
654
        if (c >= 32 && c <= 126) {
655
            monitor_printf(mon, "%c", c);
656
        } else {
657
            monitor_printf(mon, "\\x%02x", c);
658
        }
659
        break;
660
    }
661
    monitor_printf(mon, "'");
662
}
663

    
664
static void memory_dump(Monitor *mon, int count, int format, int wsize,
665
                        target_phys_addr_t addr, int is_physical)
666
{
667
    CPUState *env;
668
    int nb_per_line, l, line_size, i, max_digits, len;
669
    uint8_t buf[16];
670
    uint64_t v;
671

    
672
    if (format == 'i') {
673
        int flags;
674
        flags = 0;
675
        env = mon_get_cpu();
676
        if (!env && !is_physical)
677
            return;
678
#ifdef TARGET_I386
679
        if (wsize == 2) {
680
            flags = 1;
681
        } else if (wsize == 4) {
682
            flags = 0;
683
        } else {
684
            /* as default we use the current CS size */
685
            flags = 0;
686
            if (env) {
687
#ifdef TARGET_X86_64
688
                if ((env->efer & MSR_EFER_LMA) &&
689
                    (env->segs[R_CS].flags & DESC_L_MASK))
690
                    flags = 2;
691
                else
692
#endif
693
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
694
                    flags = 1;
695
            }
696
        }
697
#endif
698
        monitor_disas(mon, env, addr, count, is_physical, flags);
699
        return;
700
    }
701

    
702
    len = wsize * count;
703
    if (wsize == 1)
704
        line_size = 8;
705
    else
706
        line_size = 16;
707
    nb_per_line = line_size / wsize;
708
    max_digits = 0;
709

    
710
    switch(format) {
711
    case 'o':
712
        max_digits = (wsize * 8 + 2) / 3;
713
        break;
714
    default:
715
    case 'x':
716
        max_digits = (wsize * 8) / 4;
717
        break;
718
    case 'u':
719
    case 'd':
720
        max_digits = (wsize * 8 * 10 + 32) / 33;
721
        break;
722
    case 'c':
723
        wsize = 1;
724
        break;
725
    }
726

    
727
    while (len > 0) {
728
        if (is_physical)
729
            monitor_printf(mon, TARGET_FMT_plx ":", addr);
730
        else
731
            monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
732
        l = len;
733
        if (l > line_size)
734
            l = line_size;
735
        if (is_physical) {
736
            cpu_physical_memory_rw(addr, buf, l, 0);
737
        } else {
738
            env = mon_get_cpu();
739
            if (!env)
740
                break;
741
            if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
742
                monitor_printf(mon, " Cannot access memory\n");
743
                break;
744
            }
745
        }
746
        i = 0;
747
        while (i < l) {
748
            switch(wsize) {
749
            default:
750
            case 1:
751
                v = ldub_raw(buf + i);
752
                break;
753
            case 2:
754
                v = lduw_raw(buf + i);
755
                break;
756
            case 4:
757
                v = (uint32_t)ldl_raw(buf + i);
758
                break;
759
            case 8:
760
                v = ldq_raw(buf + i);
761
                break;
762
            }
763
            monitor_printf(mon, " ");
764
            switch(format) {
765
            case 'o':
766
                monitor_printf(mon, "%#*" PRIo64, max_digits, v);
767
                break;
768
            case 'x':
769
                monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
770
                break;
771
            case 'u':
772
                monitor_printf(mon, "%*" PRIu64, max_digits, v);
773
                break;
774
            case 'd':
775
                monitor_printf(mon, "%*" PRId64, max_digits, v);
776
                break;
777
            case 'c':
778
                monitor_printc(mon, v);
779
                break;
780
            }
781
            i += wsize;
782
        }
783
        monitor_printf(mon, "\n");
784
        addr += l;
785
        len -= l;
786
    }
787
}
788

    
789
static void do_memory_dump(Monitor *mon, const QDict *qdict)
790
{
791
    int count = qdict_get_int(qdict, "count");
792
    int format = qdict_get_int(qdict, "format");
793
    int size = qdict_get_int(qdict, "size");
794
    target_long addr = qdict_get_int(qdict, "addr");
795

    
796
    memory_dump(mon, count, format, size, addr, 0);
797
}
798

    
799
static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
800
{
801
    int count = qdict_get_int(qdict, "count");
802
    int format = qdict_get_int(qdict, "format");
803
    int size = qdict_get_int(qdict, "size");
804
    target_phys_addr_t addr = qdict_get_int(qdict, "addr");
805

    
806
    memory_dump(mon, count, format, size, addr, 1);
807
}
808

    
809
static void do_print(Monitor *mon, const QDict *qdict)
810
{
811
    int format = qdict_get_int(qdict, "format");
812
    target_phys_addr_t val = qdict_get_int(qdict, "val");
813

    
814
#if TARGET_PHYS_ADDR_BITS == 32
815
    switch(format) {
816
    case 'o':
817
        monitor_printf(mon, "%#o", val);
818
        break;
819
    case 'x':
820
        monitor_printf(mon, "%#x", val);
821
        break;
822
    case 'u':
823
        monitor_printf(mon, "%u", val);
824
        break;
825
    default:
826
    case 'd':
827
        monitor_printf(mon, "%d", val);
828
        break;
829
    case 'c':
830
        monitor_printc(mon, val);
831
        break;
832
    }
833
#else
834
    switch(format) {
835
    case 'o':
836
        monitor_printf(mon, "%#" PRIo64, val);
837
        break;
838
    case 'x':
839
        monitor_printf(mon, "%#" PRIx64, val);
840
        break;
841
    case 'u':
842
        monitor_printf(mon, "%" PRIu64, val);
843
        break;
844
    default:
845
    case 'd':
846
        monitor_printf(mon, "%" PRId64, val);
847
        break;
848
    case 'c':
849
        monitor_printc(mon, val);
850
        break;
851
    }
852
#endif
853
    monitor_printf(mon, "\n");
854
}
855

    
856
static void do_memory_save(Monitor *mon, const QDict *qdict)
857
{
858
    FILE *f;
859
    uint32_t size = qdict_get_int(qdict, "size");
860
    const char *filename = qdict_get_str(qdict, "filename");
861
    target_long addr = qdict_get_int(qdict, "val");
862
    uint32_t l;
863
    CPUState *env;
864
    uint8_t buf[1024];
865

    
866
    env = mon_get_cpu();
867
    if (!env)
868
        return;
869

    
870
    f = fopen(filename, "wb");
871
    if (!f) {
872
        monitor_printf(mon, "could not open '%s'\n", filename);
873
        return;
874
    }
875
    while (size != 0) {
876
        l = sizeof(buf);
877
        if (l > size)
878
            l = size;
879
        cpu_memory_rw_debug(env, addr, buf, l, 0);
880
        fwrite(buf, 1, l, f);
881
        addr += l;
882
        size -= l;
883
    }
884
    fclose(f);
885
}
886

    
887
static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
888
{
889
    FILE *f;
890
    uint32_t l;
891
    uint8_t buf[1024];
892
    uint32_t size = qdict_get_int(qdict, "size");
893
    const char *filename = qdict_get_str(qdict, "filename");
894
    target_phys_addr_t addr = qdict_get_int(qdict, "val");
895

    
896
    f = fopen(filename, "wb");
897
    if (!f) {
898
        monitor_printf(mon, "could not open '%s'\n", filename);
899
        return;
900
    }
901
    while (size != 0) {
902
        l = sizeof(buf);
903
        if (l > size)
904
            l = size;
905
        cpu_physical_memory_rw(addr, buf, l, 0);
906
        fwrite(buf, 1, l, f);
907
        fflush(f);
908
        addr += l;
909
        size -= l;
910
    }
911
    fclose(f);
912
}
913

    
914
static void do_sum(Monitor *mon, const QDict *qdict)
915
{
916
    uint32_t addr;
917
    uint8_t buf[1];
918
    uint16_t sum;
919
    uint32_t start = qdict_get_int(qdict, "start");
920
    uint32_t size = qdict_get_int(qdict, "size");
921

    
922
    sum = 0;
923
    for(addr = start; addr < (start + size); addr++) {
924
        cpu_physical_memory_rw(addr, buf, 1, 0);
925
        /* BSD sum algorithm ('sum' Unix command) */
926
        sum = (sum >> 1) | (sum << 15);
927
        sum += buf[0];
928
    }
929
    monitor_printf(mon, "%05d\n", sum);
930
}
931

    
932
typedef struct {
933
    int keycode;
934
    const char *name;
935
} KeyDef;
936

    
937
static const KeyDef key_defs[] = {
938
    { 0x2a, "shift" },
939
    { 0x36, "shift_r" },
940

    
941
    { 0x38, "alt" },
942
    { 0xb8, "alt_r" },
943
    { 0x64, "altgr" },
944
    { 0xe4, "altgr_r" },
945
    { 0x1d, "ctrl" },
946
    { 0x9d, "ctrl_r" },
947

    
948
    { 0xdd, "menu" },
949

    
950
    { 0x01, "esc" },
951

    
952
    { 0x02, "1" },
953
    { 0x03, "2" },
954
    { 0x04, "3" },
955
    { 0x05, "4" },
956
    { 0x06, "5" },
957
    { 0x07, "6" },
958
    { 0x08, "7" },
959
    { 0x09, "8" },
960
    { 0x0a, "9" },
961
    { 0x0b, "0" },
962
    { 0x0c, "minus" },
963
    { 0x0d, "equal" },
964
    { 0x0e, "backspace" },
965

    
966
    { 0x0f, "tab" },
967
    { 0x10, "q" },
968
    { 0x11, "w" },
969
    { 0x12, "e" },
970
    { 0x13, "r" },
971
    { 0x14, "t" },
972
    { 0x15, "y" },
973
    { 0x16, "u" },
974
    { 0x17, "i" },
975
    { 0x18, "o" },
976
    { 0x19, "p" },
977

    
978
    { 0x1c, "ret" },
979

    
980
    { 0x1e, "a" },
981
    { 0x1f, "s" },
982
    { 0x20, "d" },
983
    { 0x21, "f" },
984
    { 0x22, "g" },
985
    { 0x23, "h" },
986
    { 0x24, "j" },
987
    { 0x25, "k" },
988
    { 0x26, "l" },
989

    
990
    { 0x2c, "z" },
991
    { 0x2d, "x" },
992
    { 0x2e, "c" },
993
    { 0x2f, "v" },
994
    { 0x30, "b" },
995
    { 0x31, "n" },
996
    { 0x32, "m" },
997
    { 0x33, "comma" },
998
    { 0x34, "dot" },
999
    { 0x35, "slash" },
1000

    
1001
    { 0x37, "asterisk" },
1002

    
1003
    { 0x39, "spc" },
1004
    { 0x3a, "caps_lock" },
1005
    { 0x3b, "f1" },
1006
    { 0x3c, "f2" },
1007
    { 0x3d, "f3" },
1008
    { 0x3e, "f4" },
1009
    { 0x3f, "f5" },
1010
    { 0x40, "f6" },
1011
    { 0x41, "f7" },
1012
    { 0x42, "f8" },
1013
    { 0x43, "f9" },
1014
    { 0x44, "f10" },
1015
    { 0x45, "num_lock" },
1016
    { 0x46, "scroll_lock" },
1017

    
1018
    { 0xb5, "kp_divide" },
1019
    { 0x37, "kp_multiply" },
1020
    { 0x4a, "kp_subtract" },
1021
    { 0x4e, "kp_add" },
1022
    { 0x9c, "kp_enter" },
1023
    { 0x53, "kp_decimal" },
1024
    { 0x54, "sysrq" },
1025

    
1026
    { 0x52, "kp_0" },
1027
    { 0x4f, "kp_1" },
1028
    { 0x50, "kp_2" },
1029
    { 0x51, "kp_3" },
1030
    { 0x4b, "kp_4" },
1031
    { 0x4c, "kp_5" },
1032
    { 0x4d, "kp_6" },
1033
    { 0x47, "kp_7" },
1034
    { 0x48, "kp_8" },
1035
    { 0x49, "kp_9" },
1036

    
1037
    { 0x56, "<" },
1038

    
1039
    { 0x57, "f11" },
1040
    { 0x58, "f12" },
1041

    
1042
    { 0xb7, "print" },
1043

    
1044
    { 0xc7, "home" },
1045
    { 0xc9, "pgup" },
1046
    { 0xd1, "pgdn" },
1047
    { 0xcf, "end" },
1048

    
1049
    { 0xcb, "left" },
1050
    { 0xc8, "up" },
1051
    { 0xd0, "down" },
1052
    { 0xcd, "right" },
1053

    
1054
    { 0xd2, "insert" },
1055
    { 0xd3, "delete" },
1056
#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1057
    { 0xf0, "stop" },
1058
    { 0xf1, "again" },
1059
    { 0xf2, "props" },
1060
    { 0xf3, "undo" },
1061
    { 0xf4, "front" },
1062
    { 0xf5, "copy" },
1063
    { 0xf6, "open" },
1064
    { 0xf7, "paste" },
1065
    { 0xf8, "find" },
1066
    { 0xf9, "cut" },
1067
    { 0xfa, "lf" },
1068
    { 0xfb, "help" },
1069
    { 0xfc, "meta_l" },
1070
    { 0xfd, "meta_r" },
1071
    { 0xfe, "compose" },
1072
#endif
1073
    { 0, NULL },
1074
};
1075

    
1076
static int get_keycode(const char *key)
1077
{
1078
    const KeyDef *p;
1079
    char *endp;
1080
    int ret;
1081

    
1082
    for(p = key_defs; p->name != NULL; p++) {
1083
        if (!strcmp(key, p->name))
1084
            return p->keycode;
1085
    }
1086
    if (strstart(key, "0x", NULL)) {
1087
        ret = strtoul(key, &endp, 0);
1088
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1089
            return ret;
1090
    }
1091
    return -1;
1092
}
1093

    
1094
#define MAX_KEYCODES 16
1095
static uint8_t keycodes[MAX_KEYCODES];
1096
static int nb_pending_keycodes;
1097
static QEMUTimer *key_timer;
1098

    
1099
static void release_keys(void *opaque)
1100
{
1101
    int keycode;
1102

    
1103
    while (nb_pending_keycodes > 0) {
1104
        nb_pending_keycodes--;
1105
        keycode = keycodes[nb_pending_keycodes];
1106
        if (keycode & 0x80)
1107
            kbd_put_keycode(0xe0);
1108
        kbd_put_keycode(keycode | 0x80);
1109
    }
1110
}
1111

    
1112
static void do_sendkey(Monitor *mon, const QDict *qdict)
1113
{
1114
    char keyname_buf[16];
1115
    char *separator;
1116
    int keyname_len, keycode, i;
1117
    const char *string = qdict_get_str(qdict, "string");
1118
    int has_hold_time = qdict_haskey(qdict, "hold_time");
1119
    int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1120

    
1121
    if (nb_pending_keycodes > 0) {
1122
        qemu_del_timer(key_timer);
1123
        release_keys(NULL);
1124
    }
1125
    if (!has_hold_time)
1126
        hold_time = 100;
1127
    i = 0;
1128
    while (1) {
1129
        separator = strchr(string, '-');
1130
        keyname_len = separator ? separator - string : strlen(string);
1131
        if (keyname_len > 0) {
1132
            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1133
            if (keyname_len > sizeof(keyname_buf) - 1) {
1134
                monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1135
                return;
1136
            }
1137
            if (i == MAX_KEYCODES) {
1138
                monitor_printf(mon, "too many keys\n");
1139
                return;
1140
            }
1141
            keyname_buf[keyname_len] = 0;
1142
            keycode = get_keycode(keyname_buf);
1143
            if (keycode < 0) {
1144
                monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1145
                return;
1146
            }
1147
            keycodes[i++] = keycode;
1148
        }
1149
        if (!separator)
1150
            break;
1151
        string = separator + 1;
1152
    }
1153
    nb_pending_keycodes = i;
1154
    /* key down events */
1155
    for (i = 0; i < nb_pending_keycodes; i++) {
1156
        keycode = keycodes[i];
1157
        if (keycode & 0x80)
1158
            kbd_put_keycode(0xe0);
1159
        kbd_put_keycode(keycode & 0x7f);
1160
    }
1161
    /* delayed key up events */
1162
    qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1163
                    muldiv64(ticks_per_sec, hold_time, 1000));
1164
}
1165

    
1166
static int mouse_button_state;
1167

    
1168
static void do_mouse_move(Monitor *mon, const QDict *qdict)
1169
{
1170
    int dx, dy, dz;
1171
    const char *dx_str = qdict_get_str(qdict, "dx_str");
1172
    const char *dy_str = qdict_get_str(qdict, "dy_str");
1173
    const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1174
    dx = strtol(dx_str, NULL, 0);
1175
    dy = strtol(dy_str, NULL, 0);
1176
    dz = 0;
1177
    if (dz_str)
1178
        dz = strtol(dz_str, NULL, 0);
1179
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
1180
}
1181

    
1182
static void do_mouse_button(Monitor *mon, const QDict *qdict)
1183
{
1184
    int button_state = qdict_get_int(qdict, "button_state");
1185
    mouse_button_state = button_state;
1186
    kbd_mouse_event(0, 0, 0, mouse_button_state);
1187
}
1188

    
1189
static void do_ioport_read(Monitor *mon, const QDict *qdict)
1190
{
1191
    int size = qdict_get_int(qdict, "size");
1192
    int addr = qdict_get_int(qdict, "addr");
1193
    int has_index = qdict_haskey(qdict, "index");
1194
    uint32_t val;
1195
    int suffix;
1196

    
1197
    if (has_index) {
1198
        int index = qdict_get_int(qdict, "index");
1199
        cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
1200
        addr++;
1201
    }
1202
    addr &= 0xffff;
1203

    
1204
    switch(size) {
1205
    default:
1206
    case 1:
1207
        val = cpu_inb(NULL, addr);
1208
        suffix = 'b';
1209
        break;
1210
    case 2:
1211
        val = cpu_inw(NULL, addr);
1212
        suffix = 'w';
1213
        break;
1214
    case 4:
1215
        val = cpu_inl(NULL, addr);
1216
        suffix = 'l';
1217
        break;
1218
    }
1219
    monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1220
                   suffix, addr, size * 2, val);
1221
}
1222

    
1223
static void do_ioport_write(Monitor *mon, const QDict *qdict)
1224
{
1225
    int size = qdict_get_int(qdict, "size");
1226
    int addr = qdict_get_int(qdict, "addr");
1227
    int val = qdict_get_int(qdict, "val");
1228

    
1229
    addr &= IOPORTS_MASK;
1230

    
1231
    switch (size) {
1232
    default:
1233
    case 1:
1234
        cpu_outb(NULL, addr, val);
1235
        break;
1236
    case 2:
1237
        cpu_outw(NULL, addr, val);
1238
        break;
1239
    case 4:
1240
        cpu_outl(NULL, addr, val);
1241
        break;
1242
    }
1243
}
1244

    
1245
static void do_boot_set(Monitor *mon, const QDict *qdict)
1246
{
1247
    int res;
1248
    const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1249

    
1250
    res = qemu_boot_set(bootdevice);
1251
    if (res == 0) {
1252
        monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1253
    } else if (res > 0) {
1254
        monitor_printf(mon, "setting boot device list failed\n");
1255
    } else {
1256
        monitor_printf(mon, "no function defined to set boot device list for "
1257
                       "this architecture\n");
1258
    }
1259
}
1260

    
1261
static void do_system_reset(Monitor *mon, const QDict *qdict)
1262
{
1263
    qemu_system_reset_request();
1264
}
1265

    
1266
static void do_system_powerdown(Monitor *mon, const QDict *qdict)
1267
{
1268
    qemu_system_powerdown_request();
1269
}
1270

    
1271
#if defined(TARGET_I386)
1272
static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1273
{
1274
    monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1275
                   addr,
1276
                   pte & mask,
1277
                   pte & PG_GLOBAL_MASK ? 'G' : '-',
1278
                   pte & PG_PSE_MASK ? 'P' : '-',
1279
                   pte & PG_DIRTY_MASK ? 'D' : '-',
1280
                   pte & PG_ACCESSED_MASK ? 'A' : '-',
1281
                   pte & PG_PCD_MASK ? 'C' : '-',
1282
                   pte & PG_PWT_MASK ? 'T' : '-',
1283
                   pte & PG_USER_MASK ? 'U' : '-',
1284
                   pte & PG_RW_MASK ? 'W' : '-');
1285
}
1286

    
1287
static void tlb_info(Monitor *mon)
1288
{
1289
    CPUState *env;
1290
    int l1, l2;
1291
    uint32_t pgd, pde, pte;
1292

    
1293
    env = mon_get_cpu();
1294
    if (!env)
1295
        return;
1296

    
1297
    if (!(env->cr[0] & CR0_PG_MASK)) {
1298
        monitor_printf(mon, "PG disabled\n");
1299
        return;
1300
    }
1301
    pgd = env->cr[3] & ~0xfff;
1302
    for(l1 = 0; l1 < 1024; l1++) {
1303
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1304
        pde = le32_to_cpu(pde);
1305
        if (pde & PG_PRESENT_MASK) {
1306
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1307
                print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1308
            } else {
1309
                for(l2 = 0; l2 < 1024; l2++) {
1310
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1311
                                             (uint8_t *)&pte, 4);
1312
                    pte = le32_to_cpu(pte);
1313
                    if (pte & PG_PRESENT_MASK) {
1314
                        print_pte(mon, (l1 << 22) + (l2 << 12),
1315
                                  pte & ~PG_PSE_MASK,
1316
                                  ~0xfff);
1317
                    }
1318
                }
1319
            }
1320
        }
1321
    }
1322
}
1323

    
1324
static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1325
                      uint32_t end, int prot)
1326
{
1327
    int prot1;
1328
    prot1 = *plast_prot;
1329
    if (prot != prot1) {
1330
        if (*pstart != -1) {
1331
            monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1332
                           *pstart, end, end - *pstart,
1333
                           prot1 & PG_USER_MASK ? 'u' : '-',
1334
                           'r',
1335
                           prot1 & PG_RW_MASK ? 'w' : '-');
1336
        }
1337
        if (prot != 0)
1338
            *pstart = end;
1339
        else
1340
            *pstart = -1;
1341
        *plast_prot = prot;
1342
    }
1343
}
1344

    
1345
static void mem_info(Monitor *mon)
1346
{
1347
    CPUState *env;
1348
    int l1, l2, prot, last_prot;
1349
    uint32_t pgd, pde, pte, start, end;
1350

    
1351
    env = mon_get_cpu();
1352
    if (!env)
1353
        return;
1354

    
1355
    if (!(env->cr[0] & CR0_PG_MASK)) {
1356
        monitor_printf(mon, "PG disabled\n");
1357
        return;
1358
    }
1359
    pgd = env->cr[3] & ~0xfff;
1360
    last_prot = 0;
1361
    start = -1;
1362
    for(l1 = 0; l1 < 1024; l1++) {
1363
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1364
        pde = le32_to_cpu(pde);
1365
        end = l1 << 22;
1366
        if (pde & PG_PRESENT_MASK) {
1367
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1368
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1369
                mem_print(mon, &start, &last_prot, end, prot);
1370
            } else {
1371
                for(l2 = 0; l2 < 1024; l2++) {
1372
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1373
                                             (uint8_t *)&pte, 4);
1374
                    pte = le32_to_cpu(pte);
1375
                    end = (l1 << 22) + (l2 << 12);
1376
                    if (pte & PG_PRESENT_MASK) {
1377
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1378
                    } else {
1379
                        prot = 0;
1380
                    }
1381
                    mem_print(mon, &start, &last_prot, end, prot);
1382
                }
1383
            }
1384
        } else {
1385
            prot = 0;
1386
            mem_print(mon, &start, &last_prot, end, prot);
1387
        }
1388
    }
1389
}
1390
#endif
1391

    
1392
#if defined(TARGET_SH4)
1393

    
1394
static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1395
{
1396
    monitor_printf(mon, " tlb%i:\t"
1397
                   "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1398
                   "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1399
                   "dirty=%hhu writethrough=%hhu\n",
1400
                   idx,
1401
                   tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1402
                   tlb->v, tlb->sh, tlb->c, tlb->pr,
1403
                   tlb->d, tlb->wt);
1404
}
1405

    
1406
static void tlb_info(Monitor *mon)
1407
{
1408
    CPUState *env = mon_get_cpu();
1409
    int i;
1410

    
1411
    monitor_printf (mon, "ITLB:\n");
1412
    for (i = 0 ; i < ITLB_SIZE ; i++)
1413
        print_tlb (mon, i, &env->itlb[i]);
1414
    monitor_printf (mon, "UTLB:\n");
1415
    for (i = 0 ; i < UTLB_SIZE ; i++)
1416
        print_tlb (mon, i, &env->utlb[i]);
1417
}
1418

    
1419
#endif
1420

    
1421
static void do_info_kvm(Monitor *mon)
1422
{
1423
#ifdef CONFIG_KVM
1424
    monitor_printf(mon, "kvm support: ");
1425
    if (kvm_enabled())
1426
        monitor_printf(mon, "enabled\n");
1427
    else
1428
        monitor_printf(mon, "disabled\n");
1429
#else
1430
    monitor_printf(mon, "kvm support: not compiled\n");
1431
#endif
1432
}
1433

    
1434
static void do_info_numa(Monitor *mon)
1435
{
1436
    int i;
1437
    CPUState *env;
1438

    
1439
    monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1440
    for (i = 0; i < nb_numa_nodes; i++) {
1441
        monitor_printf(mon, "node %d cpus:", i);
1442
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1443
            if (env->numa_node == i) {
1444
                monitor_printf(mon, " %d", env->cpu_index);
1445
            }
1446
        }
1447
        monitor_printf(mon, "\n");
1448
        monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1449
            node_mem[i] >> 20);
1450
    }
1451
}
1452

    
1453
#ifdef CONFIG_PROFILER
1454

    
1455
static void do_info_profile(Monitor *mon)
1456
{
1457
    int64_t total;
1458
    total = qemu_time;
1459
    if (total == 0)
1460
        total = 1;
1461
    monitor_printf(mon, "async time  %" PRId64 " (%0.3f)\n",
1462
                   dev_time, dev_time / (double)ticks_per_sec);
1463
    monitor_printf(mon, "qemu time   %" PRId64 " (%0.3f)\n",
1464
                   qemu_time, qemu_time / (double)ticks_per_sec);
1465
    qemu_time = 0;
1466
    dev_time = 0;
1467
}
1468
#else
1469
static void do_info_profile(Monitor *mon)
1470
{
1471
    monitor_printf(mon, "Internal profiler not compiled\n");
1472
}
1473
#endif
1474

    
1475
/* Capture support */
1476
static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1477

    
1478
static void do_info_capture(Monitor *mon)
1479
{
1480
    int i;
1481
    CaptureState *s;
1482

    
1483
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1484
        monitor_printf(mon, "[%d]: ", i);
1485
        s->ops.info (s->opaque);
1486
    }
1487
}
1488

    
1489
#ifdef HAS_AUDIO
1490
static void do_stop_capture(Monitor *mon, const QDict *qdict)
1491
{
1492
    int i;
1493
    int n = qdict_get_int(qdict, "n");
1494
    CaptureState *s;
1495

    
1496
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1497
        if (i == n) {
1498
            s->ops.destroy (s->opaque);
1499
            LIST_REMOVE (s, entries);
1500
            qemu_free (s);
1501
            return;
1502
        }
1503
    }
1504
}
1505

    
1506
static void do_wav_capture(Monitor *mon, const QDict *qdict)
1507
{
1508
    const char *path = qdict_get_str(qdict, "path");
1509
    int has_freq = qdict_haskey(qdict, "freq");
1510
    int freq = qdict_get_try_int(qdict, "freq", -1);
1511
    int has_bits = qdict_haskey(qdict, "bits");
1512
    int bits = qdict_get_try_int(qdict, "bits", -1);
1513
    int has_channels = qdict_haskey(qdict, "nchannels");
1514
    int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1515
    CaptureState *s;
1516

    
1517
    s = qemu_mallocz (sizeof (*s));
1518

    
1519
    freq = has_freq ? freq : 44100;
1520
    bits = has_bits ? bits : 16;
1521
    nchannels = has_channels ? nchannels : 2;
1522

    
1523
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1524
        monitor_printf(mon, "Faied to add wave capture\n");
1525
        qemu_free (s);
1526
    }
1527
    LIST_INSERT_HEAD (&capture_head, s, entries);
1528
}
1529
#endif
1530

    
1531
#if defined(TARGET_I386)
1532
static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1533
{
1534
    CPUState *env;
1535
    int cpu_index = qdict_get_int(qdict, "cpu_index");
1536

    
1537
    for (env = first_cpu; env != NULL; env = env->next_cpu)
1538
        if (env->cpu_index == cpu_index) {
1539
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1540
            break;
1541
        }
1542
}
1543
#endif
1544

    
1545
static void do_info_status(Monitor *mon)
1546
{
1547
    if (vm_running) {
1548
        if (singlestep) {
1549
            monitor_printf(mon, "VM status: running (single step mode)\n");
1550
        } else {
1551
            monitor_printf(mon, "VM status: running\n");
1552
        }
1553
    } else
1554
       monitor_printf(mon, "VM status: paused\n");
1555
}
1556

    
1557

    
1558
static void do_balloon(Monitor *mon, const QDict *qdict)
1559
{
1560
    int value = qdict_get_int(qdict, "value");
1561
    ram_addr_t target = value;
1562
    qemu_balloon(target << 20);
1563
}
1564

    
1565
static void do_info_balloon(Monitor *mon)
1566
{
1567
    ram_addr_t actual;
1568

    
1569
    actual = qemu_balloon_status();
1570
    if (kvm_enabled() && !kvm_has_sync_mmu())
1571
        monitor_printf(mon, "Using KVM without synchronous MMU, "
1572
                       "ballooning disabled\n");
1573
    else if (actual == 0)
1574
        monitor_printf(mon, "Ballooning not activated in VM\n");
1575
    else
1576
        monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1577
}
1578

    
1579
static qemu_acl *find_acl(Monitor *mon, const char *name)
1580
{
1581
    qemu_acl *acl = qemu_acl_find(name);
1582

    
1583
    if (!acl) {
1584
        monitor_printf(mon, "acl: unknown list '%s'\n", name);
1585
    }
1586
    return acl;
1587
}
1588

    
1589
static void do_acl_show(Monitor *mon, const QDict *qdict)
1590
{
1591
    const char *aclname = qdict_get_str(qdict, "aclname");
1592
    qemu_acl *acl = find_acl(mon, aclname);
1593
    qemu_acl_entry *entry;
1594
    int i = 0;
1595

    
1596
    if (acl) {
1597
        monitor_printf(mon, "policy: %s\n",
1598
                       acl->defaultDeny ? "deny" : "allow");
1599
        TAILQ_FOREACH(entry, &acl->entries, next) {
1600
            i++;
1601
            monitor_printf(mon, "%d: %s %s\n", i,
1602
                           entry->deny ? "deny" : "allow", entry->match);
1603
        }
1604
    }
1605
}
1606

    
1607
static void do_acl_reset(Monitor *mon, const QDict *qdict)
1608
{
1609
    const char *aclname = qdict_get_str(qdict, "aclname");
1610
    qemu_acl *acl = find_acl(mon, aclname);
1611

    
1612
    if (acl) {
1613
        qemu_acl_reset(acl);
1614
        monitor_printf(mon, "acl: removed all rules\n");
1615
    }
1616
}
1617

    
1618
static void do_acl_policy(Monitor *mon, const QDict *qdict)
1619
{
1620
    const char *aclname = qdict_get_str(qdict, "aclname");
1621
    const char *policy = qdict_get_str(qdict, "policy");
1622
    qemu_acl *acl = find_acl(mon, aclname);
1623

    
1624
    if (acl) {
1625
        if (strcmp(policy, "allow") == 0) {
1626
            acl->defaultDeny = 0;
1627
            monitor_printf(mon, "acl: policy set to 'allow'\n");
1628
        } else if (strcmp(policy, "deny") == 0) {
1629
            acl->defaultDeny = 1;
1630
            monitor_printf(mon, "acl: policy set to 'deny'\n");
1631
        } else {
1632
            monitor_printf(mon, "acl: unknown policy '%s', "
1633
                           "expected 'deny' or 'allow'\n", policy);
1634
        }
1635
    }
1636
}
1637

    
1638
static void do_acl_add(Monitor *mon, const QDict *qdict)
1639
{
1640
    const char *aclname = qdict_get_str(qdict, "aclname");
1641
    const char *match = qdict_get_str(qdict, "match");
1642
    const char *policy = qdict_get_str(qdict, "policy");
1643
    int has_index = qdict_haskey(qdict, "index");
1644
    int index = qdict_get_try_int(qdict, "index", -1);
1645
    qemu_acl *acl = find_acl(mon, aclname);
1646
    int deny, ret;
1647

    
1648
    if (acl) {
1649
        if (strcmp(policy, "allow") == 0) {
1650
            deny = 0;
1651
        } else if (strcmp(policy, "deny") == 0) {
1652
            deny = 1;
1653
        } else {
1654
            monitor_printf(mon, "acl: unknown policy '%s', "
1655
                           "expected 'deny' or 'allow'\n", policy);
1656
            return;
1657
        }
1658
        if (has_index)
1659
            ret = qemu_acl_insert(acl, deny, match, index);
1660
        else
1661
            ret = qemu_acl_append(acl, deny, match);
1662
        if (ret < 0)
1663
            monitor_printf(mon, "acl: unable to add acl entry\n");
1664
        else
1665
            monitor_printf(mon, "acl: added rule at position %d\n", ret);
1666
    }
1667
}
1668

    
1669
static void do_acl_remove(Monitor *mon, const QDict *qdict)
1670
{
1671
    const char *aclname = qdict_get_str(qdict, "aclname");
1672
    const char *match = qdict_get_str(qdict, "match");
1673
    qemu_acl *acl = find_acl(mon, aclname);
1674
    int ret;
1675

    
1676
    if (acl) {
1677
        ret = qemu_acl_remove(acl, match);
1678
        if (ret < 0)
1679
            monitor_printf(mon, "acl: no matching acl entry\n");
1680
        else
1681
            monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1682
    }
1683
}
1684

    
1685
#if defined(TARGET_I386)
1686
static void do_inject_mce(Monitor *mon, const QDict *qdict)
1687
{
1688
    CPUState *cenv;
1689
    int cpu_index = qdict_get_int(qdict, "cpu_index");
1690
    int bank = qdict_get_int(qdict, "bank");
1691
    uint64_t status = qdict_get_int(qdict, "status");
1692
    uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1693
    uint64_t addr = qdict_get_int(qdict, "addr");
1694
    uint64_t misc = qdict_get_int(qdict, "misc");
1695

    
1696
    for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1697
        if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1698
            cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1699
            break;
1700
        }
1701
}
1702
#endif
1703

    
1704
static void do_getfd(Monitor *mon, const QDict *qdict)
1705
{
1706
    const char *fdname = qdict_get_str(qdict, "fdname");
1707
    mon_fd_t *monfd;
1708
    int fd;
1709

    
1710
    fd = qemu_chr_get_msgfd(mon->chr);
1711
    if (fd == -1) {
1712
        monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1713
        return;
1714
    }
1715

    
1716
    if (qemu_isdigit(fdname[0])) {
1717
        monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1718
        return;
1719
    }
1720

    
1721
    fd = dup(fd);
1722
    if (fd == -1) {
1723
        monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1724
                       strerror(errno));
1725
        return;
1726
    }
1727

    
1728
    LIST_FOREACH(monfd, &mon->fds, next) {
1729
        if (strcmp(monfd->name, fdname) != 0) {
1730
            continue;
1731
        }
1732

    
1733
        close(monfd->fd);
1734
        monfd->fd = fd;
1735
        return;
1736
    }
1737

    
1738
    monfd = qemu_mallocz(sizeof(mon_fd_t));
1739
    monfd->name = qemu_strdup(fdname);
1740
    monfd->fd = fd;
1741

    
1742
    LIST_INSERT_HEAD(&mon->fds, monfd, next);
1743
}
1744

    
1745
static void do_closefd(Monitor *mon, const QDict *qdict)
1746
{
1747
    const char *fdname = qdict_get_str(qdict, "fdname");
1748
    mon_fd_t *monfd;
1749

    
1750
    LIST_FOREACH(monfd, &mon->fds, next) {
1751
        if (strcmp(monfd->name, fdname) != 0) {
1752
            continue;
1753
        }
1754

    
1755
        LIST_REMOVE(monfd, next);
1756
        close(monfd->fd);
1757
        qemu_free(monfd->name);
1758
        qemu_free(monfd);
1759
        return;
1760
    }
1761

    
1762
    monitor_printf(mon, "Failed to find file descriptor named %s\n",
1763
                   fdname);
1764
}
1765

    
1766
static void do_loadvm(Monitor *mon, const QDict *qdict)
1767
{
1768
    int saved_vm_running  = vm_running;
1769
    const char *name = qdict_get_str(qdict, "name");
1770

    
1771
    vm_stop(0);
1772

    
1773
    if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1774
        vm_start();
1775
}
1776

    
1777
int monitor_get_fd(Monitor *mon, const char *fdname)
1778
{
1779
    mon_fd_t *monfd;
1780

    
1781
    LIST_FOREACH(monfd, &mon->fds, next) {
1782
        int fd;
1783

    
1784
        if (strcmp(monfd->name, fdname) != 0) {
1785
            continue;
1786
        }
1787

    
1788
        fd = monfd->fd;
1789

    
1790
        /* caller takes ownership of fd */
1791
        LIST_REMOVE(monfd, next);
1792
        qemu_free(monfd->name);
1793
        qemu_free(monfd);
1794

    
1795
        return fd;
1796
    }
1797

    
1798
    return -1;
1799
}
1800

    
1801
static const mon_cmd_t mon_cmds[] = {
1802
#include "qemu-monitor.h"
1803
    { NULL, NULL, },
1804
};
1805

    
1806
/* Please update qemu-monitor.hx when adding or changing commands */
1807
static const mon_cmd_t info_cmds[] = {
1808
    { "version", "", do_info_version,
1809
      "", "show the version of QEMU" },
1810
    { "network", "", do_info_network,
1811
      "", "show the network state" },
1812
    { "chardev", "", qemu_chr_info,
1813
      "", "show the character devices" },
1814
    { "block", "", bdrv_info,
1815
      "", "show the block devices" },
1816
    { "blockstats", "", bdrv_info_stats,
1817
      "", "show block device statistics" },
1818
    { "registers", "", do_info_registers,
1819
      "", "show the cpu registers" },
1820
    { "cpus", "", do_info_cpus,
1821
      "", "show infos for each CPU" },
1822
    { "history", "", do_info_history,
1823
      "", "show the command line history", },
1824
    { "irq", "", irq_info,
1825
      "", "show the interrupts statistics (if available)", },
1826
    { "pic", "", pic_info,
1827
      "", "show i8259 (PIC) state", },
1828
    { "pci", "", pci_info,
1829
      "", "show PCI info", },
1830
#if defined(TARGET_I386) || defined(TARGET_SH4)
1831
    { "tlb", "", tlb_info,
1832
      "", "show virtual to physical memory mappings", },
1833
#endif
1834
#if defined(TARGET_I386)
1835
    { "mem", "", mem_info,
1836
      "", "show the active virtual memory mappings", },
1837
    { "hpet", "", do_info_hpet,
1838
      "", "show state of HPET", },
1839
#endif
1840
    { "jit", "", do_info_jit,
1841
      "", "show dynamic compiler info", },
1842
    { "kvm", "", do_info_kvm,
1843
      "", "show KVM information", },
1844
    { "numa", "", do_info_numa,
1845
      "", "show NUMA information", },
1846
    { "usb", "", usb_info,
1847
      "", "show guest USB devices", },
1848
    { "usbhost", "", usb_host_info,
1849
      "", "show host USB devices", },
1850
    { "profile", "", do_info_profile,
1851
      "", "show profiling information", },
1852
    { "capture", "", do_info_capture,
1853
      "", "show capture information" },
1854
    { "snapshots", "", do_info_snapshots,
1855
      "", "show the currently saved VM snapshots" },
1856
    { "status", "", do_info_status,
1857
      "", "show the current VM status (running|paused)" },
1858
    { "pcmcia", "", pcmcia_info,
1859
      "", "show guest PCMCIA status" },
1860
    { "mice", "", do_info_mice,
1861
      "", "show which guest mouse is receiving events" },
1862
    { "vnc", "", do_info_vnc,
1863
      "", "show the vnc server status"},
1864
    { "name", "", do_info_name,
1865
      "", "show the current VM name" },
1866
    { "uuid", "", do_info_uuid,
1867
      "", "show the current VM UUID" },
1868
#if defined(TARGET_PPC)
1869
    { "cpustats", "", do_info_cpu_stats,
1870
      "", "show CPU statistics", },
1871
#endif
1872
#if defined(CONFIG_SLIRP)
1873
    { "usernet", "", do_info_usernet,
1874
      "", "show user network stack connection states", },
1875
#endif
1876
    { "migrate", "", do_info_migrate, "", "show migration status" },
1877
    { "balloon", "", do_info_balloon,
1878
      "", "show balloon information" },
1879
    { "qtree", "", do_info_qtree,
1880
      "", "show device tree" },
1881
    { "qdm", "", do_info_qdm,
1882
      "", "show qdev device model list" },
1883
    { NULL, NULL, },
1884
};
1885

    
1886
/*******************************************************************/
1887

    
1888
static const char *pch;
1889
static jmp_buf expr_env;
1890

    
1891
#define MD_TLONG 0
1892
#define MD_I32   1
1893

    
1894
typedef struct MonitorDef {
1895
    const char *name;
1896
    int offset;
1897
    target_long (*get_value)(const struct MonitorDef *md, int val);
1898
    int type;
1899
} MonitorDef;
1900

    
1901
#if defined(TARGET_I386)
1902
static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1903
{
1904
    CPUState *env = mon_get_cpu();
1905
    if (!env)
1906
        return 0;
1907
    return env->eip + env->segs[R_CS].base;
1908
}
1909
#endif
1910

    
1911
#if defined(TARGET_PPC)
1912
static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1913
{
1914
    CPUState *env = mon_get_cpu();
1915
    unsigned int u;
1916
    int i;
1917

    
1918
    if (!env)
1919
        return 0;
1920

    
1921
    u = 0;
1922
    for (i = 0; i < 8; i++)
1923
        u |= env->crf[i] << (32 - (4 * i));
1924

    
1925
    return u;
1926
}
1927

    
1928
static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1929
{
1930
    CPUState *env = mon_get_cpu();
1931
    if (!env)
1932
        return 0;
1933
    return env->msr;
1934
}
1935

    
1936
static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1937
{
1938
    CPUState *env = mon_get_cpu();
1939
    if (!env)
1940
        return 0;
1941
    return env->xer;
1942
}
1943

    
1944
static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1945
{
1946
    CPUState *env = mon_get_cpu();
1947
    if (!env)
1948
        return 0;
1949
    return cpu_ppc_load_decr(env);
1950
}
1951

    
1952
static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1953
{
1954
    CPUState *env = mon_get_cpu();
1955
    if (!env)
1956
        return 0;
1957
    return cpu_ppc_load_tbu(env);
1958
}
1959

    
1960
static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1961
{
1962
    CPUState *env = mon_get_cpu();
1963
    if (!env)
1964
        return 0;
1965
    return cpu_ppc_load_tbl(env);
1966
}
1967
#endif
1968

    
1969
#if defined(TARGET_SPARC)
1970
#ifndef TARGET_SPARC64
1971
static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1972
{
1973
    CPUState *env = mon_get_cpu();
1974
    if (!env)
1975
        return 0;
1976
    return GET_PSR(env);
1977
}
1978
#endif
1979

    
1980
static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1981
{
1982
    CPUState *env = mon_get_cpu();
1983
    if (!env)
1984
        return 0;
1985
    return env->regwptr[val];
1986
}
1987
#endif
1988

    
1989
static const MonitorDef monitor_defs[] = {
1990
#ifdef TARGET_I386
1991

    
1992
#define SEG(name, seg) \
1993
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1994
    { name ".base", offsetof(CPUState, segs[seg].base) },\
1995
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1996

    
1997
    { "eax", offsetof(CPUState, regs[0]) },
1998
    { "ecx", offsetof(CPUState, regs[1]) },
1999
    { "edx", offsetof(CPUState, regs[2]) },
2000
    { "ebx", offsetof(CPUState, regs[3]) },
2001
    { "esp|sp", offsetof(CPUState, regs[4]) },
2002
    { "ebp|fp", offsetof(CPUState, regs[5]) },
2003
    { "esi", offsetof(CPUState, regs[6]) },
2004
    { "edi", offsetof(CPUState, regs[7]) },
2005
#ifdef TARGET_X86_64
2006
    { "r8", offsetof(CPUState, regs[8]) },
2007
    { "r9", offsetof(CPUState, regs[9]) },
2008
    { "r10", offsetof(CPUState, regs[10]) },
2009
    { "r11", offsetof(CPUState, regs[11]) },
2010
    { "r12", offsetof(CPUState, regs[12]) },
2011
    { "r13", offsetof(CPUState, regs[13]) },
2012
    { "r14", offsetof(CPUState, regs[14]) },
2013
    { "r15", offsetof(CPUState, regs[15]) },
2014
#endif
2015
    { "eflags", offsetof(CPUState, eflags) },
2016
    { "eip", offsetof(CPUState, eip) },
2017
    SEG("cs", R_CS)
2018
    SEG("ds", R_DS)
2019
    SEG("es", R_ES)
2020
    SEG("ss", R_SS)
2021
    SEG("fs", R_FS)
2022
    SEG("gs", R_GS)
2023
    { "pc", 0, monitor_get_pc, },
2024
#elif defined(TARGET_PPC)
2025
    /* General purpose registers */
2026
    { "r0", offsetof(CPUState, gpr[0]) },
2027
    { "r1", offsetof(CPUState, gpr[1]) },
2028
    { "r2", offsetof(CPUState, gpr[2]) },
2029
    { "r3", offsetof(CPUState, gpr[3]) },
2030
    { "r4", offsetof(CPUState, gpr[4]) },
2031
    { "r5", offsetof(CPUState, gpr[5]) },
2032
    { "r6", offsetof(CPUState, gpr[6]) },
2033
    { "r7", offsetof(CPUState, gpr[7]) },
2034
    { "r8", offsetof(CPUState, gpr[8]) },
2035
    { "r9", offsetof(CPUState, gpr[9]) },
2036
    { "r10", offsetof(CPUState, gpr[10]) },
2037
    { "r11", offsetof(CPUState, gpr[11]) },
2038
    { "r12", offsetof(CPUState, gpr[12]) },
2039
    { "r13", offsetof(CPUState, gpr[13]) },
2040
    { "r14", offsetof(CPUState, gpr[14]) },
2041
    { "r15", offsetof(CPUState, gpr[15]) },
2042
    { "r16", offsetof(CPUState, gpr[16]) },
2043
    { "r17", offsetof(CPUState, gpr[17]) },
2044
    { "r18", offsetof(CPUState, gpr[18]) },
2045
    { "r19", offsetof(CPUState, gpr[19]) },
2046
    { "r20", offsetof(CPUState, gpr[20]) },
2047
    { "r21", offsetof(CPUState, gpr[21]) },
2048
    { "r22", offsetof(CPUState, gpr[22]) },
2049
    { "r23", offsetof(CPUState, gpr[23]) },
2050
    { "r24", offsetof(CPUState, gpr[24]) },
2051
    { "r25", offsetof(CPUState, gpr[25]) },
2052
    { "r26", offsetof(CPUState, gpr[26]) },
2053
    { "r27", offsetof(CPUState, gpr[27]) },
2054
    { "r28", offsetof(CPUState, gpr[28]) },
2055
    { "r29", offsetof(CPUState, gpr[29]) },
2056
    { "r30", offsetof(CPUState, gpr[30]) },
2057
    { "r31", offsetof(CPUState, gpr[31]) },
2058
    /* Floating point registers */
2059
    { "f0", offsetof(CPUState, fpr[0]) },
2060
    { "f1", offsetof(CPUState, fpr[1]) },
2061
    { "f2", offsetof(CPUState, fpr[2]) },
2062
    { "f3", offsetof(CPUState, fpr[3]) },
2063
    { "f4", offsetof(CPUState, fpr[4]) },
2064
    { "f5", offsetof(CPUState, fpr[5]) },
2065
    { "f6", offsetof(CPUState, fpr[6]) },
2066
    { "f7", offsetof(CPUState, fpr[7]) },
2067
    { "f8", offsetof(CPUState, fpr[8]) },
2068
    { "f9", offsetof(CPUState, fpr[9]) },
2069
    { "f10", offsetof(CPUState, fpr[10]) },
2070
    { "f11", offsetof(CPUState, fpr[11]) },
2071
    { "f12", offsetof(CPUState, fpr[12]) },
2072
    { "f13", offsetof(CPUState, fpr[13]) },
2073
    { "f14", offsetof(CPUState, fpr[14]) },
2074
    { "f15", offsetof(CPUState, fpr[15]) },
2075
    { "f16", offsetof(CPUState, fpr[16]) },
2076
    { "f17", offsetof(CPUState, fpr[17]) },
2077
    { "f18", offsetof(CPUState, fpr[18]) },
2078
    { "f19", offsetof(CPUState, fpr[19]) },
2079
    { "f20", offsetof(CPUState, fpr[20]) },
2080
    { "f21", offsetof(CPUState, fpr[21]) },
2081
    { "f22", offsetof(CPUState, fpr[22]) },
2082
    { "f23", offsetof(CPUState, fpr[23]) },
2083
    { "f24", offsetof(CPUState, fpr[24]) },
2084
    { "f25", offsetof(CPUState, fpr[25]) },
2085
    { "f26", offsetof(CPUState, fpr[26]) },
2086
    { "f27", offsetof(CPUState, fpr[27]) },
2087
    { "f28", offsetof(CPUState, fpr[28]) },
2088
    { "f29", offsetof(CPUState, fpr[29]) },
2089
    { "f30", offsetof(CPUState, fpr[30]) },
2090
    { "f31", offsetof(CPUState, fpr[31]) },
2091
    { "fpscr", offsetof(CPUState, fpscr) },
2092
    /* Next instruction pointer */
2093
    { "nip|pc", offsetof(CPUState, nip) },
2094
    { "lr", offsetof(CPUState, lr) },
2095
    { "ctr", offsetof(CPUState, ctr) },
2096
    { "decr", 0, &monitor_get_decr, },
2097
    { "ccr", 0, &monitor_get_ccr, },
2098
    /* Machine state register */
2099
    { "msr", 0, &monitor_get_msr, },
2100
    { "xer", 0, &monitor_get_xer, },
2101
    { "tbu", 0, &monitor_get_tbu, },
2102
    { "tbl", 0, &monitor_get_tbl, },
2103
#if defined(TARGET_PPC64)
2104
    /* Address space register */
2105
    { "asr", offsetof(CPUState, asr) },
2106
#endif
2107
    /* Segment registers */
2108
    { "sdr1", offsetof(CPUState, sdr1) },
2109
    { "sr0", offsetof(CPUState, sr[0]) },
2110
    { "sr1", offsetof(CPUState, sr[1]) },
2111
    { "sr2", offsetof(CPUState, sr[2]) },
2112
    { "sr3", offsetof(CPUState, sr[3]) },
2113
    { "sr4", offsetof(CPUState, sr[4]) },
2114
    { "sr5", offsetof(CPUState, sr[5]) },
2115
    { "sr6", offsetof(CPUState, sr[6]) },
2116
    { "sr7", offsetof(CPUState, sr[7]) },
2117
    { "sr8", offsetof(CPUState, sr[8]) },
2118
    { "sr9", offsetof(CPUState, sr[9]) },
2119
    { "sr10", offsetof(CPUState, sr[10]) },
2120
    { "sr11", offsetof(CPUState, sr[11]) },
2121
    { "sr12", offsetof(CPUState, sr[12]) },
2122
    { "sr13", offsetof(CPUState, sr[13]) },
2123
    { "sr14", offsetof(CPUState, sr[14]) },
2124
    { "sr15", offsetof(CPUState, sr[15]) },
2125
    /* Too lazy to put BATs and SPRs ... */
2126
#elif defined(TARGET_SPARC)
2127
    { "g0", offsetof(CPUState, gregs[0]) },
2128
    { "g1", offsetof(CPUState, gregs[1]) },
2129
    { "g2", offsetof(CPUState, gregs[2]) },
2130
    { "g3", offsetof(CPUState, gregs[3]) },
2131
    { "g4", offsetof(CPUState, gregs[4]) },
2132
    { "g5", offsetof(CPUState, gregs[5]) },
2133
    { "g6", offsetof(CPUState, gregs[6]) },
2134
    { "g7", offsetof(CPUState, gregs[7]) },
2135
    { "o0", 0, monitor_get_reg },
2136
    { "o1", 1, monitor_get_reg },
2137
    { "o2", 2, monitor_get_reg },
2138
    { "o3", 3, monitor_get_reg },
2139
    { "o4", 4, monitor_get_reg },
2140
    { "o5", 5, monitor_get_reg },
2141
    { "o6", 6, monitor_get_reg },
2142
    { "o7", 7, monitor_get_reg },
2143
    { "l0", 8, monitor_get_reg },
2144
    { "l1", 9, monitor_get_reg },
2145
    { "l2", 10, monitor_get_reg },
2146
    { "l3", 11, monitor_get_reg },
2147
    { "l4", 12, monitor_get_reg },
2148
    { "l5", 13, monitor_get_reg },
2149
    { "l6", 14, monitor_get_reg },
2150
    { "l7", 15, monitor_get_reg },
2151
    { "i0", 16, monitor_get_reg },
2152
    { "i1", 17, monitor_get_reg },
2153
    { "i2", 18, monitor_get_reg },
2154
    { "i3", 19, monitor_get_reg },
2155
    { "i4", 20, monitor_get_reg },
2156
    { "i5", 21, monitor_get_reg },
2157
    { "i6", 22, monitor_get_reg },
2158
    { "i7", 23, monitor_get_reg },
2159
    { "pc", offsetof(CPUState, pc) },
2160
    { "npc", offsetof(CPUState, npc) },
2161
    { "y", offsetof(CPUState, y) },
2162
#ifndef TARGET_SPARC64
2163
    { "psr", 0, &monitor_get_psr, },
2164
    { "wim", offsetof(CPUState, wim) },
2165
#endif
2166
    { "tbr", offsetof(CPUState, tbr) },
2167
    { "fsr", offsetof(CPUState, fsr) },
2168
    { "f0", offsetof(CPUState, fpr[0]) },
2169
    { "f1", offsetof(CPUState, fpr[1]) },
2170
    { "f2", offsetof(CPUState, fpr[2]) },
2171
    { "f3", offsetof(CPUState, fpr[3]) },
2172
    { "f4", offsetof(CPUState, fpr[4]) },
2173
    { "f5", offsetof(CPUState, fpr[5]) },
2174
    { "f6", offsetof(CPUState, fpr[6]) },
2175
    { "f7", offsetof(CPUState, fpr[7]) },
2176
    { "f8", offsetof(CPUState, fpr[8]) },
2177
    { "f9", offsetof(CPUState, fpr[9]) },
2178
    { "f10", offsetof(CPUState, fpr[10]) },
2179
    { "f11", offsetof(CPUState, fpr[11]) },
2180
    { "f12", offsetof(CPUState, fpr[12]) },
2181
    { "f13", offsetof(CPUState, fpr[13]) },
2182
    { "f14", offsetof(CPUState, fpr[14]) },
2183
    { "f15", offsetof(CPUState, fpr[15]) },
2184
    { "f16", offsetof(CPUState, fpr[16]) },
2185
    { "f17", offsetof(CPUState, fpr[17]) },
2186
    { "f18", offsetof(CPUState, fpr[18]) },
2187
    { "f19", offsetof(CPUState, fpr[19]) },
2188
    { "f20", offsetof(CPUState, fpr[20]) },
2189
    { "f21", offsetof(CPUState, fpr[21]) },
2190
    { "f22", offsetof(CPUState, fpr[22]) },
2191
    { "f23", offsetof(CPUState, fpr[23]) },
2192
    { "f24", offsetof(CPUState, fpr[24]) },
2193
    { "f25", offsetof(CPUState, fpr[25]) },
2194
    { "f26", offsetof(CPUState, fpr[26]) },
2195
    { "f27", offsetof(CPUState, fpr[27]) },
2196
    { "f28", offsetof(CPUState, fpr[28]) },
2197
    { "f29", offsetof(CPUState, fpr[29]) },
2198
    { "f30", offsetof(CPUState, fpr[30]) },
2199
    { "f31", offsetof(CPUState, fpr[31]) },
2200
#ifdef TARGET_SPARC64
2201
    { "f32", offsetof(CPUState, fpr[32]) },
2202
    { "f34", offsetof(CPUState, fpr[34]) },
2203
    { "f36", offsetof(CPUState, fpr[36]) },
2204
    { "f38", offsetof(CPUState, fpr[38]) },
2205
    { "f40", offsetof(CPUState, fpr[40]) },
2206
    { "f42", offsetof(CPUState, fpr[42]) },
2207
    { "f44", offsetof(CPUState, fpr[44]) },
2208
    { "f46", offsetof(CPUState, fpr[46]) },
2209
    { "f48", offsetof(CPUState, fpr[48]) },
2210
    { "f50", offsetof(CPUState, fpr[50]) },
2211
    { "f52", offsetof(CPUState, fpr[52]) },
2212
    { "f54", offsetof(CPUState, fpr[54]) },
2213
    { "f56", offsetof(CPUState, fpr[56]) },
2214
    { "f58", offsetof(CPUState, fpr[58]) },
2215
    { "f60", offsetof(CPUState, fpr[60]) },
2216
    { "f62", offsetof(CPUState, fpr[62]) },
2217
    { "asi", offsetof(CPUState, asi) },
2218
    { "pstate", offsetof(CPUState, pstate) },
2219
    { "cansave", offsetof(CPUState, cansave) },
2220
    { "canrestore", offsetof(CPUState, canrestore) },
2221
    { "otherwin", offsetof(CPUState, otherwin) },
2222
    { "wstate", offsetof(CPUState, wstate) },
2223
    { "cleanwin", offsetof(CPUState, cleanwin) },
2224
    { "fprs", offsetof(CPUState, fprs) },
2225
#endif
2226
#endif
2227
    { NULL },
2228
};
2229

    
2230
static void expr_error(Monitor *mon, const char *msg)
2231
{
2232
    monitor_printf(mon, "%s\n", msg);
2233
    longjmp(expr_env, 1);
2234
}
2235

    
2236
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
2237
static int get_monitor_def(target_long *pval, const char *name)
2238
{
2239
    const MonitorDef *md;
2240
    void *ptr;
2241

    
2242
    for(md = monitor_defs; md->name != NULL; md++) {
2243
        if (compare_cmd(name, md->name)) {
2244
            if (md->get_value) {
2245
                *pval = md->get_value(md, md->offset);
2246
            } else {
2247
                CPUState *env = mon_get_cpu();
2248
                if (!env)
2249
                    return -2;
2250
                ptr = (uint8_t *)env + md->offset;
2251
                switch(md->type) {
2252
                case MD_I32:
2253
                    *pval = *(int32_t *)ptr;
2254
                    break;
2255
                case MD_TLONG:
2256
                    *pval = *(target_long *)ptr;
2257
                    break;
2258
                default:
2259
                    *pval = 0;
2260
                    break;
2261
                }
2262
            }
2263
            return 0;
2264
        }
2265
    }
2266
    return -1;
2267
}
2268

    
2269
static void next(void)
2270
{
2271
    if (*pch != '\0') {
2272
        pch++;
2273
        while (qemu_isspace(*pch))
2274
            pch++;
2275
    }
2276
}
2277

    
2278
static int64_t expr_sum(Monitor *mon);
2279

    
2280
static int64_t expr_unary(Monitor *mon)
2281
{
2282
    int64_t n;
2283
    char *p;
2284
    int ret;
2285

    
2286
    switch(*pch) {
2287
    case '+':
2288
        next();
2289
        n = expr_unary(mon);
2290
        break;
2291
    case '-':
2292
        next();
2293
        n = -expr_unary(mon);
2294
        break;
2295
    case '~':
2296
        next();
2297
        n = ~expr_unary(mon);
2298
        break;
2299
    case '(':
2300
        next();
2301
        n = expr_sum(mon);
2302
        if (*pch != ')') {
2303
            expr_error(mon, "')' expected");
2304
        }
2305
        next();
2306
        break;
2307
    case '\'':
2308
        pch++;
2309
        if (*pch == '\0')
2310
            expr_error(mon, "character constant expected");
2311
        n = *pch;
2312
        pch++;
2313
        if (*pch != '\'')
2314
            expr_error(mon, "missing terminating \' character");
2315
        next();
2316
        break;
2317
    case '$':
2318
        {
2319
            char buf[128], *q;
2320
            target_long reg=0;
2321

    
2322
            pch++;
2323
            q = buf;
2324
            while ((*pch >= 'a' && *pch <= 'z') ||
2325
                   (*pch >= 'A' && *pch <= 'Z') ||
2326
                   (*pch >= '0' && *pch <= '9') ||
2327
                   *pch == '_' || *pch == '.') {
2328
                if ((q - buf) < sizeof(buf) - 1)
2329
                    *q++ = *pch;
2330
                pch++;
2331
            }
2332
            while (qemu_isspace(*pch))
2333
                pch++;
2334
            *q = 0;
2335
            ret = get_monitor_def(&reg, buf);
2336
            if (ret == -1)
2337
                expr_error(mon, "unknown register");
2338
            else if (ret == -2)
2339
                expr_error(mon, "no cpu defined");
2340
            n = reg;
2341
        }
2342
        break;
2343
    case '\0':
2344
        expr_error(mon, "unexpected end of expression");
2345
        n = 0;
2346
        break;
2347
    default:
2348
#if TARGET_PHYS_ADDR_BITS > 32
2349
        n = strtoull(pch, &p, 0);
2350
#else
2351
        n = strtoul(pch, &p, 0);
2352
#endif
2353
        if (pch == p) {
2354
            expr_error(mon, "invalid char in expression");
2355
        }
2356
        pch = p;
2357
        while (qemu_isspace(*pch))
2358
            pch++;
2359
        break;
2360
    }
2361
    return n;
2362
}
2363

    
2364

    
2365
static int64_t expr_prod(Monitor *mon)
2366
{
2367
    int64_t val, val2;
2368
    int op;
2369

    
2370
    val = expr_unary(mon);
2371
    for(;;) {
2372
        op = *pch;
2373
        if (op != '*' && op != '/' && op != '%')
2374
            break;
2375
        next();
2376
        val2 = expr_unary(mon);
2377
        switch(op) {
2378
        default:
2379
        case '*':
2380
            val *= val2;
2381
            break;
2382
        case '/':
2383
        case '%':
2384
            if (val2 == 0)
2385
                expr_error(mon, "division by zero");
2386
            if (op == '/')
2387
                val /= val2;
2388
            else
2389
                val %= val2;
2390
            break;
2391
        }
2392
    }
2393
    return val;
2394
}
2395

    
2396
static int64_t expr_logic(Monitor *mon)
2397
{
2398
    int64_t val, val2;
2399
    int op;
2400

    
2401
    val = expr_prod(mon);
2402
    for(;;) {
2403
        op = *pch;
2404
        if (op != '&' && op != '|' && op != '^')
2405
            break;
2406
        next();
2407
        val2 = expr_prod(mon);
2408
        switch(op) {
2409
        default:
2410
        case '&':
2411
            val &= val2;
2412
            break;
2413
        case '|':
2414
            val |= val2;
2415
            break;
2416
        case '^':
2417
            val ^= val2;
2418
            break;
2419
        }
2420
    }
2421
    return val;
2422
}
2423

    
2424
static int64_t expr_sum(Monitor *mon)
2425
{
2426
    int64_t val, val2;
2427
    int op;
2428

    
2429
    val = expr_logic(mon);
2430
    for(;;) {
2431
        op = *pch;
2432
        if (op != '+' && op != '-')
2433
            break;
2434
        next();
2435
        val2 = expr_logic(mon);
2436
        if (op == '+')
2437
            val += val2;
2438
        else
2439
            val -= val2;
2440
    }
2441
    return val;
2442
}
2443

    
2444
static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2445
{
2446
    pch = *pp;
2447
    if (setjmp(expr_env)) {
2448
        *pp = pch;
2449
        return -1;
2450
    }
2451
    while (qemu_isspace(*pch))
2452
        pch++;
2453
    *pval = expr_sum(mon);
2454
    *pp = pch;
2455
    return 0;
2456
}
2457

    
2458
static int get_str(char *buf, int buf_size, const char **pp)
2459
{
2460
    const char *p;
2461
    char *q;
2462
    int c;
2463

    
2464
    q = buf;
2465
    p = *pp;
2466
    while (qemu_isspace(*p))
2467
        p++;
2468
    if (*p == '\0') {
2469
    fail:
2470
        *q = '\0';
2471
        *pp = p;
2472
        return -1;
2473
    }
2474
    if (*p == '\"') {
2475
        p++;
2476
        while (*p != '\0' && *p != '\"') {
2477
            if (*p == '\\') {
2478
                p++;
2479
                c = *p++;
2480
                switch(c) {
2481
                case 'n':
2482
                    c = '\n';
2483
                    break;
2484
                case 'r':
2485
                    c = '\r';
2486
                    break;
2487
                case '\\':
2488
                case '\'':
2489
                case '\"':
2490
                    break;
2491
                default:
2492
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
2493
                    goto fail;
2494
                }
2495
                if ((q - buf) < buf_size - 1) {
2496
                    *q++ = c;
2497
                }
2498
            } else {
2499
                if ((q - buf) < buf_size - 1) {
2500
                    *q++ = *p;
2501
                }
2502
                p++;
2503
            }
2504
        }
2505
        if (*p != '\"') {
2506
            qemu_printf("unterminated string\n");
2507
            goto fail;
2508
        }
2509
        p++;
2510
    } else {
2511
        while (*p != '\0' && !qemu_isspace(*p)) {
2512
            if ((q - buf) < buf_size - 1) {
2513
                *q++ = *p;
2514
            }
2515
            p++;
2516
        }
2517
    }
2518
    *q = '\0';
2519
    *pp = p;
2520
    return 0;
2521
}
2522

    
2523
/*
2524
 * Store the command-name in cmdname, and return a pointer to
2525
 * the remaining of the command string.
2526
 */
2527
static const char *get_command_name(const char *cmdline,
2528
                                    char *cmdname, size_t nlen)
2529
{
2530
    size_t len;
2531
    const char *p, *pstart;
2532

    
2533
    p = cmdline;
2534
    while (qemu_isspace(*p))
2535
        p++;
2536
    if (*p == '\0')
2537
        return NULL;
2538
    pstart = p;
2539
    while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2540
        p++;
2541
    len = p - pstart;
2542
    if (len > nlen - 1)
2543
        len = nlen - 1;
2544
    memcpy(cmdname, pstart, len);
2545
    cmdname[len] = '\0';
2546
    return p;
2547
}
2548

    
2549
/**
2550
 * Read key of 'type' into 'key' and return the current
2551
 * 'type' pointer.
2552
 */
2553
static char *key_get_info(const char *type, char **key)
2554
{
2555
    size_t len;
2556
    char *p, *str;
2557

    
2558
    if (*type == ',')
2559
        type++;
2560

    
2561
    p = strchr(type, ':');
2562
    if (!p) {
2563
        *key = NULL;
2564
        return NULL;
2565
    }
2566
    len = p - type;
2567

    
2568
    str = qemu_malloc(len + 1);
2569
    memcpy(str, type, len);
2570
    str[len] = '\0';
2571

    
2572
    *key = str;
2573
    return ++p;
2574
}
2575

    
2576
static int default_fmt_format = 'x';
2577
static int default_fmt_size = 4;
2578

    
2579
#define MAX_ARGS 16
2580

    
2581
static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2582
                                              const char *cmdline,
2583
                                              QDict *qdict)
2584
{
2585
    const char *p, *typestr;
2586
    int c, nb_args, has_arg;
2587
    const mon_cmd_t *cmd;
2588
    char cmdname[256];
2589
    char buf[1024];
2590
    char *key;
2591
    void *args[MAX_ARGS];
2592

    
2593
#ifdef DEBUG
2594
    monitor_printf(mon, "command='%s'\n", cmdline);
2595
#endif
2596

    
2597
    /* extract the command name */
2598
    p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2599
    if (!p)
2600
        return NULL;
2601

    
2602
    /* find the command */
2603
    for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2604
        if (compare_cmd(cmdname, cmd->name))
2605
            break;
2606
    }
2607

    
2608
    if (cmd->name == NULL) {
2609
        monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2610
        return NULL;
2611
    }
2612

    
2613
    /* parse the parameters */
2614
    typestr = cmd->args_type;
2615
    nb_args = 0;
2616
    for(;;) {
2617
        typestr = key_get_info(typestr, &key);
2618
        if (!typestr)
2619
            break;
2620
        c = *typestr;
2621
        typestr++;
2622
        switch(c) {
2623
        case 'F':
2624
        case 'B':
2625
        case 's':
2626
            {
2627
                int ret;
2628
                char *str;
2629

    
2630
                while (qemu_isspace(*p))
2631
                    p++;
2632
                if (*typestr == '?') {
2633
                    typestr++;
2634
                    if (*p == '\0') {
2635
                        /* no optional string: NULL argument */
2636
                        str = NULL;
2637
                        goto add_str;
2638
                    }
2639
                }
2640
                ret = get_str(buf, sizeof(buf), &p);
2641
                if (ret < 0) {
2642
                    switch(c) {
2643
                    case 'F':
2644
                        monitor_printf(mon, "%s: filename expected\n",
2645
                                       cmdname);
2646
                        break;
2647
                    case 'B':
2648
                        monitor_printf(mon, "%s: block device name expected\n",
2649
                                       cmdname);
2650
                        break;
2651
                    default:
2652
                        monitor_printf(mon, "%s: string expected\n", cmdname);
2653
                        break;
2654
                    }
2655
                    goto fail;
2656
                }
2657
                str = qemu_malloc(strlen(buf) + 1);
2658
                pstrcpy(str, sizeof(buf), buf);
2659
            add_str:
2660
                if (nb_args >= MAX_ARGS) {
2661
                error_args:
2662
                    monitor_printf(mon, "%s: too many arguments\n", cmdname);
2663
                    goto fail;
2664
                }
2665
                args[nb_args++] = str;
2666
                if (str)
2667
                    qdict_put(qdict, key, qstring_from_str(str));
2668
                qemu_free(str);
2669
            }
2670
            break;
2671
        case '/':
2672
            {
2673
                int count, format, size;
2674

    
2675
                while (qemu_isspace(*p))
2676
                    p++;
2677
                if (*p == '/') {
2678
                    /* format found */
2679
                    p++;
2680
                    count = 1;
2681
                    if (qemu_isdigit(*p)) {
2682
                        count = 0;
2683
                        while (qemu_isdigit(*p)) {
2684
                            count = count * 10 + (*p - '0');
2685
                            p++;
2686
                        }
2687
                    }
2688
                    size = -1;
2689
                    format = -1;
2690
                    for(;;) {
2691
                        switch(*p) {
2692
                        case 'o':
2693
                        case 'd':
2694
                        case 'u':
2695
                        case 'x':
2696
                        case 'i':
2697
                        case 'c':
2698
                            format = *p++;
2699
                            break;
2700
                        case 'b':
2701
                            size = 1;
2702
                            p++;
2703
                            break;
2704
                        case 'h':
2705
                            size = 2;
2706
                            p++;
2707
                            break;
2708
                        case 'w':
2709
                            size = 4;
2710
                            p++;
2711
                            break;
2712
                        case 'g':
2713
                        case 'L':
2714
                            size = 8;
2715
                            p++;
2716
                            break;
2717
                        default:
2718
                            goto next;
2719
                        }
2720
                    }
2721
                next:
2722
                    if (*p != '\0' && !qemu_isspace(*p)) {
2723
                        monitor_printf(mon, "invalid char in format: '%c'\n",
2724
                                       *p);
2725
                        goto fail;
2726
                    }
2727
                    if (format < 0)
2728
                        format = default_fmt_format;
2729
                    if (format != 'i') {
2730
                        /* for 'i', not specifying a size gives -1 as size */
2731
                        if (size < 0)
2732
                            size = default_fmt_size;
2733
                        default_fmt_size = size;
2734
                    }
2735
                    default_fmt_format = format;
2736
                } else {
2737
                    count = 1;
2738
                    format = default_fmt_format;
2739
                    if (format != 'i') {
2740
                        size = default_fmt_size;
2741
                    } else {
2742
                        size = -1;
2743
                    }
2744
                }
2745
                if (nb_args + 3 > MAX_ARGS)
2746
                    goto error_args;
2747
                args[nb_args++] = (void*)(long)count;
2748
                args[nb_args++] = (void*)(long)format;
2749
                args[nb_args++] = (void*)(long)size;
2750
                qdict_put(qdict, "count", qint_from_int(count));
2751
                qdict_put(qdict, "format", qint_from_int(format));
2752
                qdict_put(qdict, "size", qint_from_int(size));
2753
            }
2754
            break;
2755
        case 'i':
2756
        case 'l':
2757
            {
2758
                int64_t val;
2759
                int dict_add = 1;
2760

    
2761
                while (qemu_isspace(*p))
2762
                    p++;
2763
                if (*typestr == '?' || *typestr == '.') {
2764
                    if (*typestr == '?') {
2765
                        if (*p == '\0')
2766
                            has_arg = 0;
2767
                        else
2768
                            has_arg = 1;
2769
                    } else {
2770
                        if (*p == '.') {
2771
                            p++;
2772
                            while (qemu_isspace(*p))
2773
                                p++;
2774
                            has_arg = 1;
2775
                        } else {
2776
                            has_arg = 0;
2777
                        }
2778
                    }
2779
                    typestr++;
2780
                    if (nb_args >= MAX_ARGS)
2781
                        goto error_args;
2782
                    dict_add = has_arg;
2783
                    args[nb_args++] = (void *)(long)has_arg;
2784
                    if (!has_arg) {
2785
                        if (nb_args >= MAX_ARGS)
2786
                            goto error_args;
2787
                        val = -1;
2788
                        goto add_num;
2789
                    }
2790
                }
2791
                if (get_expr(mon, &val, &p))
2792
                    goto fail;
2793
            add_num:
2794
                if (c == 'i') {
2795
                    if (nb_args >= MAX_ARGS)
2796
                        goto error_args;
2797
                    args[nb_args++] = (void *)(long)val;
2798
                    if (dict_add)
2799
                        qdict_put(qdict, key, qint_from_int(val));
2800
                } else {
2801
                    if ((nb_args + 1) >= MAX_ARGS)
2802
                        goto error_args;
2803
#if TARGET_PHYS_ADDR_BITS > 32
2804
                    args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2805
#else
2806
                    args[nb_args++] = (void *)0;
2807
#endif
2808
                    args[nb_args++] = (void *)(long)(val & 0xffffffff);
2809
                    qdict_put(qdict, key, qint_from_int(val));
2810
                }
2811
            }
2812
            break;
2813
        case '-':
2814
            {
2815
                int has_option;
2816
                /* option */
2817

    
2818
                c = *typestr++;
2819
                if (c == '\0')
2820
                    goto bad_type;
2821
                while (qemu_isspace(*p))
2822
                    p++;
2823
                has_option = 0;
2824
                if (*p == '-') {
2825
                    p++;
2826
                    if (*p != c) {
2827
                        monitor_printf(mon, "%s: unsupported option -%c\n",
2828
                                       cmdname, *p);
2829
                        goto fail;
2830
                    }
2831
                    p++;
2832
                    has_option = 1;
2833
                }
2834
                if (nb_args >= MAX_ARGS)
2835
                    goto error_args;
2836
                args[nb_args++] = (void *)(long)has_option;
2837
                qdict_put(qdict, key, qint_from_int(has_option));
2838
            }
2839
            break;
2840
        default:
2841
        bad_type:
2842
            monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2843
            goto fail;
2844
        }
2845
        qemu_free(key);
2846
        key = NULL;
2847
    }
2848
    /* check that all arguments were parsed */
2849
    while (qemu_isspace(*p))
2850
        p++;
2851
    if (*p != '\0') {
2852
        monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2853
                       cmdname);
2854
        goto fail;
2855
    }
2856

    
2857
    return cmd;
2858

    
2859
fail:
2860
    qemu_free(key);
2861
    return NULL;
2862
}
2863

    
2864
static void monitor_handle_command(Monitor *mon, const char *cmdline)
2865
{
2866
    QDict *qdict;
2867
    const mon_cmd_t *cmd;
2868

    
2869
    qdict = qdict_new();
2870

    
2871
    cmd = monitor_parse_command(mon, cmdline, qdict);
2872
    if (cmd) {
2873
        void (*handler)(Monitor *mon, const QDict *qdict);
2874

    
2875
        qemu_errors_to_mon(mon);
2876

    
2877
        handler = cmd->handler;
2878
        handler(mon, qdict);
2879

    
2880
        qemu_errors_to_previous();
2881
    }
2882

    
2883
    QDECREF(qdict);
2884
}
2885

    
2886
static void cmd_completion(const char *name, const char *list)
2887
{
2888
    const char *p, *pstart;
2889
    char cmd[128];
2890
    int len;
2891

    
2892
    p = list;
2893
    for(;;) {
2894
        pstart = p;
2895
        p = strchr(p, '|');
2896
        if (!p)
2897
            p = pstart + strlen(pstart);
2898
        len = p - pstart;
2899
        if (len > sizeof(cmd) - 2)
2900
            len = sizeof(cmd) - 2;
2901
        memcpy(cmd, pstart, len);
2902
        cmd[len] = '\0';
2903
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2904
            readline_add_completion(cur_mon->rs, cmd);
2905
        }
2906
        if (*p == '\0')
2907
            break;
2908
        p++;
2909
    }
2910
}
2911

    
2912
static void file_completion(const char *input)
2913
{
2914
    DIR *ffs;
2915
    struct dirent *d;
2916
    char path[1024];
2917
    char file[1024], file_prefix[1024];
2918
    int input_path_len;
2919
    const char *p;
2920

    
2921
    p = strrchr(input, '/');
2922
    if (!p) {
2923
        input_path_len = 0;
2924
        pstrcpy(file_prefix, sizeof(file_prefix), input);
2925
        pstrcpy(path, sizeof(path), ".");
2926
    } else {
2927
        input_path_len = p - input + 1;
2928
        memcpy(path, input, input_path_len);
2929
        if (input_path_len > sizeof(path) - 1)
2930
            input_path_len = sizeof(path) - 1;
2931
        path[input_path_len] = '\0';
2932
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2933
    }
2934
#ifdef DEBUG_COMPLETION
2935
    monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2936
                   input, path, file_prefix);
2937
#endif
2938
    ffs = opendir(path);
2939
    if (!ffs)
2940
        return;
2941
    for(;;) {
2942
        struct stat sb;
2943
        d = readdir(ffs);
2944
        if (!d)
2945
            break;
2946
        if (strstart(d->d_name, file_prefix, NULL)) {
2947
            memcpy(file, input, input_path_len);
2948
            if (input_path_len < sizeof(file))
2949
                pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2950
                        d->d_name);
2951
            /* stat the file to find out if it's a directory.
2952
             * In that case add a slash to speed up typing long paths
2953
             */
2954
            stat(file, &sb);
2955
            if(S_ISDIR(sb.st_mode))
2956
                pstrcat(file, sizeof(file), "/");
2957
            readline_add_completion(cur_mon->rs, file);
2958
        }
2959
    }
2960
    closedir(ffs);
2961
}
2962

    
2963
static void block_completion_it(void *opaque, BlockDriverState *bs)
2964
{
2965
    const char *name = bdrv_get_device_name(bs);
2966
    const char *input = opaque;
2967

    
2968
    if (input[0] == '\0' ||
2969
        !strncmp(name, (char *)input, strlen(input))) {
2970
        readline_add_completion(cur_mon->rs, name);
2971
    }
2972
}
2973

    
2974
/* NOTE: this parser is an approximate form of the real command parser */
2975
static void parse_cmdline(const char *cmdline,
2976
                         int *pnb_args, char **args)
2977
{
2978
    const char *p;
2979
    int nb_args, ret;
2980
    char buf[1024];
2981

    
2982
    p = cmdline;
2983
    nb_args = 0;
2984
    for(;;) {
2985
        while (qemu_isspace(*p))
2986
            p++;
2987
        if (*p == '\0')
2988
            break;
2989
        if (nb_args >= MAX_ARGS)
2990
            break;
2991
        ret = get_str(buf, sizeof(buf), &p);
2992
        args[nb_args] = qemu_strdup(buf);
2993
        nb_args++;
2994
        if (ret < 0)
2995
            break;
2996
    }
2997
    *pnb_args = nb_args;
2998
}
2999

    
3000
static const char *next_arg_type(const char *typestr)
3001
{
3002
    const char *p = strchr(typestr, ':');
3003
    return (p != NULL ? ++p : typestr);
3004
}
3005

    
3006
static void monitor_find_completion(const char *cmdline)
3007
{
3008
    const char *cmdname;
3009
    char *args[MAX_ARGS];
3010
    int nb_args, i, len;
3011
    const char *ptype, *str;
3012
    const mon_cmd_t *cmd;
3013
    const KeyDef *key;
3014

    
3015
    parse_cmdline(cmdline, &nb_args, args);
3016
#ifdef DEBUG_COMPLETION
3017
    for(i = 0; i < nb_args; i++) {
3018
        monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3019
    }
3020
#endif
3021

    
3022
    /* if the line ends with a space, it means we want to complete the
3023
       next arg */
3024
    len = strlen(cmdline);
3025
    if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3026
        if (nb_args >= MAX_ARGS)
3027
            return;
3028
        args[nb_args++] = qemu_strdup("");
3029
    }
3030
    if (nb_args <= 1) {
3031
        /* command completion */
3032
        if (nb_args == 0)
3033
            cmdname = "";
3034
        else
3035
            cmdname = args[0];
3036
        readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3037
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3038
            cmd_completion(cmdname, cmd->name);
3039
        }
3040
    } else {
3041
        /* find the command */
3042
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3043
            if (compare_cmd(args[0], cmd->name))
3044
                goto found;
3045
        }
3046
        return;
3047
    found:
3048
        ptype = next_arg_type(cmd->args_type);
3049
        for(i = 0; i < nb_args - 2; i++) {
3050
            if (*ptype != '\0') {
3051
                ptype = next_arg_type(ptype);
3052
                while (*ptype == '?')
3053
                    ptype = next_arg_type(ptype);
3054
            }
3055
        }
3056
        str = args[nb_args - 1];
3057
        if (*ptype == '-' && ptype[1] != '\0') {
3058
            ptype += 2;
3059
        }
3060
        switch(*ptype) {
3061
        case 'F':
3062
            /* file completion */
3063
            readline_set_completion_index(cur_mon->rs, strlen(str));
3064
            file_completion(str);
3065
            break;
3066
        case 'B':
3067
            /* block device name completion */
3068
            readline_set_completion_index(cur_mon->rs, strlen(str));
3069
            bdrv_iterate(block_completion_it, (void *)str);
3070
            break;
3071
        case 's':
3072
            /* XXX: more generic ? */
3073
            if (!strcmp(cmd->name, "info")) {
3074
                readline_set_completion_index(cur_mon->rs, strlen(str));
3075
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3076
                    cmd_completion(str, cmd->name);
3077
                }
3078
            } else if (!strcmp(cmd->name, "sendkey")) {
3079
                char *sep = strrchr(str, '-');
3080
                if (sep)
3081
                    str = sep + 1;
3082
                readline_set_completion_index(cur_mon->rs, strlen(str));
3083
                for(key = key_defs; key->name != NULL; key++) {
3084
                    cmd_completion(str, key->name);
3085
                }
3086
            } else if (!strcmp(cmd->name, "help|?")) {
3087
                readline_set_completion_index(cur_mon->rs, strlen(str));
3088
                for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3089
                    cmd_completion(str, cmd->name);
3090
                }
3091
            }
3092
            break;
3093
        default:
3094
            break;
3095
        }
3096
    }
3097
    for(i = 0; i < nb_args; i++)
3098
        qemu_free(args[i]);
3099
}
3100

    
3101
static int monitor_can_read(void *opaque)
3102
{
3103
    Monitor *mon = opaque;
3104

    
3105
    return (mon->suspend_cnt == 0) ? 128 : 0;
3106
}
3107

    
3108
static void monitor_read(void *opaque, const uint8_t *buf, int size)
3109
{
3110
    Monitor *old_mon = cur_mon;
3111
    int i;
3112

    
3113
    cur_mon = opaque;
3114

    
3115
    if (cur_mon->rs) {
3116
        for (i = 0; i < size; i++)
3117
            readline_handle_byte(cur_mon->rs, buf[i]);
3118
    } else {
3119
        if (size == 0 || buf[size - 1] != 0)
3120
            monitor_printf(cur_mon, "corrupted command\n");
3121
        else
3122
            monitor_handle_command(cur_mon, (char *)buf);
3123
    }
3124

    
3125
    cur_mon = old_mon;
3126
}
3127

    
3128
static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3129
{
3130
    monitor_suspend(mon);
3131
    monitor_handle_command(mon, cmdline);
3132
    monitor_resume(mon);
3133
}
3134

    
3135
int monitor_suspend(Monitor *mon)
3136
{
3137
    if (!mon->rs)
3138
        return -ENOTTY;
3139
    mon->suspend_cnt++;
3140
    return 0;
3141
}
3142

    
3143
void monitor_resume(Monitor *mon)
3144
{
3145
    if (!mon->rs)
3146
        return;
3147
    if (--mon->suspend_cnt == 0)
3148
        readline_show_prompt(mon->rs);
3149
}
3150

    
3151
static void monitor_event(void *opaque, int event)
3152
{
3153
    Monitor *mon = opaque;
3154

    
3155
    switch (event) {
3156
    case CHR_EVENT_MUX_IN:
3157
        readline_restart(mon->rs);
3158
        monitor_resume(mon);
3159
        monitor_flush(mon);
3160
        break;
3161

    
3162
    case CHR_EVENT_MUX_OUT:
3163
        if (mon->suspend_cnt == 0)
3164
            monitor_printf(mon, "\n");
3165
        monitor_flush(mon);
3166
        monitor_suspend(mon);
3167
        break;
3168

    
3169
    case CHR_EVENT_RESET:
3170
        monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3171
                       "information\n", QEMU_VERSION);
3172
        if (mon->chr->focus == 0)
3173
            readline_show_prompt(mon->rs);
3174
        break;
3175
    }
3176
}
3177

    
3178

    
3179
/*
3180
 * Local variables:
3181
 *  c-indent-level: 4
3182
 *  c-basic-offset: 4
3183
 *  tab-width: 8
3184
 * End:
3185
 */
3186

    
3187
void monitor_init(CharDriverState *chr, int flags)
3188
{
3189
    static int is_first_init = 1;
3190
    Monitor *mon;
3191

    
3192
    if (is_first_init) {
3193
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3194
        is_first_init = 0;
3195
    }
3196

    
3197
    mon = qemu_mallocz(sizeof(*mon));
3198

    
3199
    mon->chr = chr;
3200
    mon->flags = flags;
3201
    if (mon->chr->focus != 0)
3202
        mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3203
    if (flags & MONITOR_USE_READLINE) {
3204
        mon->rs = readline_init(mon, monitor_find_completion);
3205
        monitor_read_command(mon, 0);
3206
    }
3207

    
3208
    qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3209
                          mon);
3210

    
3211
    LIST_INSERT_HEAD(&mon_list, mon, entry);
3212
    if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3213
        cur_mon = mon;
3214
}
3215

    
3216
static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3217
{
3218
    BlockDriverState *bs = opaque;
3219
    int ret = 0;
3220

    
3221
    if (bdrv_set_key(bs, password) != 0) {
3222
        monitor_printf(mon, "invalid password\n");
3223
        ret = -EPERM;
3224
    }
3225
    if (mon->password_completion_cb)
3226
        mon->password_completion_cb(mon->password_opaque, ret);
3227

    
3228
    monitor_read_command(mon, 1);
3229
}
3230

    
3231
void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3232
                                 BlockDriverCompletionFunc *completion_cb,
3233
                                 void *opaque)
3234
{
3235
    int err;
3236

    
3237
    if (!bdrv_key_required(bs)) {
3238
        if (completion_cb)
3239
            completion_cb(opaque, 0);
3240
        return;
3241
    }
3242

    
3243
    monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3244
                   bdrv_get_encrypted_filename(bs));
3245

    
3246
    mon->password_completion_cb = completion_cb;
3247
    mon->password_opaque = opaque;
3248

    
3249
    err = monitor_read_password(mon, bdrv_password_cb, bs);
3250

    
3251
    if (err && completion_cb)
3252
        completion_cb(opaque, err);
3253
}
3254

    
3255
typedef struct QemuErrorSink QemuErrorSink;
3256
struct QemuErrorSink {
3257
    enum {
3258
        ERR_SINK_FILE,
3259
        ERR_SINK_MONITOR,
3260
    } dest;
3261
    union {
3262
        FILE    *fp;
3263
        Monitor *mon;
3264
    };
3265
    QemuErrorSink *previous;
3266
};
3267

    
3268
static QemuErrorSink *qemu_error_sink;
3269

    
3270
void qemu_errors_to_file(FILE *fp)
3271
{
3272
    QemuErrorSink *sink;
3273

    
3274
    sink = qemu_mallocz(sizeof(*sink));
3275
    sink->dest = ERR_SINK_FILE;
3276
    sink->fp = fp;
3277
    sink->previous = qemu_error_sink;
3278
    qemu_error_sink = sink;
3279
}
3280

    
3281
void qemu_errors_to_mon(Monitor *mon)
3282
{
3283
    QemuErrorSink *sink;
3284

    
3285
    sink = qemu_mallocz(sizeof(*sink));
3286
    sink->dest = ERR_SINK_MONITOR;
3287
    sink->mon = mon;
3288
    sink->previous = qemu_error_sink;
3289
    qemu_error_sink = sink;
3290
}
3291

    
3292
void qemu_errors_to_previous(void)
3293
{
3294
    QemuErrorSink *sink;
3295

    
3296
    assert(qemu_error_sink != NULL);
3297
    sink = qemu_error_sink;
3298
    qemu_error_sink = sink->previous;
3299
    qemu_free(sink);
3300
}
3301

    
3302
void qemu_error(const char *fmt, ...)
3303
{
3304
    va_list args;
3305

    
3306
    assert(qemu_error_sink != NULL);
3307
    switch (qemu_error_sink->dest) {
3308
    case ERR_SINK_FILE:
3309
        va_start(args, fmt);
3310
        vfprintf(qemu_error_sink->fp, fmt, args);
3311
        va_end(args);
3312
        break;
3313
    case ERR_SINK_MONITOR:
3314
        va_start(args, fmt);
3315
        monitor_vprintf(qemu_error_sink->mon, fmt, args);
3316
        va_end(args);
3317
        break;
3318
    }
3319
}