Statistics
| Branch: | Revision:

root / monitor.c @ cc1d9c70

History | View | Annotate | Download (93.1 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 "hw/loader.h"
33
#include "gdbstub.h"
34
#include "net.h"
35
#include "qemu-char.h"
36
#include "sysemu.h"
37
#include "monitor.h"
38
#include "readline.h"
39
#include "console.h"
40
#include "block.h"
41
#include "audio/audio.h"
42
#include "disas.h"
43
#include "balloon.h"
44
#include "qemu-timer.h"
45
#include "migration.h"
46
#include "kvm.h"
47
#include "acl.h"
48
#include "qint.h"
49
#include "qdict.h"
50
#include "qstring.h"
51

    
52
//#define DEBUG
53
//#define DEBUG_COMPLETION
54

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

    
71
typedef struct mon_cmd_t {
72
    const char *name;
73
    const char *args_type;
74
    const char *params;
75
    const char *help;
76
    void (*user_print)(Monitor *mon, const QObject *data);
77
    union {
78
        void (*info)(Monitor *mon);
79
        void (*info_new)(Monitor *mon, QObject **ret_data);
80
        void (*cmd)(Monitor *mon, const QDict *qdict);
81
        void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
82
    } mhandler;
83
} mon_cmd_t;
84

    
85
/* file descriptors passed via SCM_RIGHTS */
86
typedef struct mon_fd_t mon_fd_t;
87
struct mon_fd_t {
88
    char *name;
89
    int fd;
90
    QLIST_ENTRY(mon_fd_t) next;
91
};
92

    
93
struct Monitor {
94
    CharDriverState *chr;
95
    int mux_out;
96
    int reset_seen;
97
    int flags;
98
    int suspend_cnt;
99
    uint8_t outbuf[1024];
100
    int outbuf_index;
101
    ReadLineState *rs;
102
    CPUState *mon_cpu;
103
    BlockDriverCompletionFunc *password_completion_cb;
104
    void *password_opaque;
105
    QLIST_HEAD(,mon_fd_t) fds;
106
    QLIST_ENTRY(Monitor) entry;
107
};
108

    
109
static QLIST_HEAD(mon_list, Monitor) mon_list;
110

    
111
static const mon_cmd_t mon_cmds[];
112
static const mon_cmd_t info_cmds[];
113

    
114
Monitor *cur_mon = NULL;
115

    
116
static void monitor_command_cb(Monitor *mon, const char *cmdline,
117
                               void *opaque);
118

    
119
static void monitor_read_command(Monitor *mon, int show_prompt)
120
{
121
    readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
122
    if (show_prompt)
123
        readline_show_prompt(mon->rs);
124
}
125

    
126
static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
127
                                 void *opaque)
128
{
129
    if (mon->rs) {
130
        readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
131
        /* prompt is printed on return from the command handler */
132
        return 0;
133
    } else {
134
        monitor_printf(mon, "terminal does not support password prompting\n");
135
        return -ENOTTY;
136
    }
137
}
138

    
139
void monitor_flush(Monitor *mon)
140
{
141
    if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
142
        qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
143
        mon->outbuf_index = 0;
144
    }
145
}
146

    
147
/* flush at every end of line or if the buffer is full */
148
static void monitor_puts(Monitor *mon, const char *str)
149
{
150
    char c;
151

    
152
    if (!mon)
153
        return;
154

    
155
    for(;;) {
156
        c = *str++;
157
        if (c == '\0')
158
            break;
159
        if (c == '\n')
160
            mon->outbuf[mon->outbuf_index++] = '\r';
161
        mon->outbuf[mon->outbuf_index++] = c;
162
        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
163
            || c == '\n')
164
            monitor_flush(mon);
165
    }
166
}
167

    
168
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
169
{
170
    char buf[4096];
171
    vsnprintf(buf, sizeof(buf), fmt, ap);
172
    monitor_puts(mon, buf);
173
}
174

    
175
void monitor_printf(Monitor *mon, const char *fmt, ...)
176
{
177
    va_list ap;
178
    va_start(ap, fmt);
179
    monitor_vprintf(mon, fmt, ap);
180
    va_end(ap);
181
}
182

    
183
void monitor_print_filename(Monitor *mon, const char *filename)
184
{
185
    int i;
186

    
187
    for (i = 0; filename[i]; i++) {
188
        switch (filename[i]) {
189
        case ' ':
190
        case '"':
191
        case '\\':
192
            monitor_printf(mon, "\\%c", filename[i]);
193
            break;
194
        case '\t':
195
            monitor_printf(mon, "\\t");
196
            break;
197
        case '\r':
198
            monitor_printf(mon, "\\r");
199
            break;
200
        case '\n':
201
            monitor_printf(mon, "\\n");
202
            break;
203
        default:
204
            monitor_printf(mon, "%c", filename[i]);
205
            break;
206
        }
207
    }
208
}
209

    
210
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
211
{
212
    va_list ap;
213
    va_start(ap, fmt);
214
    monitor_vprintf((Monitor *)stream, fmt, ap);
215
    va_end(ap);
216
    return 0;
217
}
218

    
219
static void monitor_user_noop(Monitor *mon, const QObject *data) { }
220

    
221
static inline int monitor_handler_ported(const mon_cmd_t *cmd)
222
{
223
    return cmd->user_print != NULL;
224
}
225

    
226
static void monitor_print_qobject(Monitor *mon, const QObject *data)
227
{
228
    switch (qobject_type(data)) {
229
        case QTYPE_QSTRING:
230
            monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
231
            break;
232
        case QTYPE_QINT:
233
            monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
234
            break;
235
        default:
236
            monitor_printf(mon, "ERROR: unsupported type: %d",
237
                                                        qobject_type(data));
238
            break;
239
    }
240

    
241
    monitor_puts(mon, "\n");
242
}
243

    
244
static int compare_cmd(const char *name, const char *list)
245
{
246
    const char *p, *pstart;
247
    int len;
248
    len = strlen(name);
249
    p = list;
250
    for(;;) {
251
        pstart = p;
252
        p = strchr(p, '|');
253
        if (!p)
254
            p = pstart + strlen(pstart);
255
        if ((p - pstart) == len && !memcmp(pstart, name, len))
256
            return 1;
257
        if (*p == '\0')
258
            break;
259
        p++;
260
    }
261
    return 0;
262
}
263

    
264
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
265
                          const char *prefix, const char *name)
266
{
267
    const mon_cmd_t *cmd;
268

    
269
    for(cmd = cmds; cmd->name != NULL; cmd++) {
270
        if (!name || !strcmp(name, cmd->name))
271
            monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
272
                           cmd->params, cmd->help);
273
    }
274
}
275

    
276
static void help_cmd(Monitor *mon, const char *name)
277
{
278
    if (name && !strcmp(name, "info")) {
279
        help_cmd_dump(mon, info_cmds, "info ", NULL);
280
    } else {
281
        help_cmd_dump(mon, mon_cmds, "", name);
282
        if (name && !strcmp(name, "log")) {
283
            const CPULogItem *item;
284
            monitor_printf(mon, "Log items (comma separated):\n");
285
            monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
286
            for(item = cpu_log_items; item->mask != 0; item++) {
287
                monitor_printf(mon, "%-10s %s\n", item->name, item->help);
288
            }
289
        }
290
    }
291
}
292

    
293
static void do_help_cmd(Monitor *mon, const QDict *qdict)
294
{
295
    help_cmd(mon, qdict_get_try_str(qdict, "name"));
296
}
297

    
298
static void do_commit(Monitor *mon, const QDict *qdict)
299
{
300
    int all_devices;
301
    DriveInfo *dinfo;
302
    const char *device = qdict_get_str(qdict, "device");
303

    
304
    all_devices = !strcmp(device, "all");
305
    QTAILQ_FOREACH(dinfo, &drives, next) {
306
        if (!all_devices)
307
            if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
308
                continue;
309
        bdrv_commit(dinfo->bdrv);
310
    }
311
}
312

    
313
static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
314
{
315
    const mon_cmd_t *cmd;
316
    const char *item = qdict_get_try_str(qdict, "item");
317

    
318
    if (!item)
319
        goto help;
320

    
321
    for (cmd = info_cmds; cmd->name != NULL; cmd++) {
322
        if (compare_cmd(item, cmd->name))
323
            break;
324
    }
325

    
326
    if (cmd->name == NULL)
327
        goto help;
328

    
329
    if (monitor_handler_ported(cmd)) {
330
        cmd->mhandler.info_new(mon, ret_data);
331
        if (*ret_data)
332
            cmd->user_print(mon, *ret_data);
333
    } else {
334
        cmd->mhandler.info(mon);
335
    }
336

    
337
    return;
338

    
339
help:
340
    help_cmd(mon, "info");
341
}
342

    
343
/**
344
 * do_info_version(): Show QEMU version
345
 */
346
static void do_info_version(Monitor *mon, QObject **ret_data)
347
{
348
    *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
349
}
350

    
351
static void do_info_name(Monitor *mon)
352
{
353
    if (qemu_name)
354
        monitor_printf(mon, "%s\n", qemu_name);
355
}
356

    
357
#if defined(TARGET_I386)
358
static void do_info_hpet(Monitor *mon)
359
{
360
    monitor_printf(mon, "HPET is %s by QEMU\n",
361
                   (no_hpet) ? "disabled" : "enabled");
362
}
363
#endif
364

    
365
static void do_info_uuid(Monitor *mon)
366
{
367
    monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
368
                   qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
369
                   qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
370
                   qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
371
                   qemu_uuid[14], qemu_uuid[15]);
372
}
373

    
374
/* get the current CPU defined by the user */
375
static int mon_set_cpu(int cpu_index)
376
{
377
    CPUState *env;
378

    
379
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
380
        if (env->cpu_index == cpu_index) {
381
            cur_mon->mon_cpu = env;
382
            return 0;
383
        }
384
    }
385
    return -1;
386
}
387

    
388
static CPUState *mon_get_cpu(void)
389
{
390
    if (!cur_mon->mon_cpu) {
391
        mon_set_cpu(0);
392
    }
393
    cpu_synchronize_state(cur_mon->mon_cpu);
394
    return cur_mon->mon_cpu;
395
}
396

    
397
static void do_info_registers(Monitor *mon)
398
{
399
    CPUState *env;
400
    env = mon_get_cpu();
401
    if (!env)
402
        return;
403
#ifdef TARGET_I386
404
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
405
                   X86_DUMP_FPU);
406
#else
407
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
408
                   0);
409
#endif
410
}
411

    
412
static void do_info_cpus(Monitor *mon)
413
{
414
    CPUState *env;
415

    
416
    /* just to set the default cpu if not already done */
417
    mon_get_cpu();
418

    
419
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
420
        cpu_synchronize_state(env);
421
        monitor_printf(mon, "%c CPU #%d:",
422
                       (env == mon->mon_cpu) ? '*' : ' ',
423
                       env->cpu_index);
424
#if defined(TARGET_I386)
425
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
426
                       env->eip + env->segs[R_CS].base);
427
#elif defined(TARGET_PPC)
428
        monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
429
#elif defined(TARGET_SPARC)
430
        monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
431
                       env->pc, env->npc);
432
#elif defined(TARGET_MIPS)
433
        monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
434
#endif
435
        if (env->halted)
436
            monitor_printf(mon, " (halted)");
437
        monitor_printf(mon, "\n");
438
    }
439
}
440

    
441
static void do_cpu_set(Monitor *mon, const QDict *qdict)
442
{
443
    int index = qdict_get_int(qdict, "index");
444
    if (mon_set_cpu(index) < 0)
445
        monitor_printf(mon, "Invalid CPU index\n");
446
}
447

    
448
static void do_info_jit(Monitor *mon)
449
{
450
    dump_exec_info((FILE *)mon, monitor_fprintf);
451
}
452

    
453
static void do_info_history(Monitor *mon)
454
{
455
    int i;
456
    const char *str;
457

    
458
    if (!mon->rs)
459
        return;
460
    i = 0;
461
    for(;;) {
462
        str = readline_get_history(mon->rs, i);
463
        if (!str)
464
            break;
465
        monitor_printf(mon, "%d: '%s'\n", i, str);
466
        i++;
467
    }
468
}
469

    
470
#if defined(TARGET_PPC)
471
/* XXX: not implemented in other targets */
472
static void do_info_cpu_stats(Monitor *mon)
473
{
474
    CPUState *env;
475

    
476
    env = mon_get_cpu();
477
    cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
478
}
479
#endif
480

    
481
/**
482
 * do_quit(): Quit QEMU execution
483
 */
484
static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
485
{
486
    exit(0);
487
}
488

    
489
static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
490
{
491
    if (bdrv_is_inserted(bs)) {
492
        if (!force) {
493
            if (!bdrv_is_removable(bs)) {
494
                monitor_printf(mon, "device is not removable\n");
495
                return -1;
496
            }
497
            if (bdrv_is_locked(bs)) {
498
                monitor_printf(mon, "device is locked\n");
499
                return -1;
500
            }
501
        }
502
        bdrv_close(bs);
503
    }
504
    return 0;
505
}
506

    
507
static void do_eject(Monitor *mon, const QDict *qdict)
508
{
509
    BlockDriverState *bs;
510
    int force = qdict_get_int(qdict, "force");
511
    const char *filename = qdict_get_str(qdict, "filename");
512

    
513
    bs = bdrv_find(filename);
514
    if (!bs) {
515
        monitor_printf(mon, "device not found\n");
516
        return;
517
    }
518
    eject_device(mon, bs, force);
519
}
520

    
521
static void do_change_block(Monitor *mon, const char *device,
522
                            const char *filename, const char *fmt)
523
{
524
    BlockDriverState *bs;
525
    BlockDriver *drv = NULL;
526

    
527
    bs = bdrv_find(device);
528
    if (!bs) {
529
        monitor_printf(mon, "device not found\n");
530
        return;
531
    }
532
    if (fmt) {
533
        drv = bdrv_find_format(fmt);
534
        if (!drv) {
535
            monitor_printf(mon, "invalid format %s\n", fmt);
536
            return;
537
        }
538
    }
539
    if (eject_device(mon, bs, 0) < 0)
540
        return;
541
    bdrv_open2(bs, filename, 0, drv);
542
    monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
543
}
544

    
545
static void change_vnc_password_cb(Monitor *mon, const char *password,
546
                                   void *opaque)
