monitor.c 131 KB
Newer Older
bellard's avatar
bellard committed
1 2
/*
 * QEMU monitor
3
 *
bellard's avatar
bellard committed
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 *
bellard's avatar
bellard committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
#include <dirent.h>
pbrook's avatar
pbrook committed
25
#include "hw/hw.h"
26
#include "hw/qdev.h"
pbrook's avatar
pbrook committed
27 28 29 30
#include "hw/usb.h"
#include "hw/pcmcia.h"
#include "hw/pc.h"
#include "hw/pci.h"
Richard W.M. Jones's avatar
Richard W.M. Jones committed
31
#include "hw/watchdog.h"
32
#include "hw/loader.h"
pbrook's avatar
pbrook committed
33 34
#include "gdbstub.h"
#include "net.h"
35
#include "net/slirp.h"
pbrook's avatar
pbrook committed
36
#include "qemu-char.h"
37
#include "ui/qemu-spice.h"
pbrook's avatar
pbrook committed
38
#include "sysemu.h"
aliguori's avatar
aliguori committed
39 40
#include "monitor.h"
#include "readline.h"
pbrook's avatar
pbrook committed
41
#include "console.h"
42
#include "blockdev.h"
pbrook's avatar
pbrook committed
43
#include "audio/audio.h"
44
#include "disas.h"
aliguori's avatar
aliguori committed
45
#include "balloon.h"
46
#include "qemu-timer.h"
aliguori's avatar
aliguori committed
47
#include "migration.h"
aliguori's avatar
aliguori committed
48
#include "kvm.h"
49
#include "acl.h"
50
#include "qint.h"
51
#include "qfloat.h"
52
#include "qlist.h"
Luiz Capitulino's avatar
Luiz Capitulino committed
53
#include "qbool.h"
54
#include "qstring.h"
Luiz Capitulino's avatar
Luiz Capitulino committed
55
#include "qjson.h"
Luiz Capitulino's avatar
Luiz Capitulino committed
56 57
#include "json-streamer.h"
#include "json-parser.h"
58
#include "osdep.h"
59
#include "cpu.h"
60
#include "trace.h"
61
#include "trace/control.h"
62
#ifdef CONFIG_TRACE_SIMPLE
63
#include "trace/simple.h"
64
#endif
Gerd Hoffmann's avatar
Gerd Hoffmann committed
65
#include "ui/qemu-spice.h"
66
#include "memory.h"
Anthony Liguori's avatar
Anthony Liguori committed
67 68
#include "qmp-commands.h"
#include "hmp.h"
ths's avatar
ths committed
69

70 71 72 73 74 75
/* for pic/irq_info */
#if defined(TARGET_SPARC)
#include "hw/sun4m.h"
#endif
#include "hw/lm32_pic.h"

bellard's avatar
bellard committed
76
//#define DEBUG
bellard's avatar
bellard committed
77
//#define DEBUG_COMPLETION
bellard's avatar
bellard committed
78

79 80
/*
 * Supported types:
81
 *
82
 * 'F'          filename
bellard's avatar
bellard committed
83
 * 'B'          block device name
84
 * 's'          string (accept optional quote)
85 86 87 88 89
 * 'O'          option string of the form NAME=VALUE,...
 *              parsed according to QemuOptsList given by its name
 *              Example: 'device:O' uses qemu_device_opts.
 *              Restriction: only lists with empty desc are supported
 *              TODO lift the restriction
bellard's avatar
bellard committed
90 91
 * 'i'          32 bit integer
 * 'l'          target long (32 or 64 bit)
92 93
 * 'M'          Non-negative target long (32 or 64 bit), in user mode the
 *              value is multiplied by 2^20 (think Mebibyte)
94 95 96 97 98
 * 'o'          octets (aka bytes)
 *              user mode accepts an optional T, t, G, g, M, m, K, k
 *              suffix, which multiplies the value by 2^40 for
 *              suffixes T and t, 2^30 for suffixes G and g, 2^20 for
 *              M and m, 2^10 for K and k
99 100 101
 * 'T'          double
 *              user mode accepts an optional ms, us, ns suffix,
 *              which divides the value by 1e3, 1e6, 1e9, respectively
102 103
 * '/'          optional gdb-like print format (like "/10x")
 *
104 105
 * '?'          optional type (for all types, except '/')
 * '.'          other form of optional type (for 'i' and 'l')
106 107
 * 'b'          boolean
 *              user mode accepts "on" or "off"
108
 * '-'          optional parameter (eg. '-f')
109 110 111
 *
 */

112 113 114 115 116 117
typedef struct MonitorCompletionData MonitorCompletionData;
struct MonitorCompletionData {
    Monitor *mon;
    void (*user_print)(Monitor *mon, const QObject *data);
};

