Statistics
| Branch: | Revision:

root / monitor.c @ f3c157c4

History | View | Annotate | Download (97.4 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 "qlist.h"
50
#include "qdict.h"
51
#include "qstring.h"
52
#include "qerror.h"
53

    
54
//#define DEBUG
55
//#define DEBUG_COMPLETION
56

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

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

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

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

    
112
static QLIST_HEAD(mon_list, Monitor) mon_list;
113

    
114
static const mon_cmd_t mon_cmds[];
115
static const mon_cmd_t info_cmds[];
116

    
117
Monitor *cur_mon = NULL;
118

    
119
static void monitor_command_cb(Monitor *mon, const char *cmdline,
120
                               void *opaque);
121

    
122
/* Return true if in control mode, false otherwise */
123
static inline int monitor_ctrl_mode(const Monitor *mon)
124
{
125
    return (mon->flags & MONITOR_USE_CONTROL);
126
}
127

    
128
static void monitor_read_command(Monitor *mon, int show_prompt)
129
{
130
    readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
131
    if (show_prompt)
132
        readline_show_prompt(mon->rs);
133
}
134

    
135
static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
136
                                 void *opaque)
137
{
138
    if (mon->rs) {
139
        readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
140
        /* prompt is printed on return from the command handler */
141
        return 0;
142
    } else {
143
        monitor_printf(mon, "terminal does not support password prompting\n");
144
        return -ENOTTY;
145
    }
146
}
147

    
148
void monitor_flush(Monitor *mon)
149
{
150
    if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
151
        qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
152
        mon->outbuf_index = 0;
153
    }
154
}
155

    
156
/* flush at every end of line or if the buffer is full */
157
static void monitor_puts(Monitor *mon, const char *str)
158
{
159
    char c;
160

    
161
    if (!mon)
162
        return;
163

    
164
    for(;;) {
165
        c = *str++;
166
        if (c == '\0')
167
            break;
168
        if (c == '\n')
169
            mon->outbuf[mon->outbuf_index++] = '\r';
170
        mon->outbuf[mon->outbuf_index++] = c;
171
        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
172
            || c == '\n')
173
            monitor_flush(mon);
174
    }
175
}
176

    
177
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
178
{
179
    char buf[4096];
180
    vsnprintf(buf, sizeof(buf), fmt, ap);
181
    monitor_puts(mon, buf);
182
}
183

    
184
void monitor_printf(Monitor *mon, const char *fmt, ...)
185
{
186
    va_list ap;
187
    va_start(ap, fmt);
188
    monitor_vprintf(mon, fmt, ap);
189
    va_end(ap);
190
}
191

    
192
void monitor_print_filename(Monitor *mon, const char *filename)
193
{
194
    int i;
195

    
196
    for (i = 0; filename[i]; i++) {
197
        switch (filename[i]) {
198
        case ' ':
199
        case '"':
200
        case '\\':
201
            monitor_printf(mon, "\\%c", filename[i]);
202
            break;
203
        case '\t':
204
            monitor_printf(mon, "\\t");
205
            break;
206
        case '\r':
207
            monitor_printf(mon, "\\r");
208
            break;
209
        case '\n':
210
            monitor_printf(mon, "\\n");
211
            break;
212
        default:
213
            monitor_printf(mon, "%c", filename[i]);
214
            break;
215
        }
216
    }
217
}
218

    
219
static int monitor_fprintf(FILE *stream, const char *fmt, ...)
220
{
221
    va_list ap;
222
    va_start(ap, fmt);
223
    monitor_vprintf((Monitor *)stream, fmt, ap);
224
    va_end(ap);
225
    return 0;
226
}
227

    
228
static void monitor_user_noop(Monitor *mon, const QObject *data) { }
229

    
230
static inline int monitor_handler_ported(const mon_cmd_t *cmd)
231
{
232
    return cmd->user_print != NULL;
233
}
234

    
235
static inline int monitor_has_error(const Monitor *mon)
236
{
237
    return mon->error != NULL;
238
}
239

    
240
static void monitor_print_qobject(Monitor *mon, const QObject *data)
241
{
242
    switch (qobject_type(data)) {
243
        case QTYPE_QSTRING:
244
            monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
245
            break;
246
        case QTYPE_QINT:
247
            monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
248
            break;
249
        default:
250
            monitor_printf(mon, "ERROR: unsupported type: %d",
251
                                                        qobject_type(data));
252
            break;
253
    }
254

    
255
    monitor_puts(mon, "\n");
256
}
257

    
258
static int compare_cmd(const char *name, const char *list)
259
{
260
    const char *p, *pstart;
261
    int len;
262
    len = strlen(name);
263
    p = list;
264
    for(;;) {
265
        pstart = p;
266
        p = strchr(p, '|');
267
        if (!p)
268
            p = pstart + strlen(pstart);
269
        if ((p - pstart) == len && !memcmp(pstart, name, len))
270
            return 1;
271
        if (*p == '\0')
272
            break;
273
        p++;
274
    }
275
    return 0;
276
}
277

    
278
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
279
                          const char *prefix, const char *name)
280
{
281
    const mon_cmd_t *cmd;
282

    
283
    for(cmd = cmds; cmd->name != NULL; cmd++) {
284
        if (!name || !strcmp(name, cmd->name))
285
            monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
286
                           cmd->params, cmd->help);
287
    }
288
}
289

    
290
static void help_cmd(Monitor *mon, const char *name)
291
{
292
    if (name && !strcmp(name, "info")) {
293
        help_cmd_dump(mon, info_cmds, "info ", NULL);
294
    } else {
295
        help_cmd_dump(mon, mon_cmds, "", name);
296
        if (name && !strcmp(name, "log")) {
297
            const CPULogItem *item;
298
            monitor_printf(mon, "Log items (comma separated):\n");
299
            monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
300
            for(item = cpu_log_items; item->mask != 0; item++) {
301
                monitor_printf(mon, "%-10s %s\n", item->name, item->help);
302
            }
303
        }
304
    }
305
}
306

    
307
static void do_help_cmd(Monitor *mon, const QDict *qdict)
308
{
309
    help_cmd(mon, qdict_get_try_str(qdict, "name"));
310
}
311

    
312
static void do_commit(Monitor *mon, const QDict *qdict)
313
{
314
    int all_devices;
315
    DriveInfo *dinfo;
316
    const char *device = qdict_get_str(qdict, "device");
317

    
318
    all_devices = !strcmp(device, "all");
319
    QTAILQ_FOREACH(dinfo, &drives, next) {
320
        if (!all_devices)
321
            if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
322
                continue;
323
        bdrv_commit(dinfo->bdrv);
324
    }
325
}
326

    
327
static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
328
{
329
    const mon_cmd_t *cmd;
330
    const char *item = qdict_get_try_str(qdict, "item");
331

    
332
    if (!item)
333
        goto help;
334

    
335
    for (cmd = info_cmds; cmd->name != NULL; cmd++) {
336
        if (compare_cmd(item, cmd->name))
337
            break;
338
    }
339

    
340
    if (cmd->name == NULL)
341
        goto help;
342

    
343
    if (monitor_handler_ported(cmd)) {
344
        cmd->mhandler.info_new(mon, ret_data);
345
        if (*ret_data)
346
            cmd->user_print(mon, *ret_data);
347
    } else {
348
        cmd->mhandler.info(mon);
349
    }
350

    
351
    return;
352

    
353
help:
354
    help_cmd(mon, "info");
355
}
356

    
357
/**
358
 * do_info_version(): Show QEMU version
359
 */
360
static void do_info_version(Monitor *mon, QObject **ret_data)
361
{
362
    *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
363
}
364

    
365
static void do_info_name(Monitor *mon)
366
{
367
    if (qemu_name)
368
        monitor_printf(mon, "%s\n", qemu_name);
369
}
370

    
371
#if defined(TARGET_I386)
372
static void do_info_hpet(Monitor *mon)
373
{
374
    monitor_printf(mon, "HPET is %s by QEMU\n",
375
                   (no_hpet) ? "disabled" : "enabled");
376
}
377
#endif
378

    
379
static void do_info_uuid(Monitor *mon)
380
{
381
    monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
382
                   qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
383
                   qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
384
                   qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
385
                   qemu_uuid[14], qemu_uuid[15]);
386
}
387

    
388
/* get the current CPU defined by the user */
389
static int mon_set_cpu(int cpu_index)
390
{
391
    CPUState *env;
392

    
393
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
394
        if (env->cpu_index == cpu_index) {
395
            cur_mon->mon_cpu = env;
396
            return 0;
397
        }
398
    }
399
    return -1;
400
}
401

    
402
static CPUState *mon_get_cpu(void)
403
{
404
    if (!cur_mon->mon_cpu) {
405
        mon_set_cpu(0);
406
    }
407
    cpu_synchronize_state(cur_mon->mon_cpu);
408
    return cur_mon->mon_cpu;
409
}
410

    
411
static void do_info_registers(Monitor *mon)
412
{
413
    CPUState *env;
414
    env = mon_get_cpu();
415
    if (!env)
416
        return;
417
#ifdef TARGET_I386
418
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
419
                   X86_DUMP_FPU);
420
#else
421
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
422
                   0);
423
#endif
424
}
425

    
426
static void print_cpu_iter(QObject *obj, void *opaque)
427
{
428
    QDict *cpu;
429
    int active = ' ';
430
    Monitor *mon = opaque;
431

    
432
    assert(qobject_type(obj) == QTYPE_QDICT);
433
    cpu = qobject_to_qdict(obj);
434

    
435
    if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
436
        active = '*';
437

    
438
    monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
439

    
440
#if defined(TARGET_I386)
441
    monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
442
                   (target_ulong) qdict_get_int(cpu, "pc"));
443
#elif defined(TARGET_PPC)
444
    monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
445
                   (target_long) qdict_get_int(cpu, "nip"));
446
#elif defined(TARGET_SPARC)
447
    monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
448
                   (target_long) qdict_get_int(cpu, "pc"));
449
    monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
450
                   (target_long) qdict_get_int(cpu, "npc"));
451
#elif defined(TARGET_MIPS)
452
    monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
453
                   (target_long) qdict_get_int(cpu, "PC"));
454
#endif
455

    
456
    if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
457
        monitor_printf(mon, " (halted)");
458

    
459
    monitor_printf(mon, "\n");
460
}
461

    
462
static void monitor_print_cpus(Monitor *mon, const QObject *data)
463
{
464
    QList *cpu_list;
465

    
466
    assert(qobject_type(data) == QTYPE_QLIST);
467
    cpu_list = qobject_to_qlist(data);
468
    qlist_iter(cpu_list, print_cpu_iter, mon);
469
}
470

    
471
/**
472
 * do_info_cpus(): Show CPU information
473
 *
474
 * Return a QList with a QDict for each CPU.
475
 *
476
 * For example:
477
 *
478
 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
479
 *   { "CPU": 1, "current": "no",  "pc": 0x..., "halted": "yes" } ]
480
 */
481
static void do_info_cpus(Monitor *mon, QObject **ret_data)
482
{
483
    CPUState *env;
484
    QList *cpu_list;
485

    
486
    cpu_list = qlist_new();
487

    
488
    /* just to set the default cpu if not already done */
489
    mon_get_cpu();
490

    
491
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
492
        const char *answer;
493
        QDict *cpu = qdict_new();
494

    
495
        cpu_synchronize_state(env);
496

    
497
        qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
498
        answer = (env == mon->mon_cpu) ? "yes" : "no";
499
        qdict_put(cpu, "current", qstring_from_str(answer));
500

    
501
#if defined(TARGET_I386)
502
        qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
503
#elif defined(TARGET_PPC)
504
        qdict_put(cpu, "nip", qint_from_int(env->nip));
505
#elif defined(TARGET_SPARC)
506
        qdict_put(cpu, "pc", qint_from_int(env->pc));
507
        qdict_put(cpu, "npc", qint_from_int(env->npc));
508
#elif defined(TARGET_MIPS)
509
        qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
510
#endif
511
        answer = env->halted ? "yes" : "no";
512
        qdict_put(cpu, "halted", qstring_from_str(answer));
513

    
514
        qlist_append(cpu_list, cpu);
515
    }
516

    
517
    *ret_data = QOBJECT(cpu_list);
518
}
519

    
520
static void do_cpu_set(Monitor *mon, const QDict *qdict)
521
{
522
    int index = qdict_get_int(qdict, "index");
523
    if (mon_set_cpu(index) < 0)
524
        monitor_printf(mon, "Invalid CPU index\n");
525
}
526

    
527
static void do_info_jit(Monitor *mon)
528
{
529
    dump_exec_info((FILE *)mon, monitor_fprintf);
530
}
531

    
532
static void do_info_history(Monitor *mon)
533
{
534
    int i;
535
    const char *str;
536

    
537
    if (!mon->rs)
538
        return;
539
    i = 0;
540
    for(;;) {
541
        str = readline_get_history(mon->rs, i);
542
        if (!str)
543
            break;
544
        monitor_printf(mon, "%d: '%s'\n", i, str);
545
        i++;
546
    }
547
}
548

    
549
#if defined(TARGET_PPC)
550
/* XXX: not implemented in other targets */
551
static void do_info_cpu_stats(Monitor *mon)
552
{
553
    CPUState *env;
554

    
555
    env = mon_get_cpu();
556
    cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
557
}
558
#endif
559

    
560
/**
561
 * do_quit(): Quit QEMU execution
562
 */
563
static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
564
{
565
    exit(0);
566
}
567

    
568
static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
569
{
570
    if (bdrv_is_inserted(bs)) {
571
        if (!force) {
572
            if (!bdrv_is_removable(bs)) {
573
                monitor_printf(mon, "device is not removable\n");
574
                return -1;
575
            }
576
            if (bdrv_is_locked(bs)) {
577
                monitor_printf(mon, "device is locked\n");
578
                return -1;
579
            }
580
        }
581
        bdrv_close(bs);
582
    }
583
    return 0;
584
}
585

    
586
static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
587
{
588
    BlockDriverState *bs;
589
    int force = qdict_get_int(qdict, "force");
590
    const char *filename = qdict_get_str(qdict, "filename");
591

    
592
    bs = bdrv_find(filename);
593
    if (!bs) {
594
        monitor_printf(mon, "device not found\n");
595
        return;
596
    }
597
    eject_device(mon, bs, force);
598
}
599

    
600
static void do_change_block(Monitor *mon, const char *device,
601
                            const char *filename, const char *fmt)
602
{
603
    BlockDriverState *bs;
604
    BlockDriver *drv = NULL;
605

    
606
    bs = bdrv_find(device);
607
    if (!bs) {
608
        monitor_printf(mon, "device not found\n");
609
        return;
610
    }
611
    if (fmt) {
612
        drv = bdrv_find_whitelisted_format(fmt);
613
        if (!drv) {
614
            monitor_printf(mon, "invalid format %s\n", fmt);
615
            return;
616
        }
617
    }
618
    if (eject_device(mon, bs, 0) < 0)
619
        return;
620
    bdrv_open2(bs, filename, 0, drv);
621
    monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
622
}
623

    
624
static void change_vnc_password_cb(Monitor *mon, const char *password,
625
                                   void *opaque)
