hmp.c 89.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Human Monitor Interface
 *
 * Copyright IBM, Corp. 2011
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
12 13
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
14 15
 */

Peter Maydell's avatar
Peter Maydell committed
16
#include "qemu/osdep.h"
17
#include "hmp.h"
Paolo Bonzini's avatar
Paolo Bonzini committed
18
#include "net/net.h"
19
#include "net/eth.h"
20
#include "chardev/char.h"
Max Reitz's avatar
Max Reitz committed
21
#include "sysemu/block-backend.h"
22
#include "sysemu/sysemu.h"
23
#include "qemu/config-file.h"
24 25 26
#include "qemu/option.h"
#include "qemu/timer.h"
#include "qemu/sockets.h"
27
#include "monitor/monitor.h"
28
#include "monitor/qdev.h"
29
#include "qapi/error.h"
30
#include "qapi/opts-visitor.h"
31
#include "qapi/qapi-builtin-visit.h"
32 33 34 35 36 37 38 39 40
#include "qapi/qapi-commands-block.h"
#include "qapi/qapi-commands-char.h"
#include "qapi/qapi-commands-migration.h"
#include "qapi/qapi-commands-misc.h"
#include "qapi/qapi-commands-net.h"
#include "qapi/qapi-commands-rocker.h"
#include "qapi/qapi-commands-run-state.h"
#include "qapi/qapi-commands-tpm.h"
#include "qapi/qapi-commands-ui.h"
41
#include "qapi/qmp/qdict.h"
42
#include "qapi/qmp/qerror.h"
43
#include "qapi/string-input-visitor.h"
Hu Tao's avatar
Hu Tao committed
44
#include "qapi/string-output-visitor.h"
45
#include "qom/object_interfaces.h"
46
#include "ui/console.h"
47
#include "block/nbd.h"
48
#include "block/qapi.h"
49
#include "qemu-io.h"
50
#include "qemu/cutils.h"
51
#include "qemu/error-report.h"
52
#include "exec/ramlist.h"
53
#include "hw/intc/intc.h"
54
#include "migration/snapshot.h"
Peter Xu's avatar
Peter Xu committed
55
#include "migration/misc.h"
56

57 58 59 60
#ifdef CONFIG_SPICE
#include <spice/enums.h>
#endif

Luiz Capitulino's avatar
Luiz Capitulino committed
61 62
static void hmp_handle_error(Monitor *mon, Error **errp)
{
63 64
    assert(errp);
    if (*errp) {
65
        error_report_err(*errp);
Luiz Capitulino's avatar
Luiz Capitulino committed
66 67 68
    }
}

69
void hmp_info_name(Monitor *mon, const QDict *qdict)
70 71 72 73 74 75 76 77 78
{
    NameInfo *info;

    info = qmp_query_name(NULL);
    if (info->has_name) {
        monitor_printf(mon, "%s\n", info->name);
    }
    qapi_free_NameInfo(info);
}
79

80
void hmp_info_version(Monitor *mon, const QDict *qdict)
81 82 83 84 85 86
{
    VersionInfo *info;

    info = qmp_query_version(NULL);

    monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
87
                   info->qemu->major, info->qemu->minor, info->qemu->micro,
88 89 90 91
                   info->package);

    qapi_free_VersionInfo(info);
}
92

93
void hmp_info_kvm(Monitor *mon, const QDict *qdict)
94 95 96 97 98 99 100 101 102 103 104 105 106 107
{
    KvmInfo *info;

    info = qmp_query_kvm(NULL);
    monitor_printf(mon, "kvm support: ");
    if (info->present) {
        monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
    } else {
        monitor_printf(mon, "not compiled\n");
    }

    qapi_free_KvmInfo(info);
}

108
void hmp_info_status(Monitor *mon, const QDict *qdict)
109 110 111 112 113 114 115 116 117 118
{
    StatusInfo *info;

    info = qmp_query_status(NULL);

    monitor_printf(mon, "VM status: %s%s",
                   info->running ? "running" : "paused",
                   info->singlestep ? " (single step mode)" : "");

    if (!info->running && info->status != RUN_STATE_PAUSED) {
119
        monitor_printf(mon, " (%s)", RunState_str(info->status));
120 121 122 123 124 125 126
    }

    monitor_printf(mon, "\n");

    qapi_free_StatusInfo(info);
}

127
void hmp_info_uuid(Monitor *mon, const QDict *qdict)
128 129 130 131 132 133 134
{
    UuidInfo *info;

    info = qmp_query_uuid(NULL);
    monitor_printf(mon, "%s\n", info->UUID);
    qapi_free_UuidInfo(info);
}
135

136
void hmp_info_chardev(Monitor *mon, const QDict *qdict)
137 138 139 140 141 142 143 144 145 146 147
{
    ChardevInfoList *char_info, *info;

    char_info = qmp_query_chardev(NULL);
    for (info = char_info; info; info = info->next) {
        monitor_printf(mon, "%s: filename=%s\n", info->value->label,
                                                 info->value->filename);
    }

    qapi_free_ChardevInfoList(char_info);
}
Luiz Capitulino's avatar
Luiz Capitulino committed
148

149
void hmp_info_mice(Monitor *mon, const QDict *qdict)
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
{
    MouseInfoList *mice_list, *mouse;

    mice_list = qmp_query_mice(NULL);
    if (!mice_list) {
        monitor_printf(mon, "No mouse devices connected\n");
        return;
    }

    for (mouse = mice_list; mouse; mouse = mouse->next) {
        monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
                       mouse->value->current ? '*' : ' ',
                       mouse->value->index, mouse->value->name,
                       mouse->value->absolute ? " (absolute)" : "");
    }

    qapi_free_MouseInfoList(mice_list);
}