118
typedef struct mon_cmd_t {
bellard's avatar
bellard committed
119
    const char *name;
120
    const char *args_type;
bellard's avatar
bellard committed
121 122
    const char *params;
    const char *help;
123
    void (*user_print)(Monitor *mon, const QObject *data);
124 125
    union {
        void (*info)(Monitor *mon);
126
        void (*cmd)(Monitor *mon, const QDict *qdict);
127
        int  (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
128 129
        int  (*cmd_async)(Monitor *mon, const QDict *params,
                          MonitorCompletion *cb, void *opaque);
130
    } mhandler;
131
    int flags;
132
} mon_cmd_t;
bellard's avatar
bellard committed
133

134
/* file descriptors passed via SCM_RIGHTS */
135 136
typedef struct mon_fd_t mon_fd_t;
struct mon_fd_t {
137 138
    char *name;
    int fd;
139
    QLIST_ENTRY(mon_fd_t) next;
140 141
};

Luiz Capitulino's avatar
Luiz Capitulino committed
142 143 144
typedef struct MonitorControl {
    QObject *id;
    JSONMessageParser parser;
145
    int command_mode;
Luiz Capitulino's avatar
Luiz Capitulino committed
146 147
} MonitorControl;

148 149
struct Monitor {
    CharDriverState *chr;
Gerd Hoffmann's avatar
Gerd Hoffmann committed
150 151
    int mux_out;
    int reset_seen;
152 153 154 155 156
    int flags;
    int suspend_cnt;
    uint8_t outbuf[1024];
    int outbuf_index;
    ReadLineState *rs;
Luiz Capitulino's avatar
Luiz Capitulino committed
157
    MonitorControl *mc;
158
    CPUArchState *mon_cpu;
159 160
    BlockDriverCompletionFunc *password_completion_cb;
    void *password_opaque;
161 162 163
#ifdef CONFIG_DEBUG_MONITOR
    int print_calls_nr;
#endif
Luiz Capitulino's avatar
Luiz Capitulino committed
164
    QError *error;
165
    QLIST_HEAD(,mon_fd_t) fds;
166
    QLIST_ENTRY(Monitor) entry;
167 168
};

Luiz Capitulino's avatar
Luiz Capitulino committed
169 170 171 172
#ifdef CONFIG_DEBUG_MONITOR
#define MON_DEBUG(fmt, ...) do {    \
    fprintf(stderr, "Monitor: ");       \
    fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

static inline void mon_print_count_inc(Monitor *mon)
{
    mon->print_calls_nr++;
}

static inline void mon_print_count_init(Monitor *mon)
{
    mon->print_calls_nr = 0;
}

static inline int mon_print_count_get(const Monitor *mon)
{
    return mon->print_calls_nr;
}

Luiz Capitulino's avatar
Luiz Capitulino committed
189 190
#else /* !CONFIG_DEBUG_MONITOR */
#define MON_DEBUG(fmt, ...) do { } while (0)
191 192 193
static inline void mon_print_count_inc(Monitor *mon) { }
static inline void mon_print_count_init(Monitor *mon) { }
static inline int mon_print_count_get(const Monitor *mon) { return 0; }
Luiz Capitulino's avatar
Luiz Capitulino committed
194 195
#endif /* CONFIG_DEBUG_MONITOR */

196 197 198
/* QMP checker flags */
#define QMP_ACCEPT_UNKNOWNS 1

199
static QLIST_HEAD(mon_list, Monitor) mon_list;
200

201 202
static mon_cmd_t mon_cmds[];
static mon_cmd_t info_cmds[];
bellard's avatar
bellard committed
203

204 205
static const mon_cmd_t qmp_cmds[];

206 207
Monitor *cur_mon;
Monitor *default_mon;
aliguori's avatar
aliguori committed
208

209 210
static void monitor_command_cb(Monitor *mon, const char *cmdline,
                               void *opaque);
211

212 213 214 215 216
static inline int qmp_cmd_mode(const Monitor *mon)
{
    return (mon->mc ? mon->mc->command_mode : 0);
}

217 218 219 220 221 222
/* Return true if in control mode, false otherwise */
static inline int monitor_ctrl_mode(const Monitor *mon)
{
    return (mon->flags & MONITOR_USE_CONTROL);
}

223 224 225 226 227 228
/* Return non-zero iff we have a current monitor, and it is in QMP mode.  */
int monitor_cur_is_qmp(void)
{
    return cur_mon && monitor_ctrl_mode(cur_mon);
}

229
void monitor_read_command(Monitor *mon, int show_prompt)
230
{
231 232 233
    if (!mon->rs)
        return;

234 235 236 237
    readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
    if (show_prompt)
        readline_show_prompt(mon->rs);
}
bellard's avatar
bellard committed
238

239 240
int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
                          void *opaque)