547
{
548
    if (vnc_display_password(NULL, password) < 0)
549
        monitor_printf(mon, "could not set VNC server password\n");
550

    
551
    monitor_read_command(mon, 1);
552
}
553

    
554
static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
555
{
556
    if (strcmp(target, "passwd") == 0 ||
557
        strcmp(target, "password") == 0) {
558
        if (arg) {
559
            char password[9];
560
            strncpy(password, arg, sizeof(password));
561
            password[sizeof(password) - 1] = '\0';
562
            change_vnc_password_cb(mon, password, NULL);
563
        } else {
564
            monitor_read_password(mon, change_vnc_password_cb, NULL);
565
        }
566
    } else {
567
        if (vnc_display_open(NULL, target) < 0)
568
            monitor_printf(mon, "could not start VNC server on %s\n", target);
569
    }
570
}
571

    
572
static void do_change(Monitor *mon, const QDict *qdict)
573
{
574
    const char *device = qdict_get_str(qdict, "device");
575
    const char *target = qdict_get_str(qdict, "target");
576
    const char *arg = qdict_get_try_str(qdict, "arg");
577
    if (strcmp(device, "vnc") == 0) {
578
        do_change_vnc(mon, target, arg);
579
    } else {
580
        do_change_block(mon, device, target, arg);
581
    }
582
}
583

    
584
static void do_screen_dump(Monitor *mon, const QDict *qdict)
585
{
586
    vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
587
}
588

    
589
static void do_logfile(Monitor *mon, const QDict *qdict)
590
{
591
    cpu_set_log_filename(qdict_get_str(qdict, "filename"));
592
}
593

    
594
static void do_log(Monitor *mon, const QDict *qdict)
595
{
596
    int mask;
597
    const char *items = qdict_get_str(qdict, "items");
598

    
599
    if (!strcmp(items, "none")) {
600
        mask = 0;
601
    } else {
602
        mask = cpu_str_to_log_mask(items);
603
        if (!mask) {
604
            help_cmd(mon, "log");
605
            return;
606
        }
607
    }
608
    cpu_set_log(mask);
609
}
610

    
611
static void do_singlestep(Monitor *mon, const QDict *qdict)
612
{
613
    const char *option = qdict_get_try_str(qdict, "option");
614
    if (!option || !strcmp(option, "on")) {
615
        singlestep = 1;
616
    } else if (!strcmp(option, "off")) {
617
        singlestep = 0;
618
    } else {
619
        monitor_printf(mon, "unexpected option %s\n", option);
620
    }
621
}
622

    
623
/**
624
 * do_stop(): Stop VM execution
625
 */
626
static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
627
{
628
    vm_stop(EXCP_INTERRUPT);
629
}
630

    
631
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
632

    
633
struct bdrv_iterate_context {
634
    Monitor *mon;
635
    int err;
636
};
637

    
638
/**
639
 * do_cont(): Resume emulation.
640
 */
641
static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
642
{
643
    struct bdrv_iterate_context context = { mon, 0 };
644

    
645
    bdrv_iterate(encrypted_bdrv_it, &context);
646
    /* only resume the vm if all keys are set and valid */
647
    if (!context.err)
648
        vm_start();
649
}
650

    
651
static void bdrv_key_cb(void *opaque, int err)
652
{
653
    Monitor *mon = opaque;
654

    
655
    /* another key was set successfully, retry to continue */
656
    if (!err)
657
        do_cont(mon, NULL, NULL);
658
}
659

    
660
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
661
{
662
    struct bdrv_iterate_context *context = opaque;
663

    
664
    if (!context->err && bdrv_key_required(bs)) {
665
        context->err = -EBUSY;
666
        monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
667
                                    context->mon);
668
    }
669
}
670

    
671
static void do_gdbserver(Monitor *mon, const QDict *qdict)
672
{
673
    const char *device = qdict_get_try_str(qdict, "device");
674
    if (!device)
675
        device = "tcp::" DEFAULT_GDBSTUB_PORT;
676
    if (gdbserver_start(device) < 0) {
677
        monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
678
                       device);
679
    } else if (strcmp(device, "none") == 0) {
680
        monitor_printf(mon, "Disabled gdbserver\n");
681
    } else {
682
        monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
683
                       device);
684
    }
685
}
686

    
687
static void do_watchdog_action(Monitor *mon, const QDict *qdict)
688
{
689
    const char *action = qdict_get_str(qdict, "action");
690
    if (select_watchdog_action(action) == -1) {
691
        monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
692
    }
693
}
694

    
695
static void monitor_printc(Monitor *mon, int c)
696
{
697
    monitor_printf(mon, "'");
698
    switch(c) {
699
    case '\'':
700
        monitor_printf(mon, "\\'");
701
        break;
702
    case '\\':
703
        monitor_printf(mon, "\\\\");
704
        break;
705
    case '\n':
706
        monitor_printf(mon, "\\n");
707
        break;
708
    case '\r':
709
        monitor_printf(mon, "\\r");
710
        break;
711
    default:
712
        if (c >= 32 && c <= 126) {
713
            monitor_printf(mon, "%c", c);
714
        } else {
715
            monitor_printf(mon, "\\x%02x", c);
716
        }
717
        break;
718
    }
719
    monitor_printf(mon, "'");
720
}
721

    
722
static void memory_dump(Monitor *mon, int count, int format, int wsize,
723
                        target_phys_addr_t addr, int is_physical)
724
{
725
    CPUState *env;
726
    int nb_per_line, l, line_size, i, max_digits, len;
727
    uint8_t buf[16];
728
    uint64_t v;
729

    
730
    if (format == 'i') {
731
        int flags;
732
        flags = 0;
733
        env = mon_get_cpu();
734
        if (!env && !is_physical)
735
            return;
736
#ifdef TARGET_I386
737
        if (wsize == 2) {
738
            flags = 1;
739
        } else if (wsize == 4) {
740
            flags = 0;
741
        } else {
742
            /* as default we use the current CS size */
743
            flags = 0;
744
            if (env) {
745
#ifdef TARGET_X86_64
746
                if ((env->efer & MSR_EFER_LMA) &&
747
                    (env->segs[R_CS].flags & DESC_L_MASK))
748
                    flags = 2;
749
                else
750
#endif
751
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
752
                    flags = 1;
753
            }
754
        }
755
#endif
756
        monitor_disas(mon, env, addr, count, is_physical, flags);
757
        return;
758
    }
759

    
760
    len = wsize * count;
761
    if (wsize == 1)
762
        line_size = 8;
763
    else
764
        line_size = 16;
765
    nb_per_line = line_size / wsize;
766
    max_digits = 0;
767

    
768
    switch(format) {
769
    case 'o':
770
        max_digits = (wsize * 8 + 2) / 3;
771
        break;
772
    default:
773
    case 'x':
774
        max_digits = (wsize * 8) / 4;
775
        break;
776
    case 'u':
777
    case 'd':
778
        max_digits = (wsize * 8 * 10 + 32) / 33;
779
        break;
780
    case 'c':
781
        wsize = 1;
782
        break;
783
    }
784

    
785
    while (len > 0) {
786
        if (is_physical)
787
            monitor_printf(mon, TARGET_FMT_plx ":", addr);
788
        else
789
            monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
790
        l = len;
791
        if (l > line_size)
792
            l = line_size;
793
        if (is_physical) {
794
            cpu_physical_memory_rw(addr, buf, l, 0);
795
        } else {
796
            env = mon_get_cpu();
797
            if (!env)
798
                break;
799
            if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
800
                monitor_printf(mon, " Cannot access memory\n");
801
                break;
802
            }
803
        }
804
        i = 0;
805
        while (i < l) {
806
            switch(wsize) {
807
            default:
808
            case 1:
809
                v = ldub_raw(buf + i);
810
                break;
811
            case 2:
812
                v = lduw_raw(buf + i);
813
                break;
814
            case 4:
815
                v = (uint32_t)ldl_raw(buf + i);
816
                break;
817
            case 8:
818
                v = ldq_raw(buf + i);
819
                break;
820
            }
821
            monitor_printf(mon, " ");
822
            switch(format) {
823
            case 'o':
824
                monitor_printf(mon, "%#*" PRIo64, max_digits, v);
825
                break;
826
            case 'x':
827
                monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
828
                break;
829
            case 'u':
830
                monitor_printf(mon, "%*" PRIu64, max_digits, v);
831
                break;
832
            case 'd':
833
                monitor_printf(mon, "%*" PRId64, max_digits, v);
834
                break;
835
            case 'c':
836
                monitor_printc(mon, v);
837
                break;
838
            }
839
            i += wsize;
840
        }
841
        monitor_printf(mon, "\n");
842
        addr += l;
843
        len -= l;
844
    }
