2 * Human Monitor Interface
4 * Copyright IBM, Corp. 2011
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.
18 #include "sysemu/char.h"
19 #include "qemu/option.h"
20 #include "qemu/timer.h"
21 #include "qmp-commands.h"
22 #include "qemu/sockets.h"
23 #include "monitor/monitor.h"
24 #include "ui/console.h"
25 #include "block/qapi.h"
28 static void hmp_handle_error(Monitor *mon, Error **errp)
30 if (error_is_set(errp)) {
31 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
36 void hmp_info_name(Monitor *mon, const QDict *qdict)
40 info = qmp_query_name(NULL);
42 monitor_printf(mon, "%s\n", info->name);
44 qapi_free_NameInfo(info);
47 void hmp_info_version(Monitor *mon, const QDict *qdict)
51 info = qmp_query_version(NULL);
53 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
54 info->qemu.major, info->qemu.minor, info->qemu.micro,
57 qapi_free_VersionInfo(info);
60 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
64 info = qmp_query_kvm(NULL);
65 monitor_printf(mon, "kvm support: ");
67 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
69 monitor_printf(mon, "not compiled\n");
72 qapi_free_KvmInfo(info);
75 void hmp_info_status(Monitor *mon, const QDict *qdict)
79 info = qmp_query_status(NULL);
81 monitor_printf(mon, "VM status: %s%s",
82 info->running ? "running" : "paused",
83 info->singlestep ? " (single step mode)" : "");
85 if (!info->running && info->status != RUN_STATE_PAUSED) {
86 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
89 monitor_printf(mon, "\n");
91 qapi_free_StatusInfo(info);
94 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
98 info = qmp_query_uuid(NULL);
99 monitor_printf(mon, "%s\n", info->UUID);
100 qapi_free_UuidInfo(info);
103 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
105 ChardevInfoList *char_info, *info;
107 char_info = qmp_query_chardev(NULL);
108 for (info = char_info; info; info = info->next) {
109 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
110 info->value->filename);
113 qapi_free_ChardevInfoList(char_info);
116 void hmp_info_mice(Monitor *mon, const QDict *qdict)
118 MouseInfoList *mice_list, *mouse;
120 mice_list = qmp_query_mice(NULL);
122 monitor_printf(mon, "No mouse devices connected\n");
126 for (mouse = mice_list; mouse; mouse = mouse->next) {
127 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
128 mouse->value->current ? '*' : ' ',
129 mouse->value->index, mouse->value->name,
130 mouse->value->absolute ? " (absolute)" : "");
133 qapi_free_MouseInfoList(mice_list);
136 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
139 MigrationCapabilityStatusList *caps, *cap;
141 info = qmp_query_migrate(NULL);
142 caps = qmp_query_migrate_capabilities(NULL);
144 /* do not display parameters during setup */
145 if (info->has_status && caps) {
146 monitor_printf(mon, "capabilities: ");
147 for (cap = caps; cap; cap = cap->next) {
148 monitor_printf(mon, "%s: %s ",
149 MigrationCapability_lookup[cap->value->capability],
150 cap->value->state ? "on" : "off");
152 monitor_printf(mon, "\n");
155 if (info->has_status) {
156 monitor_printf(mon, "Migration status: %s\n", info->status);
157 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
159 if (info->has_expected_downtime) {
160 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
161 info->expected_downtime);
163 if (info->has_downtime) {
164 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
167 if (info->has_setup_time) {
168 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
174 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
175 info->ram->transferred >> 10);
176 monitor_printf(mon, "throughput: %0.2f mbps\n",
178 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
179 info->ram->remaining >> 10);
180 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
181 info->ram->total >> 10);
182 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
183 info->ram->duplicate);
184 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
186 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
188 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
189 info->ram->normal_bytes >> 10);
190 if (info->ram->dirty_pages_rate) {
191 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
192 info->ram->dirty_pages_rate);
196 if (info->has_disk) {
197 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
198 info->disk->transferred >> 10);
199 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
200 info->disk->remaining >> 10);
201 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
202 info->disk->total >> 10);
205 if (info->has_xbzrle_cache) {
206 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
207 info->xbzrle_cache->cache_size);
208 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
209 info->xbzrle_cache->bytes >> 10);
210 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
211 info->xbzrle_cache->pages);
212 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
213 info->xbzrle_cache->cache_miss);
214 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
215 info->xbzrle_cache->overflow);
218 qapi_free_MigrationInfo(info);
219 qapi_free_MigrationCapabilityStatusList(caps);
222 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
224 MigrationCapabilityStatusList *caps, *cap;
226 caps = qmp_query_migrate_capabilities(NULL);
229 monitor_printf(mon, "capabilities: ");
230 for (cap = caps; cap; cap = cap->next) {
231 monitor_printf(mon, "%s: %s ",
232 MigrationCapability_lookup[cap->value->capability],
233 cap->value->state ? "on" : "off");
235 monitor_printf(mon, "\n");
238 qapi_free_MigrationCapabilityStatusList(caps);
241 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
243 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
244 qmp_query_migrate_cache_size(NULL) >> 10);
247 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
249 CpuInfoList *cpu_list, *cpu;
251 cpu_list = qmp_query_cpus(NULL);
253 for (cpu = cpu_list; cpu; cpu = cpu->next) {
256 if (cpu->value->CPU == monitor_get_cpu_index()) {
260 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
262 if (cpu->value->has_pc) {
263 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
265 if (cpu->value->has_nip) {
266 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
268 if (cpu->value->has_npc) {
269 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
271 if (cpu->value->has_PC) {
272 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
275 if (cpu->value->halted) {
276 monitor_printf(mon, " (halted)");
279 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
282 qapi_free_CpuInfoList(cpu_list);
285 void hmp_info_block(Monitor *mon, const QDict *qdict)
287 BlockInfoList *block_list, *info;
288 ImageInfo *image_info;
289 const char *device = qdict_get_try_str(qdict, "device");
290 bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
292 block_list = qmp_query_block(NULL);
294 for (info = block_list; info; info = info->next) {
295 if (device && strcmp(device, info->value->device)) {
299 if (info != block_list) {
300 monitor_printf(mon, "\n");
303 monitor_printf(mon, "%s", info->value->device);
304 if (info->value->has_inserted) {
305 monitor_printf(mon, ": %s (%s%s%s)\n",
306 info->value->inserted->file,
307 info->value->inserted->drv,
308 info->value->inserted->ro ? ", read-only" : "",
309 info->value->inserted->encrypted ? ", encrypted" : "");
311 monitor_printf(mon, ": [not inserted]\n");
314 if (info->value->has_io_status && info->value->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
315 monitor_printf(mon, " I/O status: %s\n",
316 BlockDeviceIoStatus_lookup[info->value->io_status]);
319 if (info->value->removable) {
320 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
321 info->value->locked ? "" : "not ",
322 info->value->tray_open ? "open" : "closed");
326 if (!info->value->has_inserted) {
330 if (info->value->inserted->has_backing_file) {
333 "(chain depth: %" PRId64 ")\n",
334 info->value->inserted->backing_file,
335 info->value->inserted->backing_file_depth);
338 if (info->value->inserted->bps
339 || info->value->inserted->bps_rd
340 || info->value->inserted->bps_wr
341 || info->value->inserted->iops
342 || info->value->inserted->iops_rd
343 || info->value->inserted->iops_wr)
345 monitor_printf(mon, " I/O throttling: bps=%" PRId64
346 " bps_rd=%" PRId64 " bps_wr=%" PRId64
347 " iops=%" PRId64 " iops_rd=%" PRId64
348 " iops_wr=%" PRId64 "\n",
349 info->value->inserted->bps,
350 info->value->inserted->bps_rd,
351 info->value->inserted->bps_wr,
352 info->value->inserted->iops,
353 info->value->inserted->iops_rd,
354 info->value->inserted->iops_wr);
358 monitor_printf(mon, "\nImages:\n");
359 image_info = info->value->inserted->image;
361 bdrv_image_info_dump((fprintf_function)monitor_printf,
363 if (image_info->has_backing_image) {
364 image_info = image_info->backing_image;
372 qapi_free_BlockInfoList(block_list);
375 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
377 BlockStatsList *stats_list, *stats;
379 stats_list = qmp_query_blockstats(NULL);
381 for (stats = stats_list; stats; stats = stats->next) {
382 if (!stats->value->has_device) {
386 monitor_printf(mon, "%s:", stats->value->device);
387 monitor_printf(mon, " rd_bytes=%" PRId64
389 " rd_operations=%" PRId64
390 " wr_operations=%" PRId64
391 " flush_operations=%" PRId64
392 " wr_total_time_ns=%" PRId64
393 " rd_total_time_ns=%" PRId64
394 " flush_total_time_ns=%" PRId64
396 stats->value->stats->rd_bytes,
397 stats->value->stats->wr_bytes,
398 stats->value->stats->rd_operations,
399 stats->value->stats->wr_operations,
400 stats->value->stats->flush_operations,
401 stats->value->stats->wr_total_time_ns,
402 stats->value->stats->rd_total_time_ns,
403 stats->value->stats->flush_total_time_ns);
406 qapi_free_BlockStatsList(stats_list);
409 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
413 VncClientInfoList *client;
415 info = qmp_query_vnc(&err);
417 monitor_printf(mon, "%s\n", error_get_pretty(err));
422 if (!info->enabled) {
423 monitor_printf(mon, "Server: disabled\n");
427 monitor_printf(mon, "Server:\n");
428 if (info->has_host && info->has_service) {
429 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
431 if (info->has_auth) {
432 monitor_printf(mon, " auth: %s\n", info->auth);
435 if (!info->has_clients || info->clients == NULL) {
436 monitor_printf(mon, "Client: none\n");
438 for (client = info->clients; client; client = client->next) {
439 monitor_printf(mon, "Client:\n");
440 monitor_printf(mon, " address: %s:%s\n",
441 client->value->host, client->value->service);
442 monitor_printf(mon, " x509_dname: %s\n",
443 client->value->x509_dname ?
444 client->value->x509_dname : "none");
445 monitor_printf(mon, " username: %s\n",
446 client->value->has_sasl_username ?
447 client->value->sasl_username : "none");
452 qapi_free_VncInfo(info);
455 void hmp_info_spice(Monitor *mon, const QDict *qdict)
457 SpiceChannelList *chan;
460 info = qmp_query_spice(NULL);
462 if (!info->enabled) {
463 monitor_printf(mon, "Server: disabled\n");
467 monitor_printf(mon, "Server:\n");
468 if (info->has_port) {
469 monitor_printf(mon, " address: %s:%" PRId64 "\n",
470 info->host, info->port);
472 if (info->has_tls_port) {
473 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
474 info->host, info->tls_port);
476 monitor_printf(mon, " migrated: %s\n",
477 info->migrated ? "true" : "false");
478 monitor_printf(mon, " auth: %s\n", info->auth);
479 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
480 monitor_printf(mon, " mouse-mode: %s\n",
481 SpiceQueryMouseMode_lookup[info->mouse_mode]);
483 if (!info->has_channels || info->channels == NULL) {
484 monitor_printf(mon, "Channels: none\n");
486 for (chan = info->channels; chan; chan = chan->next) {
487 monitor_printf(mon, "Channel:\n");
488 monitor_printf(mon, " address: %s:%s%s\n",
489 chan->value->host, chan->value->port,
490 chan->value->tls ? " [tls]" : "");
491 monitor_printf(mon, " session: %" PRId64 "\n",
492 chan->value->connection_id);
493 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
494 chan->value->channel_type, chan->value->channel_id);
499 qapi_free_SpiceInfo(info);
502 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
507 info = qmp_query_balloon(&err);
509 monitor_printf(mon, "%s\n", error_get_pretty(err));
514 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
516 qapi_free_BalloonInfo(info);
519 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
521 PciMemoryRegionList *region;
523 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
524 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
525 dev->slot, dev->function);
526 monitor_printf(mon, " ");
528 if (dev->class_info.has_desc) {
529 monitor_printf(mon, "%s", dev->class_info.desc);
531 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
534 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
535 dev->id.vendor, dev->id.device);
538 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
541 if (dev->has_pci_bridge) {
542 monitor_printf(mon, " BUS %" PRId64 ".\n",
543 dev->pci_bridge->bus.number);
544 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
545 dev->pci_bridge->bus.secondary);
546 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
547 dev->pci_bridge->bus.subordinate);
549 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
550 dev->pci_bridge->bus.io_range->base,
551 dev->pci_bridge->bus.io_range->limit);
554 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
555 dev->pci_bridge->bus.memory_range->base,
556 dev->pci_bridge->bus.memory_range->limit);
558 monitor_printf(mon, " prefetchable memory range "
559 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
560 dev->pci_bridge->bus.prefetchable_range->base,
561 dev->pci_bridge->bus.prefetchable_range->limit);
564 for (region = dev->regions; region; region = region->next) {
567 addr = region->value->address;
568 size = region->value->size;
570 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
572 if (!strcmp(region->value->type, "io")) {
573 monitor_printf(mon, "I/O at 0x%04" PRIx64
574 " [0x%04" PRIx64 "].\n",
575 addr, addr + size - 1);
577 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
578 " [0x%08" PRIx64 "].\n",
579 region->value->mem_type_64 ? 64 : 32,
580 region->value->prefetch ? " prefetchable" : "",
581 addr, addr + size - 1);
585 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
587 if (dev->has_pci_bridge) {
588 if (dev->pci_bridge->has_devices) {
589 PciDeviceInfoList *cdev;
590 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
591 hmp_info_pci_device(mon, cdev->value);
597 void hmp_info_pci(Monitor *mon, const QDict *qdict)
599 PciInfoList *info_list, *info;
602 info_list = qmp_query_pci(&err);
604 monitor_printf(mon, "PCI devices not supported\n");
609 for (info = info_list; info; info = info->next) {
610 PciDeviceInfoList *dev;
612 for (dev = info->value->devices; dev; dev = dev->next) {
613 hmp_info_pci_device(mon, dev->value);
617 qapi_free_PciInfoList(info_list);
620 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
622 BlockJobInfoList *list;
625 list = qmp_query_block_jobs(&err);
629 monitor_printf(mon, "No active jobs\n");
634 if (strcmp(list->value->type, "stream") == 0) {
635 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
636 " of %" PRId64 " bytes, speed limit %" PRId64
643 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
644 " of %" PRId64 " bytes, speed limit %" PRId64
656 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
658 TPMInfoList *info_list, *info;
661 TPMPassthroughOptions *tpo;
663 info_list = qmp_query_tpm(&err);
665 monitor_printf(mon, "TPM device not supported\n");
671 monitor_printf(mon, "TPM device:\n");
674 for (info = info_list; info; info = info->next) {
675 TPMInfo *ti = info->value;
676 monitor_printf(mon, " tpm%d: model=%s\n",
677 c, TpmModel_lookup[ti->model]);
679 monitor_printf(mon, " \\ %s: type=%s",
680 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
682 switch (ti->options->kind) {
683 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
684 tpo = ti->options->passthrough;
685 monitor_printf(mon, "%s%s%s%s",
686 tpo->has_path ? ",path=" : "",
687 tpo->has_path ? tpo->path : "",
688 tpo->has_cancel_path ? ",cancel-path=" : "",
689 tpo->has_cancel_path ? tpo->cancel_path : "");
691 case TPM_TYPE_OPTIONS_KIND_MAX:
694 monitor_printf(mon, "\n");
697 qapi_free_TPMInfoList(info_list);
700 void hmp_quit(Monitor *mon, const QDict *qdict)
702 monitor_suspend(mon);
706 void hmp_stop(Monitor *mon, const QDict *qdict)
711 void hmp_system_reset(Monitor *mon, const QDict *qdict)
713 qmp_system_reset(NULL);
716 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
718 qmp_system_powerdown(NULL);
721 void hmp_cpu(Monitor *mon, const QDict *qdict)
725 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
726 use it are converted to the QAPI */
727 cpu_index = qdict_get_int(qdict, "index");
728 if (monitor_set_cpu(cpu_index) < 0) {
729 monitor_printf(mon, "invalid CPU index\n");
733 void hmp_memsave(Monitor *mon, const QDict *qdict)
735 uint32_t size = qdict_get_int(qdict, "size");
736 const char *filename = qdict_get_str(qdict, "filename");
737 uint64_t addr = qdict_get_int(qdict, "val");
740 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
741 hmp_handle_error(mon, &errp);
744 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
746 uint32_t size = qdict_get_int(qdict, "size");
747 const char *filename = qdict_get_str(qdict, "filename");
748 uint64_t addr = qdict_get_int(qdict, "val");
751 qmp_pmemsave(addr, size, filename, &errp);
752 hmp_handle_error(mon, &errp);
755 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
757 const char *chardev = qdict_get_str(qdict, "device");
758 const char *data = qdict_get_str(qdict, "data");
761 qmp_ringbuf_write(chardev, data, false, 0, &errp);
763 hmp_handle_error(mon, &errp);
766 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
768 uint32_t size = qdict_get_int(qdict, "size");
769 const char *chardev = qdict_get_str(qdict, "device");
774 data = qmp_ringbuf_read(chardev, size, false, 0, &errp);
776 monitor_printf(mon, "%s\n", error_get_pretty(errp));
781 for (i = 0; data[i]; i++) {
782 unsigned char ch = data[i];
785 monitor_printf(mon, "\\\\");
786 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
787 monitor_printf(mon, "\\u%04X", ch);
789 monitor_printf(mon, "%c", ch);
793 monitor_printf(mon, "\n");
797 static void hmp_cont_cb(void *opaque, int err)
804 static bool key_is_missing(const BlockInfo *bdev)
806 return (bdev->inserted && bdev->inserted->encryption_key_missing);
809 void hmp_cont(Monitor *mon, const QDict *qdict)
811 BlockInfoList *bdev_list, *bdev;
814 bdev_list = qmp_query_block(NULL);
815 for (bdev = bdev_list; bdev; bdev = bdev->next) {
816 if (key_is_missing(bdev->value)) {
817 monitor_read_block_device_key(mon, bdev->value->device,
824 hmp_handle_error(mon, &errp);
827 qapi_free_BlockInfoList(bdev_list);
830 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
832 qmp_system_wakeup(NULL);
835 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
839 qmp_inject_nmi(&errp);
840 hmp_handle_error(mon, &errp);
843 void hmp_set_link(Monitor *mon, const QDict *qdict)
845 const char *name = qdict_get_str(qdict, "name");
846 int up = qdict_get_bool(qdict, "up");
849 qmp_set_link(name, up, &errp);
850 hmp_handle_error(mon, &errp);
853 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
855 const char *device = qdict_get_str(qdict, "device");
856 const char *password = qdict_get_str(qdict, "password");
859 qmp_block_passwd(device, password, &errp);
860 hmp_handle_error(mon, &errp);
863 void hmp_balloon(Monitor *mon, const QDict *qdict)
865 int64_t value = qdict_get_int(qdict, "value");
868 qmp_balloon(value, &errp);
869 if (error_is_set(&errp)) {
870 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
875 void hmp_block_resize(Monitor *mon, const QDict *qdict)
877 const char *device = qdict_get_str(qdict, "device");
878 int64_t size = qdict_get_int(qdict, "size");
881 qmp_block_resize(device, size, &errp);
882 hmp_handle_error(mon, &errp);
885 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
887 const char *device = qdict_get_str(qdict, "device");
888 const char *filename = qdict_get_str(qdict, "target");
889 const char *format = qdict_get_try_str(qdict, "format");
890 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
891 int full = qdict_get_try_bool(qdict, "full", 0);
892 enum NewImageMode mode;
896 error_set(&errp, QERR_MISSING_PARAMETER, "target");
897 hmp_handle_error(mon, &errp);
902 mode = NEW_IMAGE_MODE_EXISTING;
904 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
907 qmp_drive_mirror(device, filename, !!format, format,
908 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
909 true, mode, false, 0, false, 0, false, 0,
910 false, 0, false, 0, &errp);
911 hmp_handle_error(mon, &errp);
914 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
916 const char *device = qdict_get_str(qdict, "device");
917 const char *filename = qdict_get_str(qdict, "target");
918 const char *format = qdict_get_try_str(qdict, "format");
919 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
920 int full = qdict_get_try_bool(qdict, "full", 0);
921 enum NewImageMode mode;
925 error_set(&errp, QERR_MISSING_PARAMETER, "target");
926 hmp_handle_error(mon, &errp);
931 mode = NEW_IMAGE_MODE_EXISTING;
933 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
936 qmp_drive_backup(device, filename, !!format, format,
937 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
938 true, mode, false, 0, false, 0, false, 0, &errp);
939 hmp_handle_error(mon, &errp);
942 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
944 const char *device = qdict_get_str(qdict, "device");
945 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
946 const char *format = qdict_get_try_str(qdict, "format");
947 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
948 enum NewImageMode mode;
952 /* In the future, if 'snapshot-file' is not specified, the snapshot
953 will be taken internally. Today it's actually required. */
954 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
955 hmp_handle_error(mon, &errp);
959 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
960 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
962 hmp_handle_error(mon, &errp);
965 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
967 qmp_migrate_cancel(NULL);
970 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
972 double value = qdict_get_double(qdict, "value");
973 qmp_migrate_set_downtime(value, NULL);
976 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
978 int64_t value = qdict_get_int(qdict, "value");
981 qmp_migrate_set_cache_size(value, &err);
983 monitor_printf(mon, "%s\n", error_get_pretty(err));
989 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
991 int64_t value = qdict_get_int(qdict, "value");
992 qmp_migrate_set_speed(value, NULL);
995 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
997 const char *cap = qdict_get_str(qdict, "capability");
998 bool state = qdict_get_bool(qdict, "state");
1000 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1003 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
1004 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1005 caps->value = g_malloc0(sizeof(*caps->value));
1006 caps->value->capability = i;
1007 caps->value->state = state;
1009 qmp_migrate_set_capabilities(caps, &err);
1014 if (i == MIGRATION_CAPABILITY_MAX) {
1015 error_set(&err, QERR_INVALID_PARAMETER, cap);
1018 qapi_free_MigrationCapabilityStatusList(caps);
1021 monitor_printf(mon, "migrate_set_capability: %s\n",
1022 error_get_pretty(err));
1027 void hmp_set_password(Monitor *mon, const QDict *qdict)
1029 const char *protocol = qdict_get_str(qdict, "protocol");
1030 const char *password = qdict_get_str(qdict, "password");
1031 const char *connected = qdict_get_try_str(qdict, "connected");
1034 qmp_set_password(protocol, password, !!connected, connected, &err);
1035 hmp_handle_error(mon, &err);
1038 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1040 const char *protocol = qdict_get_str(qdict, "protocol");
1041 const char *whenstr = qdict_get_str(qdict, "time");
1044 qmp_expire_password(protocol, whenstr, &err);
1045 hmp_handle_error(mon, &err);
1048 void hmp_eject(Monitor *mon, const QDict *qdict)
1050 int force = qdict_get_try_bool(qdict, "force", 0);
1051 const char *device = qdict_get_str(qdict, "device");
1054 qmp_eject(device, true, force, &err);
1055 hmp_handle_error(mon, &err);
1058 static void hmp_change_read_arg(Monitor *mon, const char *password,
1061 qmp_change_vnc_password(password, NULL);
1062 monitor_read_command(mon, 1);
1065 void hmp_change(Monitor *mon, const QDict *qdict)
1067 const char *device = qdict_get_str(qdict, "device");
1068 const char *target = qdict_get_str(qdict, "target");
1069 const char *arg = qdict_get_try_str(qdict, "arg");
1072 if (strcmp(device, "vnc") == 0 &&
1073 (strcmp(target, "passwd") == 0 ||
1074 strcmp(target, "password") == 0)) {
1076 monitor_read_password(mon, hmp_change_read_arg, NULL);
1081 qmp_change(device, target, !!arg, arg, &err);
1082 if (error_is_set(&err) &&
1083 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1085 monitor_read_block_device_key(mon, device, NULL, NULL);
1088 hmp_handle_error(mon, &err);
1091 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1095 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1096 qdict_get_int(qdict, "bps"),
1097 qdict_get_int(qdict, "bps_rd"),
1098 qdict_get_int(qdict, "bps_wr"),
1099 qdict_get_int(qdict, "iops"),
1100 qdict_get_int(qdict, "iops_rd"),
1101 qdict_get_int(qdict, "iops_wr"), &err);
1102 hmp_handle_error(mon, &err);
1105 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1107 Error *error = NULL;
1108 const char *device = qdict_get_str(qdict, "device");
1109 const char *base = qdict_get_try_str(qdict, "base");
1110 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1112 qmp_block_stream(device, base != NULL, base,
1113 qdict_haskey(qdict, "speed"), speed,
1114 BLOCKDEV_ON_ERROR_REPORT, true, &error);
1116 hmp_handle_error(mon, &error);
1119 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1121 Error *error = NULL;
1122 const char *device = qdict_get_str(qdict, "device");
1123 int64_t value = qdict_get_int(qdict, "speed");
1125 qmp_block_job_set_speed(device, value, &error);
1127 hmp_handle_error(mon, &error);
1130 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1132 Error *error = NULL;
1133 const char *device = qdict_get_str(qdict, "device");
1134 bool force = qdict_get_try_bool(qdict, "force", 0);
1136 qmp_block_job_cancel(device, true, force, &error);
1138 hmp_handle_error(mon, &error);
1141 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1143 Error *error = NULL;
1144 const char *device = qdict_get_str(qdict, "device");
1146 qmp_block_job_pause(device, &error);
1148 hmp_handle_error(mon, &error);
1151 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1153 Error *error = NULL;
1154 const char *device = qdict_get_str(qdict, "device");
1156 qmp_block_job_resume(device, &error);
1158 hmp_handle_error(mon, &error);
1161 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1163 Error *error = NULL;
1164 const char *device = qdict_get_str(qdict, "device");
1166 qmp_block_job_complete(device, &error);
1168 hmp_handle_error(mon, &error);
1171 typedef struct MigrationStatus
1175 bool is_block_migration;
1178 static void hmp_migrate_status_cb(void *opaque)
1180 MigrationStatus *status = opaque;
1181 MigrationInfo *info;
1183 info = qmp_query_migrate(NULL);
1184 if (!info->has_status || strcmp(info->status, "active") == 0) {
1185 if (info->has_disk) {
1188 if (info->disk->remaining) {
1189 progress = info->disk->transferred * 100 / info->disk->total;
1194 monitor_printf(status->mon, "Completed %d %%\r", progress);
1195 monitor_flush(status->mon);
1198 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1200 if (status->is_block_migration) {
1201 monitor_printf(status->mon, "\n");
1203 monitor_resume(status->mon);
1204 qemu_del_timer(status->timer);
1208 qapi_free_MigrationInfo(info);
1211 void hmp_migrate(Monitor *mon, const QDict *qdict)
1213 int detach = qdict_get_try_bool(qdict, "detach", 0);
1214 int blk = qdict_get_try_bool(qdict, "blk", 0);
1215 int inc = qdict_get_try_bool(qdict, "inc", 0);
1216 const char *uri = qdict_get_str(qdict, "uri");
1219 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1221 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1227 MigrationStatus *status;
1229 if (monitor_suspend(mon) < 0) {
1230 monitor_printf(mon, "terminal does not allow synchronous "
1231 "migration, continuing detached\n");
1235 status = g_malloc0(sizeof(*status));
1237 status->is_block_migration = blk || inc;
1238 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1240 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1244 void hmp_device_del(Monitor *mon, const QDict *qdict)
1246 const char *id = qdict_get_str(qdict, "id");
1249 qmp_device_del(id, &err);
1250 hmp_handle_error(mon, &err);
1253 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1256 int paging = qdict_get_try_bool(qdict, "paging", 0);
1257 const char *file = qdict_get_str(qdict, "filename");
1258 bool has_begin = qdict_haskey(qdict, "begin");
1259 bool has_length = qdict_haskey(qdict, "length");
1265 begin = qdict_get_int(qdict, "begin");
1268 length = qdict_get_int(qdict, "length");
1271 prot = g_strconcat("file:", file, NULL);
1273 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1275 hmp_handle_error(mon, &errp);
1279 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1284 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1285 if (error_is_set(&err)) {
1289 netdev_add(opts, &err);
1290 if (error_is_set(&err)) {
1291 qemu_opts_del(opts);
1295 hmp_handle_error(mon, &err);
1298 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1300 const char *id = qdict_get_str(qdict, "id");
1303 qmp_netdev_del(id, &err);
1304 hmp_handle_error(mon, &err);
1307 void hmp_getfd(Monitor *mon, const QDict *qdict)
1309 const char *fdname = qdict_get_str(qdict, "fdname");
1312 qmp_getfd(fdname, &errp);
1313 hmp_handle_error(mon, &errp);
1316 void hmp_closefd(Monitor *mon, const QDict *qdict)
1318 const char *fdname = qdict_get_str(qdict, "fdname");
1321 qmp_closefd(fdname, &errp);
1322 hmp_handle_error(mon, &errp);
1325 void hmp_send_key(Monitor *mon, const QDict *qdict)
1327 const char *keys = qdict_get_str(qdict, "keys");
1328 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1329 int has_hold_time = qdict_haskey(qdict, "hold-time");
1330 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1332 char keyname_buf[16];
1337 separator = strchr(keys, '-');
1338 keyname_len = separator ? separator - keys : strlen(keys);
1339 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1341 /* Be compatible with old interface, convert user inputted "<" */
1342 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1343 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1346 keyname_buf[keyname_len] = 0;
1348 keylist = g_malloc0(sizeof(*keylist));
1349 keylist->value = g_malloc0(sizeof(*keylist->value));
1355 tmp->next = keylist;
1359 if (strstart(keyname_buf, "0x", NULL)) {
1361 int value = strtoul(keyname_buf, &endp, 0);
1362 if (*endp != '\0') {
1365 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1366 keylist->value->number = value;
1368 int idx = index_from_key(keyname_buf);
1369 if (idx == Q_KEY_CODE_MAX) {
1372 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1373 keylist->value->qcode = idx;
1379 keys = separator + 1;
1382 qmp_send_key(head, has_hold_time, hold_time, &err);
1383 hmp_handle_error(mon, &err);
1386 qapi_free_KeyValueList(head);
1390 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1394 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1396 const char *filename = qdict_get_str(qdict, "filename");
1399 qmp_screendump(filename, &err);
1400 hmp_handle_error(mon, &err);
1403 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1405 const char *uri = qdict_get_str(qdict, "uri");
1406 int writable = qdict_get_try_bool(qdict, "writable", 0);
1407 int all = qdict_get_try_bool(qdict, "all", 0);
1408 Error *local_err = NULL;
1409 BlockInfoList *block_list, *info;
1410 SocketAddress *addr;
1412 if (writable && !all) {
1413 error_setg(&local_err, "-w only valid together with -a");
1417 /* First check if the address is valid and start the server. */
1418 addr = socket_parse(uri, &local_err);
1419 if (local_err != NULL) {
1423 qmp_nbd_server_start(addr, &local_err);
1424 qapi_free_SocketAddress(addr);
1425 if (local_err != NULL) {
1433 /* Then try adding all block devices. If one fails, close all and
1436 block_list = qmp_query_block(NULL);
1438 for (info = block_list; info; info = info->next) {
1439 if (!info->value->has_inserted) {
1443 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1445 if (local_err != NULL) {
1446 qmp_nbd_server_stop(NULL);
1451 qapi_free_BlockInfoList(block_list);
1454 hmp_handle_error(mon, &local_err);
1457 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1459 const char *device = qdict_get_str(qdict, "device");
1460 int writable = qdict_get_try_bool(qdict, "writable", 0);
1461 Error *local_err = NULL;
1463 qmp_nbd_server_add(device, true, writable, &local_err);
1465 if (local_err != NULL) {
1466 hmp_handle_error(mon, &local_err);
1470 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1474 qmp_nbd_server_stop(&errp);
1475 hmp_handle_error(mon, &errp);
1478 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1480 const char *args = qdict_get_str(qdict, "args");
1484 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1486 error_setg(&err, "Parsing chardev args failed");
1488 qemu_chr_new_from_opts(opts, NULL, &err);
1490 hmp_handle_error(mon, &err);
1493 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1495 Error *local_err = NULL;
1497 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1498 hmp_handle_error(mon, &local_err);
1501 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1503 BlockDriverState *bs;
1504 const char* device = qdict_get_str(qdict, "device");
1505 const char* command = qdict_get_str(qdict, "command");
1508 bs = bdrv_find(device);
1510 qemuio_command(bs, command);
1512 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1515 hmp_handle_error(mon, &err);