169
void hmp_info_migrate(Monitor *mon, const QDict *qdict)
170 171
{
    MigrationInfo *info;
172
    MigrationCapabilityStatusList *caps, *cap;
173 174

    info = qmp_query_migrate(NULL);
175 176
    caps = qmp_query_migrate_capabilities(NULL);

Peter Xu's avatar
Peter Xu committed
177 178
    migration_global_dump(mon);

179 180 181 182 183
    /* do not display parameters during setup */
    if (info->has_status && caps) {
        monitor_printf(mon, "capabilities: ");
        for (cap = caps; cap; cap = cap->next) {
            monitor_printf(mon, "%s: %s ",
184
                           MigrationCapability_str(cap->value->capability),
185 186 187 188
                           cap->value->state ? "on" : "off");
        }
        monitor_printf(mon, "\n");
    }
189 190

    if (info->has_status) {
191
        monitor_printf(mon, "Migration status: %s",
192
                       MigrationStatus_str(info->status));
193 194 195 196 197 198 199
        if (info->status == MIGRATION_STATUS_FAILED &&
            info->has_error_desc) {
            monitor_printf(mon, " (%s)\n", info->error_desc);
        } else {
            monitor_printf(mon, "\n");
        }

200 201
        monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
                       info->total_time);
202 203 204 205
        if (info->has_expected_downtime) {
            monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
                           info->expected_downtime);
        }
206 207 208 209
        if (info->has_downtime) {
            monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
                           info->downtime);
        }
210 211 212 213
        if (info->has_setup_time) {
            monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
                           info->setup_time);
        }
214 215 216 217 218
    }

    if (info->has_ram) {
        monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
                       info->ram->transferred >> 10);
219 220
        monitor_printf(mon, "throughput: %0.2f mbps\n",
                       info->ram->mbps);
221 222 223 224
        monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
                       info->ram->remaining >> 10);
        monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
                       info->ram->total >> 10);
225 226
        monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
                       info->ram->duplicate);
227 228
        monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
                       info->ram->skipped);
229 230 231 232
        monitor_printf(mon, "normal: %" PRIu64 " pages\n",
                       info->ram->normal);
        monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
                       info->ram->normal_bytes >> 10);
233 234
        monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
                       info->ram->dirty_sync_count);
235 236 237
        monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
                       info->ram->page_size >> 10);

238 239 240 241
        if (info->ram->dirty_pages_rate) {
            monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
                           info->ram->dirty_pages_rate);
        }
242 243 244 245
        if (info->ram->postcopy_requests) {
            monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
                           info->ram->postcopy_requests);
        }
246 247 248 249 250 251 252 253 254 255 256
    }

    if (info->has_disk) {
        monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
                       info->disk->transferred >> 10);
        monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
                       info->disk->remaining >> 10);
        monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
                       info->disk->total >> 10);
    }

Orit Wasserman's avatar
Orit Wasserman committed
257 258 259 260 261 262 263 264 265
    if (info->has_xbzrle_cache) {
        monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
                       info->xbzrle_cache->cache_size);
        monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
                       info->xbzrle_cache->bytes >> 10);
        monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
                       info->xbzrle_cache->pages);
        monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
                       info->xbzrle_cache->cache_miss);
266 267
        monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
                       info->xbzrle_cache->cache_miss_rate);
Orit Wasserman's avatar
Orit Wasserman committed
268 269 270 271
        monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
                       info->xbzrle_cache->overflow);
    }

272
    if (info->has_cpu_throttle_percentage) {
273
        monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
274
                       info->cpu_throttle_percentage);
275 276
    }

277
    qapi_free_MigrationInfo(info);
278 279 280
    qapi_free_MigrationCapabilityStatusList(caps);
}

281
void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
282 283 284 285 286 287 288
{
    MigrationCapabilityStatusList *caps, *cap;

    caps = qmp_query_migrate_capabilities(NULL);

    if (caps) {
        for (cap = caps; cap; cap = cap->next) {
289
            monitor_printf(mon, "%s: %s\n",
290
                           MigrationCapability_str(cap->value->capability),
291 292 293 294 295
                           cap->value->state ? "on" : "off");
        }
    }

    qapi_free_MigrationCapabilityStatusList(caps);
296 297
}

298 299 300 301 302 303 304
void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
{
    MigrationParameters *params;

    params = qmp_query_migrate_parameters(NULL);

    if (params) {
305
        assert(params->has_compress_level);
306
        monitor_printf(mon, "%s: %u\n",
307
            MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_LEVEL),
308
            params->compress_level);
309
        assert(params->has_compress_threads);
310
        monitor_printf(mon, "%s: %u\n",
311
            MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_THREADS),
312
            params->compress_threads);
313
        assert(params->has_decompress_threads);
314
        monitor_printf(mon, "%s: %u\n",
315
            MigrationParameter_str(MIGRATION_PARAMETER_DECOMPRESS_THREADS),
316
            params->decompress_threads);
317
        assert(params->has_cpu_throttle_initial);
318
        monitor_printf(mon, "%s: %u\n",
319
            MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
320
            params->cpu_throttle_initial);
321
        assert(params->has_cpu_throttle_increment);
322
        monitor_printf(mon, "%s: %u\n",
323
            MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
324
            params->cpu_throttle_increment);
325
        assert(params->has_tls_creds);
326
        monitor_printf(mon, "%s: '%s'\n",
327
            MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
328 329
            params->tls_creds);
        assert(params->has_tls_hostname);
330
        monitor_printf(mon, "%s: '%s'\n",
331
            MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
332
            params->tls_hostname);
333
        assert(params->has_max_bandwidth);
334
        monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
335
            MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
336 337
            params->max_bandwidth);
        assert(params->has_downtime_limit);
338
        monitor_printf(mon, "%s: %" PRIu64 " milliseconds\n",
339
            MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
340
            params->downtime_limit);
341
        assert(params->has_x_checkpoint_delay);
342
        monitor_printf(mon, "%s: %u\n",
343
            MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
344
            params->x_checkpoint_delay);