241
{
242
    if (monitor_ctrl_mode(mon)) {
243
        qerror_report(QERR_MISSING_PARAMETER, "password");
244 245
        return -EINVAL;
    } else if (mon->rs) {
246 247 248 249 250 251 252
        readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
        /* prompt is printed on return from the command handler */
        return 0;
    } else {
        monitor_printf(mon, "terminal does not support password prompting\n");
        return -ENOTTY;
    }
253 254
}

aliguori's avatar
aliguori committed
255
void monitor_flush(Monitor *mon)
256
{
Gerd Hoffmann's avatar
Gerd Hoffmann committed
257
    if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
258
        qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
259
        mon->outbuf_index = 0;
260 261 262 263
    }
}

/* flush at every end of line or if the buffer is full */
aliguori's avatar
aliguori committed
264
static void monitor_puts(Monitor *mon, const char *str)
265
{
266
    char c;
267

268 269 270 271
    for(;;) {
        c = *str++;
        if (c == '\0')
            break;
bellard's avatar
bellard committed
272
        if (c == '\n')
273 274 275 276
            mon->outbuf[mon->outbuf_index++] = '\r';
        mon->outbuf[mon->outbuf_index++] = c;
        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
            || c == '\n')
aliguori's avatar
aliguori committed
277
            monitor_flush(mon);
278 279 280
    }
}

aliguori's avatar
aliguori committed
281
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
bellard's avatar
bellard committed
282
{
283 284
    char buf[4096];

285 286 287
    if (!mon)
        return;

288 289
    mon_print_count_inc(mon);

290 291
    if (monitor_ctrl_mode(mon)) {
        return;
292
    }
293 294 295

    vsnprintf(buf, sizeof(buf), fmt, ap);
    monitor_puts(mon, buf);
bellard's avatar
bellard committed
296 297
}

aliguori's avatar
aliguori committed
298
void monitor_printf(Monitor *mon, const char *fmt, ...)
bellard's avatar
bellard committed
299
{
300 301
    va_list ap;
    va_start(ap, fmt);
aliguori's avatar
aliguori committed
302
    monitor_vprintf(mon, fmt, ap);
303
    va_end(ap);
bellard's avatar
bellard committed
304 305
}

aliguori's avatar
aliguori committed
306
void monitor_print_filename(Monitor *mon, const char *filename)
307 308 309 310
{
    int i;

    for (i = 0; filename[i]; i++) {
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
        switch (filename[i]) {
        case ' ':
        case '"':
        case '\\':
            monitor_printf(mon, "\\%c", filename[i]);
            break;
        case '\t':
            monitor_printf(mon, "\\t");
            break;
        case '\r':
            monitor_printf(mon, "\\r");
            break;
        case '\n':
            monitor_printf(mon, "\\n");
            break;
        default:
            monitor_printf(mon, "%c", filename[i]);
            break;
        }
330 331 332
    }
}

333 334
static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
                                              const char *fmt, ...)
bellard's avatar
bellard committed
335 336 337
{
    va_list ap;
    va_start(ap, fmt);
aliguori's avatar
aliguori committed
338
    monitor_vprintf((Monitor *)stream, fmt, ap);
bellard's avatar
bellard committed
339 340 341 342
    va_end(ap);
    return 0;
}

343 344
static void monitor_user_noop(Monitor *mon, const QObject *data) { }

345
static inline int handler_is_qobject(const mon_cmd_t *cmd)
346 347 348 349
{
    return cmd->user_print != NULL;
}

350
static inline bool handler_is_async(const mon_cmd_t *cmd)
351
{
352
    return cmd->flags & MONITOR_CMD_ASYNC;
353 354
}

Luiz Capitulino's avatar
Luiz Capitulino committed
355 356 357 358 359
static inline int monitor_has_error(const Monitor *mon)
{
    return mon->error != NULL;
}

Luiz Capitulino's avatar
Luiz Capitulino committed
360 361 362 363
static void monitor_json_emitter(Monitor *mon, const QObject *data)
{
    QString *json;

364 365
    json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
                                             qobject_to_json(data);
Luiz Capitulino's avatar
Luiz Capitulino committed
366 367
    assert(json != NULL);

368 369
    qstring_append_chr(json, '\n');
    monitor_puts(mon, qstring_get_str(json));
370

Luiz Capitulino's avatar
Luiz Capitulino committed
371 372 373
    QDECREF(json);
}