626
{
627
    if (vnc_display_password(NULL, password) < 0)
628
        monitor_printf(mon, "could not set VNC server password\n");
629

    
630
    monitor_read_command(mon, 1);
631
}
632

    
633
static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
634
{
635
    if (strcmp(target, "passwd") == 0 ||
636
        strcmp(target, "password") == 0) {
637
        if (arg) {
638
            char password[9];
639
            strncpy(password, arg, sizeof(password));
640
            password[sizeof(password) - 1] = '\0';
641
            change_vnc_password_cb(mon, password, NULL);
642
        } else {
643
            monitor_read_password(mon, change_vnc_password_cb, NULL);
644
        }
645
    } else {
646
        if (vnc_display_open(NULL, target) < 0)
647
            monitor_printf(mon, "could not start VNC server on %s\n", target);
648
    }
649
}
650

    
651
static void do_change(Monitor *mon, const QDict *qdict)
652
{
653
    const char *device = qdict_get_str(qdict, "device");
654
    const char *target = qdict_get_str(qdict, "target");
655
    const char *arg = qdict_get_try_str(qdict, "arg");
656
    if (strcmp(device, "vnc") == 0) {
657
        do_change_vnc(mon, target, arg);
658
    } else {
659
        do_change_block(mon, device, target, arg);
660
    }
661
}
662

    
663
static void do_screen_dump(Monitor *mon, const QDict *qdict)
664
{
665
    vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
666
}
667

    
668
static void do_logfile(Monitor *mon, const QDict *qdict)
669
{
670
    cpu_set_log_filename(qdict_get_str(qdict, "filename"));
671
}
672

    
673
static void do_log(Monitor *mon, const QDict *qdict)
674
{
675
    int mask;
676
    const char *items = qdict_get_str(qdict, "items");
677

    
678
    if (!strcmp(items, "none")) {
679
        mask = 0;
680
    } else {
681
        mask = cpu_str_to_log_mask(items);
682
        if (!mask) {
683
            help_cmd(mon, "log");
684
            return;
685
        }
686
    }
687
    cpu_set_log(mask);
688
}
689

    
690
static void do_singlestep(Monitor *mon, const QDict *qdict)
691
{
692
    const char *option = qdict_get_try_str(qdict, "option");
693
    if (!option || !strcmp(option, "on")) {
694
        singlestep = 1;
695
    } else if (!strcmp(option, "off")) {
696
        singlestep = 0;
697
    } else {
698
        monitor_printf(mon, "unexpected option %s\n", option);
699
    }
700
}
701

    
702
/**
703
 * do_stop(): Stop VM execution
704
 */
705
static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
706
{
707
    vm_stop(EXCP_INTERRUPT);
708
}
709

    
710
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
711

    
712
struct bdrv_iterate_context {
713
    Monitor *mon;
714
    int err;
715
};
716

    
717
/**
718
 * do_cont(): Resume emulation.
719
 */
720
static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
721
{
722
    struct bdrv_iterate_context context = { mon, 0 };
723

    
724
    bdrv_iterate(encrypted_bdrv_it, &context);
725
    /* only resume the vm if all keys are set and valid */
726
    if (!context.err)
727
        vm_start();
728
}
729

    
730
static void bdrv_key_cb(void *opaque, int err)
731
{
732
    Monitor *mon = opaque;
733

    
734
    /* another key was set successfully, retry to continue */
735
    if (!err)
736
        do_cont(mon, NULL, NULL);
737
}
738

    
739
static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
740
{
741
    struct bdrv_iterate_context *context = opaque;
742

    
743
    if (!context->err && bdrv_key_required(bs)) {
744
        context->err = -EBUSY;
745
        monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
746
                                    context->mon);
747
    }
748
}
749

    
750
static void do_gdbserver(Monitor *mon, const QDict *qdict)
751
{
752
    const char *device = qdict_get_try_str(qdict, "device");
753
    if (!device)
754
        device = "tcp::" DEFAULT_GDBSTUB_PORT;
755
    if (gdbserver_start(device) < 0) {
756
        monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
757
                       device);
758
    } else if (strcmp(device, "none") == 0) {
759
        monitor_printf(mon, "Disabled gdbserver\n");
760
    } else {
761
        monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
762
                       device);
763
    }
764
}
765

    
766
static void do_watchdog_action(Monitor *mon, const QDict *qdict)
767
{
768
    const char *action = qdict_get_str(qdict, "action");
769
    if (select_watchdog_action(action) == -1) {
770
        monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
771
    }
772
}
773

    
774
static void monitor_printc(Monitor *mon, int c)
775
{
776
    monitor_printf(mon, "'");
777
    switch(c) {
778
    case '\'':
779
        monitor_printf(mon, "\\'");
780
        break;
781
    case '\\':
782
        monitor_printf(mon, "\\\\");
783
        break;
784
    case '\n':
785
        monitor_printf(mon, "\\n");
786
        break;
787
    case '\r':
788
        monitor_printf(mon, "\\r");
789
        break;
790
    default:
791
        if (c >= 32 && c <= 126) {
792
            monitor_printf(mon, "%c", c);
793
        } else {
794
            monitor_printf(mon, "\\x%02x", c);
795
        }
796
        break;
797
    }
798
    monitor_printf(mon, "'");
799
}
800

    
801
static void memory_dump(Monitor *mon, int count, int format, int wsize,
802
                        target_phys_addr_t addr, int is_physical)
803
{
804
    CPUState *env;
805
    int nb_per_line, l, line_size, i, max_digits, len;
806
    uint8_t buf[16];
807
    uint64_t v;
808

    
809
    if (format == 'i') {
810
        int flags;
811
        flags = 0;
812
        env = mon_get_cpu();
813
        if (!env && !is_physical)
814
            return;
815
#ifdef TARGET_I386
816
        if (wsize == 2) {
817
            flags = 1;
818
        } else if (wsize == 4) {
819
            flags = 0;
820
        } else {
821
            /* as default we use the current CS size */
822
            flags = 0;
823
            if (env) {
824
#ifdef TARGET_X86_64
825
                if ((env->efer & MSR_EFER_LMA) &&
826
                    (env->segs[R_CS].flags & DESC_L_MASK))
827
                    flags = 2;
828
                else
829
#endif
830
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
831
                    flags = 1;
832
            }
833
        }
834
#endif
835
        monitor_disas(mon, env, addr, count, is_physical, flags);
836
        return;
837
    }
838

    
839
    len = wsize * count;
840
    if (wsize == 1)
841
        line_size = 8;
842
    else
843
        line_size = 16;
844
    nb_per_line = line_size / wsize;
845
    max_digits = 0;
846

    
847
    switch(format) {
848
    case 'o':
849
        max_digits = (wsize * 8 + 2) / 3;
850
        break;
851
    default:
852
    case 'x':
853
        max_digits = (wsize * 8) / 4;
854
        break;
855
    case 'u':
856
    case 'd':
857
        max_digits = (wsize * 8 * 10 + 32) / 33;
858
        break;
859
    case 'c':
860
        wsize = 1;
861
        break;
862
    }
863

    
864
    while (len > 0) {
865
        if (is_physical)
866
            monitor_printf(mon, TARGET_FMT_plx ":", addr);
867
        else
868
            monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
869
        l = len;
870
        if (l > line_size)
871
            l = line_size;
872
        if (is_physical) {
873
            cpu_physical_memory_rw(addr, buf, l, 0);
874
        } else {
875
            env = mon_get_cpu();
876
            if (!env)
877
                break;
878
            if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
879
                monitor_printf(mon, " Cannot access memory\n");
880
                break;
881
            }
882
        }
883
        i = 0;
884
        while (i < l) {
885
            switch(wsize) {
886
            default:
887
            case 1:
888
                v = ldub_raw(buf + i);
889
                break;
890
            case 2:
891
                v = lduw_raw(buf + i);
892
                break;
893
            case 4:
894
                v = (uint32_t)ldl_raw(buf + i);
895
                break;
896
            case 8:
897
                v = ldq_raw(buf + i);
898
                break;
899
            }
900
            monitor_printf(mon, " ");
901
            switch(format) {
902
            case 'o':
903
                monitor_printf(mon, "%#*" PRIo64, max_digits, v);
904
                break;
905
            case 'x':
906
                monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
907
                break;
908
            case 'u':
909
                monitor_printf(mon, "%*" PRIu64, max_digits, v);
910
                break;
911
            case 'd':
912
                monitor_printf(mon, "%*" PRId64, max_digits, v);
913
                break;
914
            case 'c':
915
                monitor_printc(mon, v);
916
                break;
917
            }
918
            i += wsize;
919
        }
920
        monitor_printf(mon, "\n");
921
        addr += l;
922
        len -= l;
923
    }
924
}
925

    
926
static void do_memory_dump(Monitor *mon, const QDict *qdict)
927
{
928
    int count = qdict_get_int(qdict, "count");
929
    int format = qdict_get_int(qdict, "format");
930
    int size = qdict_get_int(qdict, "size");
931
    target_long addr = qdict_get_int(qdict, "addr");
932

    
933
    memory_dump(mon, count, format, size, addr, 0);
934
}
935

    
936
static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
937
{
938
    int count = qdict_get_int(qdict, "count");
939
    int format = qdict_get_int(qdict, "format");
940
    int size = qdict_get_int(qdict, "size");
941
    target_phys_addr_t addr = qdict_get_int(qdict, "addr");
942

    
943
    memory_dump(mon, count, format, size, addr, 1);
944
}
945

    
946
static void do_print(Monitor *mon, const QDict *qdict)
947
{
948
    int format = qdict_get_int(qdict, "format");
949
    target_phys_addr_t val = qdict_get_int(qdict, "val");
950

    
951
#if TARGET_PHYS_ADDR_BITS == 32
952
    switch(format) {
953
    case 'o':
954
        monitor_printf(mon, "%#o", val);
955
        break;
956
    case 'x':
957
        monitor_printf(mon, "%#x", val);
958
        break;
959
    case 'u':
960
        monitor_printf(mon, "%u", val);
961
        break;
962
    default:
963
    case 'd':
964
        monitor_printf(mon, "%d", val);
965
        break;
966
    case 'c':
967
        monitor_printc(mon, val);
968
        break;
969
    }
970
#else
971
    switch(format) {
972
    case 'o':
973
        monitor_printf(mon, "%#" PRIo64, val);
974
        break;
975
    case 'x':
976
        monitor_printf(mon, "%#" PRIx64, val);
977
        break;
978
    case 'u':
979
        monitor_printf(mon, "%" PRIu64, val);
980
        break;
981
    default:
982
    case 'd':
983
        monitor_printf(mon, "%" PRId64, val);
984
        break;
985
    case 'c':
986
        monitor_printc(mon, val);
987
        break;
988
    }
989
#endif
990
    monitor_printf(mon, "\n");
991
}
992

    
993
static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
994
{
995
    FILE *f;
996
    uint32_t size = qdict_get_int(qdict, "size");
997
    const char *filename = qdict_get_str(qdict, "filename");
998
    target_long addr = qdict_get_int(qdict, "val");
999
    uint32_t l;
1000
    CPUState *env;
1001
    uint8_t buf[1024];
1002

    
1003
    env = mon_get_cpu();
1004
    if (!env)
1005
        return;
1006

    
1007
    f = fopen(filename, "wb");
1008
    if (!f) {
1009
        monitor_printf(mon, "could not open '%s'\n", filename);
1010
        return;
1011
    }
1012
    while (size != 0) {
1013
        l = sizeof(buf);
1014
        if (l > size)
1015
            l = size;
1016
        cpu_memory_rw_debug(env, addr, buf, l, 0);
1017
        fwrite(buf, 1, l, f);
1018
        addr += l;
1019
        size -= l;
1020
    }
1021
    fclose(f);
1022
}
1023

    
1024
static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1025
                                    QObject **ret_data)