345 346
        assert(params->has_block_incremental);
        monitor_printf(mon, "%s: %s\n",
347 348
            MigrationParameter_str(MIGRATION_PARAMETER_BLOCK_INCREMENTAL),
            params->block_incremental ? "on" : "off");
349
        monitor_printf(mon, "%s: %u\n",
350 351
            MigrationParameter_str(MIGRATION_PARAMETER_X_MULTIFD_CHANNELS),
            params->x_multifd_channels);
352
        monitor_printf(mon, "%s: %u\n",
353 354
            MigrationParameter_str(MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT),
            params->x_multifd_page_count);
355
        monitor_printf(mon, "%s: %" PRIu64 "\n",
356 357
            MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
            params->xbzrle_cache_size);
358 359 360 361 362
    }

    qapi_free_MigrationParameters(params);
}

363
void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
364 365 366 367 368
{
    monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
                   qmp_query_migrate_cache_size(NULL) >> 10);
}

369
void hmp_info_cpus(Monitor *mon, const QDict *qdict)
370
{
371
    CpuInfoFastList *cpu_list, *cpu;
372

373
    cpu_list = qmp_query_cpus_fast(NULL);
374 375 376 377

    for (cpu = cpu_list; cpu; cpu = cpu->next) {
        int active = ' ';

378
        if (cpu->value->cpu_index == monitor_get_cpu_index()) {
379 380 381
            active = '*';
        }

382 383
        monitor_printf(mon, "%c CPU #%" PRId64 ":", active,
                       cpu->value->cpu_index);
384
        monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
385 386
    }

387
    qapi_free_CpuInfoFastList(cpu_list);
388 389
}

390 391
static void print_block_info(Monitor *mon, BlockInfo *info,
                             BlockDeviceInfo *inserted, bool verbose)
392
{
393
    ImageInfo *image_info;
394

395 396
    assert(!info || !info->has_inserted || info->inserted == inserted);

397
    if (info && *info->device) {
398 399 400 401 402
        monitor_printf(mon, "%s", info->device);
        if (inserted && inserted->has_node_name) {
            monitor_printf(mon, " (%s)", inserted->node_name);
        }
    } else {
403
        assert(info || inserted);
404
        monitor_printf(mon, "%s",
405 406
                       inserted && inserted->has_node_name ? inserted->node_name
                       : info && info->has_qdev ? info->qdev
407 408 409
                       : "<anonymous>");
    }

410 411 412 413 414 415 416 417 418
    if (inserted) {
        monitor_printf(mon, ": %s (%s%s%s)\n",
                       inserted->file,
                       inserted->drv,
                       inserted->ro ? ", read-only" : "",
                       inserted->encrypted ? ", encrypted" : "");
    } else {
        monitor_printf(mon, ": [not inserted]\n");
    }
419

420
    if (info) {
421 422 423
        if (info->has_qdev) {
            monitor_printf(mon, "    Attached to:      %s\n", info->qdev);
        }
424 425
        if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
            monitor_printf(mon, "    I/O status:       %s\n",
426
                           BlockDeviceIoStatus_str(info->io_status));
427
        }
428

429 430 431 432 433
        if (info->removable) {
            monitor_printf(mon, "    Removable device: %slocked, tray %s\n",
                           info->locked ? "" : "not ",
                           info->tray_open ? "open" : "closed");
        }
434
    }
435

436

437 438 439
    if (!inserted) {
        return;
    }
440

441 442 443 444 445 446 447 448 449 450 451 452
    monitor_printf(mon, "    Cache mode:       %s%s%s\n",
                   inserted->cache->writeback ? "writeback" : "writethrough",
                   inserted->cache->direct ? ", direct" : "",
                   inserted->cache->no_flush ? ", ignore flushes" : "");

    if (inserted->has_backing_file) {
        monitor_printf(mon,
                       "    Backing file:     %s "
                       "(chain depth: %" PRId64 ")\n",
                       inserted->backing_file,
                       inserted->backing_file_depth);
    }
453

454 455
    if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
        monitor_printf(mon, "    Detect zeroes:    %s\n",
456
                BlockdevDetectZeroesOptions_str(inserted->detect_zeroes));
457
    }
458

459 460 461 462 463 464 465 466 467 468 469 470 471
    if (inserted->bps  || inserted->bps_rd  || inserted->bps_wr  ||
        inserted->iops || inserted->iops_rd || inserted->iops_wr)
    {
        monitor_printf(mon, "    I/O throttling:   bps=%" PRId64
                        " bps_rd=%" PRId64  " bps_wr=%" PRId64
                        " bps_max=%" PRId64
                        " bps_rd_max=%" PRId64
                        " bps_wr_max=%" PRId64
                        " iops=%" PRId64 " iops_rd=%" PRId64
                        " iops_wr=%" PRId64
                        " iops_max=%" PRId64
                        " iops_rd_max=%" PRId64
                        " iops_wr_max=%" PRId64
472 473
                        " iops_size=%" PRId64
                        " group=%s\n",
474 475 476 477 478 479 480 481 482 483 484 485
                        inserted->bps,
                        inserted->bps_rd,
                        inserted->bps_wr,
                        inserted->bps_max,
                        inserted->bps_rd_max,
                        inserted->bps_wr_max,
                        inserted->iops,
                        inserted->iops_rd,
                        inserted->iops_wr,
                        inserted->iops_max,
                        inserted->iops_rd_max,
                        inserted->iops_wr_max,
486 487
                        inserted->iops_size,
                        inserted->group);
488
    }
489

490
    if (verbose) {
491 492 493 494 495 496 497 498 499 500 501 502 503
        monitor_printf(mon, "\nImages:\n");
        image_info = inserted->image;
        while (1) {
                bdrv_image_info_dump((fprintf_function)monitor_printf,
                                     mon, image_info);
            if (image_info->has_backing_image) {
                image_info = image_info->backing_image;
            } else {
                break;
            }
        }
    }
}
504