Luiz Capitulino's avatar
Luiz Capitulino committed
374 375 376 377
static void monitor_protocol_emitter(Monitor *mon, QObject *data)
{
    QDict *qmp;

378 379
    trace_monitor_protocol_emitter(mon);

Luiz Capitulino's avatar
Luiz Capitulino committed
380 381 382 383 384 385 386 387
    qmp = qdict_new();

    if (!monitor_has_error(mon)) {
        /* success response */
        if (data) {
            qobject_incref(data);
            qdict_put_obj(qmp, "return", data);
        } else {
388 389
            /* return an empty QDict by default */
            qdict_put(qmp, "return", qdict_new());
Luiz Capitulino's avatar
Luiz Capitulino committed
390 391 392
        }
    } else {
        /* error response */
393
        qdict_put(mon->error->error, "desc", qerror_human(mon->error));
Luiz Capitulino's avatar
Luiz Capitulino committed
394 395 396 397 398 399
        qdict_put(qmp, "error", mon->error->error);
        QINCREF(mon->error->error);
        QDECREF(mon->error);
        mon->error = NULL;
    }

Luiz Capitulino's avatar
Luiz Capitulino committed
400 401 402 403 404
    if (mon->mc->id) {
        qdict_put_obj(qmp, "id", mon->mc->id);
        mon->mc->id = NULL;
    }

Luiz Capitulino's avatar
Luiz Capitulino committed
405 406 407 408
    monitor_json_emitter(mon, QOBJECT(qmp));
    QDECREF(qmp);
}

409 410 411 412
static void timestamp_put(QDict *qdict)
{
    int err;
    QObject *obj;
413
    qemu_timeval tv;
414

415
    err = qemu_gettimeofday(&tv);
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    if (err < 0)
        return;

    obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
                                "'microseconds': %" PRId64 " }",
                                (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
    qdict_put_obj(qdict, "timestamp", obj);
}

/**
 * monitor_protocol_event(): Generate a Monitor event
 *
 * Event-specific data can be emitted through the (optional) 'data' parameter.
 */
void monitor_protocol_event(MonitorEvent event, QObject *data)
{
    QDict *qmp;
    const char *event_name;
434
    Monitor *mon;
435

436
    assert(event < QEVENT_MAX);
437 438

    switch (event) {
439
        case QEVENT_SHUTDOWN:
440 441
            event_name = "SHUTDOWN";
            break;
442
        case QEVENT_RESET:
443 444
            event_name = "RESET";
            break;
445
        case QEVENT_POWERDOWN:
446 447
            event_name = "POWERDOWN";
            break;
448
        case QEVENT_STOP:
449 450
            event_name = "STOP";
            break;
451 452 453
        case QEVENT_RESUME:
            event_name = "RESUME";
            break;
454 455 456
        case QEVENT_VNC_CONNECTED:
            event_name = "VNC_CONNECTED";
            break;
457 458 459
        case QEVENT_VNC_INITIALIZED:
            event_name = "VNC_INITIALIZED";
            break;
460 461 462
        case QEVENT_VNC_DISCONNECTED:
            event_name = "VNC_DISCONNECTED";
            break;
463 464 465
        case QEVENT_BLOCK_IO_ERROR:
            event_name = "BLOCK_IO_ERROR";
            break;
466 467 468
        case QEVENT_RTC_CHANGE:
            event_name = "RTC_CHANGE";
            break;
469 470 471
        case QEVENT_WATCHDOG:
            event_name = "WATCHDOG";
            break;
Gerd Hoffmann's avatar
Gerd Hoffmann committed
472 473 474 475 476 477 478 479 480
        case QEVENT_SPICE_CONNECTED:
            event_name = "SPICE_CONNECTED";
            break;
        case QEVENT_SPICE_INITIALIZED:
            event_name = "SPICE_INITIALIZED";
            break;
        case QEVENT_SPICE_DISCONNECTED:
            event_name = "SPICE_DISCONNECTED";
            break;
481 482 483
        case QEVENT_BLOCK_JOB_COMPLETED:
            event_name = "BLOCK_JOB_COMPLETED";
            break;
484 485 486
        case QEVENT_BLOCK_JOB_CANCELLED:
            event_name = "BLOCK_JOB_CANCELLED";
            break;
487 488 489
        case QEVENT_DEVICE_TRAY_MOVED:
             event_name = "DEVICE_TRAY_MOVED";
            break;
Gerd Hoffmann's avatar
Gerd Hoffmann committed
490 491 492 493 494 495
        case QEVENT_SUSPEND:
            event_name = "SUSPEND";
            break;
        case QEVENT_WAKEUP:
            event_name = "WAKEUP";
            break;
496 497 498 499 500 501 502 503
        default:
            abort();
            break;
    }

    qmp = qdict_new();
    timestamp_put(qmp);
    qdict_put(qmp, "event", qstring_from_str(event_name));
504 505
    if (data) {
        qobject_incref(data);
506
        qdict_put_obj(qmp, "data", data);
507
    }
508

509
    QLIST_FOREACH(mon, &mon_list, entry) {
510
        if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
511 512
            monitor_json_emitter(mon, QOBJECT(qmp));
        }
513
    }
514 515 516
    QDECREF(qmp);
}