1026
{
1027
    FILE *f;
1028
    uint32_t l;
1029
    uint8_t buf[1024];
1030
    uint32_t size = qdict_get_int(qdict, "size");
1031
    const char *filename = qdict_get_str(qdict, "filename");
1032
    target_phys_addr_t addr = qdict_get_int(qdict, "val");
1033

    
1034
    f = fopen(filename, "wb");
1035
    if (!f) {
1036
        monitor_printf(mon, "could not open '%s'\n", filename);
1037
        return;
1038
    }
1039
    while (size != 0) {
1040
        l = sizeof(buf);
1041
        if (l > size)
1042
            l = size;
1043
        cpu_physical_memory_rw(addr, buf, l, 0);
1044
        fwrite(buf, 1, l, f);
1045
        fflush(f);
1046
        addr += l;
1047
        size -= l;
1048
    }
1049
    fclose(f);
1050
}
1051

    
1052
static void do_sum(Monitor *mon, const QDict *qdict)
1053
{
1054
    uint32_t addr;
1055
    uint8_t buf[1];
1056
    uint16_t sum;
1057
    uint32_t start = qdict_get_int(qdict, "start");
1058
    uint32_t size = qdict_get_int(qdict, "size");
1059

    
1060
    sum = 0;
1061
    for(addr = start; addr < (start + size); addr++) {
1062
        cpu_physical_memory_rw(addr, buf, 1, 0);
1063
        /* BSD sum algorithm ('sum' Unix command) */
1064
        sum = (sum >> 1) | (sum << 15);
1065
        sum += buf[0];
1066
    }
1067
    monitor_printf(mon, "%05d\n", sum);
1068
}
1069

    
1070
typedef struct {
1071
    int keycode;
1072
    const char *name;
1073
} KeyDef;
1074

    
1075
static const KeyDef key_defs[] = {
1076
    { 0x2a, "shift" },
1077
    { 0x36, "shift_r" },
1078

    
1079
    { 0x38, "alt" },
1080
    { 0xb8, "alt_r" },
1081
    { 0x64, "altgr" },
1082
    { 0xe4, "altgr_r" },
1083
    { 0x1d, "ctrl" },
1084
    { 0x9d, "ctrl_r" },
1085

    
1086
    { 0xdd, "menu" },
1087

    
1088
    { 0x01, "esc" },
1089

    
1090
    { 0x02, "1" },
1091
    { 0x03, "2" },
1092
    { 0x04, "3" },
1093
    { 0x05, "4" },
1094
    { 0x06, "5" },
1095
    { 0x07, "6" },
1096
    { 0x08, "7" },
1097
    { 0x09, "8" },
1098
    { 0x0a, "9" },
1099
    { 0x0b, "0" },
1100
    { 0x0c, "minus" },
1101
    { 0x0d, "equal" },
1102
    { 0x0e, "backspace" },
1103

    
1104
    { 0x0f, "tab" },
1105
    { 0x10, "q" },
1106
    { 0x11, "w" },
1107
    { 0x12, "e" },
1108
    { 0x13, "r" },
1109
    { 0x14, "t" },
1110
    { 0x15, "y" },
1111
    { 0x16, "u" },
1112
    { 0x17, "i" },
1113
    { 0x18, "o" },
1114
    { 0x19, "p" },
1115

    
1116
    { 0x1c, "ret" },
1117

    
1118
    { 0x1e, "a" },
1119
    { 0x1f, "s" },
1120
    { 0x20, "d" },
1121
    { 0x21, "f" },
1122
    { 0x22, "g" },
1123
    { 0x23, "h" },
1124
    { 0x24, "j" },
1125
    { 0x25, "k" },
1126
    { 0x26, "l" },
1127

    
1128
    { 0x2c, "z" },
1129
    { 0x2d, "x" },
1130
    { 0x2e, "c" },
1131
    { 0x2f, "v" },
1132
    { 0x30, "b" },
1133
    { 0x31, "n" },
1134
    { 0x32, "m" },
1135
    { 0x33, "comma" },
1136
    { 0x34, "dot" },
1137
    { 0x35, "slash" },
1138

    
1139
    { 0x37, "asterisk" },
1140

    
1141
    { 0x39, "spc" },
1142
    { 0x3a, "caps_lock" },
1143
    { 0x3b, "f1" },
1144
    { 0x3c, "f2" },
1145
    { 0x3d, "f3" },
1146
    { 0x3e, "f4" },
1147
    { 0x3f, "f5" },
1148
    { 0x40, "f6" },
1149
    { 0x41, "f7" },
1150
    { 0x42, "f8" },
1151
    { 0x43, "f9" },
1152
    { 0x44, "f10" },
1153
    { 0x45, "num_lock" },
1154
    { 0x46, "scroll_lock" },
1155

    
1156
    { 0xb5, "kp_divide" },
1157
    { 0x37, "kp_multiply" },
1158
    { 0x4a, "kp_subtract" },
1159
    { 0x4e, "kp_add" },
1160
    { 0x9c, "kp_enter" },
1161
    { 0x53, "kp_decimal" },
1162
    { 0x54, "sysrq" },
1163

    
1164
    { 0x52, "kp_0" },
1165
    { 0x4f, "kp_1" },
1166
    { 0x50, "kp_2" },
1167
    { 0x51, "kp_3" },
1168
    { 0x4b, "kp_4" },
1169
    { 0x4c, "kp_5" },
1170
    { 0x4d, "kp_6" },
1171
    { 0x47, "kp_7" },
1172
    { 0x48, "kp_8" },
1173
    { 0x49, "kp_9" },
1174

    
1175
    { 0x56, "<" },
1176

    
1177
    { 0x57, "f11" },
1178
    { 0x58, "f12" },
1179

    
1180
    { 0xb7, "print" },
1181

    
1182
    { 0xc7, "home" },
1183
    { 0xc9, "pgup" },
1184
    { 0xd1, "pgdn" },
1185
    { 0xcf, "end" },
1186

    
1187
    { 0xcb, "left" },
1188
    { 0xc8, "up" },
1189
    { 0xd0, "down" },
1190
    { 0xcd, "right" },
1191

    
1192
    { 0xd2, "insert" },
1193
    { 0xd3, "delete" },
1194
#if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1195
    { 0xf0, "stop" },
1196
    { 0xf1, "again" },
1197
    { 0xf2, "props" },
1198
    { 0xf3, "undo" },
1199
    { 0xf4, "front" },
1200
    { 0xf5, "copy" },
1201
    { 0xf6, "open" },
1202
    { 0xf7, "paste" },
1203
    { 0xf8, "find" },
1204
    { 0xf9, "cut" },
1205
    { 0xfa, "lf" },
1206
    { 0xfb, "help" },
1207
    { 0xfc, "meta_l" },
1208
    { 0xfd, "meta_r" },
1209
    { 0xfe, "compose" },
1210
#endif
1211
    { 0, NULL },
1212
};
1213

    
1214
static int get_keycode(const char *key)
1215
{
1216
    const KeyDef *p;
1217
    char *endp;
1218
    int ret;
1219

    
1220
    for(p = key_defs; p->name != NULL; p++) {
1221
        if (!strcmp(key, p->name))
1222
            return p->keycode;
1223
    }
1224
    if (strstart(key, "0x", NULL)) {
1225
        ret = strtoul(key, &endp, 0);
1226
        if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1227
            return ret;
1228
    }
1229
    return -1;
1230
}
1231

    
1232
#define MAX_KEYCODES 16
1233
static uint8_t keycodes[MAX_KEYCODES];
1234
static int nb_pending_keycodes;
1235
static QEMUTimer *key_timer;
1236

    
1237
static void release_keys(void *opaque)
1238
{
1239
    int keycode;
1240

    
1241
    while (nb_pending_keycodes > 0) {
1242
        nb_pending_keycodes--;
1243
        keycode = keycodes[nb_pending_keycodes];
1244
        if (keycode & 0x80)
1245
            kbd_put_keycode(0xe0);
1246
        kbd_put_keycode(keycode | 0x80);
1247
    }
1248
}
1249

    
1250
static void do_sendkey(Monitor *mon, const QDict *qdict)
1251
{
1252
    char keyname_buf[16];
1253
    char *separator;
1254
    int keyname_len, keycode, i;
1255
    const char *string = qdict_get_str(qdict, "string");
1256
    int has_hold_time = qdict_haskey(qdict, "hold_time");
1257
    int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1258

    
1259
    if (nb_pending_keycodes > 0) {
1260
        qemu_del_timer(key_timer);
1261
        release_keys(NULL);
1262
    }
1263
    if (!has_hold_time)
1264
        hold_time = 100;
1265
    i = 0;
1266
    while (1) {
1267
        separator = strchr(string, '-');
1268
        keyname_len = separator ? separator - string : strlen(string);
1269
        if (keyname_len > 0) {
1270
            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1271
            if (keyname_len > sizeof(keyname_buf) - 1) {
1272
                monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1273
                return;
1274
            }
1275
            if (i == MAX_KEYCODES) {
1276
                monitor_printf(mon, "too many keys\n");
1277
                return;
1278
            }
1279
            keyname_buf[keyname_len] = 0;
1280
            keycode = get_keycode(keyname_buf);
1281
            if (keycode < 0) {
1282
                monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1283
                return;
1284
            }
1285
            keycodes[i++] = keycode;
1286
        }
1287
        if (!separator)
1288
            break;
1289
        string = separator + 1;
1290
    }
1291
    nb_pending_keycodes = i;
1292
    /* key down events */
1293
    for (i = 0; i < nb_pending_keycodes; i++) {
1294
        keycode = keycodes[i];
1295
        if (keycode & 0x80)
1296
            kbd_put_keycode(0xe0);
1297
        kbd_put_keycode(keycode & 0x7f);
1298
    }
1299
    /* delayed key up events */
1300
    qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1301
                   muldiv64(get_ticks_per_sec(), hold_time, 1000));
1302
}
1303

    
1304
static int mouse_button_state;
1305

    
1306
static void do_mouse_move(Monitor *mon, const QDict *qdict)
1307
{
1308
    int dx, dy, dz;
1309
    const char *dx_str = qdict_get_str(qdict, "dx_str");
1310
    const char *dy_str = qdict_get_str(qdict, "dy_str");
1311
    const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1312
    dx = strtol(dx_str, NULL, 0);
1313
    dy = strtol(dy_str, NULL, 0);
1314
    dz = 0;
1315
    if (dz_str)
1316
        dz = strtol(dz_str, NULL, 0);
1317
    kbd_mouse_event(dx, dy, dz, mouse_button_state);
1318
}
1319

    
1320
static void do_mouse_button(Monitor *mon, const QDict *qdict)
1321
{
1322
    int button_state = qdict_get_int(qdict, "button_state");
1323
    mouse_button_state = button_state;
1324
    kbd_mouse_event(0, 0, 0, mouse_button_state);
1325
}
1326

    
1327
static void do_ioport_read(Monitor *mon, const QDict *qdict)
1328
{
1329
    int size = qdict_get_int(qdict, "size");
1330
    int addr = qdict_get_int(qdict, "addr");
1331
    int has_index = qdict_haskey(qdict, "index");
1332
    uint32_t val;
1333
    int suffix;
1334

    
1335
    if (has_index) {
1336
        int index = qdict_get_int(qdict, "index");
1337
        cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1338
        addr++;
1339
    }
1340
    addr &= 0xffff;
1341

    
1342
    switch(size) {
1343
    default:
1344
    case 1:
1345
        val = cpu_inb(addr);
1346
        suffix = 'b';
1347
        break;
1348
    case 2:
1349
        val = cpu_inw(addr);
1350
        suffix = 'w';
1351
        break;
1352
    case 4:
1353
        val = cpu_inl(addr);
1354
        suffix = 'l';
1355
        break;
1356
    }
1357
    monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1358
                   suffix, addr, size * 2, val);
1359
}
1360

    
1361
static void do_ioport_write(Monitor *mon, const QDict *qdict)
1362
{
1363
    int size = qdict_get_int(qdict, "size");
1364
    int addr = qdict_get_int(qdict, "addr");
1365
    int val = qdict_get_int(qdict, "val");
1366

    
1367
    addr &= IOPORTS_MASK;
1368

    
1369
    switch (size) {
1370
    default:
1371
    case 1:
1372
        cpu_outb(addr, val);
1373
        break;
1374
    case 2:
1375
        cpu_outw(addr, val);
1376
        break;
1377
    case 4:
1378
        cpu_outl(addr, val);
1379
        break;
1380
    }
1381
}
1382

    
1383
static void do_boot_set(Monitor *mon, const QDict *qdict)
1384
{
1385
    int res;
1386
    const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1387

    
1388
    res = qemu_boot_set(bootdevice);
1389
    if (res == 0) {
1390
        monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1391
    } else if (res > 0) {
1392
        monitor_printf(mon, "setting boot device list failed\n");
1393
    } else {
1394
        monitor_printf(mon, "no function defined to set boot device list for "
1395
                       "this architecture\n");
1396
    }
1397
}
1398

    
1399
/**
1400
 * do_system_reset(): Issue a machine reset
1401
 */
1402
static void do_system_reset(Monitor *mon, const QDict *qdict,
1403
                            QObject **ret_data)
1404
{
1405
    qemu_system_reset_request();
1406
}
1407

    
1408
/**
1409
 * do_system_powerdown(): Issue a machine powerdown
1410
 */
1411
static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1412
                                QObject **ret_data)
1413
{
1414
    qemu_system_powerdown_request();
1415
}
1416

    
1417
#if defined(TARGET_I386)
1418
static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1419
{
1420
    monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1421
                   addr,
1422
                   pte & mask,
1423
                   pte & PG_GLOBAL_MASK ? 'G' : '-',
1424
                   pte & PG_PSE_MASK ? 'P' : '-',
1425
                   pte & PG_DIRTY_MASK ? 'D' : '-',
1426
                   pte & PG_ACCESSED_MASK ? 'A' : '-',
1427
                   pte & PG_PCD_MASK ? 'C' : '-',
1428
                   pte & PG_PWT_MASK ? 'T' : '-',
1429
                   pte & PG_USER_MASK ? 'U' : '-',
1430
                   pte & PG_RW_MASK ? 'W' : '-');
1431
}
1432

    
1433
static void tlb_info(Monitor *mon)
1434
{
1435
    CPUState *env;
1436
    int l1, l2;
1437
    uint32_t pgd, pde, pte;
1438

    
1439
    env = mon_get_cpu();
1440
    if (!env)
1441
        return;
1442

    
1443
    if (!(env->cr[0] & CR0_PG_MASK)) {
1444
        monitor_printf(mon, "PG disabled\n");
1445
        return;
1446
    }
1447
    pgd = env->cr[3] & ~0xfff;
1448
    for(l1 = 0; l1 < 1024; l1++) {
1449
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1450
        pde = le32_to_cpu(pde);
1451
        if (pde & PG_PRESENT_MASK) {
1452
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1453
                print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1454
            } else {
1455
                for(l2 = 0; l2 < 1024; l2++) {
1456
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1457
                                             (uint8_t *)&pte, 4);
1458
                    pte = le32_to_cpu(pte);
1459
                    if (pte & PG_PRESENT_MASK) {
1460
                        print_pte(mon, (l1 << 22) + (l2 << 12),
1461
                                  pte & ~PG_PSE_MASK,
1462
                                  ~0xfff);
1463
                    }
1464
                }
1465
            }
1466
        }
1467
    }
1468
}
1469

    
1470
static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1471
                      uint32_t end, int prot)
1472
{
1473
    int prot1;
1474
    prot1 = *plast_prot;
1475
    if (prot != prot1) {
1476
        if (*pstart != -1) {
1477
            monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1478
                           *pstart, end, end - *pstart,
1479
                           prot1 & PG_USER_MASK ? 'u' : '-',
1480
                           'r',
1481
                           prot1 & PG_RW_MASK ? 'w' : '-');
1482
        }
1483
        if (prot != 0)
1484
            *pstart = end;
1485
        else
1486
            *pstart = -1;
1487
        *plast_prot = prot;
1488
    }
1489
}
1490

    
1491
static void mem_info(Monitor *mon)
1492
{
1493
    CPUState *env;
1494
    int l1, l2, prot, last_prot;
1495
    uint32_t pgd, pde, pte, start, end;
1496

    
1497
    env = mon_get_cpu();
1498
    if (!env)
1499
        return;
1500

    
1501
    if (!(env->cr[0] & CR0_PG_MASK)) {
1502
        monitor_printf(mon, "PG disabled\n");
1503
        return;
1504
    }
1505
    pgd = env->cr[3] & ~0xfff;
1506
    last_prot = 0;
1507
    start = -1;
1508
    for(l1 = 0; l1 < 1024; l1++) {
1509
        cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1510
        pde = le32_to_cpu(pde);
1511
        end = l1 << 22;
1512
        if (pde & PG_PRESENT_MASK) {
1513
            if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1514
                prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1515
                mem_print(mon, &start, &last_prot, end, prot);
1516
            } else {
1517
                for(l2 = 0; l2 < 1024; l2++) {
1518
                    cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1519
                                             (uint8_t *)&pte, 4);
1520
                    pte = le32_to_cpu(pte);
1521
                    end = (l1 << 22) + (l2 << 12);
1522
                    if (pte & PG_PRESENT_MASK) {
1523
                        prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1524
                    } else {
1525
                        prot = 0;
1526
                    }
1527
                    mem_print(mon, &start, &last_prot, end, prot);
1528
                }
1529
            }
1530
        } else {
1531
            prot = 0;
1532
            mem_print(mon, &start, &last_prot, end, prot);
1533
        }
1534
    }