505 506 507
void hmp_info_block(Monitor *mon, const QDict *qdict)
{
    BlockInfoList *block_list, *info;
508
    BlockDeviceInfoList *blockdev_list, *blockdev;
509
    const char *device = qdict_get_try_str(qdict, "device");
510 511
    bool verbose = qdict_get_try_bool(qdict, "verbose", false);
    bool nodes = qdict_get_try_bool(qdict, "nodes", false);
512
    bool printed = false;
513

514 515
    /* Print BlockBackend information */
    if (!nodes) {
516
        block_list = qmp_query_block(NULL);
517 518 519
    } else {
        block_list = NULL;
    }
520

521 522 523
    for (info = block_list; info; info = info->next) {
        if (device && strcmp(device, info->value->device)) {
            continue;
524 525
        }

526 527
        if (info != block_list) {
            monitor_printf(mon, "\n");
528
        }
529

530 531 532
        print_block_info(mon, info->value, info->value->has_inserted
                                           ? info->value->inserted : NULL,
                         verbose);
533
        printed = true;
534 535 536
    }

    qapi_free_BlockInfoList(block_list);
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

    if ((!device && !nodes) || printed) {
        return;
    }

    /* Print node information */
    blockdev_list = qmp_query_named_block_nodes(NULL);
    for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
        assert(blockdev->value->has_node_name);
        if (device && strcmp(device, blockdev->value->node_name)) {
            continue;
        }

        if (blockdev != blockdev_list) {
            monitor_printf(mon, "\n");
        }

        print_block_info(mon, NULL, blockdev->value, verbose);
    }
    qapi_free_BlockDeviceInfoList(blockdev_list);
557 558
}

559
void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
560 561 562
{
    BlockStatsList *stats_list, *stats;

563
    stats_list = qmp_query_blockstats(false, false, NULL);
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

    for (stats = stats_list; stats; stats = stats->next) {
        if (!stats->value->has_device) {
            continue;
        }

        monitor_printf(mon, "%s:", stats->value->device);
        monitor_printf(mon, " rd_bytes=%" PRId64
                       " wr_bytes=%" PRId64
                       " rd_operations=%" PRId64
                       " wr_operations=%" PRId64
                       " flush_operations=%" PRId64
                       " wr_total_time_ns=%" PRId64
                       " rd_total_time_ns=%" PRId64
                       " flush_total_time_ns=%" PRId64
579 580
                       " rd_merged=%" PRId64
                       " wr_merged=%" PRId64
581
                       " idle_time_ns=%" PRId64
582 583 584 585 586 587 588 589
                       "\n",
                       stats->value->stats->rd_bytes,
                       stats->value->stats->wr_bytes,
                       stats->value->stats->rd_operations,
                       stats->value->stats->wr_operations,
                       stats->value->stats->flush_operations,
                       stats->value->stats->wr_total_time_ns,
                       stats->value->stats->rd_total_time_ns,
590 591
                       stats->value->stats->flush_total_time_ns,
                       stats->value->stats->rd_merged,
592 593
                       stats->value->stats->wr_merged,
                       stats->value->stats->idle_time_ns);
594 595 596 597 598
    }

    qapi_free_BlockStatsList(stats_list);
}

599 600 601 602 603 604 605 606
/* Helper for hmp_info_vnc_clients, _servers */
static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
                                  const char *name)
{
    monitor_printf(mon, "  %s: %s:%s (%s%s)\n",
                   name,
                   info->host,
                   info->service,
607
                   NetworkAddressFamily_str(info->family),
608 609 610 611 612 613 614 615 616
                   info->websocket ? " (Websocket)" : "");
}

/* Helper displaying and auth and crypt info */
static void hmp_info_vnc_authcrypt(Monitor *mon, const char *indent,
                                   VncPrimaryAuth auth,
                                   VncVencryptSubAuth *vencrypt)
{
    monitor_printf(mon, "%sAuth: %s (Sub: %s)\n", indent,
617 618
                   VncPrimaryAuth_str(auth),
                   vencrypt ? VncVencryptSubAuth_str(*vencrypt) : "none");
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
}

static void hmp_info_vnc_clients(Monitor *mon, VncClientInfoList *client)
{
    while (client) {
        VncClientInfo *cinfo = client->value;

        hmp_info_VncBasicInfo(mon, qapi_VncClientInfo_base(cinfo), "Client");
        monitor_printf(mon, "    x509_dname: %s\n",
                       cinfo->has_x509_dname ?
                       cinfo->x509_dname : "none");
        monitor_printf(mon, "    sasl_username: %s\n",
                       cinfo->has_sasl_username ?
                       cinfo->sasl_username : "none");

        client = client->next;
    }
}

static void hmp_info_vnc_servers(Monitor *mon, VncServerInfo2List *server)
{
    while (server) {
        VncServerInfo2 *sinfo = server->value;
        hmp_info_VncBasicInfo(mon, qapi_VncServerInfo2_base(sinfo), "Server");
        hmp_info_vnc_authcrypt(mon, "    ", sinfo->auth,
                               sinfo->has_vencrypt ? &sinfo->vencrypt : NULL);
        server = server->next;
    }
}

649
void hmp_info_vnc(Monitor *mon, const QDict *qdict)
650
{
651
    VncInfo2List *info2l;
652 653
    Error *err = NULL;

654
    info2l = qmp_query_vnc_servers(&err);
655
    if (err) {
656
        hmp_handle_error(mon, &err);
657 658
        return;
    }
659 660 661
    if (!info2l) {
        monitor_printf(mon, "None\n");
        return;
662 663
    }

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    while (info2l) {
        VncInfo2 *info = info2l->value;
        monitor_printf(mon, "%s:\n", info->id);
        hmp_info_vnc_servers(mon, info->server);
        hmp_info_vnc_clients(mon, info->clients);
        if (!info->server) {
            /* The server entry displays its auth, we only
             * need to display in the case of 'reverse' connections
             * where there's no server.
             */
            hmp_info_vnc_authcrypt(mon, "  ", info->auth,
                               info->has_vencrypt ? &info->vencrypt : NULL);
        }
        if (info->has_display) {
            monitor_printf(mon, "  Display: %s\n", info->display);
679
        }
680
        info2l = info2l->next;
681 682
    }

683 684
    qapi_free_VncInfo2List(info2l);

685 686
}