845
}
846

    
847
static void do_memory_dump(Monitor *mon, const QDict *qdict)
848
{
849
    int count = qdict_get_int(qdict, "count");
850
    int format = qdict_get_int(qdict, "format");
851
    int size = qdict_get_int(qdict, "size");
852
    target_long addr = qdict_get_int(qdict, "addr");
853

    
854
    memory_dump(mon, count, format, size, addr, 0);
855
}
856

    
857
static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
858
{
859
    int count = qdict_get_int(qdict, "count");
860
    int format = qdict_get_int(qdict, "format");
861
    int size = qdict_get_int(qdict, "size");
862
    target_phys_addr_t addr = qdict_get_int(qdict, "addr");
863

    
864
    memory_dump(mon, count, format, size, addr, 1);
865
}
866

    
867
static void do_print(Monitor *mon, const QDict *qdict)
868
{
869
    int format = qdict_get_int(qdict, "format");
870
    target_phys_addr_t val = qdict_get_int(qdict, "val");
871

    
872
#if TARGET_PHYS_ADDR_BITS == 32
873
    switch(format) {
874
    case 'o':
875
        monitor_printf(mon, "%#o", val);
876
        break;
877
    case 'x':
878
        monitor_printf(mon, "%#x", val);
879
        break;
880
    case 'u':
881
        monitor_printf(mon, "%u", val);
882
        break;
883
    default:
884
    case 'd':
885
        monitor_printf(mon, "%d", val);
886
        break;
887
    case 'c':
888
        monitor_printc(mon, val);
889
        break;
890
    }
891
#else
892
    switch(format) {
893
    case 'o':
894
        monitor_printf(mon, "%#" PRIo64, val);
895
        break;
896
    case 'x':
897
        monitor_printf(mon, "%#" PRIx64, val);
898
        break;
899
    case 'u':
900
        monitor_printf(mon, "%" PRIu64, val);
901
        break;
902
    default:
903
    case 'd':
904
        monitor_printf(mon, "%" PRId64, val);
905
        break;
906
    case 'c':
907
        monitor_printc(mon, val);
908
        break;
909
    }
910
#endif
911
    monitor_printf(mon, "\n");
912
}
913

    
914
static void do_memory_save(Monitor *mon, const QDict *qdict)
915
{
916
    FILE *f;
917
    uint32_t size = qdict_get_int(qdict, "size");
918
    const char *filename = qdict_get_str(qdict, "filename");
919
    target_long addr = qdict_get_int(qdict, "val");
920
    uint32_t l;
921
    CPUState *env;
922
    uint8_t buf[1024];
923

    
924
    env = mon_get_cpu();
925
    if (!env)
926
        return;
927

    
928
    f = fopen(filename, "wb");
929
    if (!f) {
930
        monitor_printf(mon, "could not open '%s'\n", filename);
931
        return;
932
    }
933
    while (size != 0) {
934
        l = sizeof(buf);
935
        if (l > size)
936
            l = size;
937
        cpu_memory_rw_debug(env, addr, buf, l, 0);
938
        fwrite(buf, 1, l, f);
939
        addr += l;
940
        size -= l;
941
    }
942
    fclose(f);
943
}
944

    
945
static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
946
{
947
    FILE *f;
948
    uint32_t l;
949
    uint8_t buf[1024];
950
    uint32_t size = qdict_get_int(qdict, "size");
951
    const char *filename = qdict_get_str(qdict, "filename");
952
    target_phys_addr_t addr = qdict_get_int(qdict, "val");
953

    
954
    f = fopen(filename, "wb");
955
    if (!f) {
956
        monitor_printf(mon, "could not open '%s'\n", filename);
957
        return;
958
    }
959
    while (size != 0) {
960
        l = sizeof(buf);
961
        if (l > size)
962
            l = size;
963
        cpu_physical_memory_rw(addr, buf, l, 0);
964
        fwrite(buf, 1, l, f);
965
        fflush(f);
966
        addr += l;
967
        size -= l;
968
    }
969
    fclose(f);
970
}
971

    
972
static void do_sum(Monitor *mon, const QDict *qdict)
973
{
974
    uint32_t addr;
975
    uint8_t buf[1];
976
    uint16_t sum;
977
    uint32_t start = qdict_get_int(qdict, "start");
978
    uint32_t size = qdict_get_int(qdict, "size");
979

    
980
    sum = 0;
981
    for(addr = start; addr < (start + size); addr++) {
982
        cpu_physical_memory_rw(addr, buf, 1, 0);
983
        /* BSD sum algorithm ('sum' Unix command) */
984
        sum = (sum >> 1) | (sum << 15);
985
        sum += buf[0];
986
    }
987
    monitor_printf(mon, "%05d\n", sum);
988
}
989

    
990
typedef struct {
991
    int keycode;
992
    const char *name;
993
} KeyDef;
994

    
995
static const KeyDef key_defs[] = {
996
    { 0x2a, "shift" },
997
    { 0x36, "shift_r" },
998

    
999
    { 0x38, "alt" },
1000
    { 0xb8, "alt_r" },
1001
    { 0x64, "altgr" },
1002
    { 0xe4, "altgr_r" },
1003
    { 0x1d, "ctrl" },
1004
    { 0x9d, "ctrl_r" },
1005

    
1006
    { 0xdd, "menu" },
1007

    
1008
    { 0x01, "esc" },
1009

    
1010
    { 0x02, "1" },
1011
    { 0x03, "2" },
1012
    { 0x04, "3" },
1013
    { 0x05, "4" },
1014
    { 0x06, "5" },
1015
    { 0x07, "6" },
1016
    { 0x08, "7" },
1017
    { 0x09, "8" },
1018
    { 0x0a, "9" },
1019
    { 0x0b, "0" },
1020
    { 0x0c, "minus" },
1021
    { 0x0d, "equal" },
1022
    { 0x0e, "backspace" },
1023

    
1024
    { 0x0f, "tab" },
1025
    { 0x10, "q" },
1026
    { 0x11, "w" },
1027
    { 0x12, "e" },
1028
    { 0x13, "r" },
1029
    { 0x14, "t" },
1030
    { 0x15, "y" },
1031
    { 0x16, "u" },
1032
    { 0x17, "i" },
1033
    { 0x18, "o" },
1034
    { 0x19, "p" },
1035

    
1036
    { 0x1c, "ret" },
1037

    
1038
    { 0x1e, "a" },
1039
    { 0x1f, "s" },
1040
    { 0x20, "d" },
1041
    { 0x21, "f" },
1042
    { 0x22, "g" },
1043
    { 0x23, "h" },
1044
    { 0x24, "j" },
1045
    { 0x25, "k" },
1046
    { 0x26, "l" },
1047

    
1048
    { 0x2c, "z" },
1049
    { 0x2d, "x" },
1050
    { 0x2e, "c" },
1051
    { 0x2f, "v" },
1052
    { 0x30, "b" },
1053
    { 0x31, "n" },
1054
    { 0x32, "m" },
1055
    { 0x33, "comma" },
1056
    { 0x34, "dot" },
1057
    { 0x35, "slash" },
1058

    
1059
    { 0x37, "asterisk" },
1060

    
1061
    { 0x39, "spc" },
1062
    { 0x3a, "caps_lock" },
1063
    { 0x3b, "f1" },
1064
    { 0x3c, "f2" },
1065
    { 0x3d, "f3" },
1066
    { 0x3e, "f4" },
1067
    { 0x3f, "f5" },
1068
    { 0x40, "f6" },
1069
    { 0x41, "f7" },
1070
    { 0x42, "f8" },
1071
    { 0x43, "f9" },
1072
    { 0x44, "f10" },
1073
    { 0x45, "num_lock" },
1074
    { 0x46, "scroll_lock" },
1075

    
1076
    { 0xb5, "kp_divide" },
1077
    { 0x37, "kp_multiply" },
1078
    { 0x4a, "kp_subtract" },
1079
    { 0x4e, "kp_add" },
1080
    { 0x9c, "kp_enter" },
1081
    { 0x53, "kp_decimal" },
1082
    { 0x54, "sysrq" },
1083

    
1084
    { 0x52, "kp_0" },
1085
    { 0x4f, "kp_1" },
1086
    { 0x50, "kp_2" },
1087
    { 0x51, "kp_3" },
1088
    { 0x4b, "kp_4" },
1089
    { 0x4c, "kp_5" },
1090
    { 0x4d, "kp_6" },
1091
    { 0x47, "kp_7" },
1092
    { 0x48, "kp_8" },
1093
    { 0x49, "kp_9" },
1094

    
1095
    { 0x56, "<" },
1096

    
1097
    { 0x57, "f11" },
1098
    { 0x58, "f12" },
1099

    
1100
    { 0xb7, "print" },
1101

    
1102
    { 0xc7, "home" },
1103
    { 0xc9, "pgup" },
1104
    { 0xd1, "pgdn" },
1105
    { 0xcf, "end" },
1106

    
1107
    { 0xcb, "left" },
1108
    { 0xc8, "up" },
1109
    { 0xd0, "down" },
1110
    { 0xcd, "right" },
1111

    
1112
    { 0xd2, "insert" },
1113
    { 0xd3, "delete" },
1114
#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1115
    { 0xf0, "stop" },
1116
    { 0xf1, "again" },
1117
    { 0xf2, "props" },
1118
    { 0xf3, "undo" },
1119
    { 0xf4, "front" },
1120
    { 0xf5, "copy" },
1121
    { 0xf6, "open" },
1122
    { 0xf7, "paste" },
1123
    { 0xf8, "find" },
1124
    { 0xf9, "cut" },
1125
    { 0xfa, "lf" },
1126
    { 0xfb, "help" },
1127
    { 0xfc, "meta_l" },
1128
    { 0xfd, "meta_r" },
1129
    { 0xfe, "compose" },
1130
#endif
1131
    { 0, NULL },
1132
};
1133

    
1134
static int get_keycode(const char *key)
1135
{
1136
    const KeyDef *p;
1137
    char *endp;
1138
    int ret;
1139

    
1140
    for(p = key_defs; p->name != NULL; p++) {
1141
        if (!strcmp(key, p->name))
1142
            return p->keycode;
1143
    }
1144
    if (strstart(key, "0x", NULL)) {
1145
        ret = strtoul(key, &endp, 0);
1146
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1147
            return ret;
1148
    }
1149
    return -1;
1150
}
1151

    
1152
#define MAX_KEYCODES 16
1153
static uint8_t keycodes[MAX_KEYCODES];
1154
static int nb_pending_keycodes;
1155
static QEMUTimer *key_timer;
1156

    
1157
static void release_keys(void *opaque)
1158
{
1159
    int keycode;
1160

    
1161
    while (nb_pending_keycodes > 0) {
1162
        nb_pending_keycodes--;
1163
        keycode = keycodes[nb_pending_keycodes];
1164
        if (keycode & 0x80)
1165
            kbd_put_keycode(0xe0);
1166
        kbd_put_keycode(keycode | 0x80);
1167
    }
1168
}
1169

    
1170
static void do_sendkey(Monitor *mon, const QDict *qdict)
1171
{
1172
    char keyname_buf[16];
1173
    char *separator;
1174
    int keyname_len, keycode, i;
1175
    const char *string = qdict_get_str(qdict, "string");
1176
    int has_hold_time = qdict_haskey(qdict, "hold_time");
1177
    int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1178

    
1179
    if (nb_pending_keycodes > 0) {
1180
        qemu_del_timer(key_timer);
1181
        release_keys(NULL);
1182
    }
1183
    if (!has_hold_time)
1184
        hold_time = 100;
1185
    i = 0;
1186
    while (1) {
1187
        separator = strchr(string, '-');
1188
        keyname_len = separator ? separator - string : strlen(string);
1189
        if (keyname_len > 0) {
1190
            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1191
            if (keyname_len > sizeof(keyname_buf) - 1) {
1192
                monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1193
                return;
1194
            }
1195
            if (i == MAX_KEYCODES) {
1196
                monitor_printf(mon, "too many keys\n");
1197
                return;
1198
            }
1199
            keyname_buf[keyname_len] = 0;
1200
            keycode = get_keycode(keyname_buf);
1201
            if (keycode < 0) {
1202
                monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1203
                return;
1204
            }
1205
            keycodes[i++] = keycode;
1206
        }
1207
        if (!separator)
1208
            break;
1209
        string = separator + 1;
1210
    }
1211
    nb_pending_keycodes = i;
1212
    /* key down events */
1213
    for (i = 0; i < nb_pending_keycodes; i++) {
1214
        keycode = keycodes[i];
1215
        if (keycode & 0x80)
1216
            kbd_put_keycode(0xe0);
1217
        kbd_put_keycode(keycode & 0x7f);
1218
    }
1219
    /* delayed key up events */
1220
    qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1221
                   muldiv64(get_ticks_per_sec(), hold_time, 1000));
1222
}
1223

    
1224
static int mouse_button_state;
1225

    
1226
static void do_mouse_move(Monitor *mon, const QDict *qdict)
1227
{
1228
    int dx, dy, dz;
1229
    const char *dx_str = qdict_get_str(qdict, "dx_str");
1230
    const char *dy_str = qdict_get_str(qdict, "dy_str");
1231
    const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1232
    dx = strtol(dx_str, NULL, 0);
1233
    dy = strtol(dy_str, NULL, 0);
1234
    dz = 0;
1235
    if (dz_str)
1236
        dz = strtol(dz_str, NULL, 0);
1237
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
1238
}
1239

    
1240
static void do_mouse_button(Monitor *mon, const QDict *qdict)
1241
{
1242
    int button_state = qdict_get_int(qdict, "button_state");
1243
    mouse_button_state = button_state;
1244
    kbd_mouse_event(0, 0, 0, mouse_button_state);
1245
}
1246

    
1247
static void do_ioport_read(Monitor *mon, const QDict *qdict)
1248
{
1249
    int size = qdict_get_int(qdict, "size");
1250
    int addr = qdict_get_int(qdict, "addr");
1251
    int has_index = qdict_haskey(qdict, "index");
1252
    uint32_t val;
1253
    int suffix;
1254

    
1255
    if (has_index) {
1256
        int index = qdict_get_int(qdict, "index");
1257
        cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1258
        addr++;
1259
    }
1260
    addr &= 0xffff;
1261

    
1262
    switch(size) {
1263
    default:
1264
    case 1:
1265
        val = cpu_inb(addr);
1266
        suffix = 'b';
1267
        break;
1268
    case 2:
1269
        val = cpu_inw(addr);
1270
        suffix = 'w';
1271
        break;
1272
    case 4:
1273
        val = cpu_inl(addr);
1274
        suffix = 'l';
1275
        break;
1276
    }
1277
    monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1278
                   suffix, addr, size * 2, val);
1279
}
1280

    
1281
static void do_ioport_write(Monitor *mon, const QDict *qdict)
1282
{
1283
    int size = qdict_get_int(qdict, "size");
1284
    int addr = qdict_get_int(qdict, "addr");
1285
    int val = qdict_get_int(qdict, "val");
1286

    
1287
    addr &= IOPORTS_MASK;
1288

    
1289
    switch (size) {
1290
    default:
1291
    case 1:
1292
        cpu_outb(addr, val);
1293
        break;
1294
    case 2:
1295
        cpu_outw(addr, val);
1296
        break;
1297
    case 4:
1298
        cpu_outl(addr, val);
1299
        break;
1300
    }
1301
}
1302

    
1303
static void do_boot_set(Monitor *mon, const QDict *qdict)
1304
{
1305
    int res;
1306
    const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1307

    
1308
    res = qemu_boot_set(bootdevice);
1309
    if (res == 0) {
1310
        monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1311
    } else if (res > 0) {
1312
        monitor_printf(mon, "setting boot device list failed\n");
1313
    } else {
1314
        monitor_printf(mon, "no function defined to set boot device list for "
1315
                       "this architecture\n");
1316
    }
1317
}
1318

    
1319
/**
1320
 * do_system_reset(): Issue a machine reset
1321
 */
1322
static void do_system_reset(Monitor *mon, const QDict *qdict,
1323
                            QObject **ret_data)
1324
{
1325
    qemu_system_reset_request();
1326
}
1327

    
1328
/**
1329
 * do_system_powerdown(): Issue a machine powerdown
1330
 */
1331
static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1332
                                QObject **ret_data)
1333
{
1334
    qemu_system_powerdown_request();
1335
}
1336

    
1337
#if defined(TARGET_I386)
1338
static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1339
{
1340
    monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1341
                   addr,
1342
                   pte & mask,
1343
                   pte & PG_GLOBAL_MASK ? 'G' : '-',
1344
                   pte & PG_PSE_MASK ? 'P' : '-',
1345
                   pte & PG_DIRTY_MASK ? 'D' : '-',
1346
                   pte & PG_ACCESSED_MASK ? 'A' : '-',
1347
                   pte & PG_PCD_MASK ? 'C' : '-',
1348
                   pte & PG_PWT_MASK ? 'T' : '-',
1349
                   pte & PG_USER_MASK ? 'U' : '-',
1350
                   pte & PG_RW_MASK ? 'W' : '-');
1351
}
1352

    
1353
static void tlb_info(Monitor *mon)
1354
{
1355
    CPUState *env;
1356
    int l1, l2;
1357
    uint32_t pgd, pde, pte;
1358

    
1359
    env = mon_get_cpu();
1360
    if (!env)
1361
        return;
1362

    
1363
    if (!(env->cr[0] & CR0_PG_MASK)) {
1364
        monitor_printf(mon, "PG disabled\n");
1365
        return;
1366
    }
1367
    pgd = env->cr[3] & ~0xfff;
1368
    for(l1 = 0; l1 < 1024; l1++) {
1369
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1370
        pde = le32_to_cpu(pde);
1371
        if (pde & PG_PRESENT_MASK) {
1372
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1373
                print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1374
            } else {
1375
                for(l2 = 0; l2 < 1024; l2++) {
1376
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1377
                                             (uint8_t *)&pte, 4);
1378
                    pte = le32_to_cpu(pte);
1379
                    if (pte & PG_PRESENT_MASK) {
1380
                        print_pte(mon, (l1 << 22) + (l2 << 12),
1381
                                  pte & ~PG_PSE_MASK,
1382
                                  ~0xfff);
1383
                    }
1384
                }
1385
            }
1386
        }
1387
    }
1388
}
1389

    
1390
static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1391
                      uint32_t end, int prot)
1392
{
1393
    int prot1;
1394
    prot1 = *plast_prot;
1395
    if (prot != prot1) {
1396
        if (*pstart != -1) {
1397
            monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1398
                           *pstart, end, end - *pstart,
1399
                           prot1 & PG_USER_MASK ? 'u' : '-',
1400
                           'r',
1401
                           prot1 & PG_RW_MASK ? 'w' : '-');
1402
        }
1403
        if (prot != 0)
1404
            *pstart = end;
1405
        else
1406
            *pstart = -1;
1407
        *plast_prot = prot;
1408
    }
1409
}
1410

    
1411
static void mem_info(Monitor *mon)
1412
{
1413
    CPUState *env;
1414
    int l1, l2, prot, last_prot;
1415
    uint32_t pgd, pde, pte, start, end;
1416

    
1417
    env = mon_get_cpu();
1418
    if (!env)
1419
        return;
1420

    
1421
    if (!(env->cr[0] & CR0_PG_MASK)) {
1422
        monitor_printf(mon, "PG disabled\n");
1423
        return;
1424
    }
1425
    pgd = env->cr[3] & ~0xfff;
1426
    last_prot = 0;
1427
    start = -1;
1428
    for(l1 = 0; l1 < 1024; l1++) {
1429
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1430
        pde = le32_to_cpu(pde);
1431
        end = l1 << 22;
1432
        if (pde & PG_PRESENT_MASK) {
1433
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1434
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1435
                mem_print(mon, &start, &last_prot, end, prot);
1436
            } else {
1437
                for(l2 = 0; l2 < 1024; l2++) {
1438
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1439
                                             (uint8_t *)&pte, 4);
1440
                    pte = le32_to_cpu(pte);
1441
                    end = (l1 << 22) + (l2 << 12);
1442
                    if (pte & PG_PRESENT_MASK) {
1443
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1444
                    } else {
1445
                        prot = 0;
1446
                    }
1447
                    mem_print(mon, &start, &last_prot, end, prot);
1448
                }
1449
            }
1450
        } else {
1451
            prot = 0;
1452
            mem_print(mon, &start, &last_prot, end, prot);
1453
        }