1535
}
1536
#endif
1537

    
1538
#if defined(TARGET_SH4)
1539

    
1540
static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1541
{
1542
    monitor_printf(mon, " tlb%i:\t"
1543
                   "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1544
                   "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1545
                   "dirty=%hhu writethrough=%hhu\n",
1546
                   idx,
1547
                   tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1548
                   tlb->v, tlb->sh, tlb->c, tlb->pr,
1549
                   tlb->d, tlb->wt);
1550
}
1551

    
1552
static void tlb_info(Monitor *mon)
1553
{
1554
    CPUState *env = mon_get_cpu();
1555
    int i;
1556

    
1557
    monitor_printf (mon, "ITLB:\n");
1558
    for (i = 0 ; i < ITLB_SIZE ; i++)
1559
        print_tlb (mon, i, &env->itlb[i]);
1560
    monitor_printf (mon, "UTLB:\n");
1561
    for (i = 0 ; i < UTLB_SIZE ; i++)
1562
        print_tlb (mon, i, &env->utlb[i]);
1563
}
1564

    
1565
#endif
1566

    
1567
static void do_info_kvm(Monitor *mon)
1568
{
1569
#ifdef CONFIG_KVM
1570
    monitor_printf(mon, "kvm support: ");
1571
    if (kvm_enabled())
1572
        monitor_printf(mon, "enabled\n");
1573
    else
1574
        monitor_printf(mon, "disabled\n");
1575
#else
1576
    monitor_printf(mon, "kvm support: not compiled\n");
1577
#endif
1578
}
1579

    
1580
static void do_info_numa(Monitor *mon)
1581
{
1582
    int i;
1583
    CPUState *env;
1584

    
1585
    monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1586
    for (i = 0; i < nb_numa_nodes; i++) {
1587
        monitor_printf(mon, "node %d cpus:", i);
1588
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1589
            if (env->numa_node == i) {
1590
                monitor_printf(mon, " %d", env->cpu_index);
1591
            }
1592
        }
1593
        monitor_printf(mon, "\n");
1594
        monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1595
            node_mem[i] >> 20);
1596
    }
1597
}
1598

    
1599
#ifdef CONFIG_PROFILER
1600

    
1601
int64_t qemu_time;
1602
int64_t dev_time;
1603

    
1604
static void do_info_profile(Monitor *mon)
1605
{
1606
    int64_t total;
1607
    total = qemu_time;
1608
    if (total == 0)
1609
        total = 1;
1610
    monitor_printf(mon, "async time  %" PRId64 " (%0.3f)\n",
1611
                   dev_time, dev_time / (double)get_ticks_per_sec());
1612
    monitor_printf(mon, "qemu time   %" PRId64 " (%0.3f)\n",
1613
                   qemu_time, qemu_time / (double)get_ticks_per_sec());
1614
    qemu_time = 0;
1615
    dev_time = 0;
1616
}
1617
#else
1618
static void do_info_profile(Monitor *mon)
1619
{
1620
    monitor_printf(mon, "Internal profiler not compiled\n");
1621
}
1622
#endif
1623

    
1624
/* Capture support */
1625
static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1626

    
1627
static void do_info_capture(Monitor *mon)
1628
{
1629
    int i;
1630
    CaptureState *s;
1631

    
1632
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1633
        monitor_printf(mon, "[%d]: ", i);
1634
        s->ops.info (s->opaque);
1635
    }
1636
}
1637

    
1638
#ifdef HAS_AUDIO
1639
static void do_stop_capture(Monitor *mon, const QDict *qdict)
1640
{
1641
    int i;
1642
    int n = qdict_get_int(qdict, "n");
1643
    CaptureState *s;
1644

    
1645
    for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1646
        if (i == n) {
1647
            s->ops.destroy (s->opaque);
1648
            QLIST_REMOVE (s, entries);
1649
            qemu_free (s);
1650
            return;
1651
        }
1652
    }
1653
}
1654

    
1655
static void do_wav_capture(Monitor *mon, const QDict *qdict)
1656
{
1657
    const char *path = qdict_get_str(qdict, "path");
1658
    int has_freq = qdict_haskey(qdict, "freq");
1659
    int freq = qdict_get_try_int(qdict, "freq", -1);
1660
    int has_bits = qdict_haskey(qdict, "bits");
1661
    int bits = qdict_get_try_int(qdict, "bits", -1);
1662
    int has_channels = qdict_haskey(qdict, "nchannels");
1663
    int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1664
    CaptureState *s;
1665

    
1666
    s = qemu_mallocz (sizeof (*s));
1667

    
1668
    freq = has_freq ? freq : 44100;
1669
    bits = has_bits ? bits : 16;
1670
    nchannels = has_channels ? nchannels : 2;
1671

    
1672
    if (wav_start_capture (s, path, freq, bits, nchannels)) {
1673
        monitor_printf(mon, "Faied to add wave capture\n");
1674
        qemu_free (s);
1675
    }
1676
    QLIST_INSERT_HEAD (&capture_head, s, entries);
1677
}
1678
#endif
1679

    
1680
#if defined(TARGET_I386)
1681
static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1682
{
1683
    CPUState *env;
1684
    int cpu_index = qdict_get_int(qdict, "cpu_index");
1685

    
1686
    for (env = first_cpu; env != NULL; env = env->next_cpu)
1687
        if (env->cpu_index == cpu_index) {
1688
            cpu_interrupt(env, CPU_INTERRUPT_NMI);
1689
            break;
1690
        }
1691
}
1692
#endif
1693

    
1694
static void do_info_status(Monitor *mon)
1695
{
1696
    if (vm_running) {
1697
        if (singlestep) {
1698
            monitor_printf(mon, "VM status: running (single step mode)\n");
1699
        } else {
1700
            monitor_printf(mon, "VM status: running\n");
1701
        }
1702
    } else
1703
       monitor_printf(mon, "VM status: paused\n");
1704
}
1705

    
1706
/**
1707
 * do_balloon(): Request VM to change its memory allocation
1708
 */
1709
static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
1710
{
1711
    int value = qdict_get_int(qdict, "value");
1712
    ram_addr_t target = value;
1713
    qemu_balloon(target << 20);
1714
}
1715

    
1716
static void monitor_print_balloon(Monitor *mon, const QObject *data)
1717
{
1718
    monitor_printf(mon, "balloon: actual=%d\n",
1719
                                     (int)qint_get_int(qobject_to_qint(data)));
1720
}
1721

    
1722
/**
1723
 * do_info_balloon(): Balloon information
1724
 */
1725
static void do_info_balloon(Monitor *mon, QObject **ret_data)
1726
{
1727
    ram_addr_t actual;
1728

    
1729
    actual = qemu_balloon_status();
1730
    if (kvm_enabled() && !kvm_has_sync_mmu())
1731
        qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
1732
    else if (actual == 0)
1733
        qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
1734
    else
1735
        *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
1736
}
1737

    
1738
static qemu_acl *find_acl(Monitor *mon, const char *name)
1739
{
1740
    qemu_acl *acl = qemu_acl_find(name);
1741

    
1742
    if (!acl) {
1743
        monitor_printf(mon, "acl: unknown list '%s'\n", name);
1744
    }
1745
    return acl;
1746
}
1747

    
1748
static void do_acl_show(Monitor *mon, const QDict *qdict)
1749
{
1750
    const char *aclname = qdict_get_str(qdict, "aclname");
1751
    qemu_acl *acl = find_acl(mon, aclname);
1752
    qemu_acl_entry *entry;
1753
    int i = 0;
1754

    
1755
    if (acl) {
1756
        monitor_printf(mon, "policy: %s\n",
1757
                       acl->defaultDeny ? "deny" : "allow");
1758
        QTAILQ_FOREACH(entry, &acl->entries, next) {
1759
            i++;
1760
            monitor_printf(mon, "%d: %s %s\n", i,
1761
                           entry->deny ? "deny" : "allow", entry->match);
1762
        }
1763
    }
1764
}
1765

    
1766
static void do_acl_reset(Monitor *mon, const QDict *qdict)
1767
{
1768
    const char *aclname = qdict_get_str(qdict, "aclname");
1769
    qemu_acl *acl = find_acl(mon, aclname);
1770

    
1771
    if (acl) {
1772
        qemu_acl_reset(acl);
1773
        monitor_printf(mon, "acl: removed all rules\n");
1774
    }
1775
}
1776

    
1777
static void do_acl_policy(Monitor *mon, const QDict *qdict)
1778
{
1779
    const char *aclname = qdict_get_str(qdict, "aclname");
1780
    const char *policy = qdict_get_str(qdict, "policy");
1781
    qemu_acl *acl = find_acl(mon, aclname);
1782

    
1783
    if (acl) {
1784
        if (strcmp(policy, "allow") == 0) {
1785
            acl->defaultDeny = 0;
1786
            monitor_printf(mon, "acl: policy set to 'allow'\n");
1787
        } else if (strcmp(policy, "deny") == 0) {
1788
            acl->defaultDeny = 1;
1789
            monitor_printf(mon, "acl: policy set to 'deny'\n");
1790
        } else {
1791
            monitor_printf(mon, "acl: unknown policy '%s', "
1792
                           "expected 'deny' or 'allow'\n", policy);
1793
        }
1794
    }
1795
}
1796

    
1797
static void do_acl_add(Monitor *mon, const QDict *qdict)
1798
{
1799
    const char *aclname = qdict_get_str(qdict, "aclname");
1800
    const char *match = qdict_get_str(qdict, "match");
1801
    const char *policy = qdict_get_str(qdict, "policy");
1802
    int has_index = qdict_haskey(qdict, "index");
1803
    int index = qdict_get_try_int(qdict, "index", -1);
1804
    qemu_acl *acl = find_acl(mon, aclname);
1805
    int deny, ret;
1806

    
1807
    if (acl) {
1808
        if (strcmp(policy, "allow") == 0) {
1809
            deny = 0;
1810
        } else if (strcmp(policy, "deny") == 0) {
1811
            deny = 1;
1812
        } else {
1813
            monitor_printf(mon, "acl: unknown policy '%s', "
1814
                           "expected 'deny' or 'allow'\n", policy);
1815
            return;
1816
        }
1817
        if (has_index)
1818
            ret = qemu_acl_insert(acl, deny, match, index);
1819
        else
1820
            ret = qemu_acl_append(acl, deny, match);
1821
        if (ret < 0)
1822
            monitor_printf(mon, "acl: unable to add acl entry\n");
1823
        else
1824
            monitor_printf(mon, "acl: added rule at position %d\n", ret);
1825
    }
1826
}
1827

    
1828
static void do_acl_remove(Monitor *mon, const QDict *qdict)
1829
{
1830
    const char *aclname = qdict_get_str(qdict, "aclname");
1831
    const char *match = qdict_get_str(qdict, "match");
1832
    qemu_acl *acl = find_acl(mon, aclname);
1833
    int ret;
1834

    
1835
    if (acl) {
1836
        ret = qemu_acl_remove(acl, match);
1837
        if (ret < 0)
1838
            monitor_printf(mon, "acl: no matching acl entry\n");
1839
        else
1840
            monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1841
    }
1842
}
1843

    
1844
#if defined(TARGET_I386)
1845
static void do_inject_mce(Monitor *mon, const QDict *qdict)
1846
{
1847
    CPUState *cenv;
1848
    int cpu_index = qdict_get_int(qdict, "cpu_index");
1849
    int bank = qdict_get_int(qdict, "bank");
1850
    uint64_t status = qdict_get_int(qdict, "status");
1851
    uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1852
    uint64_t addr = qdict_get_int(qdict, "addr");
1853
    uint64_t misc = qdict_get_int(qdict, "misc");
1854

    
1855
    for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1856
        if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1857
            cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1858
            break;
1859
        }
1860
}
1861
#endif
1862

    
1863
static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
1864
{
1865
    const char *fdname = qdict_get_str(qdict, "fdname");
1866
    mon_fd_t *monfd;
1867
    int fd;
1868

    
1869
    fd = qemu_chr_get_msgfd(mon->chr);
1870
    if (fd == -1) {
1871
        monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1872
        return;
1873
    }
1874

    
1875
    if (qemu_isdigit(fdname[0])) {
1876
        monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1877
        return;
1878
    }
1879

    
1880
    fd = dup(fd);
1881
    if (fd == -1) {
1882
        monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1883
                       strerror(errno));
1884
        return;
1885
    }
1886

    
1887
    QLIST_FOREACH(monfd, &mon->fds, next) {
1888
        if (strcmp(monfd->name, fdname) != 0) {
1889
            continue;
1890
        }
1891

    
1892
        close(monfd->fd);
1893
        monfd->fd = fd;
1894
        return;
1895
    }
1896

    
1897
    monfd = qemu_mallocz(sizeof(mon_fd_t));
1898
    monfd->name = qemu_strdup(fdname);
1899
    monfd->fd = fd;