687
#ifdef CONFIG_SPICE
688
void hmp_info_spice(Monitor *mon, const QDict *qdict)
689 690 691
{
    SpiceChannelList *chan;
    SpiceInfo *info;
692 693 694 695 696 697 698 699 700 701 702 703
    const char *channel_name;
    const char * const channel_names[] = {
        [SPICE_CHANNEL_MAIN] = "main",
        [SPICE_CHANNEL_DISPLAY] = "display",
        [SPICE_CHANNEL_INPUTS] = "inputs",
        [SPICE_CHANNEL_CURSOR] = "cursor",
        [SPICE_CHANNEL_PLAYBACK] = "playback",
        [SPICE_CHANNEL_RECORD] = "record",
        [SPICE_CHANNEL_TUNNEL] = "tunnel",
        [SPICE_CHANNEL_SMARTCARD] = "smartcard",
        [SPICE_CHANNEL_USBREDIR] = "usbredir",
        [SPICE_CHANNEL_PORT] = "port",
704 705 706 707
#if 0
        /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
         * no easy way to #ifdef (SPICE_CHANNEL_* is a enum).  Disable
         * as quick fix for build failures with older versions. */
708
        [SPICE_CHANNEL_WEBDAV] = "webdav",
709
#endif
710
    };
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727

    info = qmp_query_spice(NULL);

    if (!info->enabled) {
        monitor_printf(mon, "Server: disabled\n");
        goto out;
    }

    monitor_printf(mon, "Server:\n");
    if (info->has_port) {
        monitor_printf(mon, "     address: %s:%" PRId64 "\n",
                       info->host, info->port);
    }
    if (info->has_tls_port) {
        monitor_printf(mon, "     address: %s:%" PRId64 " [tls]\n",
                       info->host, info->tls_port);
    }
728 729
    monitor_printf(mon, "    migrated: %s\n",
                   info->migrated ? "true" : "false");
730 731
    monitor_printf(mon, "        auth: %s\n", info->auth);
    monitor_printf(mon, "    compiled: %s\n", info->compiled_version);
Alon Levy's avatar
Alon Levy committed
732
    monitor_printf(mon, "  mouse-mode: %s\n",
733
                   SpiceQueryMouseMode_str(info->mouse_mode));
734 735 736 737 738 739 740

    if (!info->has_channels || info->channels == NULL) {
        monitor_printf(mon, "Channels: none\n");
    } else {
        for (chan = info->channels; chan; chan = chan->next) {
            monitor_printf(mon, "Channel:\n");
            monitor_printf(mon, "     address: %s:%s%s\n",
Eric Blake's avatar
Eric Blake committed
741
                           chan->value->host, chan->value->port,
742 743 744 745 746
                           chan->value->tls ? " [tls]" : "");
            monitor_printf(mon, "     session: %" PRId64 "\n",
                           chan->value->connection_id);
            monitor_printf(mon, "     channel: %" PRId64 ":%" PRId64 "\n",
                           chan->value->channel_type, chan->value->channel_id);
747 748 749 750 751 752 753 754 755

            channel_name = "unknown";
            if (chan->value->channel_type > 0 &&
                chan->value->channel_type < ARRAY_SIZE(channel_names) &&
                channel_names[chan->value->channel_type]) {
                channel_name = channel_names[chan->value->channel_type];
            }

            monitor_printf(mon, "     channel name: %s\n", channel_name);
756 757 758 759 760 761
        }
    }

out:
    qapi_free_SpiceInfo(info);
}
762
#endif
763

764
void hmp_info_balloon(Monitor *mon, const QDict *qdict)
765 766 767 768 769 770
{
    BalloonInfo *info;
    Error *err = NULL;

    info = qmp_query_balloon(&err);
    if (err) {
771
        hmp_handle_error(mon, &err);
772 773 774
        return;
    }

775
    monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
776 777 778 779

    qapi_free_BalloonInfo(info);
}

780 781 782 783 784 785 786 787 788
static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
{
    PciMemoryRegionList *region;

    monitor_printf(mon, "  Bus %2" PRId64 ", ", dev->bus);
    monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
                   dev->slot, dev->function);
    monitor_printf(mon, "    ");

789 790
    if (dev->class_info->has_desc) {
        monitor_printf(mon, "%s", dev->class_info->desc);
791
    } else {
792
        monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
793 794 795
    }

    monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
796
                   dev->id->vendor, dev->id->device);
797 798 799 800 801 802 803

    if (dev->has_irq) {
        monitor_printf(mon, "      IRQ %" PRId64 ".\n", dev->irq);
    }

    if (dev->has_pci_bridge) {
        monitor_printf(mon, "      BUS %" PRId64 ".\n",
804
                       dev->pci_bridge->bus->number);
805
        monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
806
                       dev->pci_bridge->bus->secondary);
807
        monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
808
                       dev->pci_bridge->bus->subordinate);
809 810

        monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
811 812
                       dev->pci_bridge->bus->io_range->base,
                       dev->pci_bridge->bus->io_range->limit);
813 814 815

        monitor_printf(mon,
                       "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
816 817
                       dev->pci_bridge->bus->memory_range->base,
                       dev->pci_bridge->bus->memory_range->limit);
