s390x/watchdog: diag288 migration support
[qemu.git] / hmp.c
blob514f22fbfa50630a278f487d4a35d1b33b15b37c
1 /*
2 * Human Monitor Interface
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "hmp.h"
17 #include "net/net.h"
18 #include "sysemu/char.h"
19 #include "sysemu/block-backend.h"
20 #include "qemu/option.h"
21 #include "qemu/timer.h"
22 #include "qmp-commands.h"
23 #include "qemu/sockets.h"
24 #include "monitor/monitor.h"
25 #include "monitor/qdev.h"
26 #include "qapi/opts-visitor.h"
27 #include "qapi/string-output-visitor.h"
28 #include "qapi-visit.h"
29 #include "ui/console.h"
30 #include "block/qapi.h"
31 #include "qemu-io.h"
33 #ifdef CONFIG_SPICE
34 #include <spice/enums.h>
35 #endif
37 static void hmp_handle_error(Monitor *mon, Error **errp)
39 assert(errp);
40 if (*errp) {
41 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
42 error_free(*errp);
46 void hmp_info_name(Monitor *mon, const QDict *qdict)
48 NameInfo *info;
50 info = qmp_query_name(NULL);
51 if (info->has_name) {
52 monitor_printf(mon, "%s\n", info->name);
54 qapi_free_NameInfo(info);
57 void hmp_info_version(Monitor *mon, const QDict *qdict)
59 VersionInfo *info;
61 info = qmp_query_version(NULL);
63 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
64 info->qemu->major, info->qemu->minor, info->qemu->micro,
65 info->package);
67 qapi_free_VersionInfo(info);
70 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
72 KvmInfo *info;
74 info = qmp_query_kvm(NULL);
75 monitor_printf(mon, "kvm support: ");
76 if (info->present) {
77 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
78 } else {
79 monitor_printf(mon, "not compiled\n");
82 qapi_free_KvmInfo(info);
85 void hmp_info_status(Monitor *mon, const QDict *qdict)
87 StatusInfo *info;
89 info = qmp_query_status(NULL);
91 monitor_printf(mon, "VM status: %s%s",
92 info->running ? "running" : "paused",
93 info->singlestep ? " (single step mode)" : "");
95 if (!info->running && info->status != RUN_STATE_PAUSED) {
96 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
99 monitor_printf(mon, "\n");
101 qapi_free_StatusInfo(info);
104 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
106 UuidInfo *info;
108 info = qmp_query_uuid(NULL);
109 monitor_printf(mon, "%s\n", info->UUID);
110 qapi_free_UuidInfo(info);
113 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
115 ChardevInfoList *char_info, *info;
117 char_info = qmp_query_chardev(NULL);
118 for (info = char_info; info; info = info->next) {
119 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
120 info->value->filename);
123 qapi_free_ChardevInfoList(char_info);
126 void hmp_info_mice(Monitor *mon, const QDict *qdict)
128 MouseInfoList *mice_list, *mouse;
130 mice_list = qmp_query_mice(NULL);
131 if (!mice_list) {
132 monitor_printf(mon, "No mouse devices connected\n");
133 return;
136 for (mouse = mice_list; mouse; mouse = mouse->next) {
137 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
138 mouse->value->current ? '*' : ' ',
139 mouse->value->index, mouse->value->name,
140 mouse->value->absolute ? " (absolute)" : "");
143 qapi_free_MouseInfoList(mice_list);
146 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
148 MigrationInfo *info;
149 MigrationCapabilityStatusList *caps, *cap;
151 info = qmp_query_migrate(NULL);
152 caps = qmp_query_migrate_capabilities(NULL);
154 /* do not display parameters during setup */
155 if (info->has_status && caps) {
156 monitor_printf(mon, "capabilities: ");
157 for (cap = caps; cap; cap = cap->next) {
158 monitor_printf(mon, "%s: %s ",
159 MigrationCapability_lookup[cap->value->capability],
160 cap->value->state ? "on" : "off");
162 monitor_printf(mon, "\n");
165 if (info->has_status) {
166 monitor_printf(mon, "Migration status: %s\n",
167 MigrationStatus_lookup[info->status]);
168 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
169 info->total_time);
170 if (info->has_expected_downtime) {
171 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
172 info->expected_downtime);
174 if (info->has_downtime) {
175 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
176 info->downtime);
178 if (info->has_setup_time) {
179 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
180 info->setup_time);
184 if (info->has_ram) {
185 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
186 info->ram->transferred >> 10);
187 monitor_printf(mon, "throughput: %0.2f mbps\n",
188 info->ram->mbps);
189 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
190 info->ram->remaining >> 10);
191 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
192 info->ram->total >> 10);
193 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
194 info->ram->duplicate);
195 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
196 info->ram->skipped);
197 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
198 info->ram->normal);
199 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
200 info->ram->normal_bytes >> 10);
201 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
202 info->ram->dirty_sync_count);
203 if (info->ram->dirty_pages_rate) {
204 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
205 info->ram->dirty_pages_rate);
209 if (info->has_disk) {
210 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
211 info->disk->transferred >> 10);
212 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
213 info->disk->remaining >> 10);
214 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
215 info->disk->total >> 10);
218 if (info->has_xbzrle_cache) {
219 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
220 info->xbzrle_cache->cache_size);
221 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
222 info->xbzrle_cache->bytes >> 10);
223 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
224 info->xbzrle_cache->pages);
225 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
226 info->xbzrle_cache->cache_miss);
227 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
228 info->xbzrle_cache->cache_miss_rate);
229 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
230 info->xbzrle_cache->overflow);
233 qapi_free_MigrationInfo(info);
234 qapi_free_MigrationCapabilityStatusList(caps);
237 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
239 MigrationCapabilityStatusList *caps, *cap;
241 caps = qmp_query_migrate_capabilities(NULL);
243 if (caps) {
244 monitor_printf(mon, "capabilities: ");
245 for (cap = caps; cap; cap = cap->next) {
246 monitor_printf(mon, "%s: %s ",
247 MigrationCapability_lookup[cap->value->capability],
248 cap->value->state ? "on" : "off");
250 monitor_printf(mon, "\n");
253 qapi_free_MigrationCapabilityStatusList(caps);
256 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
258 MigrationParameters *params;
260 params = qmp_query_migrate_parameters(NULL);
262 if (params) {
263 monitor_printf(mon, "parameters:");
264 monitor_printf(mon, " %s: %" PRId64,
265 MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_LEVEL],
266 params->compress_level);
267 monitor_printf(mon, " %s: %" PRId64,
268 MigrationParameter_lookup[MIGRATION_PARAMETER_COMPRESS_THREADS],
269 params->compress_threads);
270 monitor_printf(mon, " %s: %" PRId64,
271 MigrationParameter_lookup[MIGRATION_PARAMETER_DECOMPRESS_THREADS],
272 params->decompress_threads);
273 monitor_printf(mon, "\n");
276 qapi_free_MigrationParameters(params);
279 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
281 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
282 qmp_query_migrate_cache_size(NULL) >> 10);
285 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
287 CpuInfoList *cpu_list, *cpu;
289 cpu_list = qmp_query_cpus(NULL);
291 for (cpu = cpu_list; cpu; cpu = cpu->next) {
292 int active = ' ';
294 if (cpu->value->CPU == monitor_get_cpu_index()) {
295 active = '*';
298 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
300 if (cpu->value->has_pc) {
301 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
303 if (cpu->value->has_nip) {
304 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
306 if (cpu->value->has_npc) {
307 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
309 if (cpu->value->has_PC) {
310 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
313 if (cpu->value->halted) {
314 monitor_printf(mon, " (halted)");
317 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
320 qapi_free_CpuInfoList(cpu_list);
323 static void print_block_info(Monitor *mon, BlockInfo *info,
324 BlockDeviceInfo *inserted, bool verbose)
326 ImageInfo *image_info;
328 assert(!info || !info->has_inserted || info->inserted == inserted);
330 if (info) {
331 monitor_printf(mon, "%s", info->device);
332 if (inserted && inserted->has_node_name) {
333 monitor_printf(mon, " (%s)", inserted->node_name);
335 } else {
336 assert(inserted);
337 monitor_printf(mon, "%s",
338 inserted->has_node_name
339 ? inserted->node_name
340 : "<anonymous>");
343 if (inserted) {
344 monitor_printf(mon, ": %s (%s%s%s)\n",
345 inserted->file,
346 inserted->drv,
347 inserted->ro ? ", read-only" : "",
348 inserted->encrypted ? ", encrypted" : "");
349 } else {
350 monitor_printf(mon, ": [not inserted]\n");
353 if (info) {
354 if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
355 monitor_printf(mon, " I/O status: %s\n",
356 BlockDeviceIoStatus_lookup[info->io_status]);
359 if (info->removable) {
360 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
361 info->locked ? "" : "not ",
362 info->tray_open ? "open" : "closed");
367 if (!inserted) {
368 return;
371 monitor_printf(mon, " Cache mode: %s%s%s\n",
372 inserted->cache->writeback ? "writeback" : "writethrough",
373 inserted->cache->direct ? ", direct" : "",
374 inserted->cache->no_flush ? ", ignore flushes" : "");
376 if (inserted->has_backing_file) {
377 monitor_printf(mon,
378 " Backing file: %s "
379 "(chain depth: %" PRId64 ")\n",
380 inserted->backing_file,
381 inserted->backing_file_depth);
384 if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
385 monitor_printf(mon, " Detect zeroes: %s\n",
386 BlockdevDetectZeroesOptions_lookup[inserted->detect_zeroes]);
389 if (inserted->bps || inserted->bps_rd || inserted->bps_wr ||
390 inserted->iops || inserted->iops_rd || inserted->iops_wr)
392 monitor_printf(mon, " I/O throttling: bps=%" PRId64
393 " bps_rd=%" PRId64 " bps_wr=%" PRId64
394 " bps_max=%" PRId64
395 " bps_rd_max=%" PRId64
396 " bps_wr_max=%" PRId64
397 " iops=%" PRId64 " iops_rd=%" PRId64
398 " iops_wr=%" PRId64
399 " iops_max=%" PRId64
400 " iops_rd_max=%" PRId64
401 " iops_wr_max=%" PRId64
402 " iops_size=%" PRId64 "\n",
403 inserted->bps,
404 inserted->bps_rd,
405 inserted->bps_wr,
406 inserted->bps_max,
407 inserted->bps_rd_max,
408 inserted->bps_wr_max,
409 inserted->iops,
410 inserted->iops_rd,
411 inserted->iops_wr,
412 inserted->iops_max,
413 inserted->iops_rd_max,
414 inserted->iops_wr_max,
415 inserted->iops_size);
418 if (verbose) {
419 monitor_printf(mon, "\nImages:\n");
420 image_info = inserted->image;
421 while (1) {
422 bdrv_image_info_dump((fprintf_function)monitor_printf,
423 mon, image_info);
424 if (image_info->has_backing_image) {
425 image_info = image_info->backing_image;
426 } else {
427 break;
433 void hmp_info_block(Monitor *mon, const QDict *qdict)
435 BlockInfoList *block_list, *info;
436 BlockDeviceInfoList *blockdev_list, *blockdev;
437 const char *device = qdict_get_try_str(qdict, "device");
438 bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
439 bool nodes = qdict_get_try_bool(qdict, "nodes", 0);
440 bool printed = false;
442 /* Print BlockBackend information */
443 if (!nodes) {
444 block_list = qmp_query_block(NULL);
445 } else {
446 block_list = NULL;
449 for (info = block_list; info; info = info->next) {
450 if (device && strcmp(device, info->value->device)) {
451 continue;
454 if (info != block_list) {
455 monitor_printf(mon, "\n");
458 print_block_info(mon, info->value, info->value->has_inserted
459 ? info->value->inserted : NULL,
460 verbose);
461 printed = true;
464 qapi_free_BlockInfoList(block_list);
466 if ((!device && !nodes) || printed) {
467 return;
470 /* Print node information */
471 blockdev_list = qmp_query_named_block_nodes(NULL);
472 for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
473 assert(blockdev->value->has_node_name);
474 if (device && strcmp(device, blockdev->value->node_name)) {
475 continue;
478 if (blockdev != blockdev_list) {
479 monitor_printf(mon, "\n");
482 print_block_info(mon, NULL, blockdev->value, verbose);
484 qapi_free_BlockDeviceInfoList(blockdev_list);
487 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
489 BlockStatsList *stats_list, *stats;
491 stats_list = qmp_query_blockstats(false, false, NULL);
493 for (stats = stats_list; stats; stats = stats->next) {
494 if (!stats->value->has_device) {
495 continue;
498 monitor_printf(mon, "%s:", stats->value->device);
499 monitor_printf(mon, " rd_bytes=%" PRId64
500 " wr_bytes=%" PRId64
501 " rd_operations=%" PRId64
502 " wr_operations=%" PRId64
503 " flush_operations=%" PRId64
504 " wr_total_time_ns=%" PRId64
505 " rd_total_time_ns=%" PRId64
506 " flush_total_time_ns=%" PRId64
507 " rd_merged=%" PRId64
508 " wr_merged=%" PRId64
509 "\n",
510 stats->value->stats->rd_bytes,
511 stats->value->stats->wr_bytes,
512 stats->value->stats->rd_operations,
513 stats->value->stats->wr_operations,
514 stats->value->stats->flush_operations,
515 stats->value->stats->wr_total_time_ns,
516 stats->value->stats->rd_total_time_ns,
517 stats->value->stats->flush_total_time_ns,
518 stats->value->stats->rd_merged,
519 stats->value->stats->wr_merged);
522 qapi_free_BlockStatsList(stats_list);
525 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
527 VncInfo *info;
528 Error *err = NULL;
529 VncClientInfoList *client;
531 info = qmp_query_vnc(&err);
532 if (err) {
533 monitor_printf(mon, "%s\n", error_get_pretty(err));
534 error_free(err);
535 return;
538 if (!info->enabled) {
539 monitor_printf(mon, "Server: disabled\n");
540 goto out;
543 monitor_printf(mon, "Server:\n");
544 if (info->has_host && info->has_service) {
545 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
547 if (info->has_auth) {
548 monitor_printf(mon, " auth: %s\n", info->auth);
551 if (!info->has_clients || info->clients == NULL) {
552 monitor_printf(mon, "Client: none\n");
553 } else {
554 for (client = info->clients; client; client = client->next) {
555 monitor_printf(mon, "Client:\n");
556 monitor_printf(mon, " address: %s:%s\n",
557 client->value->base->host,
558 client->value->base->service);
559 monitor_printf(mon, " x509_dname: %s\n",
560 client->value->x509_dname ?
561 client->value->x509_dname : "none");
562 monitor_printf(mon, " username: %s\n",
563 client->value->has_sasl_username ?
564 client->value->sasl_username : "none");
568 out:
569 qapi_free_VncInfo(info);
572 #ifdef CONFIG_SPICE
573 void hmp_info_spice(Monitor *mon, const QDict *qdict)
575 SpiceChannelList *chan;
576 SpiceInfo *info;
577 const char *channel_name;
578 const char * const channel_names[] = {
579 [SPICE_CHANNEL_MAIN] = "main",
580 [SPICE_CHANNEL_DISPLAY] = "display",
581 [SPICE_CHANNEL_INPUTS] = "inputs",
582 [SPICE_CHANNEL_CURSOR] = "cursor",
583 [SPICE_CHANNEL_PLAYBACK] = "playback",
584 [SPICE_CHANNEL_RECORD] = "record",
585 [SPICE_CHANNEL_TUNNEL] = "tunnel",
586 [SPICE_CHANNEL_SMARTCARD] = "smartcard",
587 [SPICE_CHANNEL_USBREDIR] = "usbredir",
588 [SPICE_CHANNEL_PORT] = "port",
589 #if 0
590 /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
591 * no easy way to #ifdef (SPICE_CHANNEL_* is a enum). Disable
592 * as quick fix for build failures with older versions. */
593 [SPICE_CHANNEL_WEBDAV] = "webdav",
594 #endif
597 info = qmp_query_spice(NULL);
599 if (!info->enabled) {
600 monitor_printf(mon, "Server: disabled\n");
601 goto out;
604 monitor_printf(mon, "Server:\n");
605 if (info->has_port) {
606 monitor_printf(mon, " address: %s:%" PRId64 "\n",
607 info->host, info->port);
609 if (info->has_tls_port) {
610 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
611 info->host, info->tls_port);
613 monitor_printf(mon, " migrated: %s\n",
614 info->migrated ? "true" : "false");
615 monitor_printf(mon, " auth: %s\n", info->auth);
616 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
617 monitor_printf(mon, " mouse-mode: %s\n",
618 SpiceQueryMouseMode_lookup[info->mouse_mode]);
620 if (!info->has_channels || info->channels == NULL) {
621 monitor_printf(mon, "Channels: none\n");
622 } else {
623 for (chan = info->channels; chan; chan = chan->next) {
624 monitor_printf(mon, "Channel:\n");
625 monitor_printf(mon, " address: %s:%s%s\n",
626 chan->value->base->host, chan->value->base->port,
627 chan->value->tls ? " [tls]" : "");
628 monitor_printf(mon, " session: %" PRId64 "\n",
629 chan->value->connection_id);
630 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
631 chan->value->channel_type, chan->value->channel_id);
633 channel_name = "unknown";
634 if (chan->value->channel_type > 0 &&
635 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
636 channel_names[chan->value->channel_type]) {
637 channel_name = channel_names[chan->value->channel_type];
640 monitor_printf(mon, " channel name: %s\n", channel_name);
644 out:
645 qapi_free_SpiceInfo(info);
647 #endif
649 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
651 BalloonInfo *info;
652 Error *err = NULL;
654 info = qmp_query_balloon(&err);
655 if (err) {
656 monitor_printf(mon, "%s\n", error_get_pretty(err));
657 error_free(err);
658 return;
661 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
663 qapi_free_BalloonInfo(info);
666 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
668 PciMemoryRegionList *region;
670 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
671 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
672 dev->slot, dev->function);
673 monitor_printf(mon, " ");
675 if (dev->class_info->has_desc) {
676 monitor_printf(mon, "%s", dev->class_info->desc);
677 } else {
678 monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
681 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
682 dev->id->vendor, dev->id->device);
684 if (dev->has_irq) {
685 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
688 if (dev->has_pci_bridge) {
689 monitor_printf(mon, " BUS %" PRId64 ".\n",
690 dev->pci_bridge->bus->number);
691 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
692 dev->pci_bridge->bus->secondary);
693 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
694 dev->pci_bridge->bus->subordinate);
696 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
697 dev->pci_bridge->bus->io_range->base,
698 dev->pci_bridge->bus->io_range->limit);
700 monitor_printf(mon,
701 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
702 dev->pci_bridge->bus->memory_range->base,
703 dev->pci_bridge->bus->memory_range->limit);
705 monitor_printf(mon, " prefetchable memory range "
706 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
707 dev->pci_bridge->bus->prefetchable_range->base,
708 dev->pci_bridge->bus->prefetchable_range->limit);
711 for (region = dev->regions; region; region = region->next) {
712 uint64_t addr, size;
714 addr = region->value->address;
715 size = region->value->size;
717 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
719 if (!strcmp(region->value->type, "io")) {
720 monitor_printf(mon, "I/O at 0x%04" PRIx64
721 " [0x%04" PRIx64 "].\n",
722 addr, addr + size - 1);
723 } else {
724 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
725 " [0x%08" PRIx64 "].\n",
726 region->value->mem_type_64 ? 64 : 32,
727 region->value->prefetch ? " prefetchable" : "",
728 addr, addr + size - 1);
732 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
734 if (dev->has_pci_bridge) {
735 if (dev->pci_bridge->has_devices) {
736 PciDeviceInfoList *cdev;
737 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
738 hmp_info_pci_device(mon, cdev->value);
744 void hmp_info_pci(Monitor *mon, const QDict *qdict)
746 PciInfoList *info_list, *info;
747 Error *err = NULL;
749 info_list = qmp_query_pci(&err);
750 if (err) {
751 monitor_printf(mon, "PCI devices not supported\n");
752 error_free(err);
753 return;
756 for (info = info_list; info; info = info->next) {
757 PciDeviceInfoList *dev;
759 for (dev = info->value->devices; dev; dev = dev->next) {
760 hmp_info_pci_device(mon, dev->value);
764 qapi_free_PciInfoList(info_list);
767 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
769 BlockJobInfoList *list;
770 Error *err = NULL;
772 list = qmp_query_block_jobs(&err);
773 assert(!err);
775 if (!list) {
776 monitor_printf(mon, "No active jobs\n");
777 return;
780 while (list) {
781 if (strcmp(list->value->type, "stream") == 0) {
782 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
783 " of %" PRId64 " bytes, speed limit %" PRId64
784 " bytes/s\n",
785 list->value->device,
786 list->value->offset,
787 list->value->len,
788 list->value->speed);
789 } else {
790 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
791 " of %" PRId64 " bytes, speed limit %" PRId64
792 " bytes/s\n",
793 list->value->type,
794 list->value->device,
795 list->value->offset,
796 list->value->len,
797 list->value->speed);
799 list = list->next;
802 qapi_free_BlockJobInfoList(list);
805 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
807 TPMInfoList *info_list, *info;
808 Error *err = NULL;
809 unsigned int c = 0;
810 TPMPassthroughOptions *tpo;
812 info_list = qmp_query_tpm(&err);
813 if (err) {
814 monitor_printf(mon, "TPM device not supported\n");
815 error_free(err);
816 return;
819 if (info_list) {
820 monitor_printf(mon, "TPM device:\n");
823 for (info = info_list; info; info = info->next) {
824 TPMInfo *ti = info->value;
825 monitor_printf(mon, " tpm%d: model=%s\n",
826 c, TpmModel_lookup[ti->model]);
828 monitor_printf(mon, " \\ %s: type=%s",
829 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
831 switch (ti->options->kind) {
832 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
833 tpo = ti->options->passthrough;
834 monitor_printf(mon, "%s%s%s%s",
835 tpo->has_path ? ",path=" : "",
836 tpo->has_path ? tpo->path : "",
837 tpo->has_cancel_path ? ",cancel-path=" : "",
838 tpo->has_cancel_path ? tpo->cancel_path : "");
839 break;
840 case TPM_TYPE_OPTIONS_KIND_MAX:
841 break;
843 monitor_printf(mon, "\n");
844 c++;
846 qapi_free_TPMInfoList(info_list);
849 void hmp_quit(Monitor *mon, const QDict *qdict)
851 monitor_suspend(mon);
852 qmp_quit(NULL);
855 void hmp_stop(Monitor *mon, const QDict *qdict)
857 qmp_stop(NULL);
860 void hmp_system_reset(Monitor *mon, const QDict *qdict)
862 qmp_system_reset(NULL);
865 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
867 qmp_system_powerdown(NULL);
870 void hmp_cpu(Monitor *mon, const QDict *qdict)
872 int64_t cpu_index;
874 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
875 use it are converted to the QAPI */
876 cpu_index = qdict_get_int(qdict, "index");
877 if (monitor_set_cpu(cpu_index) < 0) {
878 monitor_printf(mon, "invalid CPU index\n");
882 void hmp_memsave(Monitor *mon, const QDict *qdict)
884 uint32_t size = qdict_get_int(qdict, "size");
885 const char *filename = qdict_get_str(qdict, "filename");
886 uint64_t addr = qdict_get_int(qdict, "val");
887 Error *err = NULL;
889 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &err);
890 hmp_handle_error(mon, &err);
893 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
895 uint32_t size = qdict_get_int(qdict, "size");
896 const char *filename = qdict_get_str(qdict, "filename");
897 uint64_t addr = qdict_get_int(qdict, "val");
898 Error *err = NULL;
900 qmp_pmemsave(addr, size, filename, &err);
901 hmp_handle_error(mon, &err);
904 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
906 const char *chardev = qdict_get_str(qdict, "device");
907 const char *data = qdict_get_str(qdict, "data");
908 Error *err = NULL;
910 qmp_ringbuf_write(chardev, data, false, 0, &err);
912 hmp_handle_error(mon, &err);
915 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
917 uint32_t size = qdict_get_int(qdict, "size");
918 const char *chardev = qdict_get_str(qdict, "device");
919 char *data;
920 Error *err = NULL;
921 int i;
923 data = qmp_ringbuf_read(chardev, size, false, 0, &err);
924 if (err) {
925 monitor_printf(mon, "%s\n", error_get_pretty(err));
926 error_free(err);
927 return;
930 for (i = 0; data[i]; i++) {
931 unsigned char ch = data[i];
933 if (ch == '\\') {
934 monitor_printf(mon, "\\\\");
935 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
936 monitor_printf(mon, "\\u%04X", ch);
937 } else {
938 monitor_printf(mon, "%c", ch);
942 monitor_printf(mon, "\n");
943 g_free(data);
946 static void hmp_cont_cb(void *opaque, int err)
948 if (!err) {
949 qmp_cont(NULL);
953 static bool key_is_missing(const BlockInfo *bdev)
955 return (bdev->inserted && bdev->inserted->encryption_key_missing);
958 void hmp_cont(Monitor *mon, const QDict *qdict)
960 BlockInfoList *bdev_list, *bdev;
961 Error *err = NULL;
963 bdev_list = qmp_query_block(NULL);
964 for (bdev = bdev_list; bdev; bdev = bdev->next) {
965 if (key_is_missing(bdev->value)) {
966 monitor_read_block_device_key(mon, bdev->value->device,
967 hmp_cont_cb, NULL);
968 goto out;
972 qmp_cont(&err);
973 hmp_handle_error(mon, &err);
975 out:
976 qapi_free_BlockInfoList(bdev_list);
979 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
981 qmp_system_wakeup(NULL);
984 void hmp_nmi(Monitor *mon, const QDict *qdict)
986 Error *err = NULL;
988 qmp_inject_nmi(&err);
989 hmp_handle_error(mon, &err);
992 void hmp_set_link(Monitor *mon, const QDict *qdict)
994 const char *name = qdict_get_str(qdict, "name");
995 int up = qdict_get_bool(qdict, "up");
996 Error *err = NULL;
998 qmp_set_link(name, up, &err);
999 hmp_handle_error(mon, &err);
1002 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1004 const char *device = qdict_get_str(qdict, "device");
1005 const char *password = qdict_get_str(qdict, "password");
1006 Error *err = NULL;
1008 qmp_block_passwd(true, device, false, NULL, password, &err);
1009 hmp_handle_error(mon, &err);
1012 void hmp_balloon(Monitor *mon, const QDict *qdict)
1014 int64_t value = qdict_get_int(qdict, "value");
1015 Error *err = NULL;
1017 qmp_balloon(value, &err);
1018 if (err) {
1019 monitor_printf(mon, "balloon: %s\n", error_get_pretty(err));
1020 error_free(err);
1024 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1026 const char *device = qdict_get_str(qdict, "device");
1027 int64_t size = qdict_get_int(qdict, "size");
1028 Error *err = NULL;
1030 qmp_block_resize(true, device, false, NULL, size, &err);
1031 hmp_handle_error(mon, &err);
1034 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1036 const char *device = qdict_get_str(qdict, "device");
1037 const char *filename = qdict_get_str(qdict, "target");
1038 const char *format = qdict_get_try_str(qdict, "format");
1039 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
1040 int full = qdict_get_try_bool(qdict, "full", 0);
1041 enum NewImageMode mode;
1042 Error *err = NULL;
1044 if (!filename) {
1045 error_set(&err, QERR_MISSING_PARAMETER, "target");
1046 hmp_handle_error(mon, &err);
1047 return;
1050 if (reuse) {
1051 mode = NEW_IMAGE_MODE_EXISTING;
1052 } else {
1053 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1056 qmp_drive_mirror(device, filename, !!format, format,
1057 false, NULL, false, NULL,
1058 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1059 true, mode, false, 0, false, 0, false, 0,
1060 false, 0, false, 0, &err);
1061 hmp_handle_error(mon, &err);
1064 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1066 const char *device = qdict_get_str(qdict, "device");
1067 const char *filename = qdict_get_str(qdict, "target");
1068 const char *format = qdict_get_try_str(qdict, "format");
1069 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
1070 int full = qdict_get_try_bool(qdict, "full", 0);
1071 enum NewImageMode mode;
1072 Error *err = NULL;
1074 if (!filename) {
1075 error_set(&err, QERR_MISSING_PARAMETER, "target");
1076 hmp_handle_error(mon, &err);
1077 return;
1080 if (reuse) {
1081 mode = NEW_IMAGE_MODE_EXISTING;
1082 } else {
1083 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1086 qmp_drive_backup(device, filename, !!format, format,
1087 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1088 true, mode, false, 0, false, NULL,
1089 false, 0, false, 0, &err);
1090 hmp_handle_error(mon, &err);
1093 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1095 const char *device = qdict_get_str(qdict, "device");
1096 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1097 const char *format = qdict_get_try_str(qdict, "format");
1098 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
1099 enum NewImageMode mode;
1100 Error *err = NULL;
1102 if (!filename) {
1103 /* In the future, if 'snapshot-file' is not specified, the snapshot
1104 will be taken internally. Today it's actually required. */
1105 error_set(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1106 hmp_handle_error(mon, &err);
1107 return;
1110 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1111 qmp_blockdev_snapshot_sync(true, device, false, NULL,
1112 filename, false, NULL,
1113 !!format, format,
1114 true, mode, &err);
1115 hmp_handle_error(mon, &err);
1118 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1120 const char *device = qdict_get_str(qdict, "device");
1121 const char *name = qdict_get_str(qdict, "name");
1122 Error *err = NULL;
1124 qmp_blockdev_snapshot_internal_sync(device, name, &err);
1125 hmp_handle_error(mon, &err);
1128 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1130 const char *device = qdict_get_str(qdict, "device");
1131 const char *name = qdict_get_str(qdict, "name");
1132 const char *id = qdict_get_try_str(qdict, "id");
1133 Error *err = NULL;
1135 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1136 true, name, &err);
1137 hmp_handle_error(mon, &err);
1140 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1142 qmp_migrate_cancel(NULL);
1145 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1147 Error *err = NULL;
1148 const char *uri = qdict_get_str(qdict, "uri");
1150 qmp_migrate_incoming(uri, &err);
1152 hmp_handle_error(mon, &err);
1155 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1157 double value = qdict_get_double(qdict, "value");
1158 qmp_migrate_set_downtime(value, NULL);
1161 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1163 int64_t value = qdict_get_int(qdict, "value");
1164 Error *err = NULL;
1166 qmp_migrate_set_cache_size(value, &err);
1167 if (err) {
1168 monitor_printf(mon, "%s\n", error_get_pretty(err));
1169 error_free(err);
1170 return;
1174 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1176 int64_t value = qdict_get_int(qdict, "value");
1177 qmp_migrate_set_speed(value, NULL);
1180 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1182 const char *cap = qdict_get_str(qdict, "capability");
1183 bool state = qdict_get_bool(qdict, "state");
1184 Error *err = NULL;
1185 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1186 int i;
1188 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
1189 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1190 caps->value = g_malloc0(sizeof(*caps->value));
1191 caps->value->capability = i;
1192 caps->value->state = state;
1193 caps->next = NULL;
1194 qmp_migrate_set_capabilities(caps, &err);
1195 break;
1199 if (i == MIGRATION_CAPABILITY_MAX) {
1200 error_set(&err, QERR_INVALID_PARAMETER, cap);
1203 qapi_free_MigrationCapabilityStatusList(caps);
1205 if (err) {
1206 monitor_printf(mon, "migrate_set_capability: %s\n",
1207 error_get_pretty(err));
1208 error_free(err);
1212 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1214 const char *param = qdict_get_str(qdict, "parameter");
1215 int value = qdict_get_int(qdict, "value");
1216 Error *err = NULL;
1217 bool has_compress_level = false;
1218 bool has_compress_threads = false;
1219 bool has_decompress_threads = false;
1220 int i;
1222 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
1223 if (strcmp(param, MigrationParameter_lookup[i]) == 0) {
1224 switch (i) {
1225 case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1226 has_compress_level = true;
1227 break;
1228 case MIGRATION_PARAMETER_COMPRESS_THREADS:
1229 has_compress_threads = true;
1230 break;
1231 case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1232 has_decompress_threads = true;
1233 break;
1235 qmp_migrate_set_parameters(has_compress_level, value,
1236 has_compress_threads, value,
1237 has_decompress_threads, value,
1238 &err);
1239 break;
1243 if (i == MIGRATION_PARAMETER_MAX) {
1244 error_set(&err, QERR_INVALID_PARAMETER, param);
1247 if (err) {
1248 monitor_printf(mon, "migrate_set_parameter: %s\n",
1249 error_get_pretty(err));
1250 error_free(err);
1254 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1256 Error *err = NULL;
1257 const char *protocol = qdict_get_str(qdict, "protocol");
1258 const char *hostname = qdict_get_str(qdict, "hostname");
1259 bool has_port = qdict_haskey(qdict, "port");
1260 int port = qdict_get_try_int(qdict, "port", -1);
1261 bool has_tls_port = qdict_haskey(qdict, "tls-port");
1262 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1263 const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1265 qmp_client_migrate_info(protocol, hostname,
1266 has_port, port, has_tls_port, tls_port,
1267 !!cert_subject, cert_subject, &err);
1268 hmp_handle_error(mon, &err);
1271 void hmp_set_password(Monitor *mon, const QDict *qdict)
1273 const char *protocol = qdict_get_str(qdict, "protocol");
1274 const char *password = qdict_get_str(qdict, "password");
1275 const char *connected = qdict_get_try_str(qdict, "connected");
1276 Error *err = NULL;
1278 qmp_set_password(protocol, password, !!connected, connected, &err);
1279 hmp_handle_error(mon, &err);
1282 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1284 const char *protocol = qdict_get_str(qdict, "protocol");
1285 const char *whenstr = qdict_get_str(qdict, "time");
1286 Error *err = NULL;
1288 qmp_expire_password(protocol, whenstr, &err);
1289 hmp_handle_error(mon, &err);
1292 void hmp_eject(Monitor *mon, const QDict *qdict)
1294 int force = qdict_get_try_bool(qdict, "force", 0);
1295 const char *device = qdict_get_str(qdict, "device");
1296 Error *err = NULL;
1298 qmp_eject(device, true, force, &err);
1299 hmp_handle_error(mon, &err);
1302 static void hmp_change_read_arg(void *opaque, const char *password,
1303 void *readline_opaque)
1305 qmp_change_vnc_password(password, NULL);
1306 monitor_read_command(opaque, 1);
1309 void hmp_change(Monitor *mon, const QDict *qdict)
1311 const char *device = qdict_get_str(qdict, "device");
1312 const char *target = qdict_get_str(qdict, "target");
1313 const char *arg = qdict_get_try_str(qdict, "arg");
1314 Error *err = NULL;
1316 if (strcmp(device, "vnc") == 0 &&
1317 (strcmp(target, "passwd") == 0 ||
1318 strcmp(target, "password") == 0)) {
1319 if (!arg) {
1320 monitor_read_password(mon, hmp_change_read_arg, NULL);
1321 return;
1325 qmp_change(device, target, !!arg, arg, &err);
1326 if (err &&
1327 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1328 error_free(err);
1329 monitor_read_block_device_key(mon, device, NULL, NULL);
1330 return;
1332 hmp_handle_error(mon, &err);
1335 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1337 Error *err = NULL;
1339 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1340 qdict_get_int(qdict, "bps"),
1341 qdict_get_int(qdict, "bps_rd"),
1342 qdict_get_int(qdict, "bps_wr"),
1343 qdict_get_int(qdict, "iops"),
1344 qdict_get_int(qdict, "iops_rd"),
1345 qdict_get_int(qdict, "iops_wr"),
1346 false, /* no burst max via HMP */
1348 false,
1350 false,
1352 false,
1354 false,
1356 false,
1358 false, /* No default I/O size */
1359 0, &err);
1360 hmp_handle_error(mon, &err);
1363 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1365 Error *error = NULL;
1366 const char *device = qdict_get_str(qdict, "device");
1367 const char *base = qdict_get_try_str(qdict, "base");
1368 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1370 qmp_block_stream(device, base != NULL, base, false, NULL,
1371 qdict_haskey(qdict, "speed"), speed,
1372 true, BLOCKDEV_ON_ERROR_REPORT, &error);
1374 hmp_handle_error(mon, &error);
1377 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1379 Error *error = NULL;
1380 const char *device = qdict_get_str(qdict, "device");
1381 int64_t value = qdict_get_int(qdict, "speed");
1383 qmp_block_job_set_speed(device, value, &error);
1385 hmp_handle_error(mon, &error);
1388 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1390 Error *error = NULL;
1391 const char *device = qdict_get_str(qdict, "device");
1392 bool force = qdict_get_try_bool(qdict, "force", 0);
1394 qmp_block_job_cancel(device, true, force, &error);
1396 hmp_handle_error(mon, &error);
1399 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1401 Error *error = NULL;
1402 const char *device = qdict_get_str(qdict, "device");
1404 qmp_block_job_pause(device, &error);
1406 hmp_handle_error(mon, &error);
1409 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1411 Error *error = NULL;
1412 const char *device = qdict_get_str(qdict, "device");
1414 qmp_block_job_resume(device, &error);
1416 hmp_handle_error(mon, &error);
1419 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1421 Error *error = NULL;
1422 const char *device = qdict_get_str(qdict, "device");
1424 qmp_block_job_complete(device, &error);
1426 hmp_handle_error(mon, &error);
1429 typedef struct HMPMigrationStatus
1431 QEMUTimer *timer;
1432 Monitor *mon;
1433 bool is_block_migration;
1434 } HMPMigrationStatus;
1436 static void hmp_migrate_status_cb(void *opaque)
1438 HMPMigrationStatus *status = opaque;
1439 MigrationInfo *info;
1441 info = qmp_query_migrate(NULL);
1442 if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1443 info->status == MIGRATION_STATUS_SETUP) {
1444 if (info->has_disk) {
1445 int progress;
1447 if (info->disk->remaining) {
1448 progress = info->disk->transferred * 100 / info->disk->total;
1449 } else {
1450 progress = 100;
1453 monitor_printf(status->mon, "Completed %d %%\r", progress);
1454 monitor_flush(status->mon);
1457 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1458 } else {
1459 if (status->is_block_migration) {
1460 monitor_printf(status->mon, "\n");
1462 monitor_resume(status->mon);
1463 timer_del(status->timer);
1464 g_free(status);
1467 qapi_free_MigrationInfo(info);
1470 void hmp_migrate(Monitor *mon, const QDict *qdict)
1472 int detach = qdict_get_try_bool(qdict, "detach", 0);
1473 int blk = qdict_get_try_bool(qdict, "blk", 0);
1474 int inc = qdict_get_try_bool(qdict, "inc", 0);
1475 const char *uri = qdict_get_str(qdict, "uri");
1476 Error *err = NULL;
1478 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1479 if (err) {
1480 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1481 error_free(err);
1482 return;
1485 if (!detach) {
1486 HMPMigrationStatus *status;
1488 if (monitor_suspend(mon) < 0) {
1489 monitor_printf(mon, "terminal does not allow synchronous "
1490 "migration, continuing detached\n");
1491 return;
1494 status = g_malloc0(sizeof(*status));
1495 status->mon = mon;
1496 status->is_block_migration = blk || inc;
1497 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1498 status);
1499 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1503 void hmp_device_add(Monitor *mon, const QDict *qdict)
1505 do_device_add(mon, qdict, NULL);
1508 void hmp_device_del(Monitor *mon, const QDict *qdict)
1510 const char *id = qdict_get_str(qdict, "id");
1511 Error *err = NULL;
1513 qmp_device_del(id, &err);
1514 hmp_handle_error(mon, &err);
1517 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1519 Error *err = NULL;
1520 int paging = qdict_get_try_bool(qdict, "paging", 0);
1521 int zlib = qdict_get_try_bool(qdict, "zlib", 0);
1522 int lzo = qdict_get_try_bool(qdict, "lzo", 0);
1523 int snappy = qdict_get_try_bool(qdict, "snappy", 0);
1524 const char *file = qdict_get_str(qdict, "filename");
1525 bool has_begin = qdict_haskey(qdict, "begin");
1526 bool has_length = qdict_haskey(qdict, "length");
1527 int64_t begin = 0;
1528 int64_t length = 0;
1529 enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
1530 char *prot;
1532 if (zlib + lzo + snappy > 1) {
1533 error_setg(&err, "only one of '-z|-l|-s' can be set");
1534 hmp_handle_error(mon, &err);
1535 return;
1538 if (zlib) {
1539 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1542 if (lzo) {
1543 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1546 if (snappy) {
1547 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1550 if (has_begin) {
1551 begin = qdict_get_int(qdict, "begin");
1553 if (has_length) {
1554 length = qdict_get_int(qdict, "length");
1557 prot = g_strconcat("file:", file, NULL);
1559 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1560 true, dump_format, &err);
1561 hmp_handle_error(mon, &err);
1562 g_free(prot);
1565 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1567 Error *err = NULL;
1568 QemuOpts *opts;
1570 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1571 if (err) {
1572 goto out;
1575 netdev_add(opts, &err);
1576 if (err) {
1577 qemu_opts_del(opts);
1580 out:
1581 hmp_handle_error(mon, &err);
1584 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1586 const char *id = qdict_get_str(qdict, "id");
1587 Error *err = NULL;
1589 qmp_netdev_del(id, &err);
1590 hmp_handle_error(mon, &err);
1593 void hmp_object_add(Monitor *mon, const QDict *qdict)
1595 Error *err = NULL;
1596 Error *err_end = NULL;
1597 QemuOpts *opts;
1598 char *type = NULL;
1599 char *id = NULL;
1600 void *dummy = NULL;
1601 OptsVisitor *ov;
1602 QDict *pdict;
1604 opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1605 if (err) {
1606 goto out;
1609 ov = opts_visitor_new(opts);
1610 pdict = qdict_clone_shallow(qdict);
1612 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
1613 if (err) {
1614 goto out_clean;
1617 qdict_del(pdict, "qom-type");
1618 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
1619 if (err) {
1620 goto out_end;
1623 qdict_del(pdict, "id");
1624 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
1625 if (err) {
1626 goto out_end;
1629 object_add(type, id, pdict, opts_get_visitor(ov), &err);
1631 out_end:
1632 visit_end_struct(opts_get_visitor(ov), &err_end);
1633 if (!err && err_end) {
1634 qmp_object_del(id, NULL);
1636 error_propagate(&err, err_end);
1637 out_clean:
1638 opts_visitor_cleanup(ov);
1640 QDECREF(pdict);
1641 qemu_opts_del(opts);
1642 g_free(id);
1643 g_free(type);
1644 g_free(dummy);
1646 out:
1647 hmp_handle_error(mon, &err);
1650 void hmp_getfd(Monitor *mon, const QDict *qdict)
1652 const char *fdname = qdict_get_str(qdict, "fdname");
1653 Error *err = NULL;
1655 qmp_getfd(fdname, &err);
1656 hmp_handle_error(mon, &err);
1659 void hmp_closefd(Monitor *mon, const QDict *qdict)
1661 const char *fdname = qdict_get_str(qdict, "fdname");
1662 Error *err = NULL;
1664 qmp_closefd(fdname, &err);
1665 hmp_handle_error(mon, &err);
1668 void hmp_sendkey(Monitor *mon, const QDict *qdict)
1670 const char *keys = qdict_get_str(qdict, "keys");
1671 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1672 int has_hold_time = qdict_haskey(qdict, "hold-time");
1673 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1674 Error *err = NULL;
1675 char keyname_buf[16];
1676 char *separator;
1677 int keyname_len;
1679 while (1) {
1680 separator = strchr(keys, '-');
1681 keyname_len = separator ? separator - keys : strlen(keys);
1682 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1684 /* Be compatible with old interface, convert user inputted "<" */
1685 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1686 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1687 keyname_len = 4;
1689 keyname_buf[keyname_len] = 0;
1691 keylist = g_malloc0(sizeof(*keylist));
1692 keylist->value = g_malloc0(sizeof(*keylist->value));
1694 if (!head) {
1695 head = keylist;
1697 if (tmp) {
1698 tmp->next = keylist;
1700 tmp = keylist;
1702 if (strstart(keyname_buf, "0x", NULL)) {
1703 char *endp;
1704 int value = strtoul(keyname_buf, &endp, 0);
1705 if (*endp != '\0') {
1706 goto err_out;
1708 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1709 keylist->value->number = value;
1710 } else {
1711 int idx = index_from_key(keyname_buf);
1712 if (idx == Q_KEY_CODE_MAX) {
1713 goto err_out;
1715 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1716 keylist->value->qcode = idx;
1719 if (!separator) {
1720 break;
1722 keys = separator + 1;
1725 qmp_send_key(head, has_hold_time, hold_time, &err);
1726 hmp_handle_error(mon, &err);
1728 out:
1729 qapi_free_KeyValueList(head);
1730 return;
1732 err_out:
1733 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1734 goto out;
1737 void hmp_screendump(Monitor *mon, const QDict *qdict)
1739 const char *filename = qdict_get_str(qdict, "filename");
1740 Error *err = NULL;
1742 qmp_screendump(filename, &err);
1743 hmp_handle_error(mon, &err);
1746 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1748 const char *uri = qdict_get_str(qdict, "uri");
1749 int writable = qdict_get_try_bool(qdict, "writable", 0);
1750 int all = qdict_get_try_bool(qdict, "all", 0);
1751 Error *local_err = NULL;
1752 BlockInfoList *block_list, *info;
1753 SocketAddress *addr;
1755 if (writable && !all) {
1756 error_setg(&local_err, "-w only valid together with -a");
1757 goto exit;
1760 /* First check if the address is valid and start the server. */
1761 addr = socket_parse(uri, &local_err);
1762 if (local_err != NULL) {
1763 goto exit;
1766 qmp_nbd_server_start(addr, &local_err);
1767 qapi_free_SocketAddress(addr);
1768 if (local_err != NULL) {
1769 goto exit;
1772 if (!all) {
1773 return;
1776 /* Then try adding all block devices. If one fails, close all and
1777 * exit.
1779 block_list = qmp_query_block(NULL);
1781 for (info = block_list; info; info = info->next) {
1782 if (!info->value->has_inserted) {
1783 continue;
1786 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1788 if (local_err != NULL) {
1789 qmp_nbd_server_stop(NULL);
1790 break;
1794 qapi_free_BlockInfoList(block_list);
1796 exit:
1797 hmp_handle_error(mon, &local_err);
1800 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1802 const char *device = qdict_get_str(qdict, "device");
1803 int writable = qdict_get_try_bool(qdict, "writable", 0);
1804 Error *local_err = NULL;
1806 qmp_nbd_server_add(device, true, writable, &local_err);
1808 if (local_err != NULL) {
1809 hmp_handle_error(mon, &local_err);
1813 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1815 Error *err = NULL;
1817 qmp_nbd_server_stop(&err);
1818 hmp_handle_error(mon, &err);
1821 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
1823 int cpuid;
1824 Error *err = NULL;
1826 cpuid = qdict_get_int(qdict, "id");
1827 qmp_cpu_add(cpuid, &err);
1828 hmp_handle_error(mon, &err);
1831 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1833 const char *args = qdict_get_str(qdict, "args");
1834 Error *err = NULL;
1835 QemuOpts *opts;
1837 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1838 if (opts == NULL) {
1839 error_setg(&err, "Parsing chardev args failed");
1840 } else {
1841 qemu_chr_new_from_opts(opts, NULL, &err);
1843 hmp_handle_error(mon, &err);
1846 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1848 Error *local_err = NULL;
1850 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1851 hmp_handle_error(mon, &local_err);
1854 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1856 BlockBackend *blk;
1857 const char* device = qdict_get_str(qdict, "device");
1858 const char* command = qdict_get_str(qdict, "command");
1859 Error *err = NULL;
1861 blk = blk_by_name(device);
1862 if (blk) {
1863 qemuio_command(blk, command);
1864 } else {
1865 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1868 hmp_handle_error(mon, &err);
1871 void hmp_object_del(Monitor *mon, const QDict *qdict)
1873 const char *id = qdict_get_str(qdict, "id");
1874 Error *err = NULL;
1876 qmp_object_del(id, &err);
1877 hmp_handle_error(mon, &err);
1880 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
1882 Error *err = NULL;
1883 MemdevList *memdev_list = qmp_query_memdev(&err);
1884 MemdevList *m = memdev_list;
1885 StringOutputVisitor *ov;
1886 char *str;
1887 int i = 0;
1890 while (m) {
1891 ov = string_output_visitor_new(false);
1892 visit_type_uint16List(string_output_get_visitor(ov),
1893 &m->value->host_nodes, NULL, NULL);
1894 monitor_printf(mon, "memory backend: %d\n", i);
1895 monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
1896 monitor_printf(mon, " merge: %s\n",
1897 m->value->merge ? "true" : "false");
1898 monitor_printf(mon, " dump: %s\n",
1899 m->value->dump ? "true" : "false");
1900 monitor_printf(mon, " prealloc: %s\n",
1901 m->value->prealloc ? "true" : "false");
1902 monitor_printf(mon, " policy: %s\n",
1903 HostMemPolicy_lookup[m->value->policy]);
1904 str = string_output_get_string(ov);
1905 monitor_printf(mon, " host nodes: %s\n", str);
1907 g_free(str);
1908 string_output_visitor_cleanup(ov);
1909 m = m->next;
1910 i++;
1913 monitor_printf(mon, "\n");
1915 qapi_free_MemdevList(memdev_list);
1918 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
1920 Error *err = NULL;
1921 MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
1922 MemoryDeviceInfoList *info;
1923 MemoryDeviceInfo *value;
1924 PCDIMMDeviceInfo *di;
1926 for (info = info_list; info; info = info->next) {
1927 value = info->value;
1929 if (value) {
1930 switch (value->kind) {
1931 case MEMORY_DEVICE_INFO_KIND_DIMM:
1932 di = value->dimm;
1934 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
1935 MemoryDeviceInfoKind_lookup[value->kind],
1936 di->id ? di->id : "");
1937 monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
1938 monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
1939 monitor_printf(mon, " node: %" PRId64 "\n", di->node);
1940 monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
1941 monitor_printf(mon, " memdev: %s\n", di->memdev);
1942 monitor_printf(mon, " hotplugged: %s\n",
1943 di->hotplugged ? "true" : "false");
1944 monitor_printf(mon, " hotpluggable: %s\n",
1945 di->hotpluggable ? "true" : "false");
1946 break;
1947 default:
1948 break;
1953 qapi_free_MemoryDeviceInfoList(info_list);
1956 void hmp_qom_list(Monitor *mon, const QDict *qdict)
1958 const char *path = qdict_get_try_str(qdict, "path");
1959 ObjectPropertyInfoList *list;
1960 Error *err = NULL;
1962 if (path == NULL) {
1963 monitor_printf(mon, "/\n");
1964 return;
1967 list = qmp_qom_list(path, &err);
1968 if (err == NULL) {
1969 ObjectPropertyInfoList *start = list;
1970 while (list != NULL) {
1971 ObjectPropertyInfo *value = list->value;
1973 monitor_printf(mon, "%s (%s)\n",
1974 value->name, value->type);
1975 list = list->next;
1977 qapi_free_ObjectPropertyInfoList(start);
1979 hmp_handle_error(mon, &err);
1982 void hmp_qom_set(Monitor *mon, const QDict *qdict)
1984 const char *path = qdict_get_str(qdict, "path");
1985 const char *property = qdict_get_str(qdict, "property");
1986 const char *value = qdict_get_str(qdict, "value");
1987 Error *err = NULL;
1988 bool ambiguous = false;
1989 Object *obj;
1991 obj = object_resolve_path(path, &ambiguous);
1992 if (obj == NULL) {
1993 error_set(&err, QERR_DEVICE_NOT_FOUND, path);
1994 } else {
1995 if (ambiguous) {
1996 monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
1998 object_property_parse(obj, value, property, &err);
2000 hmp_handle_error(mon, &err);