1900

    
1901
    QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1902
}
1903

    
1904
static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
1905
{
1906
    const char *fdname = qdict_get_str(qdict, "fdname");
1907
    mon_fd_t *monfd;
1908

    
1909
    QLIST_FOREACH(monfd, &mon->fds, next) {
1910
        if (strcmp(monfd->name, fdname) != 0) {
1911
            continue;
1912
        }
1913

    
1914
        QLIST_REMOVE(monfd, next);
1915
        close(monfd->fd);
1916
        qemu_free(monfd->name);
1917
        qemu_free(monfd);
1918
        return;
1919
    }
1920

    
1921
    monitor_printf(mon, "Failed to find file descriptor named %s\n",
1922
                   fdname);
1923
}
1924

    
1925
static void do_loadvm(Monitor *mon, const QDict *qdict)
1926
{
1927
    int saved_vm_running  = vm_running;
1928
    const char *name = qdict_get_str(qdict, "name");
1929

    
1930
    vm_stop(0);
1931

    
1932
    if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1933
        vm_start();
1934
}
1935

    
1936
int monitor_get_fd(Monitor *mon, const char *fdname)
1937
{
1938
    mon_fd_t *monfd;
1939

    
1940
    QLIST_FOREACH(monfd, &mon->fds, next) {
1941
        int fd;
1942

    
1943
        if (strcmp(monfd->name, fdname) != 0) {
1944
            continue;
1945
        }
1946

    
1947
        fd = monfd->fd;
1948

    
1949
        /* caller takes ownership of fd */
1950
        QLIST_REMOVE(monfd, next);
1951
        qemu_free(monfd->name);
1952
        qemu_free(monfd);
1953

    
1954
        return fd;
1955
    }
1956

    
1957
    return -1;
1958
}
1959

    
1960
static const mon_cmd_t mon_cmds[] = {
1961
#include "qemu-monitor.h"
1962
    { NULL, NULL, },
1963
};
1964

    
1965
/* Please update qemu-monitor.hx when adding or changing commands */
1966
static const mon_cmd_t info_cmds[] = {
1967
    {
1968
        .name       = "version",
1969
        .args_type  = "",
1970
        .params     = "",
1971
        .help       = "show the version of QEMU",
1972
        .user_print = monitor_print_qobject,
1973
        .mhandler.info_new = do_info_version,
1974
    },
1975
    {
1976
        .name       = "network",
1977
        .args_type  = "",
1978
        .params     = "",
1979
        .help       = "show the network state",
1980
        .mhandler.info = do_info_network,
1981
    },
1982
    {
1983
        .name       = "chardev",
1984
        .args_type  = "",
1985
        .params     = "",
1986
        .help       = "show the character devices",
1987
        .mhandler.info = qemu_chr_info,
1988
    },
1989
    {
1990
        .name       = "block",
1991
        .args_type  = "",
1992
        .params     = "",
1993
        .help       = "show the block devices",
1994
        .mhandler.info = bdrv_info,
1995
    },
1996
    {
1997
        .name       = "blockstats",
1998
        .args_type  = "",
1999
        .params     = "",
2000
        .help       = "show block device statistics",
2001
        .mhandler.info = bdrv_info_stats,
2002
    },
2003
    {
2004
        .name       = "registers",
2005
        .args_type  = "",
2006
        .params     = "",
2007
        .help       = "show the cpu registers",
2008
        .mhandler.info = do_info_registers,
2009
    },
2010
    {
2011
        .name       = "cpus",
2012
        .args_type  = "",
2013
        .params     = "",
2014
        .help       = "show infos for each CPU",
2015
        .user_print = monitor_print_cpus,
2016
        .mhandler.info_new = do_info_cpus,
2017
    },
2018
    {
2019
        .name       = "history",
2020
        .args_type  = "",
2021
        .params     = "",
2022
        .help       = "show the command line history",
2023
        .mhandler.info = do_info_history,
2024
    },
2025
    {
2026
        .name       = "irq",
2027
        .args_type  = "",
2028
        .params     = "",
2029
        .help       = "show the interrupts statistics (if available)",
2030
        .mhandler.info = irq_info,
2031
    },
2032
    {
2033
        .name       = "pic",
2034
        .args_type  = "",
2035
        .params     = "",
2036
        .help       = "show i8259 (PIC) state",
2037
        .mhandler.info = pic_info,
2038
    },
2039
    {
2040
        .name       = "pci",
2041
        .args_type  = "",
2042
        .params     = "",
2043
        .help       = "show PCI info",
2044
        .mhandler.info = pci_info,
2045
    },
2046
#if defined(TARGET_I386) || defined(TARGET_SH4)
2047
    {
2048
        .name       = "tlb",
2049
        .args_type  = "",
2050
        .params     = "",
2051
        .help       = "show virtual to physical memory mappings",
2052
        .mhandler.info = tlb_info,
2053
    },
2054
#endif
2055
#if defined(TARGET_I386)
2056
    {
2057
        .name       = "mem",
2058
        .args_type  = "",
2059
        .params     = "",
2060
        .help       = "show the active virtual memory mappings",
2061
        .mhandler.info = mem_info,
2062
    },
2063
    {
2064
        .name       = "hpet",
2065
        .args_type  = "",
2066
        .params     = "",
2067
        .help       = "show state of HPET",
2068
        .mhandler.info = do_info_hpet,
2069
    },
2070
#endif
2071
    {
2072
        .name       = "jit",
2073
        .args_type  = "",
2074
        .params     = "",
2075
        .help       = "show dynamic compiler info",
2076
        .mhandler.info = do_info_jit,
2077
    },
2078
    {
2079
        .name       = "kvm",
2080
        .args_type  = "",
2081
        .params     = "",
2082
        .help       = "show KVM information",
2083
        .mhandler.info = do_info_kvm,
2084
    },
2085
    {
2086
        .name       = "numa",
2087
        .args_type  = "",
2088
        .params     = "",
2089
        .help       = "show NUMA information",
2090
        .mhandler.info = do_info_numa,
2091
    },
2092
    {
2093
        .name       = "usb",
2094
        .args_type  = "",
2095
        .params     = "",
2096
        .help       = "show guest USB devices",
2097
        .mhandler.info = usb_info,
2098
    },
2099
    {
2100
        .name       = "usbhost",
2101
        .args_type  = "",
2102
        .params     = "",
2103
        .help       = "show host USB devices",
2104
        .mhandler.info = usb_host_info,
2105
    },
2106
    {
2107
        .name       = "profile",
2108
        .args_type  = "",
2109
        .params     = "",
2110
        .help       = "show profiling information",
2111
        .mhandler.info = do_info_profile,
2112
    },
2113
    {
2114
        .name       = "capture",
2115
        .args_type  = "",
2116
        .params     = "",
2117
        .help       = "show capture information",
2118
        .mhandler.info = do_info_capture,
2119
    },
2120
    {
2121
        .name       = "snapshots",
2122
        .args_type  = "",
2123
        .params     = "",
2124
        .help       = "show the currently saved VM snapshots",
2125
        .mhandler.info = do_info_snapshots,
2126
    },
2127
    {
2128
        .name       = "status",
2129
        .args_type  = "",
2130
        .params     = "",
2131
        .help       = "show the current VM status (running|paused)",
2132
        .mhandler.info = do_info_status,
2133
    },
2134
    {
2135
        .name       = "pcmcia",
2136
        .args_type  = "",
2137
        .params     = "",
2138
        .help       = "show guest PCMCIA status",
2139
        .mhandler.info = pcmcia_info,
2140
    },
2141
    {
2142
        .name       = "mice",
2143
        .args_type  = "",
2144
        .params     = "",
2145
        .help       = "show which guest mouse is receiving events",
2146
        .mhandler.info = do_info_mice,
2147
    },
2148
    {
2149
        .name       = "vnc",
2150
        .args_type  = "",
2151
        .params     = "",
2152
        .help       = "show the vnc server status",
2153
        .mhandler.info = do_info_vnc,
2154
    },
2155
    {
2156
        .name       = "name",
2157
        .args_type  = "",
2158
        .params     = "",
2159
        .help       = "show the current VM name",
2160
        .mhandler.info = do_info_name,
2161
    },
2162
    {
2163
        .name       = "uuid",
2164
        .args_type  = "",
2165
        .params     = "",
2166
        .help       = "show the current VM UUID",
2167
        .mhandler.info = do_info_uuid,
2168
    },
2169
#if defined(TARGET_PPC)
2170
    {
2171
        .name       = "cpustats",
2172
        .args_type  = "",
2173
        .params     = "",
2174
        .help       = "show CPU statistics",
2175
        .mhandler.info = do_info_cpu_stats,
2176
    },
2177
#endif
2178
#if defined(CONFIG_SLIRP)
2179
    {
2180
        .name       = "usernet",
2181
        .args_type  = "",
2182
        .params     = "",
2183
        .help       = "show user network stack connection states",
2184
        .mhandler.info = do_info_usernet,
2185
    },
2186
#endif
2187
    {
2188
        .name       = "migrate",
2189
        .args_type  = "",
2190
        .params     = "",
2191
        .help       = "show migration status",
2192
        .mhandler.info = do_info_migrate,
2193
    },
2194
    {
2195
        .name       = "balloon",
2196
        .args_type  = "",
2197
        .params     = "",
2198
        .help       = "show balloon information",
2199
        .user_print = monitor_print_balloon,
2200
        .mhandler.info_new = do_info_balloon,
2201
    },
2202
    {
2203
        .name       = "qtree",
2204
        .args_type  = "",
2205
        .params     = "",
2206
        .help       = "show device tree",
2207
        .mhandler.info = do_info_qtree,
2208
    },
2209
    {
2210
        .name       = "qdm",
2211
        .args_type  = "",
2212
        .params     = "",
2213
        .help       = "show qdev device model list",
2214
        .mhandler.info = do_info_qdm,
2215
    },
2216
    {
2217
        .name       = "roms",
2218
        .args_type  = "",
2219
        .params     = "",
2220
        .help       = "show roms",
2221
        .mhandler.info = do_info_roms,
2222
    },
2223
    {
2224
        .name       = NULL,
2225
    },
2226
};
2227

    
2228
/*******************************************************************/
2229

    
2230
static const char *pch;
2231
static jmp_buf expr_env;
2232

    
2233
#define MD_TLONG 0
2234
#define MD_I32   1
2235

    
2236
typedef struct MonitorDef {
2237
    const char *name;
2238
    int offset;
2239
    target_long (*get_value)(const struct MonitorDef *md, int val);
2240
    int type;
2241
} MonitorDef;
2242

    
2243
#if defined(TARGET_I386)
2244
static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2245
{
2246
    CPUState *env = mon_get_cpu();
2247
    if (!env)
2248
        return 0;
2249
    return env->eip + env->segs[R_CS].base;
2250
}
2251
#endif
2252

    
2253
#if defined(TARGET_PPC)
2254
static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2255
{
2256
    CPUState *env = mon_get_cpu();
2257
    unsigned int u;
2258
    int i;
2259

    
2260
    if (!env)
2261
        return 0;
2262

    
2263
    u = 0;
2264
    for (i = 0; i < 8; i++)
2265
        u |= env->crf[i] << (32 - (4 * i));
2266

    
2267
    return u;
2268
}
2269

    
2270
static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2271
{
2272
    CPUState *env = mon_get_cpu();
2273
    if (!env)
2274
        return 0;
2275
    return env->msr;
2276
}
2277

    
2278
static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2279
{
2280
    CPUState *env = mon_get_cpu();
2281
    if (!env)
2282
        return 0;
2283
    return env->xer;
2284
}
2285

    
2286
static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2287
{
2288
    CPUState *env = mon_get_cpu();
2289
    if (!env)
2290
        return 0;
2291
    return cpu_ppc_load_decr(env);
2292
}
2293

    
2294
static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2295
{
2296
    CPUState *env = mon_get_cpu();
2297
    if (!env)
2298
        return 0;
2299
    return cpu_ppc_load_tbu(env);
2300
}
2301

    
2302
static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2303
{
2304
    CPUState *env = mon_get_cpu();
2305
    if (!env)
2306
        return 0;
2307
    return cpu_ppc_load_tbl(env);
2308
}
2309
#endif
2310

    
2311
#if defined(TARGET_SPARC)
2312
#ifndef TARGET_SPARC64
2313
static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2314
{
2315
    CPUState *env = mon_get_cpu();
2316
    if (!env)
2317
        return 0;
2318
    return GET_PSR(env);
2319
}
2320
#endif
2321

    
2322
static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2323
{
2324
    CPUState *env = mon_get_cpu();
2325
    if (!env)
2326
        return 0;
2327
    return env->regwptr[val];
2328
}
2329
#endif
2330

    
2331
static const MonitorDef monitor_defs[] = {
2332
#ifdef TARGET_I386
2333

    
2334
#define SEG(name, seg) \
2335
    { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2336
    { name ".base", offsetof(CPUState, segs[seg].base) },\
2337
    { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2338

    
2339
    { "eax", offsetof(CPUState, regs[0]) },
2340
    { "ecx", offsetof(CPUState, regs[1]) },
2341
    { "edx", offsetof(CPUState, regs[2]) },
2342
    { "ebx", offsetof(CPUState, regs[3]) },
2343
    { "esp|sp", offsetof(CPUState, regs[4]) },
2344
    { "ebp|fp", offsetof(CPUState, regs[5]) },
2345
    { "esi", offsetof(CPUState, regs[6]) },
2346
    { "edi", offsetof(CPUState, regs[7]) },
2347
#ifdef TARGET_X86_64
2348
    { "r8", offsetof(CPUState, regs[8]) },
2349
    { "r9", offsetof(CPUState, regs[9]) },
2350
    { "r10", offsetof(CPUState, regs[10]) },
2351
    { "r11", offsetof(CPUState, regs[11]) },
2352
    { "r12", offsetof(CPUState, regs[12]) },
2353
    { "r13", offsetof(CPUState, regs[13]) },
2354
    { "r14", offsetof(CPUState, regs[14]) },
2355
    { "r15", offsetof(CPUState, regs[15]) },
2356
#endif
2357
    { "eflags", offsetof(CPUState, eflags) },
2358
    { "eip", offsetof(CPUState, eip) },
2359
    SEG("cs", R_CS)
2360
    SEG("ds", R_DS)
2361
    SEG("es", R_ES)
2362
    SEG("ss", R_SS)
2363
    SEG("fs", R_FS)
2364
    SEG("gs", R_GS)
2365
    { "pc", 0, monitor_get_pc, },
2366
#elif defined(TARGET_PPC)
2367
    /* General purpose registers */
2368
    { "r0", offsetof(CPUState, gpr[0]) },
2369
    { "r1", offsetof(CPUState, gpr[1]) },
2370
    { "r2", offsetof(CPUState, gpr[2]) },
2371
    { "r3", offsetof(CPUState, gpr[3]) },
2372
    { "r4", offsetof(CPUState, gpr[4]) },
2373
    { "r5", offsetof(CPUState, gpr[5]) },
2374
    { "r6", offsetof(CPUState, gpr[6]) },
2375
    { "r7", offsetof(CPUState, gpr[7]) },
2376
    { "r8", offsetof(CPUState, gpr[8]) },
2377
    { "r9", offsetof(CPUState, gpr[9]) },
2378
    { "r10", offsetof(CPUState, gpr[10]) },
2379
    { "r11", offsetof(CPUState, gpr[11]) },
2380
    { "r12", offsetof(CPUState, gpr[12]) },
2381
    { "r13", offsetof(CPUState, gpr[13]) },
2382
    { "r14", offsetof(CPUState, gpr[14]) },
2383
    { "r15", offsetof(CPUState, gpr[15]) },
2384
    { "r16", offsetof(CPUState, gpr[16]) },
2385
    { "r17", offsetof(CPUState, gpr[17]) },
2386
    { "r18", offsetof(CPUState, gpr[18]) },
2387
    { "r19", offsetof(CPUState, gpr[19]) },
2388
    { "r20", offsetof(CPUState, gpr[20]) },
2389
    { "r21", offsetof(CPUState, gpr[21]) },
2390
    { "r22", offsetof(CPUState, gpr[22]) },
2391
    { "r23", offsetof(CPUState, gpr[23]) },
2392
    { "r24", offsetof(CPUState, gpr[24]) },
2393
    { "r25", offsetof(CPUState, gpr[25]) },
2394
    { "r26", offsetof(CPUState, gpr[26]) },
2395
    { "r27", offsetof(CPUState, gpr[27]) },
2396
    { "r28", offsetof(CPUState, gpr[28]) },
2397
    { "r29", offsetof(CPUState, gpr[29]) },
2398
    { "r30", offsetof(CPUState, gpr[30]) },
2399
    { "r31", offsetof(CPUState, gpr[31]) },
2400
    /* Floating point registers */
2401
    { "f0", offsetof(CPUState, fpr[0]) },
2402
    { "f1", offsetof(CPUState, fpr[1]) },
2403
    { "f2", offsetof(CPUState, fpr[2]) },
2404
    { "f3", offsetof(CPUState, fpr[3]) },
2405
    { "f4", offsetof(CPUState, fpr[4]) },
2406
    { "f5", offsetof(CPUState, fpr[5]) },
2407
    { "f6", offsetof(CPUState, fpr[6]) },
2408
    { "f7", offsetof(CPUState, fpr[7]) },
2409
    { "f8", offsetof(CPUState, fpr[8]) },
2410
    { "f9", offsetof(CPUState, fpr[9]) },
2411
    { "f10", offsetof(CPUState, fpr[10]) },
2412
    { "f11", offsetof(CPUState, fpr[11]) },
2413
    { "f12", offsetof(CPUState, fpr[12]) },
2414
    { "f13", offsetof(CPUState, fpr[13]) },
2415
    { "f14", offsetof(CPUState, fpr[14]) },
2416
    { "f15", offsetof(CPUState, fpr[15]) },
2417
    { "f16", offsetof(CPUState, fpr[16]) },
2418
    { "f17", offsetof(CPUState, fpr[17]) },
2419
    { "f18", offsetof(CPUState, fpr[18]) },
2420
    { "f19", offsetof(CPUState, fpr[19]) },
2421
    { "f20", offsetof(CPUState, fpr[20]) },
2422
    { "f21", offsetof(CPUState, fpr[21]) },
2423
    { "f22", offsetof(CPUState, fpr[22]) },
2424
    { "f23", offsetof(CPUState, fpr[23]) },
2425
    { "f24", offsetof(CPUState, fpr[24]) },
2426
    { "f25", offsetof(CPUState, fpr[25]) },
2427
    { "f26", offsetof(CPUState, fpr[26]) },
2428
    { "f27", offsetof(CPUState, fpr[27]) },
2429
    { "f28", offsetof(CPUState, fpr[28]) },
2430
    { "f29", offsetof(CPUState, fpr[29]) },
2431
    { "f30", offsetof(CPUState, fpr[30]) },
2432
    { "f31", offsetof(CPUState, fpr[31]) },
2433
    { "fpscr", offsetof(CPUState, fpscr) },
2434
    /* Next instruction pointer */
2435
    { "nip|pc", offsetof(CPUState, nip) },
2436
    { "lr", offsetof(CPUState, lr) },
2437
    { "ctr", offsetof(CPUState, ctr) },
2438
    { "decr", 0, &monitor_get_decr, },
2439
    { "ccr", 0, &monitor_get_ccr, },
2440
    /* Machine state register */
2441
    { "msr", 0, &monitor_get_msr, },
2442
    { "xer", 0, &monitor_get_xer, },
2443
    { "tbu", 0, &monitor_get_tbu, },
2444
    { "tbl", 0, &monitor_get_tbl, },
2445
#if defined(TARGET_PPC64)
2446
    /* Address space register */
2447
    { "asr", offsetof(CPUState, asr) },
2448
#endif
2449
    /* Segment registers */
2450
    { "sdr1", offsetof(CPUState, sdr1) },
2451
    { "sr0", offsetof(CPUState, sr[0]) },
2452
    { "sr1", offsetof(CPUState, sr[1]) },
2453
    { "sr2", offsetof(CPUState, sr[2]) },
2454
    { "sr3", offsetof(CPUState, sr[3]) },
2455
    { "sr4", offsetof(CPUState, sr[4]) },
2456
    { "sr5", offsetof(CPUState, sr[5]) },
2457
    { "sr6", offsetof(CPUState, sr[6]) },
2458
    { "sr7", offsetof(CPUState, sr[7]) },
2459
    { "sr8", offsetof(CPUState, sr[8]) },
2460
    { "sr9", offsetof(CPUState, sr[9]) },
2461
    { "sr10", offsetof(CPUState, sr[10]) },
2462
    { "sr11", offsetof(CPUState, sr[11]) },
2463
    { "sr12", offsetof(CPUState, sr[12]) },
2464
    { "sr13", offsetof(CPUState, sr[13]) },
2465
    { "sr14", offsetof(CPUState, sr[14]) },
2466
    { "sr15", offsetof(CPUState, sr[15]) },
2467
    /* Too lazy to put BATs and SPRs ... */
2468
#elif defined(TARGET_SPARC)
2469
    { "g0", offsetof(CPUState, gregs[0]) },
2470
    { "g1", offsetof(CPUState, gregs[1]) },
2471
    { "g2", offsetof(CPUState, gregs[2]) },
2472
    { "g3", offsetof(CPUState, gregs[3]) },
2473
    { "g4", offsetof(CPUState, gregs[4]) },
2474
    { "g5", offsetof(CPUState, gregs[5]) },
2475
    { "g6", offsetof(CPUState, gregs[6]) },
2476
    { "g7", offsetof(CPUState, gregs[7]) },
2477
    { "o0", 0, monitor_get_reg },
2478
    { "o1", 1, monitor_get_reg },
2479
    { "o2", 2, monitor_get_reg },
2480
    { "o3", 3, monitor_get_reg },
2481
    { "o4", 4, monitor_get_reg },
2482
    { "o5", 5, monitor_get_reg },
2483
    { "o6", 6, monitor_get_reg },
2484
    { "o7", 7, monitor_get_reg },
2485
    { "l0", 8, monitor_get_reg },
2486
    { "l1", 9, monitor_get_reg },
2487
    { "l2", 10, monitor_get_reg },
2488
    { "l3", 11, monitor_get_reg },
2489
    { "l4", 12, monitor_get_reg },
2490
    { "l5", 13, monitor_get_reg },
2491
    { "l6", 14, monitor_get_reg },
2492
    { "l7", 15, monitor_get_reg },
2493
    { "i0", 16, monitor_get_reg },
2494
    { "i1", 17, monitor_get_reg },
2495
    { "i2", 18, monitor_get_reg },
2496
    { "i3", 19, monitor_get_reg },
2497
    { "i4", 20, monitor_get_reg },
2498
    { "i5", 21, monitor_get_reg },
2499
    { "i6", 22, monitor_get_reg },
2500
    { "i7", 23, monitor_get_reg },
2501
    { "pc", offsetof(CPUState, pc) },
2502
    { "npc", offsetof(CPUState, npc) },
2503
    { "y", offsetof(CPUState, y) },
2504
#ifndef TARGET_SPARC64
2505
    { "psr", 0, &monitor_get_psr, },
2506
    { "wim", offsetof(CPUState, wim) },
2507
#endif
2508
    { "tbr", offsetof(CPUState, tbr) },
2509
    { "fsr", offsetof(CPUState, fsr) },
2510
    { "f0", offsetof(CPUState, fpr[0]) },
2511
    { "f1", offsetof(CPUState, fpr[1]) },
2512
    { "f2", offsetof(CPUState, fpr[2]) },
2513
    { "f3", offsetof(CPUState, fpr[3]) },
2514
    { "f4", offsetof(CPUState, fpr[4]) },
2515
    { "f5", offsetof(CPUState, fpr[5]) },
2516
    { "f6", offsetof(CPUState, fpr[6]) },
2517
    { "f7", offsetof(CPUState, fpr[7]) },
2518
    { "f8", offsetof(CPUState, fpr[8]) },
2519
    { "f9", offsetof(CPUState, fpr[9]) },
2520
    { "f10", offsetof(CPUState, fpr[10]) },
2521
    { "f11", offsetof(CPUState, fpr[11]) },
2522
    { "f12", offsetof(CPUState, fpr[12]) },
2523
    { "f13", offsetof(CPUState, fpr[13]) },
2524
    { "f14", offsetof(CPUState, fpr[14]) },
2525
    { "f15", offsetof(CPUState, fpr[15]) },
2526
    { "f16", offsetof(CPUState, fpr[16]) },
2527
    { "f17", offsetof(CPUState, fpr[17]) },
2528
    { "f18", offsetof(CPUState, fpr[18]) },
2529
    { "f19", offsetof(CPUState, fpr[19]) },
2530
    { "f20", offsetof(CPUState, fpr[20]) },
2531
    { "f21", offsetof(CPUState, fpr[21]) },
2532
    { "f22", offsetof(CPUState, fpr[22]) },
2533
    { "f23", offsetof(CPUState, fpr[23]) },
2534
    { "f24", offsetof(CPUState, fpr[24]) },
2535
    { "f25", offsetof(CPUState, fpr[25]) },
2536
    { "f26", offsetof(CPUState, fpr[26]) },
2537
    { "f27", offsetof(CPUState, fpr[27]) },
2538
    { "f28", offsetof(CPUState, fpr[28]) },
2539
    { "f29", offsetof(CPUState, fpr[29]) },
2540
    { "f30", offsetof(CPUState, fpr[30]) },
2541
    { "f31", offsetof(CPUState, fpr[31]) },
2542
#ifdef TARGET_SPARC64
2543
    { "f32", offsetof(CPUState, fpr[32]) },
2544
    { "f34", offsetof(CPUState, fpr[34]) },
2545
    { "f36", offsetof(CPUState, fpr[36]) },
2546
    { "f38", offsetof(CPUState, fpr[38]) },
2547
    { "f40", offsetof(CPUState, fpr[40]) },
2548
    { "f42", offsetof(CPUState, fpr[42]) },
2549
    { "f44", offsetof(CPUState, fpr[44]) },
2550
    { "f46", offsetof(CPUState, fpr[46]) },
2551
    { "f48", offsetof(CPUState, fpr[48]) },
2552
    { "f50", offsetof(CPUState, fpr[50]) },
2553
    { "f52", offsetof(CPUState, fpr[52]) },
2554
    { "f54", offsetof(CPUState, fpr[54]) },
2555
    { "f56", offsetof(CPUState, fpr[56]) },
2556
    { "f58", offsetof(CPUState, fpr[58]) },
2557
    { "f60", offsetof(CPUState, fpr[60]) },
2558
    { "f62", offsetof(CPUState, fpr[62]) },
2559
    { "asi", offsetof(CPUState, asi) },
2560
    { "pstate", offsetof(CPUState, pstate) },
2561
    { "cansave", offsetof(CPUState, cansave) },
2562
    { "canrestore", offsetof(CPUState, canrestore) },
2563
    { "otherwin", offsetof(CPUState, otherwin) },
2564
    { "wstate", offsetof(CPUState, wstate) },
2565
    { "cleanwin", offsetof(CPUState, cleanwin) },
2566
    { "fprs", offsetof(CPUState, fprs) },
2567
#endif
2568
#endif
2569
    { NULL },
2570
};
2571

    
2572
static void expr_error(Monitor *mon, const char *msg)
2573
{
2574
    monitor_printf(mon, "%s\n", msg);
2575
    longjmp(expr_env, 1);
2576
}
2577

    
2578
/* return 0 if OK, -1 if not found, -2 if no CPU defined */
2579
static int get_monitor_def(target_long *pval, const char *name)
2580
{
2581
    const MonitorDef *md;
2582
    void *ptr;
2583

    
2584
    for(md = monitor_defs; md->name != NULL; md++) {
2585
        if (compare_cmd(name, md->name)) {
2586
            if (md->get_value) {
2587
                *pval = md->get_value(md, md->offset);
2588
            } else {
2589
                CPUState *env = mon_get_cpu();
2590
                if (!env)
2591
                    return -2;
2592
                ptr = (uint8_t *)env + md->offset;
2593
                switch(md->type) {
2594
                case MD_I32:
2595
                    *pval = *(int32_t *)ptr;
2596
                    break;
2597
                case MD_TLONG:
2598
                    *pval = *(target_long *)ptr;
2599
                    break;
2600
                default:
2601
                    *pval = 0;
2602
                    break;
2603
                }
2604
            }
2605
            return 0;
2606
        }
2607
    }
2608
    return -1;
2609
}
2610

    
2611
static void next(void)
2612
{
2613
    if (*pch != '\0') {
2614
        pch++;
2615
        while (qemu_isspace(*pch))
2616
            pch++;
2617
    }
2618
}
2619

    
2620
static int64_t expr_sum(Monitor *mon);
2621

    
2622
static int64_t expr_unary(Monitor *mon)
2623
{
2624
    int64_t n;
2625
    char *p;
2626
    int ret;
2627

    
2628
    switch(*pch) {
2629
    case '+':
2630
        next();
2631
        n = expr_unary(mon);
2632
        break;
2633
    case '-':
2634
        next();
2635
        n = -expr_unary(mon);
2636
        break;
2637
    case '~':
2638
        next();
2639
        n = ~expr_unary(mon);
2640
        break;
2641
    case '(':
2642
        next();
2643
        n = expr_sum(mon);
2644
        if (*pch != ')') {
2645
            expr_error(mon, "')' expected");
2646
        }
2647
        next();
2648
        break;
2649
    case '\'':
2650
        pch++;
2651
        if (*pch == '\0')
2652
            expr_error(mon, "character constant expected");
2653
        n = *pch;
2654
        pch++;
2655
        if (*pch != '\'')
2656
            expr_error(mon, "missing terminating \' character");
2657
        next();
2658
        break;
2659
    case '$':
2660
        {
2661
            char buf[128], *q;
2662
            target_long reg=0;
2663

    
2664
            pch++;
2665
            q = buf;
2666
            while ((*pch >= 'a' && *pch <= 'z') ||
2667
                   (*pch >= 'A' && *pch <= 'Z') ||
2668
                   (*pch >= '0' && *pch <= '9') ||
2669
                   *pch == '_' || *pch == '.') {
2670
                if ((q - buf) < sizeof(buf) - 1)
2671
                    *q++ = *pch;
2672
                pch++;
2673
            }
2674
            while (qemu_isspace(*pch))
2675
                pch++;
2676
            *q = 0;
2677
            ret = get_monitor_def(&reg, buf);
2678
            if (ret == -1)
2679
                expr_error(mon, "unknown register");
2680
            else if (ret == -2)
2681
                expr_error(mon, "no cpu defined");
2682
            n = reg;
2683
        }
2684
        break;
2685
    case '\0':
2686
        expr_error(mon, "unexpected end of expression");
2687
        n = 0;
2688
        break;
2689
    default:
2690
#if TARGET_PHYS_ADDR_BITS > 32
2691
        n = strtoull(pch, &p, 0);
2692
#else
2693
        n = strtoul(pch, &p, 0);
2694
#endif
2695
        if (pch == p) {
2696
            expr_error(mon, "invalid char in expression");
2697
        }
2698
        pch = p;
2699
        while (qemu_isspace(*pch))
2700
            pch++;
2701
        break;
2702
    }
2703
    return n;
2704
}
2705

    
2706

    
2707
static int64_t expr_prod(Monitor *mon)
2708
{
2709
    int64_t val, val2;
2710
    int op;
2711

    
2712
    val = expr_unary(mon);
2713
    for(;;) {
2714
        op = *pch;
2715
        if (op != '*' && op != '/' && op != '%')
2716
            break;
2717
        next();
2718
        val2 = expr_unary(mon);
2719
        switch(op) {
2720
        default:
2721
        case '*':
2722
            val *= val2;
2723
            break;
2724
        case '/':
2725
        case '%':
2726
            if (val2 == 0)
2727
                expr_error(mon, "division by zero");
2728
            if (op == '/')
2729
                val /= val2;
2730
            else
2731
                val %= val2;
2732
            break;
2733
        }
2734
    }
2735
    return val;
2736
}
2737

    
2738
static int64_t expr_logic(Monitor *mon)
2739
{
2740
    int64_t val, val2;
2741
    int op;
2742

    
2743
    val = expr_prod(mon);
2744
    for(;;) {
2745
        op = *pch;
2746
        if (op != '&' && op != '|' && op != '^')
2747
            break;
2748
        next();
2749
        val2 = expr_prod(mon);
2750
        switch(op) {
2751
        default:
2752
        case '&':
2753
            val &= val2;
2754
            break;
2755
        case '|':
2756
            val |= val2;
2757
            break;
2758
        case '^':
2759
            val ^= val2;
2760
            break;
2761
        }
2762
    }
2763
    return val;
2764
}
2765

    
2766
static int64_t expr_sum(Monitor *mon)
2767
{
2768
    int64_t val, val2;
2769
    int op;
2770

    
2771
    val = expr_logic(mon);
2772
    for(;;) {
2773
        op = *pch;
2774
        if (op != '+' && op != '-')
2775
            break;
2776
        next();
2777
        val2 = expr_logic(mon);
2778
        if (op == '+')
2779
            val += val2;
2780
        else
2781
            val -= val2;
2782
    }
2783
    return val;
2784
}
2785

    
2786
static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2787
{
2788
    pch = *pp;
2789
    if (setjmp(expr_env)) {
2790
        *pp = pch;
2791
        return -1;
2792
    }
2793
    while (qemu_isspace(*pch))
2794
        pch++;
2795
    *pval = expr_sum(mon);
2796
    *pp = pch;
2797
    return 0;
2798
}
2799

    
2800
static int get_str(char *buf, int buf_size, const char **pp)
2801
{
2802
    const char *p;
2803
    char *q;
2804
    int c;
2805

    
2806
    q = buf;
2807
    p = *pp;
2808
    while (qemu_isspace(*p))
2809
        p++;
2810
    if (*p == '\0') {
2811
    fail:
2812
        *q = '\0';
2813
        *pp = p;
2814
        return -1;
2815
    }
2816
    if (*p == '\"') {
2817
        p++;
2818
        while (*p != '\0' && *p != '\"') {
2819
            if (*p == '\\') {
2820
                p++;
2821
                c = *p++;
2822
                switch(c) {
2823
                case 'n':
2824
                    c = '\n';
2825
                    break;
2826
                case 'r':
2827
                    c = '\r';
2828
                    break;
2829
                case '\\':
2830
                case '\'':
2831
                case '\"':
2832
                    break;
2833
                default:
2834
                    qemu_printf("unsupported escape code: '\\%c'\n", c);
2835
                    goto fail;
2836
                }
2837
                if ((q - buf) < buf_size - 1) {
2838
                    *q++ = c;
2839
                }
2840
            } else {
2841
                if ((q - buf) < buf_size - 1) {
2842
                    *q++ = *p;
2843
                }
2844
                p++;
2845
            }
2846
        }
2847
        if (*p != '\"') {
2848
            qemu_printf("unterminated string\n");
2849
            goto fail;
2850
        }
2851
        p++;
2852
    } else {
2853
        while (*p != '\0' && !qemu_isspace(*p)) {
2854
            if ((q - buf) < buf_size - 1) {
2855
                *q++ = *p;
2856
            }
2857
            p++;
2858
        }
2859
    }
2860
    *q = '\0';
2861
    *pp = p;
2862
    return 0;
2863
}
2864

    
2865
/*
2866
 * Store the command-name in cmdname, and return a pointer to
2867
 * the remaining of the command string.
2868
 */
2869
static const char *get_command_name(const char *cmdline,
2870
                                    char *cmdname, size_t nlen)
2871
{
2872
    size_t len;
2873
    const char *p, *pstart;
2874

    
2875
    p = cmdline;
2876
    while (qemu_isspace(*p))
2877
        p++;
2878
    if (*p == '\0')
2879
        return NULL;
2880
    pstart = p;
2881
    while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2882
        p++;
2883
    len = p - pstart;
2884
    if (len > nlen - 1)
2885
        len = nlen - 1;
2886
    memcpy(cmdname, pstart, len);
2887
    cmdname[len] = '\0';
2888
    return p;
2889
}
2890

    
2891
/**
2892
 * Read key of 'type' into 'key' and return the current
2893
 * 'type' pointer.
2894
 */
2895
static char *key_get_info(const char *type, char **key)
2896
{
2897
    size_t len;
2898
    char *p, *str;
2899

    
2900
    if (*type == ',')
2901
        type++;
2902

    
2903
    p = strchr(type, ':');
2904
    if (!p) {
2905
        *key = NULL;
2906
        return NULL;
2907
    }
2908
    len = p - type;
2909

    
2910
    str = qemu_malloc(len + 1);
2911
    memcpy(str, type, len);
2912
    str[len] = '\0';
2913

    
2914
    *key = str;
2915
    return ++p;
2916
}
2917

    
2918
static int default_fmt_format = 'x';
2919
static int default_fmt_size = 4;
2920

    
2921
#define MAX_ARGS 16
2922

    
2923
static int is_valid_option(const char *c, const char *typestr)
2924
{
2925
    char option[3];
2926
  
2927
    option[0] = '-';
2928
    option[1] = *c;
2929
    option[2] = '\0';
2930
  
2931
    typestr = strstr(typestr, option);
2932
    return (typestr != NULL);
2933
}
2934

    
2935
static const mon_cmd_t *monitor_find_command(const char *cmdname)
2936
{
2937
    const mon_cmd_t *cmd;
2938

    
2939
    for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
2940
        if (compare_cmd(cmdname, cmd->name)) {
2941
            return cmd;
2942
        }
2943
    }
2944

    
2945
    return NULL;
2946
}
2947

    
2948
static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2949
                                              const char *cmdline,
2950
                                              QDict *qdict)