517 518
static int do_qmp_capabilities(Monitor *mon, const QDict *params,
                               QObject **ret_data)
519 520 521 522 523
{
    /* Will setup QMP capabilities in the future */
    if (monitor_ctrl_mode(mon)) {
        mon->mc->command_mode = 1;
    }
524 525

    return 0;
526 527
}

528 529
static void handle_user_command(Monitor *mon, const char *cmdline);

530 531
char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
                                int64_t cpu_index, Error **errp)
532
{
533
    char *output = NULL;
534 535 536 537 538 539 540 541 542 543
    Monitor *old_mon, hmp;
    CharDriverState mchar;

    memset(&hmp, 0, sizeof(hmp));
    qemu_chr_init_mem(&mchar);
    hmp.chr = &mchar;

    old_mon = cur_mon;
    cur_mon = &hmp;

544 545
    if (has_cpu_index) {
        int ret = monitor_set_cpu(cpu_index);
546 547
        if (ret < 0) {
            cur_mon = old_mon;
548 549
            error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
                      "a CPU number");
550 551 552 553
            goto out;
        }
    }

554
    handle_user_command(&hmp, command_line);
555 556 557
    cur_mon = old_mon;

    if (qemu_chr_mem_osize(hmp.chr) > 0) {
558 559 560 561 562
        QString *str = qemu_chr_mem_to_qs(hmp.chr);
        output = g_strdup(qstring_get_str(str));
        QDECREF(str);
    } else {
        output = g_strdup("");
563 564 565 566
    }

out:
    qemu_chr_close_mem(hmp.chr);
567
    return output;
568 569
}

bellard's avatar
bellard committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
static int compare_cmd(const char *name, const char *list)
{
    const char *p, *pstart;
    int len;
    len = strlen(name);
    p = list;
    for(;;) {
        pstart = p;
        p = strchr(p, '|');
        if (!p)
            p = pstart + strlen(pstart);
        if ((p - pstart) == len && !memcmp(pstart, name, len))
            return 1;
        if (*p == '\0')
            break;
        p++;
    }
    return 0;
}

590
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
aliguori's avatar
aliguori committed
591
                          const char *prefix, const char *name)
bellard's avatar
bellard committed
592
{
593
    const mon_cmd_t *cmd;
bellard's avatar
bellard committed
594 595 596

    for(cmd = cmds; cmd->name != NULL; cmd++) {
        if (!name || !strcmp(name, cmd->name))
aliguori's avatar
aliguori committed
597 598
            monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
                           cmd->params, cmd->help);
bellard's avatar
bellard committed
599 600 601
    }
}

aliguori's avatar
aliguori committed
602
static void help_cmd(Monitor *mon, const char *name)
bellard's avatar
bellard committed
603 604
{
    if (name && !strcmp(name, "info")) {
aliguori's avatar
aliguori committed
605
        help_cmd_dump(mon, info_cmds, "info ", NULL);
bellard's avatar
bellard committed
606
    } else {
aliguori's avatar
aliguori committed
607
        help_cmd_dump(mon, mon_cmds, "", name);
608
        if (name && !strcmp(name, "log")) {
609
            const CPULogItem *item;
aliguori's avatar
aliguori committed
610 611
            monitor_printf(mon, "Log items (comma separated):\n");
            monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
612
            for(item = cpu_log_items; item->mask != 0; item++) {
aliguori's avatar
aliguori committed
613
                monitor_printf(mon, "%-10s %s\n", item->name, item->help);
614 615
            }
        }
bellard's avatar
bellard committed
616 617 618
    }
}

619
static void do_help_cmd(Monitor *mon, const QDict *qdict)
620
{
621
    help_cmd(mon, qdict_get_try_str(qdict, "name"));
622 623
}

624
static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
625 626 627
{
    const char *tp_name = qdict_get_str(qdict, "name");
    bool new_state = qdict_get_bool(qdict, "option");
628
    int ret = trace_event_set_state(tp_name, new_state);
629 630 631 632

    if (!ret) {
        monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
    }
633
}
634

635
#ifdef CONFIG_TRACE_SIMPLE
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
static void do_trace_file(Monitor *mon, const QDict *qdict)
{
    const char *op = qdict_get_try_str(qdict, "op");
    const char *arg = qdict_get_try_str(qdict, "arg");

    if (!op) {
        st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
    } else if (!strcmp(op, "on")) {
        st_set_trace_file_enabled(true);
    } else if (!strcmp(op, "off")) {
        st_set_trace_file_enabled(false);
    } else if (!strcmp(op, "flush")) {
        st_flush_trace_buffer();
    } else if (!strcmp(op, "set")) {
        if (arg) {
            st_set_trace_file(arg);
        }
    } else {
        monitor_printf(mon, "unexpected argument \"%s\"\n", op);
        help_cmd(mon, "trace-file");
    }
}
658 659
#endif