1454
    }
1455
}
1456
#endif
1457

    
1458
#if defined(TARGET_SH4)
1459

    
1460
static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1461
{
1462
    monitor_printf(mon, " tlb%i:\t"
1463
                   "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1464
                   "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1465
                   "dirty=%hhu writethrough=%hhu\n",
1466
                   idx,
1467
                   tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1468
                   tlb->v, tlb->sh, tlb->c, tlb->pr,
1469
                   tlb->d, tlb->wt);
1470
}
1471

    
1472
static void tlb_info(Monitor *mon)
1473
{
1474
    CPUState *env = mon_get_cpu();
1475
    int i;
1476

    
1477
    monitor_printf (mon, "ITLB:\n");
1478
    for (i = 0 ; i < ITLB_SIZE ; i++)
1479
        print_tlb (mon, i, &env->itlb[i]);
1480
    monitor_printf (mon, "UTLB:\n");
1481
    for (i = 0 ; i < UTLB_SIZE ; i++)
1482
        print_tlb (mon, i, &env->utlb[i]);
1483
}
1484

    
1485
#endif
1486

    
1487
static void do_info_kvm(Monitor *mon)
1488
{
1489
#ifdef CONFIG_KVM
1490
    monitor_printf(mon, "kvm support: ");
1491
    if (kvm_enabled())
1492
        monitor_printf(mon, "enabled\n");
1493
    else
1494
        monitor_printf(mon, "disabled\n");
1495
#else
1496
    monitor_printf(mon, "kvm support: not compiled\n");
1497
#endif
1498
}
1499

    
1500
static void do_info_numa(Monitor *mon)
1501
{
1502
    int i;
1503
    CPUState *env;
1504

    
1505
    monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1506
    for (i = 0; i < nb_numa_nodes; i++) {
1507
        monitor_printf(mon, "node %d cpus:", i);
1508
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1509
            if (env->numa_node == i) {
1510
                monitor_printf(mon, " %d", env->cpu_index);
1511
            }
1512
        }
1513
        monitor_printf(mon, "\n");
1514
        monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1515
            node_mem[i] >> 20);
1516
    }
1517
}
1518

    
1519
#ifdef CONFIG_PROFILER
1520

    
1521
int64_t qemu_time;
1522
int64_t dev_time;
1523

    
1524
static void do_info_profile(Monitor *mon)
1525
{
1526
    int64_t total;
1527
    total = qemu_time;
1528
    if (total == 0)
1529
        total = 1;
1530
    monitor_printf(mon, "async time  %" PRId64 " (%0.3f)\n",
1531
                   dev_time, dev_time / (double)get_ticks_per_sec());
1532
    monitor_printf(mon, "qemu time   %" PRId64 " (%0.3f)\n",
1533
                   qemu_time, qemu_time / (double)get_ticks_per_sec());
1534
    qemu_time = 0;
1535
    dev_time = 0;
1536
}
1537
#else
1538
static void do_info_profile(Monitor *mon)
1539
{
1540
    monitor_printf(mon, "Internal profiler not compiled\n");
1541
}
1542
#endif
1543

    
1544
/* Capture support */
1545
static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1546

    
1547
static void do_info_capture(Monitor *mon)
1548
{
1549
    int i;
1550
    CaptureState *s;
1551

    
1552
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1553
        monitor_printf(mon, "[%d]: ", i);
1554
        s->ops.info (s->opaque);
1555
    }
1556
}
1557

    
1558
#ifdef HAS_AUDIO
1559
static void do_stop_capture(Monitor *mon, const QDict *qdict)
1560
{
1561
    int i;
1562
    int n = qdict_get_int(qdict, "n");
1563
    CaptureState *s;
1564

    
1565
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1566
        if (i == n) {
1567
            s->ops.destroy (s->opaque);
1568
            QLIST_REMOVE (s, entries);
1569
            qemu_free (s);
1570
            return;
1571
        }
1572
    }
1573
}
1574

    
1575
static void do_wav_capture(Monitor *mon, const QDict *qdict)
1576
{
1577
    const char *path = qdict_get_str(qdict, "path");
1578
    int has_freq = qdict_haskey(qdict, "freq");
1579
    int freq = qdict_get_try_int(qdict, "freq", -1);
1580
    int has_bits = qdict_haskey(qdict, "bits");
1581
    int bits = qdict_get_try_int(qdict, "bits", -1);
1582
    int has_channels = qdict_haskey(qdict, "nchannels");
1583
    int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1584
    CaptureState *s;
1585

    
1586
    s = qemu_mallocz (sizeof (*s));
1587

    
1588
    freq = has_freq ? freq : 44100;
1589
    bits = has_bits ? bits : 16;
1590
    nchannels = has_channels ? nchannels : 2;
1591

    
1592
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1593
        monitor_printf(mon, "Faied to add wave capture\n");
1594
        qemu_free (s);
1595
    }
1596
    QLIST_INSERT_HEAD (&capture_head, s, entries);
1597
}
1598
#endif
1599

    
1600
#if defined(TARGET_I386)
1601
static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1602
{
1603
    CPUState *env;
1604
    int cpu_index = qdict_get_int(qdict, "cpu_index");
1605

    
1606
    for (env = first_cpu; env != NULL; env = env->next_cpu)
1607
        if (env->cpu_index == cpu_index) {
1608
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1609
            break;
1610
        }
1611
}
1612
#endif
1613

    
1614
static void do_info_status(Monitor *mon)
1615
{
1616
    if (vm_running) {
1617
        if (singlestep) {
1618
            monitor_printf(mon, "VM status: running (single step mode)\n");
1619
        } else {
1620
            monitor_printf(mon, "VM status: running\n");
1621
        }
1622
    } else
1623
       monitor_printf(mon, "VM status: paused\n");
1624
}
1625

    
1626
/**
1627
 * do_balloon(): Request VM to change its memory allocation
1628
 */
1629
static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
1630
{
1631
    int value = qdict_get_int(qdict, "value");
1632
    ram_addr_t target = value;
1633
    qemu_balloon(target << 20);
1634
}
1635

    
1636
static void monitor_print_balloon(Monitor *mon, const QObject *data)
1637
{
1638
    monitor_printf(mon, "balloon: actual=%d\n",
1639
                                     (int)qint_get_int(qobject_to_qint(data)));
1640
}
1641

    
1642
/**
1643
 * do_info_balloon(): Balloon information
1644
 */
1645
static void do_info_balloon(Monitor *mon, QObject **ret_data)
1646
{
1647
    ram_addr_t actual;
1648

    
1649
    actual = qemu_balloon_status();
1650
    if (kvm_enabled() && !kvm_has_sync_mmu())
1651
        monitor_printf(mon, "Using KVM without synchronous MMU, "
1652
                       "ballooning disabled\n");
1653
    else if (actual == 0)
1654
        monitor_printf(mon, "Ballooning not activated in VM\n");
1655
    else
1656
        *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
1657
}
1658

    
1659
static qemu_acl *find_acl(Monitor *mon, const char *name)
1660
{
1661
    qemu_acl *acl = qemu_acl_find(name);
1662

    
1663
    if (!acl) {
1664
        monitor_printf(mon, "acl: unknown list '%s'\n", name);
1665
    }
1666
    return acl;
1667
}
1668

    
1669
static void do_acl_show(Monitor *mon, const QDict *qdict)
1670
{
1671
    const char *aclname = qdict_get_str(qdict, "aclname");
1672
    qemu_acl *acl = find_acl(mon, aclname);
1673
    qemu_acl_entry *entry;
1674
    int i = 0;
1675

    
1676
    if (acl) {
1677
        monitor_printf(mon, "policy: %s\n",
1678
                       acl->defaultDeny ? "deny" : "allow");
1679
        QTAILQ_FOREACH(entry, &acl->entries, next) {
1680
            i++;
1681
            monitor_printf(mon, "%d: %s %s\n", i,
1682
                           entry->deny ? "deny" : "allow", entry->match);
1683
        }
1684
    }
1685
}
1686

    
1687
static void do_acl_reset(Monitor *mon, const QDict *qdict)
1688
{
1689
    const char *aclname = qdict_get_str(qdict, "aclname");
1690
    qemu_acl *acl = find_acl(mon, aclname);
1691

    
1692
    if (acl) {
1693
        qemu_acl_reset(acl);
1694
        monitor_printf(mon, "acl: removed all rules\n");
1695
    }
1696
}
1697

    
1698
static void do_acl_policy(Monitor *mon, const QDict *qdict)
1699
{
1700
    const char *aclname = qdict_get_str(qdict, "aclname");
1701
    const char *policy = qdict_get_str(qdict, "policy");
1702
    qemu_acl *acl = find_acl(mon, aclname);
1703

    
1704
    if (acl) {
1705
        if (strcmp(policy, "allow") == 0) {
1706
            acl->defaultDeny = 0;
1707
            monitor_printf(mon, "acl: policy set to 'allow'\n");
1708
        } else if (strcmp(policy, "deny") == 0) {
1709
            acl->defaultDeny = 1;
1710
            monitor_printf(mon, "acl: policy set to 'deny'\n");
1711
        } else {
1712
            monitor_printf(mon, "acl: unknown policy '%s', "
1713
                           "expected 'deny' or 'allow'\n", policy);
1714
        }
1715
    }
1716
}
1717

    
1718
static void do_acl_add(Monitor *mon, const QDict *qdict)
1719
{
1720
    const char *aclname = qdict_get_str(qdict, "aclname");
1721
    const char *match = qdict_get_str(qdict, "match");
1722
    const char *policy = qdict_get_str(qdict, "policy");
1723
    int has_index = qdict_haskey(qdict, "index");
1724
    int index = qdict_get_try_int(qdict, "index", -1);
1725
    qemu_acl *acl = find_acl(mon, aclname);
1726
    int deny, ret;
1727

    
1728
    if (acl) {
1729
        if (strcmp(policy, "allow") == 0) {
1730
            deny = 0;
1731
        } else if (strcmp(policy, "deny") == 0) {
1732
            deny = 1;
1733
        } else {
1734
            monitor_printf(mon, "acl: unknown policy '%s', "
1735
                           "expected 'deny' or 'allow'\n", policy);
1736
            return;
1737
        }
1738
        if (has_index)
1739
            ret = qemu_acl_insert(acl, deny, match, index);
1740
        else
1741
            ret = qemu_acl_append(acl, deny, match);
1742
        if (ret < 0)
1743
            monitor_printf(mon, "acl: unable to add acl entry\n");
1744
        else
1745
            monitor_printf(mon, "acl: added rule at position %d\n", ret);
1746
    }
1747
}
1748

    
1749
static void do_acl_remove(Monitor *mon, const QDict *qdict)
1750
{
1751
    const char *aclname = qdict_get_str(qdict, "aclname");
1752
    const char *match = qdict_get_str(qdict, "match");
1753
    qemu_acl *acl = find_acl(mon, aclname);
1754
    int ret;
1755

    
1756
    if (acl) {
1757
        ret = qemu_acl_remove(acl, match);
1758
        if (ret < 0)
1759
            monitor_printf(mon, "acl: no matching acl entry\n");
1760
        else
1761
            monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1762
    }
1763
}
1764

    
1765
#if defined(TARGET_I386)
1766
static void do_inject_mce(Monitor *mon, const QDict *qdict)
1767
{
1768
    CPUState *cenv;
1769
    int cpu_index = qdict_get_int(qdict, "cpu_index");
1770
    int bank = qdict_get_int(qdict, "bank");
1771
    uint64_t status = qdict_get_int(qdict, "status");
1772
    uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1773
    uint64_t addr = qdict_get_int(qdict, "addr");
1774
    uint64_t misc = qdict_get_int(qdict, "misc");
1775

    
1776
    for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1777
        if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1778
            cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1779
            break;
1780
        }
1781
}
1782
#endif
1783

    
1784
static void do_getfd(Monitor *mon, const QDict *qdict)
1785
{
1786
    const char *fdname = qdict_get_str(qdict, "fdname");
1787
    mon_fd_t *monfd;
1788
    int fd;
1789

    
1790
    fd = qemu_chr_get_msgfd(mon->chr);
1791
    if (fd == -1) {
1792
        monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1793
        return;
1794
    }
1795

    
1796
    if (qemu_isdigit(fdname[0])) {
1797
        monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1798
        return;
1799
    }
1800

    
1801
    fd = dup(fd);
1802
    if (fd == -1) {
1803
        monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1804
                       strerror(errno));
1805
        return;
1806
    }
1807

    
1808
    QLIST_FOREACH(monfd, &mon->fds, next) {
1809
        if (strcmp(monfd->name, fdname) != 0) {
1810
            continue;
1811
        }
1812

    
1813
        close(monfd->fd);
1814
        monfd->fd = fd;
1815
        return;
1816
    }
1817

    
1818
    monfd = qemu_mallocz(sizeof(mon_fd_t));
1819
    monfd->name = qemu_strdup(fdname);
1820
    monfd->fd = fd;