818 819 820

        monitor_printf(mon, "      prefetchable memory range "
                       "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
821 822
                       dev->pci_bridge->bus->prefetchable_range->base,
                       dev->pci_bridge->bus->prefetchable_range->limit);
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
    }

    for (region = dev->regions; region; region = region->next) {
        uint64_t addr, size;

        addr = region->value->address;
        size = region->value->size;

        monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);

        if (!strcmp(region->value->type, "io")) {
            monitor_printf(mon, "I/O at 0x%04" PRIx64
                                " [0x%04" PRIx64 "].\n",
                           addr, addr + size - 1);
        } else {
            monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
                               " [0x%08" PRIx64 "].\n",
                           region->value->mem_type_64 ? 64 : 32,
                           region->value->prefetch ? " prefetchable" : "",
                           addr, addr + size - 1);
        }
    }

    monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);

    if (dev->has_pci_bridge) {
        if (dev->pci_bridge->has_devices) {
            PciDeviceInfoList *cdev;
            for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
                hmp_info_pci_device(mon, cdev->value);
            }
        }
    }
}

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
static int hmp_info_irq_foreach(Object *obj, void *opaque)
{
    InterruptStatsProvider *intc;
    InterruptStatsProviderClass *k;
    Monitor *mon = opaque;

    if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
        intc = INTERRUPT_STATS_PROVIDER(obj);
        k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
        uint64_t *irq_counts;
        unsigned int nb_irqs, i;
        if (k->get_statistics &&
            k->get_statistics(intc, &irq_counts, &nb_irqs)) {
            if (nb_irqs > 0) {
                monitor_printf(mon, "IRQ statistics for %s:\n",
                               object_get_typename(obj));
                for (i = 0; i < nb_irqs; i++) {
                    if (irq_counts[i] > 0) {
                        monitor_printf(mon, "%2d: %" PRId64 "\n", i,
                                       irq_counts[i]);
                    }
                }
            }
        } else {
            monitor_printf(mon, "IRQ statistics not available for %s.\n",
                           object_get_typename(obj));
        }
    }

    return 0;
}

void hmp_info_irq(Monitor *mon, const QDict *qdict)
{
    object_child_foreach_recursive(object_get_root(),
                                   hmp_info_irq_foreach, mon);
}

static int hmp_info_pic_foreach(Object *obj, void *opaque)
{
    InterruptStatsProvider *intc;
    InterruptStatsProviderClass *k;
    Monitor *mon = opaque;

    if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
        intc = INTERRUPT_STATS_PROVIDER(obj);
        k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
        if (k->print_info) {
            k->print_info(intc, mon);
        } else {
            monitor_printf(mon, "Interrupt controller information not available for %s.\n",
                           object_get_typename(obj));
        }
    }

    return 0;
}

void hmp_info_pic(Monitor *mon, const QDict *qdict)
{
    object_child_foreach_recursive(object_get_root(),
                                   hmp_info_pic_foreach, mon);
}

922
void hmp_info_pci(Monitor *mon, const QDict *qdict)
923
{
924
    PciInfoList *info_list, *info;
925 926
    Error *err = NULL;

927
    info_list = qmp_query_pci(&err);
928 929 930 931 932 933
    if (err) {
        monitor_printf(mon, "PCI devices not supported\n");
        error_free(err);
        return;
    }

934
    for (info = info_list; info; info = info->next) {
935 936 937 938 939 940 941
        PciDeviceInfoList *dev;

        for (dev = info->value->devices; dev; dev = dev->next) {
            hmp_info_pci_device(mon, dev->value);
        }
    }

942
    qapi_free_PciInfoList(info_list);
943 944
}

945
void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
{
    BlockJobInfoList *list;
    Error *err = NULL;

    list = qmp_query_block_jobs(&err);
    assert(!err);

    if (!list) {
        monitor_printf(mon, "No active jobs\n");
        return;
    }

    while (list) {
        if (strcmp(list->value->type, "stream") == 0) {
            monitor_printf(mon, "Streaming device %s: Completed %" PRId64
                           " of %" PRId64 " bytes, speed limit %" PRId64
                           " bytes/s\n",
                           list->value->device,
                           list->value->offset,
                           list->value->len,
                           list->value->speed);
        } else {
            monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
                           " of %" PRId64 " bytes, speed limit %" PRId64
                           " bytes/s\n",
                           list->value->type,
                           list->value->device,
                           list->value->offset,
                           list->value->len,
                           list->value->speed);
        }
        list = list->next;
    }
979 980

    qapi_free_BlockJobInfoList(list);
981 982
}

983 984 985 986 987 988
void hmp_info_tpm(Monitor *mon, const QDict *qdict)
{
    TPMInfoList *info_list, *info;
    Error *err = NULL;
    unsigned int c = 0;
    TPMPassthroughOptions *tpo;
989
    TPMEmulatorOptions *teo;
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004

    info_list = qmp_query_tpm(&err);
    if (err) {
        monitor_printf(mon, "TPM device not supported\n");
        error_free(err);
        return;
    }

    if (info_list) {
        monitor_printf(mon, "TPM device:\n");
    }

    for (info = info_list; info; info = info->next) {
        TPMInfo *ti = info->value;
        monitor_printf(mon, " tpm%d: model=%s\n",
1005
                       c, TpmModel_str(ti->model));
1006 1007

        monitor_printf(mon, "  \\ %s: type=%s",
1008
                       ti->id, TpmTypeOptionsKind_str(ti->options->type));
1009

1010
        switch (ti->options->type) {
1011
        case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
1012
            tpo = ti->options->u.passthrough.data;
1013 1014 1015 1016 1017 1018
            monitor_printf(mon, "%s%s%s%s",
                           tpo->has_path ? ",path=" : "",
                           tpo->has_path ? tpo->path : "",
                           tpo->has_cancel_path ? ",cancel-path=" : "",
                           tpo->has_cancel_path ? tpo->cancel_path : "");
            break;
1019 1020 1021 1022
        case TPM_TYPE_OPTIONS_KIND_EMULATOR:
            teo = ti->options->u.emulator.data;
            monitor_printf(mon, ",chardev=%s", teo->chardev);
            break;
1023
        case TPM_TYPE_OPTIONS_KIND__MAX:
1024 1025 1026 1027 1028 1029 1030 1031
            break;
        }
        monitor_printf(mon, "\n");
        c++;
    }
    qapi_free_TPMInfoList(info_list);
}