2951
{
2952
    const char *p, *typestr;
2953
    int c;
2954
    const mon_cmd_t *cmd;
2955
    char cmdname[256];
2956
    char buf[1024];
2957
    char *key;
2958

    
2959
#ifdef DEBUG
2960
    monitor_printf(mon, "command='%s'\n", cmdline);
2961
#endif
2962

    
2963
    /* extract the command name */
2964
    p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2965
    if (!p)
2966
        return NULL;
2967

    
2968
    cmd = monitor_find_command(cmdname);
2969
    if (!cmd) {
2970
        monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2971
        return NULL;
2972
    }
2973

    
2974
    /* parse the parameters */
2975
    typestr = cmd->args_type;
2976
    for(;;) {
2977
        typestr = key_get_info(typestr, &key);
2978
        if (!typestr)
2979
            break;
2980
        c = *typestr;
2981
        typestr++;
2982
        switch(c) {
2983
        case 'F':
2984
        case 'B':
2985
        case 's':
2986
            {
2987
                int ret;
2988

    
2989
                while (qemu_isspace(*p))
2990
                    p++;
2991
                if (*typestr == '?') {
2992
                    typestr++;
2993
                    if (*p == '\0') {
2994
                        /* no optional string: NULL argument */
2995
                        break;
2996
                    }
2997
                }
2998
                ret = get_str(buf, sizeof(buf), &p);
2999
                if (ret < 0) {
3000
                    switch(c) {
3001
                    case 'F':
3002
                        monitor_printf(mon, "%s: filename expected\n",
3003
                                       cmdname);
3004
                        break;
3005
                    case 'B':
3006
                        monitor_printf(mon, "%s: block device name expected\n",
3007
                                       cmdname);
3008
                        break;
3009
                    default:
3010
                        monitor_printf(mon, "%s: string expected\n", cmdname);
3011
                        break;
3012
                    }
3013
                    goto fail;
3014
                }
3015
                qdict_put(qdict, key, qstring_from_str(buf));
3016
            }
3017
            break;
3018
        case '/':
3019
            {
3020
                int count, format, size;
3021

    
3022
                while (qemu_isspace(*p))
3023
                    p++;
3024
                if (*p == '/') {
3025
                    /* format found */
3026
                    p++;
3027
                    count = 1;
3028
                    if (qemu_isdigit(*p)) {
3029
                        count = 0;
3030
                        while (qemu_isdigit(*p)) {
3031
                            count = count * 10 + (*p - '0');
3032
                            p++;
3033
                        }
3034
                    }
3035
                    size = -1;
3036
                    format = -1;
3037
                    for(;;) {
3038
                        switch(*p) {
3039
                        case 'o':
3040
                        case 'd':
3041
                        case 'u':
3042
                        case 'x':
3043
                        case 'i':
3044
                        case 'c':
3045
                            format = *p++;
3046
                            break;
3047
                        case 'b':
3048
                            size = 1;
3049
                            p++;
3050
                            break;
3051
                        case 'h':
3052
                            size = 2;
3053
                            p++;
3054
                            break;
3055
                        case 'w':
3056
                            size = 4;
3057
                            p++;
3058
                            break;
3059
                        case 'g':
3060
                        case 'L':
3061
                            size = 8;
3062
                            p++;
3063
                            break;
3064
                        default:
3065
                            goto next;
3066
                        }
3067
                    }
3068
                next:
3069
                    if (*p != '\0' && !qemu_isspace(*p)) {
3070
                        monitor_printf(mon, "invalid char in format: '%c'\n",
3071
                                       *p);
3072
                        goto fail;
3073
                    }
3074
                    if (format < 0)
3075
                        format = default_fmt_format;
3076
                    if (format != 'i') {
3077
                        /* for 'i', not specifying a size gives -1 as size */
3078
                        if (size < 0)
3079
                            size = default_fmt_size;
3080
                        default_fmt_size = size;
3081
                    }
3082
                    default_fmt_format = format;
3083
                } else {
3084
                    count = 1;
3085
                    format = default_fmt_format;
3086
                    if (format != 'i') {
3087
                        size = default_fmt_size;
3088
                    } else {
3089
                        size = -1;
3090
                    }
3091
                }
3092
                qdict_put(qdict, "count", qint_from_int(count));
3093
                qdict_put(qdict, "format", qint_from_int(format));
3094
                qdict_put(qdict, "size", qint_from_int(size));
3095
            }
3096
            break;
3097
        case 'i':
3098
        case 'l':
3099
            {
3100
                int64_t val;
3101

    
3102
                while (qemu_isspace(*p))
3103
                    p++;
3104
                if (*typestr == '?' || *typestr == '.') {
3105
                    if (*typestr == '?') {
3106
                        if (*p == '\0') {
3107
                            typestr++;
3108
                            break;
3109
                        }
3110
                    } else {
3111
                        if (*p == '.') {
3112
                            p++;
3113
                            while (qemu_isspace(*p))
3114
                                p++;
3115
                        } else {
3116
                            typestr++;
3117
                            break;
3118
                        }
3119
                    }
3120
                    typestr++;
3121
                }
3122
                if (get_expr(mon, &val, &p))
3123
                    goto fail;
3124
                /* Check if 'i' is greater than 32-bit */
3125
                if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3126
                    monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3127
                    monitor_printf(mon, "integer is for 32-bit values\n");
3128
                    goto fail;
3129
                }
3130
                qdict_put(qdict, key, qint_from_int(val));
3131
            }
3132
            break;
3133
        case '-':
3134
            {
3135
                const char *tmp = p;
3136
                int has_option, skip_key = 0;
3137
                /* option */
3138

    
3139
                c = *typestr++;
3140
                if (c == '\0')
3141
                    goto bad_type;
3142
                while (qemu_isspace(*p))
3143
                    p++;
3144
                has_option = 0;
3145
                if (*p == '-') {
3146
                    p++;
3147
                    if(c != *p) {
3148
                        if(!is_valid_option(p, typestr)) {
3149
                  
3150
                            monitor_printf(mon, "%s: unsupported option -%c\n",
3151
                                           cmdname, *p);
3152
                            goto fail;
3153
                        } else {
3154
                            skip_key = 1;
3155
                        }
3156
                    }
3157
                    if(skip_key) {
3158
                        p = tmp;
3159
                    } else {
3160
                        p++;
3161
                        has_option = 1;
3162
                    }
3163
                }
3164
                qdict_put(qdict, key, qint_from_int(has_option));
3165
            }
3166
            break;
3167
        default:
3168
        bad_type:
3169
            monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3170
            goto fail;
3171
        }
3172
        qemu_free(key);
3173
        key = NULL;
3174
    }