1821

    
1822
    QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1823
}
1824

    
1825
static void do_closefd(Monitor *mon, const QDict *qdict)
1826
{
1827
    const char *fdname = qdict_get_str(qdict, "fdname");
1828
    mon_fd_t *monfd;
1829

    
1830
    QLIST_FOREACH(monfd, &mon->fds, next) {
1831
        if (strcmp(monfd->name, fdname) != 0) {
1832
            continue;
1833
        }
1834

    
1835
        QLIST_REMOVE(monfd, next);
1836
        close(monfd->fd);
1837
        qemu_free(monfd->name);
1838
        qemu_free(monfd);
1839
        return;
1840
    }
1841

    
1842
    monitor_printf(mon, "Failed to find file descriptor named %s\n",
1843
                   fdname);
1844
}
1845

    
1846
static void do_loadvm(Monitor *mon, const QDict *qdict)
1847
{
1848
    int saved_vm_running  = vm_running;
1849
    const char *name = qdict_get_str(qdict, "name");
1850

    
1851
    vm_stop(0);
1852

    
1853
    if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1854
        vm_start();
1855
}
1856

    
1857
int monitor_get_fd(Monitor *mon, const char *fdname)
1858
{
1859
    mon_fd_t *monfd;
1860

    
1861
    QLIST_FOREACH(monfd, &mon->fds, next) {
1862
        int fd;
1863

    
1864
        if (strcmp(monfd->name, fdname) != 0) {
1865
            continue;
1866
        }
1867

    
1868
        fd = monfd->fd;
1869

    
1870
        /* caller takes ownership of fd */
1871
        QLIST_REMOVE(monfd, next);
1872
        qemu_free(monfd->name);
1873
        qemu_free(monfd);
1874

    
1875
        return fd;
1876
    }
1877

    
1878
    return -1;
1879
}
1880

    
1881
static const mon_cmd_t mon_cmds[] = {
1882
#include "qemu-monitor.h"
1883
    { NULL, NULL, },
1884
};
1885

    
1886
/* Please update qemu-monitor.hx when adding or changing commands */
1887
static const mon_cmd_t info_cmds[] = {
1888
    {
1889
        .name       = "version",
1890
        .args_type  = "",
1891
        .params     = "",
1892
        .help       = "show the version of QEMU",
1893
        .user_print = monitor_print_qobject,
1894
        .mhandler.info_new = do_info_version,
1895
    },
1896
    {
1897
        .name       = "network",
1898
        .args_type  = "",
1899
        .params     = "",
1900
        .help       = "show the network state",
1901
        .mhandler.info = do_info_network,
1902
    },
1903
    {
1904
        .name       = "chardev",
1905
        .args_type  = "",
1906
        .params     = "",
1907
        .help       = "show the character devices",
1908
        .mhandler.info = qemu_chr_info,
1909
    },
1910
    {
1911
        .name       = "block",
1912
        .args_type  = "",
1913
        .params     = "",
1914
        .help       = "show the block devices",
1915
        .mhandler.info = bdrv_info,
1916
    },
1917
    {
1918
        .name       = "blockstats",
1919
        .args_type  = "",
1920
        .params     = "",
1921
        .help       = "show block device statistics",
1922
        .mhandler.info = bdrv_info_stats,
1923
    },
1924
    {
1925
        .name       = "registers",
1926
        .args_type  = "",
1927
        .params     = "",
1928
        .help       = "show the cpu registers",
1929
        .mhandler.info = do_info_registers,
1930
    },
1931
    {
1932
        .name       = "cpus",
1933
        .args_type  = "",
1934
        .params     = "",
1935
        .help       = "show infos for each CPU",
1936
        .mhandler.info = do_info_cpus,
1937
    },
1938
    {
1939
        .name       = "history",
1940
        .args_type  = "",
1941
        .params     = "",
1942
        .help       = "show the command line history",
1943
        .mhandler.info = do_info_history,
1944
    },
1945
    {
1946
        .name       = "irq",
1947
        .args_type  = "",
1948
        .params     = "",
1949
        .help       = "show the interrupts statistics (if available)",
1950
        .mhandler.info = irq_info,
1951
    },
1952
    {
1953
        .name       = "pic",
1954
        .args_type  = "",
1955
        .params     = "",
1956
        .help       = "show i8259 (PIC) state",
1957
        .mhandler.info = pic_info,
1958
    },
1959
    {
1960
        .name       = "pci",
1961
        .args_type  = "",
1962
        .params     = "",
1963
        .help       = "show PCI info",
1964
        .mhandler.info = pci_info,
1965
    },
1966
#if defined(TARGET_I386) || defined(TARGET_SH4)
1967
    {
1968
        .name       = "tlb",
1969
        .args_type  = "",
1970
        .params     = "",
1971
        .help       = "show virtual to physical memory mappings",
1972
        .mhandler.info = tlb_info,
1973
    },
1974
#endif
1975
#if defined(TARGET_I386)
1976
    {
1977
        .name       = "mem",
1978
        .args_type  = "",
1979
        .params     = "",
1980
        .help       = "show the active virtual memory mappings",
1981
        .mhandler.info = mem_info,
1982
    },
1983
    {
1984
        .name       = "hpet",
1985
        .args_type  = "",
1986
        .params     = "",
1987
        .help       = "show state of HPET",
1988
        .mhandler.info = do_info_hpet,
1989
    },
1990
#endif
1991
    {
1992
        .name       = "jit",
1993
        .args_type  = "",
1994
        .params     = "",
1995
        .help       = "show dynamic compiler info",
1996
        .mhandler.info = do_info_jit,
1997
    },
1998
    {
1999
        .name       = "kvm",
2000
        .args_type  = "",
2001
        .params     = "",
2002
        .help       = "show KVM information",
2003
        .mhandler.info = do_info_kvm,
2004
    },
2005
    {
2006
        .name       = "numa",
2007
        .args_type  = "",
2008
        .params     = "",
2009
        .help       = "show NUMA information",
2010
        .mhandler.info = do_info_numa,
2011
    },
2012
    {
2013
        .name       = "usb",
2014
        .args_type  = "",
2015
        .params     = "",
2016
        .help       = "show guest USB devices",
2017
        .mhandler.info = usb_info,
2018
    },
2019
    {
2020
        .name       = "usbhost",
2021
        .args_type  = "",
2022
        .params     = "",
2023
        .help       = "show host USB devices",
2024
        .mhandler.info = usb_host_info,
2025
    },
2026
    {
2027
        .name       = "profile",
2028
        .args_type  = "",
2029
        .params     = "",
2030
        .help       = "show profiling information",
2031
        .mhandler.info = do_info_profile,
2032
    },
2033
    {
2034
        .name       = "capture",
2035
        .args_type  = "",
2036
        .params     = "",
2037
        .help       = "show capture information",
2038
        .mhandler.info = do_info_capture,
2039
    },
2040
    {
2041
        .name       = "snapshots",
2042
        .args_type  = "",
2043
        .params     = "",
2044
        .help       = "show the currently saved VM snapshots",
2045
        .mhandler.info = do_info_snapshots,
2046
    },
2047
    {
2048
        .name       = "status",
2049
        .args_type  = "",
2050
        .params     = "",
2051
        .help       = "show the current VM status (running|paused)",
2052
        .mhandler.info = do_info_status,
2053
    },
2054
    {
2055
        .name       = "pcmcia",
2056
        .args_type  = "",
2057
        .params     = "",
2058
        .help       = "show guest PCMCIA status",
2059
        .mhandler.info = pcmcia_info,
2060
    },
2061
    {
2062
        .name       = "mice",
2063
        .args_type  = "",
2064
        .params     = "",
2065
        .help       = "show which guest mouse is receiving events",
2066
        .mhandler.info = do_info_mice,
2067
    },
2068
    {
2069
        .name       = "vnc",
2070
        .args_type  = "",
2071
        .params     = "",
2072
        .help       = "show the vnc server status",
2073
        .mhandler.info = do_info_vnc,
2074
    },
2075
    {
2076
        .name       = "name",
2077
        .args_type  = "",
2078
        .params     = "",
2079
        .help       = "show the current VM name",
2080
        .mhandler.info = do_info_name,
2081
    },
2082
    {
2083
        .name       = "uuid",
2084
        .args_type  = "",
2085
        .params     = "",
2086
        .help       = "show the current VM UUID",
2087
        .mhandler.info = do_info_uuid,
2088
    },
2089
#if defined(TARGET_PPC)
2090
    {
2091
        .name       = "cpustats",
2092
        .args_type  = "",
2093
        .params     = "",
2094
        .help       = "show CPU statistics",
2095
        .mhandler.info = do_info_cpu_stats,
2096
    },
2097
#endif
2098
#if defined(CONFIG_SLIRP)
2099
    {
2100
        .name       = "usernet",
2101
        .args_type  = "",
2102
        .params     = "",
2103
        .help       = "show user network stack connection states",
2104
        .mhandler.info = do_info_usernet,
2105
    },
2106
#endif
2107
    {
2108
        .name       = "migrate",
2109
        .args_type  = "",
2110
        .params     = "",
2111
        .help       = "show migration status",
2112
        .mhandler.info = do_info_migrate,
2113
    },
2114
    {
2115
        .name       = "balloon",
2116
        .args_type  = "",
2117
        .params     = "",
2118
        .help       = "show balloon information",
2119
        .user_print = monitor_print_balloon,
2120
        .mhandler.info_new = do_info_balloon,
2121
    },
2122
    {
2123
        .name       = "qtree",
2124
        .args_type  = "",
2125
        .params     = "",
2126
        .help       = "show device tree",
2127
        .mhandler.info = do_info_qtree,
2128
    },
2129
    {
2130
        .name       = "qdm",
2131
        .args_type  = "",
2132
        .params     = "",
2133
        .help       = "show qdev device model list",
2134
        .mhandler.info = do_info_qdm,
2135
    },
2136
    {
2137
        .name       = "roms",
2138
        .args_type  = "",
2139
        .params     = "",
2140
        .help       = "show roms",
2141
        .mhandler.info = do_info_roms,
2142
    },
2143
    {
2144
        .name       = NULL,
2145
    },
2146
};
2147

    
2148
/*******************************************************************/
2149

    
2150
static const char *pch;
2151
static jmp_buf expr_env;
2152

    
2153
#define MD_TLONG 0
2154
#define MD_I32   1
2155

    
2156
typedef struct MonitorDef {
2157
    const char *name;
2158
    int offset;
2159
    target_long (*get_value)(const struct MonitorDef *md, int val);
2160
    int type;
2161
} MonitorDef;
2162

    
2163
#if defined(TARGET_I386)
2164
static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2165
{
2166
    CPUState *env = mon_get_cpu();
2167
    if (!env)
2168
        return 0;
2169
    return env->eip + env->segs[R_CS].base;
2170
}
2171
#endif
2172

    
2173
#if defined(TARGET_PPC)
2174
static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2175
{
2176
    CPUState *env = mon_get_cpu();
2177
    unsigned int u;
2178
    int i;
2179

    
2180
    if (!env)
2181
        return 0;
2182

    
2183
    u = 0;
2184
    for (i = 0; i < 8; i++)
2185
        u |= env->crf[i] << (32 - (4 * i));
2186

    
2187
    return u;
2188
}
2189

    
2190
static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2191
{
2192
    CPUState *env = mon_get_cpu();
2193
    if (!env)
2194
        return 0;
2195
    return env->msr;
2196
}
2197

    
2198
static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2199
{
2200
    CPUState *env = mon_get_cpu();
2201
    if (!env)
2202
        return 0;
2203
    return env->xer;
2204
}
2205

    
2206
static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2207
{
2208
    CPUState *env = mon_get_cpu();
2209
    if (!env)
2210
        return 0;
2211
    return cpu_ppc_load_decr(env);
2212
}
2213

    
2214
static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2215
{
2216
    CPUState *env = mon_get_cpu();
2217
    if (!env)
2218
        return 0;
2219
    return cpu_ppc_load_tbu(env);
2220
}
2221

    
2222
static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2223
{
2224
    CPUState *env = mon_get_cpu();
2225
    if (!env)
2226
        return 0;
2227
    return cpu_ppc_load_tbl(env);
2228
}
2229
#endif
2230

    
2231
#if defined(TARGET_SPARC)
2232
#ifndef TARGET_SPARC64
2233
static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2234
{
2235
    CPUState *env = mon_get_cpu();
2236
    if (!env)
2237
        return 0;
2238
    return GET_PSR(env);
2239
}
2240
#endif
2241

    
2242
static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2243
{
2244
    CPUState *env = mon_get_cpu();
2245
    if (!env)
2246
        return 0;
2247
    return env->regwptr[val];
2248
}
2249
#endif
2250

    
2251
static const MonitorDef monitor_defs[] = {
2252
#ifdef TARGET_I386
2253

    
2254
#define SEG(name, seg) \
2255
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2256
    { name ".base", offsetof(CPUState, segs[seg].base) },\
2257
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2258

    
2259
    { "eax", offsetof(CPUState, regs[0]) },
2260
    { "ecx", offsetof(CPUState, regs[1]) },
2261
    { "edx", offsetof(CPUState, regs[2]) },
2262
    { "ebx", offsetof(CPUState, regs[3]) },
2263
    { "esp|sp", offsetof(CPUState, regs[4]) },
2264
    { "ebp|fp", offsetof(CPUState, regs[5]) },
2265
    { "esi", offsetof(CPUState, regs[6]) },
2266
    { "edi", offsetof(CPUState, regs[7]) },
2267
#ifdef TARGET_X86_64
2268
    { "r8", offsetof(CPUState, regs[8]) },
2269
    { "r9", offsetof(CPUState, regs[9]) },
2270
    { "r10", offsetof(CPUState, regs[10]) },
2271
    { "r11", offsetof(CPUState, regs[11]) },
2272
    { "r12", offsetof(CPUState, regs[12]) },
2273
    { "r13", offsetof(CPUState, regs[13]) },
2274
    { "r14", offsetof(CPUState, regs[14]) },
2275
    { "r15", offsetof(CPUState, regs[15]) },
2276
#endif
2277
    { "eflags", offsetof(CPUState, eflags) },
2278
    { "eip", offsetof(CPUState, eip) },
2279
    SEG("cs", R_CS)
2280
    SEG("ds", R_DS)
2281
    SEG("es", R_ES)
2282
    SEG("ss", R_SS)
2283
    SEG("fs", R_FS)
2284
    SEG("gs", R_GS)
2285
    { "pc", 0, monitor_get_pc, },
2286
#elif defined(TARGET_PPC)
2287
    /* General purpose registers */
2288
    { "r0", offsetof(CPUState, gpr[0]) },
2289
    { "r1", offsetof(CPUState, gpr[1]) },
2290
    { "r2", offsetof(CPUState, gpr[2]) },
2291
    { "r3", offsetof(CPUState, gpr[3]) },
2292
    { "r4", offsetof(CPUState, gpr[4]) },
2293
    { "r5", offsetof(CPUState, gpr[5]) },
2294
    { "r6", offsetof(CPUState, gpr[6]) },
2295
    { "r7", offsetof(CPUState, gpr[7]) },
2296
    { "r8", offsetof(CPUState, gpr[8]) },
2297
    { "r9", offsetof(CPUState, gpr[9]) },
2298
    { "r10", offsetof(CPUState, gpr[10]) },
2299
    { "r11", offsetof(CPUState, gpr[11]) },
2300
    { "r12", offsetof(CPUState, gpr[12]) },
2301
    { "r13", offsetof(CPUState, gpr[13]) },
2302
    { "r14", offsetof(CPUState, gpr[14]) },
2303
    { "r15", offsetof(CPUState, gpr[15]) },
2304
    { "r16", offsetof(CPUState, gpr[16]) },
2305
    { "r17", offsetof(CPUState, gpr[17]) },
2306
    { "r18", offsetof(CPUState, gpr[18]) },
2307
    { "r19", offsetof(CPUState, gpr[19]) },
2308
    { "r20", offsetof(CPUState, gpr[20]) },
2309
    { "r21", offsetof(CPUState, gpr[21]) },
2310
    { "r22", offsetof(CPUState, gpr[22]) },
2311
    { "r23", offsetof(CPUState, gpr[23]) },
2312
    { "r24", offsetof(CPUState, gpr[24]) },
2313
    { "r25", offsetof(CPUState, gpr[25]) },
2314
    { "r26", offsetof(CPUState, gpr[26]) },
2315
    { "r27", offsetof(CPUState, gpr[27]) },
2316
    { "r28", offsetof(CPUState, gpr[28]) },
2317
    { "r29", offsetof(CPUState, gpr[29]) },
2318
    { "r30", offsetof(CPUState, gpr[30]) },
2319
    { "r31", offsetof(CPUState, gpr[31]) },
2320
    /* Floating point registers */
2321
    { "f0", offsetof(CPUState, fpr[0]) },
2322
    { "f1", offsetof(CPUState, fpr[1]) },
2323
    { "f2", offsetof(CPUState, fpr[2]) },
2324
    { "f3", offsetof(CPUState, fpr[3]) },
2325
    { "f4", offsetof(CPUState, fpr[4]) },
2326
    { "f5", offsetof(CPUState, fpr[5]) },
2327
    { "f6", offsetof(CPUState, fpr[6]) },
2328
    { "f7", offsetof(CPUState, fpr[7]) },
2329
    { "f8", offsetof(CPUState, fpr[8]) },
2330
    { "f9", offsetof(CPUState, fpr[9]) },
2331
    { "f10", offsetof(CPUState, fpr[10]) },
2332
    { "f11", offsetof(CPUState, fpr[11]) },
2333
    { "f12", offsetof(CPUState, fpr[12]) },
2334
    { "f13", offsetof(CPUState, fpr[13]) },
2335
    { "f14", offsetof(CPUState, fpr[14]) },
2336
    { "f15", offsetof(CPUState, fpr[15]) },
2337
    { "f16", offsetof(CPUState, fpr[16]) },
2338
    { "f17", offsetof(CPUState, fpr[17]) },
2339
    { "f18", offsetof(CPUState, fpr[18]) },
2340
    { "f19", offsetof(CPUState, fpr[19]) },
2341
    { "f20", offsetof(CPUState, fpr[20]) },
2342
    { "f21", offsetof(CPUState, fpr[21]) },
2343
    { "f22", offsetof(CPUState, fpr[22]) },
2344
    { "f23", offsetof(CPUState, fpr[23]) },
2345
    { "f24", offsetof(CPUState, fpr[24]) },
2346
    { "f25", offsetof(CPUState, fpr[25]) },
2347
    { "f26", offsetof(CPUState, fpr[26]) },
2348
    { "f27", offsetof(CPUState, fpr[27]) },
2349
    { "f28", offsetof(CPUState, fpr[28]) },
2350
    { "f29", offsetof(CPUState, fpr[29]) },
2351
    { "f30", offsetof(CPUState, fpr[30]) },
2352
    { "f31", offsetof(CPUState, fpr[31]) },
2353
    { "fpscr", offsetof(CPUState, fpscr) },
2354
    /* Next instruction pointer */
2355
    { "nip|pc", offsetof(CPUState, nip) },
2356
    { "lr", offsetof(CPUState, lr) },
2357
    { "ctr", offsetof(CPUState, ctr) },
2358
    { "decr", 0, &monitor_get_decr, },
2359
    { "ccr", 0, &monitor_get_ccr, },
2360
    /* Machine state register */
2361
    { "msr", 0, &monitor_get_msr, },
2362
    { "xer", 0, &monitor_get_xer, },
2363
    { "tbu", 0, &monitor_get_tbu, },
2364
    { "tbl", 0, &monitor_get_tbl, },
2365
#if defined(TARGET_PPC64)
2366
    /* Address space register */
2367
    { "asr", offsetof(CPUState, asr) },
2368
#endif
2369
    /* Segment registers */
2370
    { "sdr1", offsetof(CPUState, sdr1) },
2371
    { "sr0", offsetof(CPUState, sr[0]) },
2372
    { "sr1", offsetof(CPUState, sr[1]) },
2373
    { "sr2", offsetof(CPUState, sr[2]) },
2374
    { "sr3", offsetof(CPUState, sr[3]) },
2375
    { "sr4", offsetof(CPUState, sr[4]) },
2376
    { "sr5", offsetof(CPUState, sr[5]) },
2377
    { "sr6", offsetof(CPUState, sr[6]) },
2378
    { "sr7", offsetof(CPUState, sr[7]) },
2379
    { "sr8", offsetof(CPUState, sr[8]) },
2380
    { "sr9", offsetof(CPUState, sr[9]) },
2381
    { "sr10", offsetof(CPUState, sr[10]) },
2382
    { "sr11", offsetof(CPUState, sr[11]) },
2383
    { "sr12", offsetof(CPUState, sr[12]) },
2384
    { "sr13", offsetof(CPUState, sr[13]) },
2385
    { "sr14", offsetof(CPUState, sr[14]) },
2386
    { "sr15", offsetof(CPUState, sr[15]) },
2387
    /* Too lazy to put BATs and SPRs ... */
2388
#elif defined(TARGET_SPARC)
2389
    { "g0", offsetof(CPUState, gregs[0]) },
2390
    { "g1", offsetof(CPUState, gregs[1]) },
2391
    { "g2", offsetof(CPUState, gregs[2]) },
2392
    { "g3", offsetof(CPUState, gregs[3]) },
2393
    { "g4", offsetof(CPUState, gregs[4]) },
2394
    { "g5", offsetof(CPUState, gregs[5]) },
2395
    { "g6", offsetof(CPUState, gregs[6]) },
2396
    { "g7", offsetof(CPUState, gregs[7]) },
2397
    { "o0", 0, monitor_get_reg },
2398
    { "o1", 1, monitor_get_reg },
2399
    { "o2", 2, monitor_get_reg },
2400
    { "o3", 3, monitor_get_reg },
2401
    { "o4", 4, monitor_get_reg },
2402
    { "o5", 5, monitor_get_reg },
2403
    { "o6", 6, monitor_get_reg },
2404
    { "o7", 7, monitor_get_reg },
2405
    { "l0", 8, monitor_get_reg },
2406
    { "l1", 9, monitor_get_reg },
2407
    { "l2", 10, monitor_get_reg },
2408
    { "l3", 11, monitor_get_reg },
2409
    { "l4", 12, monitor_get_reg },
2410
    { "l5", 13, monitor_get_reg },
2411
    { "l6", 14, monitor_get_reg },
2412
    { "l7", 15, monitor_get_reg },
2413
    { "i0", 16, monitor_get_reg },
2414
    { "i1", 17, monitor_get_reg },
2415
    { "i2", 18, monitor_get_reg },
2416
    { "i3", 19, monitor_get_reg },
2417
    { "i4", 20, monitor_get_reg },
2418
    { "i5", 21, monitor_get_reg },
2419
    { "i6", 22, monitor_get_reg },
2420
    { "i7", 23, monitor_get_reg },
2421
    { "pc", offsetof(CPUState, pc) },
2422
    { "npc", offsetof(CPUState, npc) },
2423
    { "y", offsetof(CPUState, y) },
2424
#ifndef TARGET_SPARC64
2425
    { "psr", 0, &monitor_get_psr, },
2426
    { "wim", offsetof(CPUState, wim) },
2427
#endif
2428
    { "tbr", offsetof(CPUState, tbr) },
2429
    { "fsr", offsetof(CPUState, fsr) },
2430
    { "f0", offsetof(CPUState, fpr[0]) },
2431
    { "f1", offsetof(CPUState, fpr[1]) },
2432
    { "f2", offsetof(CPUState, fpr[2]) },
2433
    { "f3", offsetof(CPUState, fpr[3]) },
2434
    { "f4", offsetof(CPUState, fpr[4]) },
2435
    { "f5", offsetof(CPUState, fpr[5]) },
2436
    { "f6", offsetof(CPUState, fpr[6]) },
2437
    { "f7", offsetof(CPUState, fpr[7]) },
2438
    { "f8", offsetof(CPUState, fpr[8]) },
2439
    { "f9", offsetof(CPUState, fpr[9]) },
2440
    { "f10", offsetof(CPUState, fpr[10]) },
2441
    { "f11", offsetof(CPUState, fpr[11]) },
2442
    { "f12", offsetof(CPUState, fpr[12]) },
2443
    { "f13", offsetof(CPUState, fpr[13]) },
2444
    { "f14", offsetof(CPUState, fpr[14]) },
2445
    { "f15", offsetof(CPUState, fpr[15]) },
2446
    { "f16", offsetof(CPUState, fpr[16]) },
2447
    { "f17", offsetof(CPUState, fpr[17]) },
2448
    { "f18", offsetof(CPUState, fpr[18]) },
2449
    { "f19", offsetof(CPUState, fpr[19]) },
2450
    { "f20", offsetof(CPUState, fpr[20]) },
2451
    { "f21", offsetof(CPUState, fpr[21]) },
2452
    { "f22", offsetof(CPUState, fpr[22]) },
2453
    { "f23", offsetof(CPUState, fpr[23]) },
2454
    { "f24", offsetof(CPUState, fpr[24]) },
2455
    { "f25", offsetof(CPUState, fpr[25]) },
2456
    { "f26", offsetof(CPUState, fpr[26]) },
2457
    { "f27", offsetof(CPUState, fpr[27]) },
2458
    { "f28", offsetof(CPUState, fpr[28]) },
2459
    { "f29", offsetof(CPUState, fpr[29]) },
2460
    { "f30", offsetof(CPUState, fpr[30]) },
2461
    { "f31", offsetof(CPUState, fpr[31]) },
2462
#ifdef TARGET_SPARC64
2463
    { "f32", offsetof(CPUState, fpr[32]) },
2464
    { "f34", offsetof(CPUState, fpr[34]) },
2465
    { "f36", offsetof(CPUState, fpr[36]) },
2466
    { "f38", offsetof(CPUState, fpr[38]) },
2467
    { "f40", offsetof(CPUState, fpr[40]) },
2468
    { "f42", offsetof(CPUState, fpr[42]) },
2469
    { "f44", offsetof(CPUState, fpr[44]) },
2470
    { "f46", offsetof(CPUState, fpr[46]) },
2471
    { "f48", offsetof(CPUState, fpr[48]) },
2472
    { "f50", offsetof(CPUState, fpr[50]) },
2473
    { "f52", offsetof(CPUState, fpr[52]) },
2474
    { "f54", offsetof(CPUState, fpr[54]) },
2475
    { "f56", offsetof(CPUState, fpr[56]) },
2476
    { "f58", offsetof(CPUState, fpr[58]) },
2477
    { "f60", offsetof(CPUState, fpr[60]) },
2478
    { "f62", offsetof(CPUState, fpr[62]) },
2479
    { "asi", offsetof(CPUState, asi) },
2480
    { "pstate", offsetof(CPUState, pstate) },
2481
    { "cansave", offsetof(CPUState, cansave) },
2482
    { "canrestore", offsetof(CPUState, canrestore) },
2483
    { "otherwin", offsetof(CPUState, otherwin) },
2484
    { "wstate", offsetof(CPUState, wstate) },
2485
    { "cleanwin", offsetof(CPUState, cleanwin) },
2486
    { "fprs", offsetof(CPUState, fprs) },
2487
#endif
2488
#endif
2489
    { NULL },
2490
};
2491

    
2492
static void expr_error(Monitor *mon, const char *msg)
2493
{
2494
    monitor_printf(mon, "%s\n", msg);
2495
    longjmp(expr_env, 1);
2496
}
2497

    
2498
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
2499
static int get_monitor_def(target_long *pval, const char *name)
2500
{
2501
    const MonitorDef *md;
2502
    void *ptr;
2503

    
2504
    for(md = monitor_defs; md->name != NULL; md++) {
2505
        if (compare_cmd(name, md->name)) {
2506
            if (md->get_value) {
2507
                *pval = md->get_value(md, md->offset);
2508
            } else {
2509
                CPUState *env = mon_get_cpu();
2510
                if (!env)
2511
                    return -2;
2512
                ptr = (uint8_t *)env + md->offset;
2513
                switch(md->type) {
2514
                case MD_I32:
2515
                    *pval = *(int32_t *)ptr;
2516
                    break;
2517
                case MD_TLONG:
2518
                    *pval = *(target_long *)ptr;
2519
                    break;
2520
                default:
2521
                    *pval = 0;
2522
                    break;
2523
                }
2524
            }
2525
            return 0;
2526
        }
2527
    }
2528
    return -1;
2529
}
2530

    
2531
static void next(void)
2532
{
2533
    if (*pch != '\0') {
2534
        pch++;
2535
        while (qemu_isspace(*pch))
2536
            pch++;
2537
    }
2538
}
2539

    
2540
static int64_t expr_sum(Monitor *mon);
2541

    
2542
static int64_t expr_unary(Monitor *mon)
2543
{
2544
    int64_t n;
2545
    char *p;
2546
    int ret;
2547

    
2548
    switch(*pch) {
2549
    case '+':
2550
        next();
2551
        n = expr_unary(mon);
2552
        break;
2553
    case '-':
2554
        next();
2555
        n = -expr_unary(mon);
2556
        break;
2557
    case '~':
2558
        next();
2559
        n = ~expr_unary(mon);
2560
        break;
2561
    case '(':
2562
        next();
2563
        n = expr_sum(mon);
2564
        if (*pch != ')') {
2565
            expr_error(mon, "')' expected");
2566
        }
2567
        next();
2568
        break;
2569
    case '\'':
2570
        pch++;
2571
        if (*pch == '\0')
2572
            expr_error(mon, "character constant expected");
2573
        n = *pch;
2574
        pch++;
2575
        if (*pch != '\'')
2576
            expr_error(mon, "missing terminating \' character");
2577
        next();
2578
        break;
2579
    case '$':
2580
        {
2581
            char buf[128], *q;
2582
            target_long reg=0;
2583

    
2584
            pch++;
2585
            q = buf;
2586
            while ((*pch >= 'a' && *pch <= 'z') ||
2587
                   (*pch >= 'A' && *pch <= 'Z') ||
2588
                   (*pch >= '0' && *pch <= '9') ||
2589
                   *pch == '_' || *pch == '.') {
2590
                if ((q - buf) < sizeof(buf) - 1)
2591
                    *q++ = *pch;
2592
                pch++;
2593
            }
2594
            while (qemu_isspace(*pch))
2595
                pch++;
2596
            *q = 0;
2597
            ret = get_monitor_def(&reg, buf);
2598
            if (ret == -1)
2599
                expr_error(mon, "unknown register");
2600
            else if (ret == -2)
2601
                expr_error(mon, "no cpu defined");
2602
            n = reg;
2603
        }
2604
        break;
2605
    case '\0':
2606
        expr_error(mon, "unexpected end of expression");
2607
        n = 0;
2608
        break;
2609
    default:
2610
#if TARGET_PHYS_ADDR_BITS > 32
2611
        n = strtoull(pch, &p, 0);
2612
#else
2613
        n = strtoul(pch, &p, 0);
2614
#endif
2615
        if (pch == p) {
2616
            expr_error(mon, "invalid char in expression");
2617
        }
2618
        pch = p;
2619
        while (qemu_isspace(*pch))
2620
            pch++;
2621
        break;
2622
    }
2623
    return n;
2624
}
2625

    
2626

    
2627
static int64_t expr_prod(Monitor *mon)
2628
{
2629
    int64_t val, val2;
2630
    int op;
2631

    
2632
    val = expr_unary(mon);
2633
    for(;;) {
2634
        op = *pch;
2635
        if (op != '*' && op != '/' && op != '%')
2636
            break;
2637
        next();
2638
        val2 = expr_unary(mon);
2639
        switch(op) {
2640
        default:
2641
        case '*':
2642
            val *= val2;
2643
            break;
2644
        case '/':
2645
        case '%':
2646
            if (val2 == 0)
2647
                expr_error(mon, "division by zero");
2648
            if (op == '/')
2649
                val /= val2;
2650
            else
2651
                val %= val2;
2652
            break;
2653
        }
2654
    }
2655
    return val;
2656
}
2657

    
2658
static int64_t expr_logic(Monitor *mon)
2659
{
2660
    int64_t val, val2;
2661
    int op;
2662

    
2663
    val = expr_prod(mon);
2664
    for(;;) {
2665
        op = *pch;
2666
        if (op != '&' && op != '|' && op != '^')
2667
            break;
2668
        next();
2669
        val2 = expr_prod(mon);
2670
        switch(op) {
2671
        default:
2672
        case '&':
2673
            val &= val2;
2674
            break;
2675
        case '|':
2676
            val |= val2;
2677
            break;
2678
        case '^':
2679
            val ^= val2;
2680
            break;
2681
        }
2682
    }
2683
    return val;
2684
}
2685

    
2686
static int64_t expr_sum(Monitor *mon)
2687
{
2688
    int64_t val, val2;
2689
    int op;
2690

    
2691
    val = expr_logic(mon);
2692
    for(;;) {
2693
        op = *pch;
2694
        if (op != '+' && op != '-')
2695
            break;
2696
        next();
2697
        val2 = expr_logic(mon);
2698
        if (op == '+')
2699
            val += val2;
2700
        else
2701
            val -= val2;
2702
    }
2703
    return val;
2704
}
2705

    
2706
static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2707
{
2708
    pch = *pp;
2709
    if (setjmp(expr_env)) {
2710
        *pp = pch;
2711
        return -1;
2712
    }
2713
    while (qemu_isspace(*pch))
2714
        pch++;
2715
    *pval = expr_sum(mon);
2716
    *pp = pch;
2717
    return 0;
2718
}
2719

    
2720
static int get_str(char *buf, int buf_size, const char **pp)
2721
{
2722
    const char *p;
2723
    char *q;
2724
    int c;
2725

    
2726
    q = buf;
2727
    p = *pp;
2728
    while (qemu_isspace(*p))
2729
        p++;
2730
    if (*p == '\0') {
2731
    fail:
2732
        *q = '\0';
2733
        *pp = p;
2734
        return -1;
2735
    }
2736
    if (*p == '\"') {
2737
        p++;
2738
        while (*p != '\0' && *p != '\"') {
2739
            if (*p == '\\') {
2740
                p++;
2741
                c = *p++;
2742
                switch(c) {
2743
                case 'n':
2744
                    c = '\n';
2745
                    break;
2746
                case 'r':
2747
                    c = '\r';
2748
                    break;
2749
                case '\\':
2750
                case '\'':
2751
                case '\"':
2752
                    break;
2753
                default:
2754
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
2755
                    goto fail;
2756
                }
2757
                if ((q - buf) < buf_size - 1) {
2758
                    *q++ = c;
2759
                }
2760
            } else {
2761
                if ((q - buf) < buf_size - 1) {
2762
                    *q++ = *p;
2763
                }
2764
                p++;
2765
            }
2766
        }
2767
        if (*p != '\"') {
2768
            qemu_printf("unterminated string\n");
2769
            goto fail;
2770
        }
2771
        p++;
2772
    } else {
2773
        while (*p != '\0' && !qemu_isspace(*p)) {
2774
            if ((q - buf) < buf_size - 1) {
2775
                *q++ = *p;
2776
            }
2777
            p++;
2778
        }
2779
    }
2780
    *q = '\0';
2781
    *pp = p;
2782
    return 0;
2783
}
2784

    
2785
/*
2786
 * Store the command-name in cmdname, and return a pointer to
2787
 * the remaining of the command string.
2788
 */