Luiz Capitulino's avatar
Luiz Capitulino committed
1032 1033 1034 1035 1036
void hmp_quit(Monitor *mon, const QDict *qdict)
{
    monitor_suspend(mon);
    qmp_quit(NULL);
}
Luiz Capitulino's avatar
Luiz Capitulino committed
1037 1038 1039 1040 1041

void hmp_stop(Monitor *mon, const QDict *qdict)
{
    qmp_stop(NULL);
}
1042 1043 1044 1045 1046

void hmp_system_reset(Monitor *mon, const QDict *qdict)
{
    qmp_system_reset(NULL);
}
1047 1048 1049 1050 1051

void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
{
    qmp_system_powerdown(NULL);
}
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063

void hmp_cpu(Monitor *mon, const QDict *qdict)
{
    int64_t cpu_index;

    /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
            use it are converted to the QAPI */
    cpu_index = qdict_get_int(qdict, "index");
    if (monitor_set_cpu(cpu_index) < 0) {
        monitor_printf(mon, "invalid CPU index\n");
    }
}
Luiz Capitulino's avatar
Luiz Capitulino committed
1064 1065 1066 1067 1068 1069

void hmp_memsave(Monitor *mon, const QDict *qdict)
{
    uint32_t size = qdict_get_int(qdict, "size");
    const char *filename = qdict_get_str(qdict, "filename");
    uint64_t addr = qdict_get_int(qdict, "val");
1070
    Error *err = NULL;
1071
    int cpu_index = monitor_get_cpu_index();
Luiz Capitulino's avatar
Luiz Capitulino committed
1072

1073 1074 1075 1076 1077 1078
    if (cpu_index < 0) {
        monitor_printf(mon, "No CPU available\n");
        return;
    }

    qmp_memsave(addr, size, filename, true, cpu_index, &err);
1079
    hmp_handle_error(mon, &err);
Luiz Capitulino's avatar
Luiz Capitulino committed
1080
}
Luiz Capitulino's avatar
Luiz Capitulino committed
1081 1082 1083 1084 1085 1086

void hmp_pmemsave(Monitor *mon, const QDict *qdict)
{
    uint32_t size = qdict_get_int(qdict, "size");
    const char *filename = qdict_get_str(qdict, "filename");
    uint64_t addr = qdict_get_int(qdict, "val");
1087
    Error *err = NULL;
Luiz Capitulino's avatar
Luiz Capitulino committed
1088

1089 1090
    qmp_pmemsave(addr, size, filename, &err);
    hmp_handle_error(mon, &err);
1091 1092
}

1093
void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1094 1095 1096
{
    const char *chardev = qdict_get_str(qdict, "device");
    const char *data = qdict_get_str(qdict, "data");
1097
    Error *err = NULL;
1098

1099
    qmp_ringbuf_write(chardev, data, false, 0, &err);
1100

1101
    hmp_handle_error(mon, &err);
Luiz Capitulino's avatar
Luiz Capitulino committed
1102
}
Luiz Capitulino's avatar
Luiz Capitulino committed
1103

1104
void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1105 1106 1107
{
    uint32_t size = qdict_get_int(qdict, "size");
    const char *chardev = qdict_get_str(qdict, "device");
1108
    char *data;
1109
    Error *err = NULL;
1110
    int i;
1111

1112 1113
    data = qmp_ringbuf_read(chardev, size, false, 0, &err);
    if (err) {
1114
        hmp_handle_error(mon, &err);
1115 1116 1117
        return;
    }

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
    for (i = 0; data[i]; i++) {
        unsigned char ch = data[i];

        if (ch == '\\') {
            monitor_printf(mon, "\\\\");
        } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
            monitor_printf(mon, "\\u%04X", ch);
        } else {
            monitor_printf(mon, "%c", ch);
        }

    }
    monitor_printf(mon, "\n");
1131
    g_free(data);
1132 1133
}

Luiz Capitulino's avatar
Luiz Capitulino committed
1134 1135
void hmp_cont(Monitor *mon, const QDict *qdict)
{
1136
    Error *err = NULL;
Luiz Capitulino's avatar
Luiz Capitulino committed
1137

1138 1139
    qmp_cont(&err);
    hmp_handle_error(mon, &err);
Luiz Capitulino's avatar
Luiz Capitulino committed
1140
}
1141

1142 1143 1144 1145 1146
void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
{
    qmp_system_wakeup(NULL);
}

1147
void hmp_nmi(Monitor *mon, const QDict *qdict)
1148
{
1149
    Error *err = NULL;
1150

1151 1152
    qmp_inject_nmi(&err);
    hmp_handle_error(mon, &err);
1153
}
Luiz Capitulino's avatar
Luiz Capitulino committed
1154 1155 1156 1157

void hmp_set_link(Monitor *mon, const QDict *qdict)
{
    const char *name = qdict_get_str(qdict, "name");
1158
    bool up = qdict_get_bool(qdict, "up");
1159
    Error *err = NULL;
Luiz Capitulino's avatar
Luiz Capitulino committed
1160

1161 1162
    qmp_set_link(name, up, &err);
    hmp_handle_error(mon, &err);
Luiz Capitulino's avatar
Luiz Capitulino committed
1163
}
1164 1165 1166 1167 1168

void hmp_block_passwd(Monitor *mon, const QDict *qdict)
{
    const char *device = qdict_get_str(qdict, "device");
    const char *password = qdict_get_str(qdict, "password");
1169
    Error *err = NULL;
1170

1171 1172
    qmp_block_passwd(true, device, false, NULL, password, &err);
    hmp_handle_error(mon, &err);
1173
}
Luiz Capitulino's avatar
Luiz Capitulino committed
1174 1175 1176 1177