660 661 662 663 664 665 666 667
static void user_monitor_complete(void *opaque, QObject *ret_data)
{
    MonitorCompletionData *data = (MonitorCompletionData *)opaque; 

    if (ret_data) {
        data->user_print(data->mon, ret_data);
    }
    monitor_resume(data->mon);
668
    g_free(data);
669 670 671 672 673 674 675
}

static void qmp_monitor_complete(void *opaque, QObject *ret_data)
{
    monitor_protocol_emitter(opaque, ret_data);
}

676 677
static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
                                 const QDict *params)
678
{
679
    return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
680 681 682 683 684 685 686
}

static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
                                   const QDict *params)
{
    int ret;

687
    MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
688 689 690 691 692 693 694
    cb_data->mon = mon;
    cb_data->user_print = cmd->user_print;
    monitor_suspend(mon);
    ret = cmd->mhandler.cmd_async(mon, params,
                                  user_monitor_complete, cb_data);
    if (ret < 0) {
        monitor_resume(mon);
695
        g_free(cb_data);
696 697 698
    }
}

699
static void do_info(Monitor *mon, const QDict *qdict)
bellard's avatar
bellard committed
700
{
701
    const mon_cmd_t *cmd;
702
    const char *item = qdict_get_try_str(qdict, "item");
bellard's avatar
bellard committed
703

Luiz Capitulino's avatar
Luiz Capitulino committed
704
    if (!item) {
bellard's avatar
bellard committed
705
        goto help;
Luiz Capitulino's avatar
Luiz Capitulino committed
706
    }
707 708

    for (cmd = info_cmds; cmd->name != NULL; cmd++) {
709
        if (compare_cmd(item, cmd->name))
710
            break;
bellard's avatar
bellard committed
711
    }
712

Luiz Capitulino's avatar
Luiz Capitulino committed
713
    if (cmd->name == NULL) {
714
        goto help;
Luiz Capitulino's avatar
Luiz Capitulino committed
715
    }
716

717
    cmd->mhandler.info(mon);
718
    return;
719 720 721

help:
    help_cmd(mon, "info");
bellard's avatar
bellard committed
722 723
}

724
CommandInfoList *qmp_query_commands(Error **errp)
725
{
726
    CommandInfoList *info, *cmd_list = NULL;
727 728
    const mon_cmd_t *cmd;

729
    for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
730 731 732
        info = g_malloc0(sizeof(*info));
        info->value = g_malloc0(sizeof(*info->value));
        info->value->name = g_strdup(cmd->name);
733

734 735
        info->next = cmd_list;
        cmd_list = info;
736 737
    }

738
    return cmd_list;
739 740
}

741 742
/* set the current CPU defined by the user */
int monitor_set_cpu(int cpu_index)
bellard's avatar
bellard committed
743
{
744
    CPUArchState *env;
bellard's avatar
bellard committed
745 746 747

    for(env = first_cpu; env != NULL; env = env->next_cpu) {
        if (env->cpu_index == cpu_index) {
748
            cur_mon->mon_cpu = env;
bellard's avatar
bellard committed
749 750 751 752 753 754
            return 0;
        }
    }
    return -1;
}

755
static CPUArchState *mon_get_cpu(void)
bellard's avatar
bellard committed
756
{
757
    if (!cur_mon->mon_cpu) {
758
        monitor_set_cpu(0);
bellard's avatar
bellard committed
759
    }
760
    cpu_synchronize_state(cur_mon->mon_cpu);
761
    return cur_mon->mon_cpu;
bellard's avatar
bellard committed
762 763
}

764 765 766 767 768
int monitor_get_cpu_index(void)
{
    return mon_get_cpu()->cpu_index;
}

aliguori's avatar
aliguori committed
769
static void do_info_registers(Monitor *mon)
770
{
771
    CPUArchState *env;
bellard's avatar
bellard committed
772
    env = mon_get_cpu();
773
#ifdef TARGET_I386
aliguori's avatar
aliguori committed
774
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard's avatar
bellard committed
775
                   X86_DUMP_FPU);
776
#else
aliguori's avatar
aliguori committed
777
    cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
bellard's avatar
bellard committed
778
                   0);
779 780 781
#endif
}

aliguori's avatar
aliguori committed
782
static void do_info_jit(Monitor *mon)
bellard's avatar
bellard committed
783
{
aliguori's avatar
aliguori committed
784
    dump_exec_info((FILE *)mon, monitor_fprintf);
bellard's avatar
bellard committed
785 786
}