2789
static const char *get_command_name(const char *cmdline,
2790
                                    char *cmdname, size_t nlen)
2791
{
2792
    size_t len;
2793
    const char *p, *pstart;
2794

    
2795
    p = cmdline;
2796
    while (qemu_isspace(*p))
2797
        p++;
2798
    if (*p == '\0')
2799
        return NULL;
2800
    pstart = p;
2801
    while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2802
        p++;
2803
    len = p - pstart;
2804
    if (len > nlen - 1)
2805
        len = nlen - 1;
2806
    memcpy(cmdname, pstart, len);
2807
    cmdname[len] = '\0';
2808
    return p;
2809
}
2810

    
2811
/**
2812
 * Read key of 'type' into 'key' and return the current
2813
 * 'type' pointer.
2814
 */
2815
static char *key_get_info(const char *type, char **key)
2816
{
2817
    size_t len;
2818
    char *p, *str;
2819

    
2820
    if (*type == ',')
2821
        type++;
2822

    
2823
    p = strchr(type, ':');
2824
    if (!p) {
2825
        *key = NULL;
2826
        return NULL;
2827
    }
2828
    len = p - type;
2829

    
2830
    str = qemu_malloc(len + 1);
2831
    memcpy(str, type, len);
2832
    str[len] = '\0';
2833

    
2834
    *key = str;
2835
    return ++p;
2836
}
2837

    
2838
static int default_fmt_format = 'x';
2839
static int default_fmt_size = 4;
2840

    
2841
#define MAX_ARGS 16
2842

    
2843
static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2844
                                              const char *cmdline,