void hmp_balloon(Monitor *mon, const QDict *qdict)
{
    int64_t value = qdict_get_int(qdict, "value");
1178
    Error *err = NULL;
Luiz Capitulino's avatar
Luiz Capitulino committed
1179

1180
    qmp_balloon(value, &err);
1181
    hmp_handle_error(mon, &err);
Luiz Capitulino's avatar
Luiz Capitulino committed
1182
}
1183 1184 1185 1186 1187

void hmp_block_resize(Monitor *mon, const QDict *qdict)
{
    const char *device = qdict_get_str(qdict, "device");
    int64_t size = qdict_get_int(qdict, "size");
1188
    Error *err = NULL;
1189

1190 1191
    qmp_block_resize(true, device, false, NULL, size, &err);
    hmp_handle_error(mon, &err);
1192
}
1193

1194 1195 1196 1197
void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
{
    const char *filename = qdict_get_str(qdict, "target");
    const char *format = qdict_get_try_str(qdict, "format");
1198 1199
    bool reuse = qdict_get_try_bool(qdict, "reuse", false);
    bool full = qdict_get_try_bool(qdict, "full", false);
1200
    Error *err = NULL;
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
    DriveMirror mirror = {
        .device = (char *)qdict_get_str(qdict, "device"),
        .target = (char *)filename,
        .has_format = !!format,
        .format = (char *)format,
        .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
        .has_mode = true,
        .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
        .unmap = true,
    };
1211 1212

    if (!filename) {
1213
        error_setg(&err, QERR_MISSING_PARAMETER, "target");
1214
        hmp_handle_error(mon, &err);
1215 1216
        return;
    }
1217
    qmp_drive_mirror(&mirror, &err);
1218
    hmp_handle_error(mon, &err);
1219 1220
}

1221 1222 1223 1224 1225
void hmp_drive_backup(Monitor *mon, const QDict *qdict)
{
    const char *device = qdict_get_str(qdict, "device");
    const char *filename = qdict_get_str(qdict, "target");
    const char *format = qdict_get_try_str(qdict, "format");
1226 1227
    bool reuse = qdict_get_try_bool(qdict, "reuse", false);
    bool full = qdict_get_try_bool(qdict, "full", false);
1228
    bool compress = qdict_get_try_bool(qdict, "compress", false);
1229
    Error *err = NULL;
1230 1231 1232 1233 1234 1235 1236 1237
    DriveBackup backup = {
        .device = (char *)device,
        .target = (char *)filename,
        .has_format = !!format,
        .format = (char *)format,
        .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
        .has_mode = true,
        .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1238 1239
        .has_compress = !!compress,
        .compress = compress,
1240
    };
1241 1242

    if (!filename) {
1243
        error_setg(&err, QERR_MISSING_PARAMETER, "target");
1244
        hmp_handle_error(mon, &err);
1245 1246 1247
        return;
    }

1248
    qmp_drive_backup(&backup, &err);
1249
    hmp_handle_error(mon, &err);
1250 1251
}

1252 1253 1254 1255 1256
void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
{
    const char *device = qdict_get_str(qdict, "device");
    const char *filename = qdict_get_try_str(qdict, "snapshot-file");
    const char *format = qdict_get_try_str(qdict, "format");
1257
    bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1258
    enum NewImageMode mode;
1259
    Error *err = NULL;
1260 1261 1262 1263

    if (!filename) {
        /* In the future, if 'snapshot-file' is not specified, the snapshot
           will be taken internally. Today it's actually required. */
1264
        error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1265
        hmp_handle_error(mon, &err);
1266 1267 1268
        return;
    }

1269
    mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1270 1271 1272
    qmp_blockdev_snapshot_sync(true, device, false, NULL,
                               filename, false, NULL,
                               !!format, format,
1273 1274
                               true, mode, &err);
    hmp_handle_error(mon, &err);
1275
}
1276

1277 1278 1279 1280
void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
{
    const char *device = qdict_get_str(qdict, "device");
    const char *name = qdict_get_str(qdict, "name");
1281
    Error *err = NULL;
1282

1283 1284
    qmp_blockdev_snapshot_internal_sync(device, name, &err);
    hmp_handle_error(mon, &err);
1285 1286
}

1287 1288 1289 1290 1291
void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
{
    const char *device = qdict_get_str(qdict, "device");
    const char *name = qdict_get_str(qdict, "name");
    const char *id = qdict_get_try_str(qdict, "id");
1292
    Error *err = NULL;
1293 1294

    qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1295 1296
                                               true, name, &err);
    hmp_handle_error(mon, &err);
1297 1298
}

1299 1300 1301 1302
void hmp_loadvm(Monitor *mon, const QDict *qdict)
{
    int saved_vm_running  = runstate_is_running();
    const char *name = qdict_get_str(qdict, "name");
1303
    Error *err = NULL;
1304 1305 1306

    vm_stop(RUN_STATE_RESTORE_VM);

1307
    if (load_snapshot(name, &err) == 0 && saved_vm_running) {
1308 1309
        vm_start();
    }
1310
    hmp_handle_error(mon, &err);
1311 1312
}

1313 1314
void hmp_savevm(Monitor *mon, const QDict *qdict)
{
1315 1316
    Error *err = NULL;

1317
    save_snapshot(qdict_get_try_str(qdict, "name"), &err);
1318
    hmp_handle_error(mon, &err);
1319 1320
}

1321 1322 1323
void hmp_delvm(Monitor *mon, const QDict *qdict)
{
    BlockDriverState *bs;
1324
    Error *err = NULL;
1325 1326 1327 1328 1329 1330 1331 1332 1333
    const char *name = qdict_get_str(qdict, "name");

    if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
        error_reportf_err(err,
                          "Error while deleting snapshot on device '%s': ",
                          bdrv_get_device_name(bs));
    }
}

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1