aliguori's avatar
aliguori committed
787
static void do_info_history(Monitor *mon)
bellard's avatar
bellard committed
788 789
{
    int i;
790
    const char *str;
791

792 793
    if (!mon->rs)
        return;
794 795
    i = 0;
    for(;;) {
796
        str = readline_get_history(mon->rs, i);
797 798
        if (!str)
            break;
aliguori's avatar
aliguori committed
799
        monitor_printf(mon, "%d: '%s'\n", i, str);
800
        i++;
bellard's avatar
bellard committed
801 802 803
    }
}

804 805
#if defined(TARGET_PPC)
/* XXX: not implemented in other targets */
aliguori's avatar
aliguori committed
806
static void do_info_cpu_stats(Monitor *mon)
807
{
808
    CPUArchState *env;
809 810

    env = mon_get_cpu();
aliguori's avatar
aliguori committed
811
    cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
812 813 814
}
#endif

815
#if defined(CONFIG_TRACE_SIMPLE)
816 817 818 819
static void do_info_trace(Monitor *mon)
{
    st_print_trace((FILE *)mon, &monitor_fprintf);
}
820
#endif
821

822
static void do_trace_print_events(Monitor *mon)
823
{
824
    trace_print_events((FILE *)mon, &monitor_fprintf);
825 826
}

827 828 829 830 831 832 833
static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
{
    const char *protocol  = qdict_get_str(qdict, "protocol");
    const char *fdname = qdict_get_str(qdict, "fdname");
    CharDriverState *s;

    if (strcmp(protocol, "spice") == 0) {
834 835 836
        int fd = monitor_get_fd(mon, fdname);
        int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
        int tls = qdict_get_try_bool(qdict, "tls", 0);
837 838 839 840 841
        if (!using_spice) {
            /* correct one? spice isn't a device ,,, */
            qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
            return -1;
        }
842 843 844 845
        if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
            close(fd);
        }
        return 0;
846
#ifdef CONFIG_VNC
847 848
    } else if (strcmp(protocol, "vnc") == 0) {
	int fd = monitor_get_fd(mon, fdname);
849
        int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
850 851
	vnc_display_add_client(NULL, fd, skipauth);
	return 0;
852
#endif
853 854 855 856 857 858 859 860 861 862 863 864 865
    } else if ((s = qemu_chr_find(protocol)) != NULL) {
	int fd = monitor_get_fd(mon, fdname);
	if (qemu_chr_add_client(s, fd) < 0) {
	    qerror_report(QERR_ADD_CLIENT_FAILED);
	    return -1;
	}
	return 0;
    }

    qerror_report(QERR_INVALID_PARAMETER, "protocol");
    return -1;
}

866 867
static int client_migrate_info(Monitor *mon, const QDict *qdict,
                               MonitorCompletion cb, void *opaque)
Gerd Hoffmann's avatar
Gerd Hoffmann committed
868 869 870 871 872 873 874 875 876 877 878 879 880 881
{
    const char *protocol = qdict_get_str(qdict, "protocol");
    const char *hostname = qdict_get_str(qdict, "hostname");
    const char *subject  = qdict_get_try_str(qdict, "cert-subject");
    int port             = qdict_get_try_int(qdict, "port", -1);
    int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);
    int ret;

    if (strcmp(protocol, "spice") == 0) {
        if (!using_spice) {
            qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
            return -1;
        }

882 883 884 885 886
        if (port == -1 && tls_port == -1) {
            qerror_report(QERR_MISSING_PARAMETER, "port/tls-port");
            return -1;
        }

887 888
        ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
                                      cb, opaque);
Gerd Hoffmann's avatar
Gerd Hoffmann committed
889 890 891 892 893 894 895 896 897 898 899
        if (ret != 0) {
            qerror_report(QERR_UNDEFINED_ERROR);
            return -1;
        }
        return 0;
    }

    qerror_report(QERR_INVALID_PARAMETER, "protocol");
    return -1;
}

900
static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
bellard's avatar
bellard committed
901
{
902
    vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
903
    return 0;
bellard's avatar
bellard committed
904 905
}

906
static void do_logfile(Monitor *mon, const QDict *qdict)
pbrook's avatar
pbrook committed
907
{
908
    cpu_set_log_filename(qdict_get_str(qdict, "filename"));
pbrook's avatar
pbrook committed
909 910
}

911
static void do_log(Monitor *mon, const QDict *qdict)
912 913
{
    int mask;
914
    const char *items = qdict_get_str(qdict, "items");
915

916
    if (!strcmp(items, "none")) {
917 918
        mask = 0;
    } else {
919
        mask = cpu_str_to_log_mask(items);
920
        if (!mask) {
aliguori's avatar
aliguori committed
921
            help_cmd(mon, "log");
922 923 924 925 926 927
            return;
        }
    }
    cpu_set_log(mask);
}