3175
    /* check that all arguments were parsed */
3176
    while (qemu_isspace(*p))
3177
        p++;
3178
    if (*p != '\0') {
3179
        monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3180
                       cmdname);
3181
        goto fail;
3182
    }
3183

    
3184
    return cmd;
3185

    
3186
fail:
3187
    qemu_free(key);
3188
    return NULL;
3189
}
3190

    
3191
static void monitor_print_error(Monitor *mon)
3192
{
3193
    qerror_print(mon->error);
3194
    QDECREF(mon->error);
3195
    mon->error = NULL;
3196
}
3197

    
3198
static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3199
                                 const QDict *params)
3200
{
3201
    QObject *data = NULL;
3202

    
3203
    cmd->mhandler.cmd_new(mon, params, &data);
3204
    if (data)
3205
        cmd->user_print(mon, data);
3206

    
3207
    qobject_decref(data);
3208
}
3209

    
3210
static void handle_user_command(Monitor *mon, const char *cmdline)
3211
{
3212
    QDict *qdict;
3213
    const mon_cmd_t *cmd;
3214

    
3215
    qdict = qdict_new();
3216

    
3217
    cmd = monitor_parse_command(mon, cmdline, qdict);
3218
    if (!cmd)
3219
        goto out;
3220

    
3221
    qemu_errors_to_mon(mon);
3222

    
3223
    if (monitor_handler_ported(cmd)) {
3224
        monitor_call_handler(mon, cmd, qdict);
3225
    } else {
3226
        cmd->mhandler.cmd(mon, qdict);
3227
    }
3228

    
3229
    if (monitor_has_error(mon))
3230
        monitor_print_error(mon);
3231

    
3232
    qemu_errors_to_previous();
3233

    
3234
out:
3235
    QDECREF(qdict);
3236
}
3237

    
3238
static void cmd_completion(const char *name, const char *list)
3239
{
3240
    const char *p, *pstart;
3241
    char cmd[128];
3242
    int len;
3243

    
3244
    p = list;
3245
    for(;;) {
3246
        pstart = p;
3247
        p = strchr(p, '|');
3248
        if (!p)
3249
            p = pstart + strlen(pstart);
3250
        len = p - pstart;
3251
        if (len > sizeof(cmd) - 2)
3252
            len = sizeof(cmd) - 2;
3253
        memcpy(cmd, pstart, len);
3254
        cmd[len] = '\0';
3255
        if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3256
            readline_add_completion(cur_mon->rs, cmd);
3257
        }
3258
        if (*p == '\0')
3259
            break;
3260
        p++;
3261
    }