2845
                                              QDict *qdict)
2846
{
2847
    const char *p, *typestr;
2848
    int c;
2849
    const mon_cmd_t *cmd;
2850
    char cmdname[256];
2851
    char buf[1024];
2852
    char *key;
2853

    
2854
#ifdef DEBUG
2855
    monitor_printf(mon, "command='%s'\n", cmdline);
2856
#endif
2857

    
2858
    /* extract the command name */
2859
    p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2860
    if (!p)
2861
        return NULL;
2862

    
2863
    /* find the command */
2864
    for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2865
        if (compare_cmd(cmdname, cmd->name))
2866
            break;
2867
    }
2868

    
2869
    if (cmd->name == NULL) {
2870
        monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2871
        return NULL;
2872
    }
2873

    
2874
    /* parse the parameters */
2875
    typestr = cmd->args_type;
2876
    for(;;) {
2877
        typestr = key_get_info(typestr, &key);
2878
        if (!typestr)
2879
            break;
2880
        c = *typestr;
2881
        typestr++;
2882
        switch(c) {
2883
        case 'F':
2884
        case 'B':
2885
        case 's':
2886
            {
2887
                int ret;
2888

    
2889
                while (qemu_isspace(*p))
2890
                    p++;
2891
                if (*typestr == '?') {
2892
                    typestr++;
2893
                    if (*p == '\0') {
2894
                        /* no optional string: NULL argument */
2895
                        break;
2896
                    }
2897
                }
2898
                ret = get_str(buf, sizeof(buf), &p);
2899
                if (ret < 0) {
2900
                    switch(c) {
2901
                    case 'F':
2902
                        monitor_printf(mon, "%s: filename expected\n",
2903
                                       cmdname);
2904
                        break;
2905
                    case 'B':
2906
                        monitor_printf(mon, "%s: block device name expected\n",
2907
                                       cmdname);
2908
                        break;
2909
                    default:
2910
                        monitor_printf(mon, "%s: string expected\n", cmdname);
2911
                        break;
2912
                    }
2913
                    goto fail;
2914
                }
2915
                qdict_put(qdict, key, qstring_from_str(buf));
2916
            }
2917
            break;
2918
        case '/':
2919
            {
2920
                int count, format, size;
2921

    
2922
                while (qemu_isspace(*p))
2923
                    p++;
2924
                if (*p == '/') {
2925
                    /* format found */
2926
                    p++;
2927
                    count = 1;
2928
                    if (qemu_isdigit(*p)) {
2929
                        count = 0;
2930
                        while (qemu_isdigit(*p)) {
2931
                            count = count * 10 + (*p - '0');
2932
                            p++;
2933
                        }
2934
                    }
2935
                    size = -1;
2936
                    format = -1;
2937
                    for(;;) {
2938
                        switch(*p) {
2939
                        case 'o':
2940
                        case 'd':
2941
                        case 'u':
2942
                        case 'x':
2943
                        case 'i':
2944
                        case 'c':
2945
                            format = *p++;
2946
                            break;
2947
                        case 'b':
2948
                            size = 1;
2949
                            p++;
2950
                            break;
2951
                        case 'h':
2952
                            size = 2;
2953
                            p++;
2954
                            break;
2955
                        case 'w':
2956
                            size = 4;
2957
                            p++;
2958
                            break;
2959
                        case 'g':
2960
                        case 'L':
2961
                            size = 8;
2962
                            p++;
2963
                            break;
2964
                        default:
2965
                            goto next;
2966
                        }
2967
                    }
2968
                next:
2969
                    if (*p != '\0' && !qemu_isspace(*p)) {
2970
                        monitor_printf(mon, "invalid char in format: '%c'\n",
2971
                                       *p);
2972
                        goto fail;
2973
                    }
2974
                    if (format < 0)
2975
                        format = default_fmt_format;
2976
                    if (format != 'i') {
2977
                        /* for 'i', not specifying a size gives -1 as size */
2978
                        if (size < 0)
2979
                            size = default_fmt_size;
2980
                        default_fmt_size = size;
2981
                    }
2982
                    default_fmt_format = format;
2983
                } else {
2984
                    count = 1;
2985
                    format = default_fmt_format;
2986
                    if (format != 'i') {
2987
                        size = default_fmt_size;
2988
                    } else {
2989
                        size = -1;
2990
                    }
2991
                }
2992
                qdict_put(qdict, "count", qint_from_int(count));
2993
                qdict_put(qdict, "format", qint_from_int(format));
2994
                qdict_put(qdict, "size", qint_from_int(size));
2995
            }
2996
            break;
2997
        case 'i':
2998
        case 'l':
2999
            {
3000
                int64_t val;
3001

    
3002
                while (qemu_isspace(*p))
3003
                    p++;
3004
                if (*typestr == '?' || *typestr == '.') {
3005
                    if (*typestr == '?') {
3006
                        if (*p == '\0') {
3007
                            typestr++;
3008
                            break;
3009
                        }
3010
                    } else {
3011
                        if (*p == '.') {
3012
                            p++;
3013
                            while (qemu_isspace(*p))
3014
                                p++;
3015
                        } else {
3016
                            typestr++;
3017
                            break;
3018
                        }
3019
                    }
3020
                    typestr++;
3021
                }
3022
                if (get_expr(mon, &val, &p))
3023
                    goto fail;
3024
                /* Check if 'i' is greater than 32-bit */
3025
                if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3026
                    monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3027
                    monitor_printf(mon, "integer is for 32-bit values\n");
3028
                    goto fail;
3029
                }
3030
                qdict_put(qdict, key, qint_from_int(val));
3031
            }
3032
            break;
3033
        case '-':
3034
            {
3035
                int has_option;
3036
                /* option */
3037

    
3038
                c = *typestr++;
3039
                if (c == '\0')
3040
                    goto bad_type;
3041
                while (qemu_isspace(*p))
3042
                    p++;
3043
                has_option = 0;
3044
                if (*p == '-') {
3045
                    p++;
3046
                    if (*p != c) {
3047
                        monitor_printf(mon, "%s: unsupported option -%c\n",
3048
                                       cmdname, *p);
3049
                        goto fail;
3050
                    }
3051
                    p++;
3052
                    has_option = 1;
3053
                }
3054
                qdict_put(qdict, key, qint_from_int(has_option));
3055
            }
3056
            break;
3057
        default:
3058
        bad_type:
3059
            monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3060
            goto fail;
3061
        }
3062
        qemu_free(key);
3063
        key = NULL;
3064
    }
3065
    /* check that all arguments were parsed */
3066
    while (qemu_isspace(*p))
3067
        p++;
3068
    if (*p != '\0') {
3069
        monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3070
                       cmdname);
3071
        goto fail;
3072
    }
3073

    
3074
    return cmd;
3075

    
3076
fail:
3077
    qemu_free(key);
3078
    return NULL;
3079
}
3080

    
3081
static void monitor_handle_command(Monitor *mon, const char *cmdline)
3082
{
3083
    QDict *qdict;
3084
    const mon_cmd_t *cmd;
3085

    
3086
    qdict = qdict_new();
3087

    
3088
    cmd = monitor_parse_command(mon, cmdline, qdict);
3089
    if (!cmd)
3090
        goto out;
3091

    
3092
    qemu_errors_to_mon(mon);
3093

    
3094
    if (monitor_handler_ported(cmd)) {
3095
        QObject *data = NULL;
3096

    
3097
        cmd->mhandler.cmd_new(mon, qdict, &data);
3098
        if (data)
3099
            cmd->user_print(mon, data);
3100

    
3101
        qobject_decref(data);
3102
    } else {
3103
        cmd->mhandler.cmd(mon, qdict);
3104
    }
3105

    
3106
   qemu_errors_to_previous();
3107

    
3108
out:
3109
    QDECREF(qdict);
3110
}
3111

    
3112
static void cmd_completion(const char *name, const char *list)
3113
{
3114
    const char *p, *pstart;
3115
    char cmd[128];
3116
    int len;
3117

    
3118
    p = list;
3119
    for(;;) {
3120
        pstart = p;
3121
        p = strchr(p, '|');
3122
        if (!p)
3123
            p = pstart + strlen(pstart);
3124
        len = p - pstart;
3125
        if (len > sizeof(cmd) - 2)
3126
            len = sizeof(cmd) - 2;
3127
        memcpy(cmd, pstart, len);
3128
        cmd[len] = '\0';
3129
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3130
            readline_add_completion(cur_mon->rs, cmd);
3131
        }
3132
        if (*p == '\0')
3133
            break;
3134
        p++;
3135
    }