928
static void do_singlestep(Monitor *mon, const QDict *qdict)
929
{
930
    const char *option = qdict_get_try_str(qdict, "option");
931 932 933 934 935 936 937 938 939
    if (!option || !strcmp(option, "on")) {
        singlestep = 1;
    } else if (!strcmp(option, "off")) {
        singlestep = 0;
    } else {
        monitor_printf(mon, "unexpected option %s\n", option);
    }
}

940
static void do_gdbserver(Monitor *mon, const QDict *qdict)
941
{
942
    const char *device = qdict_get_try_str(qdict, "device");
943 944 945 946 947 948
    if (!device)
        device = "tcp::" DEFAULT_GDBSTUB_PORT;
    if (gdbserver_start(device) < 0) {
        monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
                       device);
    } else if (strcmp(device, "none") == 0) {
949
        monitor_printf(mon, "Disabled gdbserver\n");
950
    } else {
951 952
        monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
                       device);
953 954 955
    }
}

956
static void do_watchdog_action(Monitor *mon, const QDict *qdict)
Richard W.M. Jones's avatar
Richard W.M. Jones committed
957
{
958
    const char *action = qdict_get_str(qdict, "action");
Richard W.M. Jones's avatar
Richard W.M. Jones committed
959 960 961 962 963
    if (select_watchdog_action(action) == -1) {
        monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
    }
}

aliguori's avatar
aliguori committed
964
static void monitor_printc(Monitor *mon, int c)
965
{
aliguori's avatar
aliguori committed
966
    monitor_printf(mon, "'");
967 968
    switch(c) {
    case '\'':
aliguori's avatar
aliguori committed
969
        monitor_printf(mon, "\\'");
970 971
        break;
    case '\\':
aliguori's avatar
aliguori committed
972
        monitor_printf(mon, "\\\\");
973 974
        break;
    case '\n':
aliguori's avatar
aliguori committed
975
        monitor_printf(mon, "\\n");
976 977
        break;
    case '\r':
aliguori's avatar
aliguori committed
978
        monitor_printf(mon, "\\r");
979 980 981
        break;
    default:
        if (c >= 32 && c <= 126) {
aliguori's avatar
aliguori committed
982
            monitor_printf(mon, "%c", c);
983
        } else {
aliguori's avatar
aliguori committed
984
            monitor_printf(mon, "\\x%02x", c);
985 986 987
        }
        break;
    }
aliguori's avatar
aliguori committed
988
    monitor_printf(mon, "'");
989 990
}

aliguori's avatar
aliguori committed
991
static void memory_dump(Monitor *mon, int count, int format, int wsize,
992
                        target_phys_addr_t addr, int is_physical)
993
{
994
    CPUArchState *env;
995
    int l, line_size, i, max_digits, len;
996 997 998 999 1000 1001
    uint8_t buf[16];
    uint64_t v;

    if (format == 'i') {
        int flags;
        flags = 0;
bellard's avatar
bellard committed
1002
        env = mon_get_cpu();
1003
#ifdef TARGET_I386
1004
        if (wsize == 2) {
1005
            flags = 1;
1006 1007 1008
        } else if (wsize == 4) {
            flags = 0;
        } else {
bellard's avatar
bellard committed
1009
            /* as default we use the current CS size */
1010
            flags = 0;
bellard's avatar
bellard committed
1011 1012
            if (env) {
#ifdef TARGET_X86_64
1013
                if ((env->efer & MSR_EFER_LMA) &&
bellard's avatar
bellard committed
1014 1015 1016 1017 1018 1019 1020
                    (env->segs[R_CS].flags & DESC_L_MASK))
                    flags = 2;
                else
#endif
                if (!(env->segs[R_CS].flags & DESC_B_MASK))
                    flags = 1;
            }
1021 1022
        }
#endif
aliguori's avatar
aliguori committed
1023
        monitor_disas(mon, env, addr, count, is_physical, flags);
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
        return;
    }

    len = wsize * count;
    if (wsize == 1)
        line_size = 8;
    else
        line_size = 16;
    max_digits = 0;

    switch(format) {
    case 'o':
        max_digits = (wsize * 8 + 2) / 3;
        break;
    default:
    case 'x':
        max_digits = (wsize * 8) / 4;
        break;
    case 'u':
    case 'd':
        max_digits = (wsize * 8 * 10 + 32) / 33;
        break;
    case 'c':
        wsize = 1;
        break;
    }

    while (len > 0) {
1052
        if (is_physical)
aliguori's avatar
aliguori committed
1053
            monitor_printf(mon, TARGET_FMT_plx ":", addr);
1054
        else
aliguori's avatar
aliguori committed
1055
            monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1056 1057 1058 1059
        l = len;
        if (l > line_size)
            l = line_size;
        if (is_physical) {
1060
            cpu_physical_memory_read(addr, buf, l);