3262
}
3263

    
3264
static void file_completion(const char *input)
3265
{
3266
    DIR *ffs;
3267
    struct dirent *d;
3268
    char path[1024];
3269
    char file[1024], file_prefix[1024];
3270
    int input_path_len;
3271
    const char *p;
3272

    
3273
    p = strrchr(input, '/');
3274
    if (!p) {
3275
        input_path_len = 0;
3276
        pstrcpy(file_prefix, sizeof(file_prefix), input);
3277
        pstrcpy(path, sizeof(path), ".");
3278
    } else {
3279
        input_path_len = p - input + 1;
3280
        memcpy(path, input, input_path_len);
3281
        if (input_path_len > sizeof(path) - 1)
3282
            input_path_len = sizeof(path) - 1;
3283
        path[input_path_len] = '\0';
3284
        pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3285
    }
3286
#ifdef DEBUG_COMPLETION
3287
    monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3288
                   input, path, file_prefix);
3289
#endif
3290
    ffs = opendir(path);
3291
    if (!ffs)
3292
        return;
3293
    for(;;) {
3294
        struct stat sb;
3295
        d = readdir(ffs);
3296
        if (!d)
3297
            break;
3298
        if (strstart(d->d_name, file_prefix, NULL)) {
3299
            memcpy(file, input, input_path_len);
3300
            if (input_path_len < sizeof(file))
3301
                pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3302
                        d->d_name);
3303
            /* stat the file to find out if it's a directory.
3304
             * In that case add a slash to speed up typing long paths
3305
             */
3306
            stat(file, &sb);
3307
            if(S_ISDIR(sb.st_mode))
3308
                pstrcat(file, sizeof(file), "/");
3309
            readline_add_completion(cur_mon->rs, file);
3310
        }
3311
    }
3312
    closedir(ffs);
3313
}
3314

    
3315
static void block_completion_it(void *opaque, BlockDriverState *bs)
3316
{
3317
    const char *name = bdrv_get_device_name(bs);
3318
    const char *input = opaque;
3319

    
3320
    if (input[0] == '\0' ||
3321
        !strncmp(name, (char *)input, strlen(input))) {
3322
        readline_add_completion(cur_mon->rs, name);
3323
    }
3324
}
3325

    
3326
/* NOTE: this parser is an approximate form of the real command parser */
3327
static void parse_cmdline(const char *cmdline,
3328
                         int *pnb_args, char **args)
3329
{
3330
    const char *p;
3331
    int nb_args, ret;
3332
    char buf[1024];
3333

    
3334
    p = cmdline;
3335
    nb_args = 0;
3336
    for(;;) {
3337
        while (qemu_isspace(*p))
3338
            p++;
3339
        if (*p == '\0')
3340
            break;
3341
        if (nb_args >= MAX_ARGS)
3342
            break;
3343
        ret = get_str(buf, sizeof(buf), &p);
3344
        args[nb_args] = qemu_strdup(buf);
3345
        nb_args++;
3346
        if (ret < 0)
3347
            break;
3348
    }
3349
    *pnb_args = nb_args;
3350
}
3351

    
3352
static const char *next_arg_type(const char *typestr)
3353
{
3354
    const char *p = strchr(typestr, ':');
3355
    return (p != NULL ? ++p : typestr);
3356
}
3357

    
3358
static void monitor_find_completion(const char *cmdline)
3359
{
3360
    const char *cmdname;
3361
    char *args[MAX_ARGS];
3362
    int nb_args, i, len;
3363
    const char *ptype, *str;
3364
    const mon_cmd_t *cmd;
3365
    const KeyDef *key;
3366

    
3367
    parse_cmdline(cmdline, &nb_args, args);
3368
#ifdef DEBUG_COMPLETION
3369
    for(i = 0; i < nb_args; i++) {
3370
        monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3371
    }
3372
#endif
3373

    
3374
    /* if the line ends with a space, it means we want to complete the
3375
       next arg */
3376
    len = strlen(cmdline);
3377
    if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3378
        if (nb_args >= MAX_ARGS)
3379
            return;
3380
        args[nb_args++] = qemu_strdup("");
3381
    }
3382
    if (nb_args <= 1) {
3383
        /* command completion */
3384
        if (nb_args == 0)
3385
            cmdname = "";
3386
        else
3387
            cmdname = args[0];
3388
        readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3389
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3390
            cmd_completion(cmdname, cmd->name);
3391
        }
3392
    } else {
3393
        /* find the command */
3394
        for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3395
            if (compare_cmd(args[0], cmd->name))
3396
                goto found;
3397
        }
3398
        return;
3399
    found:
3400
        ptype = next_arg_type(cmd->args_type);
3401
        for(i = 0; i < nb_args - 2; i++) {
3402
            if (*ptype != '\0') {
3403
                ptype = next_arg_type(ptype);
3404
                while (*ptype == '?')
3405
                    ptype = next_arg_type(ptype);
3406
            }
3407
        }
3408
        str = args[nb_args - 1];
3409
        if (*ptype == '-' && ptype[1] != '\0') {
3410
            ptype += 2;
3411
        }
3412
        switch(*ptype) {
3413
        case 'F':
3414
            /* file completion */
3415
            readline_set_completion_index(cur_mon->rs, strlen(str));
3416
            file_completion(str);
3417
            break;
3418
        case 'B':
3419
            /* block device name completion */
3420
            readline_set_completion_index(cur_mon->rs, strlen(str));
3421
            bdrv_iterate(block_completion_it, (void *)str);
3422
            break;
3423
        case 's':
3424
            /* XXX: more generic ? */
3425
            if (!strcmp(cmd->name, "info")) {
3426
                readline_set_completion_index(cur_mon->rs, strlen(str));
3427
                for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3428
                    cmd_completion(str, cmd->name);
3429
                }
3430
            } else if (!strcmp(cmd->name, "sendkey")) {
3431
                char *sep = strrchr(str, '-');
3432
                if (sep)
3433
                    str = sep + 1;
3434
                readline_set_completion_index(cur_mon->rs, strlen(str));
3435
                for(key = key_defs; key->name != NULL; key++) {
3436
                    cmd_completion(str, key->name);
3437
                }
3438
            } else if (!strcmp(cmd->name, "help|?")) {
3439
                readline_set_completion_index(cur_mon->rs, strlen(str));
3440
                for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3441
                    cmd_completion(str, cmd->name);
3442
                }
3443
            }
3444
            break;
3445
        default:
3446
            break;
3447
        }
3448
    }
3449
    for(i = 0; i < nb_args; i++)
3450
        qemu_free(args[i]);
3451
}
3452

    
3453
static int monitor_can_read(void *opaque)
3454
{
3455
    Monitor *mon = opaque;
3456

    
3457
    return (mon->suspend_cnt == 0) ? 128 : 0;
3458
}
3459

    
3460
static void monitor_read(void *opaque, const uint8_t *buf, int size)
3461
{
3462
    Monitor *old_mon = cur_mon;
3463
    int i;
3464

    
3465
    cur_mon = opaque;
3466

    
3467
    if (cur_mon->rs) {
3468
        for (i = 0; i < size; i++)
3469
            readline_handle_byte(cur_mon->rs, buf[i]);
3470
    } else {
3471
        if (size == 0 || buf[size - 1] != 0)
3472
            monitor_printf(cur_mon, "corrupted command\n");
3473
        else
3474
            handle_user_command(cur_mon, (char *)buf);
3475
    }
3476

    
3477
    cur_mon = old_mon;
3478
}
3479

    
3480
static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3481
{
3482
    monitor_suspend(mon);
3483
    handle_user_command(mon, cmdline);
3484
    monitor_resume(mon);
3485
}
3486

    
3487
int monitor_suspend(Monitor *mon)
3488
{
3489
    if (!mon->rs)
3490
        return -ENOTTY;
3491
    mon->suspend_cnt++;
3492
    return 0;
3493
}
3494

    
3495
void monitor_resume(Monitor *mon)
3496
{
3497
    if (!mon->rs)
3498
        return;
3499
    if (--mon->suspend_cnt == 0)
3500
        readline_show_prompt(mon->rs);
3501
}
3502

    
3503
static void monitor_event(void *opaque, int event)
3504
{
3505
    Monitor *mon = opaque;
3506

    
3507
    switch (event) {
3508
    case CHR_EVENT_MUX_IN:
3509
        mon->mux_out = 0;
3510
        if (mon->reset_seen) {
3511
            readline_restart(mon->rs);
3512
            monitor_resume(mon);
3513
            monitor_flush(mon);
3514
        } else {
3515
            mon->suspend_cnt = 0;
3516
        }
3517
        break;
3518

    
3519
    case CHR_EVENT_MUX_OUT:
3520
        if (mon->reset_seen) {
3521
            if (mon->suspend_cnt == 0) {
3522
                monitor_printf(mon, "\n");
3523
            }
3524
            monitor_flush(mon);
3525
            monitor_suspend(mon);
3526
        } else {
3527
            mon->suspend_cnt++;
3528
        }
3529
        mon->mux_out = 1;
3530
        break;
3531

    
3532
    case CHR_EVENT_OPENED:
3533
        monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3534
                       "information\n", QEMU_VERSION);
3535
        if (!mon->mux_out) {
3536
            readline_show_prompt(mon->rs);
3537
        }
3538
        mon->reset_seen = 1;
3539
        break;
3540
    }
3541
}
3542

    
3543

    
3544
/*
3545
 * Local variables:
3546
 *  c-indent-level: 4
3547
 *  c-basic-offset: 4
3548
 *  tab-width: 8
3549
 * End:
3550
 */
3551

    
3552
const char *monitor_cmdline_parse(const char *cmdline, int *flags)
3553
{
3554
    const char *dev;
3555

    
3556
    if (strstart(cmdline, "control,", &dev)) {
3557
        if (strstart(dev, "vc", NULL)) {
3558
            fprintf(stderr, "qemu: control mode is for low-level interaction ");
3559
            fprintf(stderr, "cannot be used with device 'vc'\n");
3560
            exit(1);
3561
        }
3562
        *flags &= ~MONITOR_USE_READLINE;
3563
        *flags |= MONITOR_USE_CONTROL;
3564
        return dev;
3565
    }
3566

    
3567
    return cmdline;
3568
}
3569

    
3570
void monitor_init(CharDriverState *chr, int flags)
3571
{
3572
    static int is_first_init = 1;
3573
    Monitor *mon;
3574

    
3575
    if (is_first_init) {
3576
        key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3577
        is_first_init = 0;
3578
    }
3579

    
3580
    mon = qemu_mallocz(sizeof(*mon));
3581

    
3582
    mon->chr = chr;
3583
    mon->flags = flags;
3584
    if (flags & MONITOR_USE_READLINE) {
3585
        mon->rs = readline_init(mon, monitor_find_completion);
3586
        monitor_read_command(mon, 0);
3587
    }
3588

    
3589
    qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3590
                          mon);
3591

    
3592
    QLIST_INSERT_HEAD(&mon_list, mon, entry);
3593
    if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3594
        cur_mon = mon;
3595
}
3596

    
3597
static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3598
{
3599
    BlockDriverState *bs = opaque;
3600
    int ret = 0;
3601

    
3602
    if (bdrv_set_key(bs, password) != 0) {
3603
        monitor_printf(mon, "invalid password\n");
3604
        ret = -EPERM;
3605
    }
3606
    if (mon->password_completion_cb)
3607
        mon->password_completion_cb(mon->password_opaque, ret);
3608

    
3609
    monitor_read_command(mon, 1);
3610
}
3611

    
3612
void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3613
                                 BlockDriverCompletionFunc *completion_cb,
3614
                                 void *opaque)
3615
{
3616
    int err;
3617

    
3618
    if (!bdrv_key_required(bs)) {
3619
        if (completion_cb)
3620
            completion_cb(opaque, 0);
3621
        return;
3622
    }
3623

    
3624
    monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3625
                   bdrv_get_encrypted_filename(bs));
3626

    
3627
    mon->password_completion_cb = completion_cb;
3628
    mon->password_opaque = opaque;
3629

    
3630
    err = monitor_read_password(mon, bdrv_password_cb, bs);
3631

    
3632
    if (err && completion_cb)
3633
        completion_cb(opaque, err);
3634
}
3635

    
3636
typedef struct QemuErrorSink QemuErrorSink;
3637
struct QemuErrorSink {
3638
    enum {
3639
        ERR_SINK_FILE,
3640
        ERR_SINK_MONITOR,
3641
    } dest;
3642
    union {
3643
        FILE    *fp;
3644
        Monitor *mon;
3645
    };
3646
    QemuErrorSink *previous;
3647
};
3648

    
3649
static QemuErrorSink *qemu_error_sink;
3650

    
3651
void qemu_errors_to_file(FILE *fp)
3652
{
3653
    QemuErrorSink *sink;
3654

    
3655
    sink = qemu_mallocz(sizeof(*sink));
3656
    sink->dest = ERR_SINK_FILE;
3657
    sink->fp = fp;
3658
    sink->previous = qemu_error_sink;
3659
    qemu_error_sink = sink;
3660
}
3661

    
3662
void qemu_errors_to_mon(Monitor *mon)
3663
{
3664
    QemuErrorSink *sink;
3665

    
3666
    sink = qemu_mallocz(sizeof(*sink));
3667
    sink->dest = ERR_SINK_MONITOR;
3668
    sink->mon = mon;
3669
    sink->previous = qemu_error_sink;
3670
    qemu_error_sink = sink;
3671
}
3672

    
3673
void qemu_errors_to_previous(void)
3674
{
3675
    QemuErrorSink *sink;
3676

    
3677
    assert(qemu_error_sink != NULL);
3678
    sink = qemu_error_sink;
3679
    qemu_error_sink = sink->previous;
3680
    qemu_free(sink);
3681
}
3682

    
3683
void qemu_error(const char *fmt, ...)
3684
{
3685
    va_list args;
3686

    
3687
    assert(qemu_error_sink != NULL);
3688
    switch (qemu_error_sink->dest) {
3689
    case ERR_SINK_FILE:
3690
        va_start(args, fmt);
3691
        vfprintf(qemu_error_sink->fp, fmt, args);
3692
        va_end(args);
3693
        break;
3694
    case ERR_SINK_MONITOR:
3695
        va_start(args, fmt);
3696
        monitor_vprintf(qemu_error_sink->mon, fmt, args);
3697
        va_end(args);
3698
        break;
3699
    }
3700
}
3701

    
3702
void qemu_error_internal(const char *file, int linenr, const char *func,
3703
                         const char *fmt, ...)
3704
{
3705
    va_list va;
3706
    QError *qerror;
3707

    
3708
    assert(qemu_error_sink != NULL);
3709

    
3710
    va_start(va, fmt);
3711
    qerror = qerror_from_info(file, linenr, func, fmt, &va);
3712
    va_end(va);
3713

    
3714
    switch (qemu_error_sink->dest) {
3715
    case ERR_SINK_FILE:
3716
        qerror_print(qerror);
3717
        QDECREF(qerror);
3718
        break;
3719
    case ERR_SINK_MONITOR:
3720
        assert(qemu_error_sink->mon->error == NULL);
3721
        qemu_error_sink->mon->error = qerror;
3722
        break;
3723
    }
3724
}