3136
}
3137

    
3138
static void file_completion(const char *input)
3139
{
3140
    DIR *ffs;
3141
    struct dirent *d;
3142
    char path[1024];
3143
    char file[1024], file_prefix[1024];
3144
    int input_path_len;
3145
    const char *p;
3146

    
3147
    p = strrchr(input, '/');
3148
    if (!p) {
3149
        input_path_len = 0;
3150
        pstrcpy(file_prefix, sizeof(file_prefix), input);
3151
        pstrcpy(path, sizeof(path), ".");
3152
    } else {
3153
        input_path_len = p - input + 1;
3154
        memcpy(path, input, input_path_len);
3155
        if (input_path_len > sizeof(path) - 1)
3156
            input_path_len = sizeof(path) - 1;
3157
        path[input_path_len] = '\0';
3158
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3159
    }
3160
#ifdef DEBUG_COMPLETION
3161
    monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3162
                   input, path, file_prefix);
3163
#endif
3164
    ffs = opendir(path);
3165
    if (!ffs)
3166
        return;
3167
    for(;;) {
3168
        struct stat sb;
3169
        d = readdir(ffs);
3170
        if (!d)
3171
            break;
3172
        if (strstart(d->d_name, file_prefix, NULL)) {
3173
            memcpy(file, input, input_path_len);
3174
            if (input_path_len < sizeof(file))
3175
                pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3176
                        d->d_name);
3177
            /* stat the file to find out if it's a directory.
3178
             * In that case add a slash to speed up typing long paths
3179
             */
3180
            stat(file, &sb);
3181
            if(S_ISDIR(sb.st_mode))
3182
                pstrcat(file, sizeof(file), "/");
3183
            readline_add_completion(cur_mon->rs, file);
3184
        }
3185
    }
3186
    closedir(ffs);
3187
}
3188

    
3189
static void block_completion_it(void *opaque, BlockDriverState *bs)
3190
{
3191
    const char *name = bdrv_get_device_name(bs);
3192
    const char *input = opaque;
3193

    
3194
    if (input[0] == '\0' ||
3195
        !strncmp(name, (char *)input, strlen(input))) {
3196
        readline_add_completion(cur_mon->rs, name);
3197
    }
3198
}
3199

    
3200
/* NOTE: this parser is an approximate form of the real command parser */
3201
static void parse_cmdline(const char *cmdline,
3202
                         int *pnb_args, char **args)
3203
{
3204
    const char *p;
3205
    int nb_args, ret;
3206
    char buf[1024];
3207

    
3208
    p = cmdline;
3209
    nb_args = 0;
3210
    for(;;) {
3211
        while (qemu_isspace(*p))
3212
            p++;
3213
        if (*p == '\0')
3214
            break;
3215
        if (nb_args >= MAX_ARGS)
3216
            break;
3217
        ret = get_str(buf, sizeof(buf), &p);
3218
        args[nb_args] = qemu_strdup(buf);
3219
        nb_args++;
3220
        if (ret < 0)
3221
            break;
3222
    }
3223
    *pnb_args = nb_args;
3224
}
3225

    
3226
static const char *next_arg_type(const char *typestr)
3227
{
3228
    const char *p = strchr(typestr, ':');
3229
    return (p != NULL ? ++p : typestr);
3230
}
3231

    
3232
static void monitor_find_completion(const char *cmdline)
3233
{
3234
    const char *cmdname;
3235
    char *args[MAX_ARGS];
3236
    int nb_args, i, len;
3237
    const char *ptype, *str;
3238
    const mon_cmd_t *cmd;
3239
    const KeyDef *key;
3240

    
3241
    parse_cmdline(cmdline, &nb_args, args);
3242
#ifdef DEBUG_COMPLETION
3243
    for(i = 0; i < nb_args; i++) {
3244
        monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3245
    }
3246
#endif
3247

    
3248
    /* if the line ends with a space, it means we want to complete the
3249
       next arg */
3250
    len = strlen(cmdline);
3251
    if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3252
        if (nb_args >= MAX_ARGS)
3253
            return;
3254
        args[nb_args++] = qemu_strdup("");
3255
    }
3256
    if (nb_args <= 1) {
3257
        /* command completion */
3258
        if (nb_args == 0)
3259
            cmdname = "";
3260
        else
3261
            cmdname = args[0];
3262
        readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3263
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3264
            cmd_completion(cmdname, cmd->name);
3265
        }
3266
    } else {
3267
        /* find the command */
3268
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3269
            if (compare_cmd(args[0], cmd->name))
3270
                goto found;
3271
        }
3272
        return;
3273
    found:
3274
        ptype = next_arg_type(cmd->args_type);
3275
        for(i = 0; i < nb_args - 2; i++) {
3276
            if (*ptype != '\0') {
3277
                ptype = next_arg_type(ptype);
3278
                while (*ptype == '?')
3279
                    ptype = next_arg_type(ptype);
3280
            }
3281
        }
3282
        str = args[nb_args - 1];
3283
        if (*ptype == '-' && ptype[1] != '\0') {
3284
            ptype += 2;
3285
        }
3286
        switch(*ptype) {
3287
        case 'F':
3288
            /* file completion */
3289
            readline_set_completion_index(cur_mon->rs, strlen(str));
3290
            file_completion(str);
3291
            break;
3292
        case 'B':
3293
            /* block device name completion */
3294
            readline_set_completion_index(cur_mon->rs, strlen(str));
3295
            bdrv_iterate(block_completion_it, (void *)str);
3296
            break;
3297
        case 's':
3298
            /* XXX: more generic ? */
3299
            if (!strcmp(cmd->name, "info")) {
3300
                readline_set_completion_index(cur_mon->rs, strlen(str));
3301
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3302
                    cmd_completion(str, cmd->name);
3303
                }
3304
            } else if (!strcmp(cmd->name, "sendkey")) {
3305
                char *sep = strrchr(str, '-');
3306
                if (sep)
3307
                    str = sep + 1;
3308
                readline_set_completion_index(cur_mon->rs, strlen(str));
3309
                for(key = key_defs; key->name != NULL; key++) {
3310
                    cmd_completion(str, key->name);
3311
                }
3312
            } else if (!strcmp(cmd->name, "help|?")) {
3313
                readline_set_completion_index(cur_mon->rs, strlen(str));
3314
                for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3315
                    cmd_completion(str, cmd->name);
3316
                }
3317
            }
3318
            break;
3319
        default:
3320
            break;
3321
        }
3322
    }
3323
    for(i = 0; i < nb_args; i++)
3324
        qemu_free(args[i]);
3325
}
3326

    
3327
static int monitor_can_read(void *opaque)
3328
{
3329
    Monitor *mon = opaque;
3330

    
3331
    return (mon->suspend_cnt == 0) ? 128 : 0;
3332
}
3333

    
3334
static void monitor_read(void *opaque, const uint8_t *buf, int size)
3335
{
3336
    Monitor *old_mon = cur_mon;
3337
    int i;
3338

    
3339
    cur_mon = opaque;
3340

    
3341
    if (cur_mon->rs) {
3342
        for (i = 0; i < size; i++)
3343
            readline_handle_byte(cur_mon->rs, buf[i]);
3344
    } else {
3345
        if (size == 0 || buf[size - 1] != 0)
3346
            monitor_printf(cur_mon, "corrupted command\n");
3347
        else
3348
            monitor_handle_command(cur_mon, (char *)buf);
3349
    }
3350

    
3351
    cur_mon = old_mon;
3352
}
3353

    
3354
static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3355
{
3356
    monitor_suspend(mon);
3357
    monitor_handle_command(mon, cmdline);
3358
    monitor_resume(mon);
3359
}
3360

    
3361
int monitor_suspend(Monitor *mon)
3362
{
3363
    if (!mon->rs)
3364
        return -ENOTTY;
3365
    mon->suspend_cnt++;
3366
    return 0;
3367
}
3368

    
3369
void monitor_resume(Monitor *mon)
3370
{
3371
    if (!mon->rs)
3372
        return;
3373
    if (--mon->suspend_cnt == 0)
3374
        readline_show_prompt(mon->rs);
3375
}
3376

    
3377
static void monitor_event(void *opaque, int event)
3378
{
3379
    Monitor *mon = opaque;
3380

    
3381
    switch (event) {
3382
    case CHR_EVENT_MUX_IN:
3383
        mon->mux_out = 0;
3384
        if (mon->reset_seen) {
3385
            readline_restart(mon->rs);
3386
            monitor_resume(mon);
3387
            monitor_flush(mon);
3388
        } else {
3389
            mon->suspend_cnt = 0;
3390
        }
3391
        break;
3392

    
3393
    case CHR_EVENT_MUX_OUT:
3394
        if (mon->reset_seen) {
3395
            if (mon->suspend_cnt == 0) {
3396
                monitor_printf(mon, "\n");
3397
            }
3398
            monitor_flush(mon);
3399
            monitor_suspend(mon);
3400
        } else {
3401
            mon->suspend_cnt++;
3402
        }
3403
        mon->mux_out = 1;
3404
        break;
3405

    
3406
    case CHR_EVENT_RESET:
3407
        monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3408
                       "information\n", QEMU_VERSION);
3409
        if (!mon->mux_out) {
3410
            readline_show_prompt(mon->rs);
3411
        }
3412
        mon->reset_seen = 1;
3413
        break;
3414
    }
3415
}
3416

    
3417

    
3418
/*
3419
 * Local variables:
3420
 *  c-indent-level: 4
3421
 *  c-basic-offset: 4
3422
 *  tab-width: 8
3423
 * End:
3424
 */
3425

    
3426
void monitor_init(CharDriverState *chr, int flags)
3427
{
3428
    static int is_first_init = 1;
3429
    Monitor *mon;
3430

    
3431
    if (is_first_init) {
3432
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3433
        is_first_init = 0;
3434
    }
3435

    
3436
    mon = qemu_mallocz(sizeof(*mon));
3437

    
3438
    mon->chr = chr;
3439
    mon->flags = flags;
3440
    if (flags & MONITOR_USE_READLINE) {
3441
        mon->rs = readline_init(mon, monitor_find_completion);
3442
        monitor_read_command(mon, 0);
3443
    }
3444

    
3445
    qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3446
                          mon);
3447

    
3448
    QLIST_INSERT_HEAD(&mon_list, mon, entry);
3449
    if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3450
        cur_mon = mon;
3451
}
3452

    
3453
static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3454
{
3455
    BlockDriverState *bs = opaque;
3456
    int ret = 0;
3457

    
3458
    if (bdrv_set_key(bs, password) != 0) {
3459
        monitor_printf(mon, "invalid password\n");
3460
        ret = -EPERM;
3461
    }
3462
    if (mon->password_completion_cb)
3463
        mon->password_completion_cb(mon->password_opaque, ret);
3464

    
3465
    monitor_read_command(mon, 1);
3466
}
3467

    
3468
void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3469
                                 BlockDriverCompletionFunc *completion_cb,
3470
                                 void *opaque)
3471
{
3472
    int err;
3473

    
3474
    if (!bdrv_key_required(bs)) {
3475
        if (completion_cb)
3476
            completion_cb(opaque, 0);
3477
        return;
3478
    }
3479

    
3480
    monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3481
                   bdrv_get_encrypted_filename(bs));
3482

    
3483
    mon->password_completion_cb = completion_cb;
3484
    mon->password_opaque = opaque;
3485

    
3486
    err = monitor_read_password(mon, bdrv_password_cb, bs);
3487

    
3488
    if (err && completion_cb)
3489
        completion_cb(opaque, err);
3490
}
3491

    
3492
typedef struct QemuErrorSink QemuErrorSink;
3493
struct QemuErrorSink {
3494
    enum {
3495
        ERR_SINK_FILE,
3496
        ERR_SINK_MONITOR,
3497
    } dest;
3498
    union {
3499
        FILE    *fp;
3500
        Monitor *mon;
3501
    };
3502
    QemuErrorSink *previous;
3503
};
3504

    
3505
static QemuErrorSink *qemu_error_sink;
3506

    
3507
void qemu_errors_to_file(FILE *fp)
3508
{
3509
    QemuErrorSink *sink;
3510

    
3511
    sink = qemu_mallocz(sizeof(*sink));
3512
    sink->dest = ERR_SINK_FILE;
3513
    sink->fp = fp;
3514
    sink->previous = qemu_error_sink;
3515
    qemu_error_sink = sink;
3516
}
3517

    
3518
void qemu_errors_to_mon(Monitor *mon)
3519
{
3520
    QemuErrorSink *sink;
3521

    
3522
    sink = qemu_mallocz(sizeof(*sink));
3523
    sink->dest = ERR_SINK_MONITOR;
3524
    sink->mon = mon;
3525
    sink->previous = qemu_error_sink;
3526
    qemu_error_sink = sink;
3527
}
3528

    
3529
void qemu_errors_to_previous(void)
3530
{
3531
    QemuErrorSink *sink;
3532

    
3533
    assert(qemu_error_sink != NULL);
3534
    sink = qemu_error_sink;
3535
    qemu_error_sink = sink->previous;
3536
    qemu_free(sink);
3537
}
3538

    
3539
void qemu_error(const char *fmt, ...)
3540
{
3541
    va_list args;
3542

    
3543
    assert(qemu_error_sink != NULL);
3544
    switch (qemu_error_sink->dest) {
3545
    case ERR_SINK_FILE:
3546
        va_start(args, fmt);
3547
        vfprintf(qemu_error_sink->fp, fmt, args);
3548
        va_end(args);
3549
        break;
3550
    case ERR_SINK_MONITOR:
3551
        va_start(args, fmt);
3552
        monitor_vprintf(qemu_error_sink->mon, fmt, args);
3553
        va_end(args);
3554
        break;
3555
    }
3556
}