Merge commit 'afb63ebd0a9599312c27ecceb839a399740e00ef' into upstream-merge
[qemu-kvm.git] / hmp.c
blob70bdec24330e310e6984a3a72b75fbe0c523846e
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.h"
18 #include "qemu-option.h"
19 #include "qemu-timer.h"
20 #include "qmp-commands.h"
21 #include "monitor.h"
22 #include "console.h"
24 static void hmp_handle_error(Monitor *mon, Error **errp)
26 if (error_is_set(errp)) {
27 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
28 error_free(*errp);
32 void hmp_info_name(Monitor *mon)
34 NameInfo *info;
36 info = qmp_query_name(NULL);
37 if (info->has_name) {
38 monitor_printf(mon, "%s\n", info->name);
40 qapi_free_NameInfo(info);
43 void hmp_info_version(Monitor *mon)
45 VersionInfo *info;
47 info = qmp_query_version(NULL);
49 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
50 info->qemu.major, info->qemu.minor, info->qemu.micro,
51 info->package);
53 qapi_free_VersionInfo(info);
56 void hmp_info_kvm(Monitor *mon)
58 KvmInfo *info;
60 info = qmp_query_kvm(NULL);
61 monitor_printf(mon, "kvm support: ");
62 if (info->present) {
63 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
64 } else {
65 monitor_printf(mon, "not compiled\n");
68 qapi_free_KvmInfo(info);
71 void hmp_info_status(Monitor *mon)
73 StatusInfo *info;
75 info = qmp_query_status(NULL);
77 monitor_printf(mon, "VM status: %s%s",
78 info->running ? "running" : "paused",
79 info->singlestep ? " (single step mode)" : "");
81 if (!info->running && info->status != RUN_STATE_PAUSED) {
82 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
85 monitor_printf(mon, "\n");
87 qapi_free_StatusInfo(info);
90 void hmp_info_uuid(Monitor *mon)
92 UuidInfo *info;
94 info = qmp_query_uuid(NULL);
95 monitor_printf(mon, "%s\n", info->UUID);
96 qapi_free_UuidInfo(info);
99 void hmp_info_chardev(Monitor *mon)
101 ChardevInfoList *char_info, *info;
103 char_info = qmp_query_chardev(NULL);
104 for (info = char_info; info; info = info->next) {
105 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
106 info->value->filename);
109 qapi_free_ChardevInfoList(char_info);
112 void hmp_info_mice(Monitor *mon)
114 MouseInfoList *mice_list, *mouse;
116 mice_list = qmp_query_mice(NULL);
117 if (!mice_list) {
118 monitor_printf(mon, "No mouse devices connected\n");
119 return;
122 for (mouse = mice_list; mouse; mouse = mouse->next) {
123 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
124 mouse->value->current ? '*' : ' ',
125 mouse->value->index, mouse->value->name,
126 mouse->value->absolute ? " (absolute)" : "");
129 qapi_free_MouseInfoList(mice_list);
132 void hmp_info_migrate(Monitor *mon)
134 MigrationInfo *info;
135 MigrationCapabilityStatusList *caps, *cap;
137 info = qmp_query_migrate(NULL);
138 caps = qmp_query_migrate_capabilities(NULL);
140 /* do not display parameters during setup */
141 if (info->has_status && caps) {
142 monitor_printf(mon, "capabilities: ");
143 for (cap = caps; cap; cap = cap->next) {
144 monitor_printf(mon, "%s: %s ",
145 MigrationCapability_lookup[cap->value->capability],
146 cap->value->state ? "on" : "off");
148 monitor_printf(mon, "\n");
151 if (info->has_status) {
152 monitor_printf(mon, "Migration status: %s\n", info->status);
153 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
154 info->total_time);
157 if (info->has_ram) {
158 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
159 info->ram->transferred >> 10);
160 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
161 info->ram->remaining >> 10);
162 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
163 info->ram->total >> 10);
164 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
165 info->ram->duplicate);
166 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
167 info->ram->normal);
168 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
169 info->ram->normal_bytes >> 10);
172 if (info->has_disk) {
173 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
174 info->disk->transferred >> 10);
175 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
176 info->disk->remaining >> 10);
177 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
178 info->disk->total >> 10);
181 if (info->has_xbzrle_cache) {
182 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
183 info->xbzrle_cache->cache_size);
184 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
185 info->xbzrle_cache->bytes >> 10);
186 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
187 info->xbzrle_cache->pages);
188 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
189 info->xbzrle_cache->cache_miss);
190 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
191 info->xbzrle_cache->overflow);
194 qapi_free_MigrationInfo(info);
195 qapi_free_MigrationCapabilityStatusList(caps);
198 void hmp_info_migrate_capabilities(Monitor *mon)
200 MigrationCapabilityStatusList *caps, *cap;
202 caps = qmp_query_migrate_capabilities(NULL);
204 if (caps) {
205 monitor_printf(mon, "capabilities: ");
206 for (cap = caps; cap; cap = cap->next) {
207 monitor_printf(mon, "%s: %s ",
208 MigrationCapability_lookup[cap->value->capability],
209 cap->value->state ? "on" : "off");
211 monitor_printf(mon, "\n");
214 qapi_free_MigrationCapabilityStatusList(caps);
217 void hmp_info_migrate_cache_size(Monitor *mon)
219 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
220 qmp_query_migrate_cache_size(NULL) >> 10);
223 void hmp_info_cpus(Monitor *mon)
225 CpuInfoList *cpu_list, *cpu;
227 cpu_list = qmp_query_cpus(NULL);
229 for (cpu = cpu_list; cpu; cpu = cpu->next) {
230 int active = ' ';
232 if (cpu->value->CPU == monitor_get_cpu_index()) {
233 active = '*';
236 monitor_printf(mon, "%c CPU #%" PRId64 ": ", active, cpu->value->CPU);
238 if (cpu->value->has_pc) {
239 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
241 if (cpu->value->has_nip) {
242 monitor_printf(mon, "nip=0x%016" PRIx64, cpu->value->nip);
244 if (cpu->value->has_npc) {
245 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
246 monitor_printf(mon, "npc=0x%016" PRIx64, cpu->value->npc);
248 if (cpu->value->has_PC) {
249 monitor_printf(mon, "PC=0x%016" PRIx64, cpu->value->PC);
252 if (cpu->value->halted) {
253 monitor_printf(mon, " (halted)");
256 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
259 qapi_free_CpuInfoList(cpu_list);
262 void hmp_info_block(Monitor *mon)
264 BlockInfoList *block_list, *info;
266 block_list = qmp_query_block(NULL);
268 for (info = block_list; info; info = info->next) {
269 monitor_printf(mon, "%s: removable=%d",
270 info->value->device, info->value->removable);
272 if (info->value->removable) {
273 monitor_printf(mon, " locked=%d", info->value->locked);
274 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
277 if (info->value->has_io_status) {
278 monitor_printf(mon, " io-status=%s",
279 BlockDeviceIoStatus_lookup[info->value->io_status]);
282 if (info->value->has_inserted) {
283 monitor_printf(mon, " file=");
284 monitor_print_filename(mon, info->value->inserted->file);
286 if (info->value->inserted->has_backing_file) {
287 monitor_printf(mon, " backing_file=");
288 monitor_print_filename(mon, info->value->inserted->backing_file);
289 monitor_printf(mon, " backing_file_depth=%" PRId64,
290 info->value->inserted->backing_file_depth);
292 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
293 info->value->inserted->ro,
294 info->value->inserted->drv,
295 info->value->inserted->encrypted);
297 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
298 " bps_wr=%" PRId64 " iops=%" PRId64
299 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
300 info->value->inserted->bps,
301 info->value->inserted->bps_rd,
302 info->value->inserted->bps_wr,
303 info->value->inserted->iops,
304 info->value->inserted->iops_rd,
305 info->value->inserted->iops_wr);
306 } else {
307 monitor_printf(mon, " [not inserted]");
310 monitor_printf(mon, "\n");
313 qapi_free_BlockInfoList(block_list);
316 void hmp_info_blockstats(Monitor *mon)
318 BlockStatsList *stats_list, *stats;
320 stats_list = qmp_query_blockstats(NULL);
322 for (stats = stats_list; stats; stats = stats->next) {
323 if (!stats->value->has_device) {
324 continue;
327 monitor_printf(mon, "%s:", stats->value->device);
328 monitor_printf(mon, " rd_bytes=%" PRId64
329 " wr_bytes=%" PRId64
330 " rd_operations=%" PRId64
331 " wr_operations=%" PRId64
332 " flush_operations=%" PRId64
333 " wr_total_time_ns=%" PRId64
334 " rd_total_time_ns=%" PRId64
335 " flush_total_time_ns=%" PRId64
336 "\n",
337 stats->value->stats->rd_bytes,
338 stats->value->stats->wr_bytes,
339 stats->value->stats->rd_operations,
340 stats->value->stats->wr_operations,
341 stats->value->stats->flush_operations,
342 stats->value->stats->wr_total_time_ns,
343 stats->value->stats->rd_total_time_ns,
344 stats->value->stats->flush_total_time_ns);
347 qapi_free_BlockStatsList(stats_list);
350 void hmp_info_vnc(Monitor *mon)
352 VncInfo *info;
353 Error *err = NULL;
354 VncClientInfoList *client;
356 info = qmp_query_vnc(&err);
357 if (err) {
358 monitor_printf(mon, "%s\n", error_get_pretty(err));
359 error_free(err);
360 return;
363 if (!info->enabled) {
364 monitor_printf(mon, "Server: disabled\n");
365 goto out;
368 monitor_printf(mon, "Server:\n");
369 if (info->has_host && info->has_service) {
370 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
372 if (info->has_auth) {
373 monitor_printf(mon, " auth: %s\n", info->auth);
376 if (!info->has_clients || info->clients == NULL) {
377 monitor_printf(mon, "Client: none\n");
378 } else {
379 for (client = info->clients; client; client = client->next) {
380 monitor_printf(mon, "Client:\n");
381 monitor_printf(mon, " address: %s:%s\n",
382 client->value->host, client->value->service);
383 monitor_printf(mon, " x509_dname: %s\n",
384 client->value->x509_dname ?
385 client->value->x509_dname : "none");
386 monitor_printf(mon, " username: %s\n",
387 client->value->has_sasl_username ?
388 client->value->sasl_username : "none");
392 out:
393 qapi_free_VncInfo(info);
396 void hmp_info_spice(Monitor *mon)
398 SpiceChannelList *chan;
399 SpiceInfo *info;
401 info = qmp_query_spice(NULL);
403 if (!info->enabled) {
404 monitor_printf(mon, "Server: disabled\n");
405 goto out;
408 monitor_printf(mon, "Server:\n");
409 if (info->has_port) {
410 monitor_printf(mon, " address: %s:%" PRId64 "\n",
411 info->host, info->port);
413 if (info->has_tls_port) {
414 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
415 info->host, info->tls_port);
417 monitor_printf(mon, " migrated: %s\n",
418 info->migrated ? "true" : "false");
419 monitor_printf(mon, " auth: %s\n", info->auth);
420 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
421 monitor_printf(mon, " mouse-mode: %s\n",
422 SpiceQueryMouseMode_lookup[info->mouse_mode]);
424 if (!info->has_channels || info->channels == NULL) {
425 monitor_printf(mon, "Channels: none\n");
426 } else {
427 for (chan = info->channels; chan; chan = chan->next) {
428 monitor_printf(mon, "Channel:\n");
429 monitor_printf(mon, " address: %s:%s%s\n",
430 chan->value->host, chan->value->port,
431 chan->value->tls ? " [tls]" : "");
432 monitor_printf(mon, " session: %" PRId64 "\n",
433 chan->value->connection_id);
434 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
435 chan->value->channel_type, chan->value->channel_id);
439 out:
440 qapi_free_SpiceInfo(info);
443 void hmp_info_balloon(Monitor *mon)
445 BalloonInfo *info;
446 Error *err = NULL;
448 info = qmp_query_balloon(&err);
449 if (err) {
450 monitor_printf(mon, "%s\n", error_get_pretty(err));
451 error_free(err);
452 return;
455 monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
456 if (info->has_mem_swapped_in) {
457 monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
459 if (info->has_mem_swapped_out) {
460 monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
462 if (info->has_major_page_faults) {
463 monitor_printf(mon, " major_page_faults=%" PRId64,
464 info->major_page_faults);
466 if (info->has_minor_page_faults) {
467 monitor_printf(mon, " minor_page_faults=%" PRId64,
468 info->minor_page_faults);
470 if (info->has_free_mem) {
471 monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
473 if (info->has_total_mem) {
474 monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
477 monitor_printf(mon, "\n");
479 qapi_free_BalloonInfo(info);
482 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
484 PciMemoryRegionList *region;
486 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
487 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
488 dev->slot, dev->function);
489 monitor_printf(mon, " ");
491 if (dev->class_info.has_desc) {
492 monitor_printf(mon, "%s", dev->class_info.desc);
493 } else {
494 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
497 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
498 dev->id.vendor, dev->id.device);
500 if (dev->has_irq) {
501 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
504 if (dev->has_pci_bridge) {
505 monitor_printf(mon, " BUS %" PRId64 ".\n",
506 dev->pci_bridge->bus.number);
507 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
508 dev->pci_bridge->bus.secondary);
509 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
510 dev->pci_bridge->bus.subordinate);
512 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
513 dev->pci_bridge->bus.io_range->base,
514 dev->pci_bridge->bus.io_range->limit);
516 monitor_printf(mon,
517 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
518 dev->pci_bridge->bus.memory_range->base,
519 dev->pci_bridge->bus.memory_range->limit);
521 monitor_printf(mon, " prefetchable memory range "
522 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
523 dev->pci_bridge->bus.prefetchable_range->base,
524 dev->pci_bridge->bus.prefetchable_range->limit);
527 for (region = dev->regions; region; region = region->next) {
528 uint64_t addr, size;
530 addr = region->value->address;
531 size = region->value->size;
533 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
535 if (!strcmp(region->value->type, "io")) {
536 monitor_printf(mon, "I/O at 0x%04" PRIx64
537 " [0x%04" PRIx64 "].\n",
538 addr, addr + size - 1);
539 } else {
540 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
541 " [0x%08" PRIx64 "].\n",
542 region->value->mem_type_64 ? 64 : 32,
543 region->value->prefetch ? " prefetchable" : "",
544 addr, addr + size - 1);
548 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
550 if (dev->has_pci_bridge) {
551 if (dev->pci_bridge->has_devices) {
552 PciDeviceInfoList *cdev;
553 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
554 hmp_info_pci_device(mon, cdev->value);
560 void hmp_info_pci(Monitor *mon)
562 PciInfoList *info_list, *info;
563 Error *err = NULL;
565 info_list = qmp_query_pci(&err);
566 if (err) {
567 monitor_printf(mon, "PCI devices not supported\n");
568 error_free(err);
569 return;
572 for (info = info_list; info; info = info->next) {
573 PciDeviceInfoList *dev;
575 for (dev = info->value->devices; dev; dev = dev->next) {
576 hmp_info_pci_device(mon, dev->value);
580 qapi_free_PciInfoList(info_list);
583 void hmp_info_block_jobs(Monitor *mon)
585 BlockJobInfoList *list;
586 Error *err = NULL;
588 list = qmp_query_block_jobs(&err);
589 assert(!err);
591 if (!list) {
592 monitor_printf(mon, "No active jobs\n");
593 return;
596 while (list) {
597 if (strcmp(list->value->type, "stream") == 0) {
598 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
599 " of %" PRId64 " bytes, speed limit %" PRId64
600 " bytes/s\n",
601 list->value->device,
602 list->value->offset,
603 list->value->len,
604 list->value->speed);
605 } else {
606 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
607 " of %" PRId64 " bytes, speed limit %" PRId64
608 " bytes/s\n",
609 list->value->type,
610 list->value->device,
611 list->value->offset,
612 list->value->len,
613 list->value->speed);
615 list = list->next;
619 void hmp_quit(Monitor *mon, const QDict *qdict)
621 monitor_suspend(mon);
622 qmp_quit(NULL);
625 void hmp_stop(Monitor *mon, const QDict *qdict)
627 qmp_stop(NULL);
630 void hmp_system_reset(Monitor *mon, const QDict *qdict)
632 qmp_system_reset(NULL);
635 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
637 qmp_system_powerdown(NULL);
640 void hmp_cpu(Monitor *mon, const QDict *qdict)
642 int64_t cpu_index;
644 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
645 use it are converted to the QAPI */
646 cpu_index = qdict_get_int(qdict, "index");
647 if (monitor_set_cpu(cpu_index) < 0) {
648 monitor_printf(mon, "invalid CPU index\n");
652 void hmp_memsave(Monitor *mon, const QDict *qdict)
654 uint32_t size = qdict_get_int(qdict, "size");
655 const char *filename = qdict_get_str(qdict, "filename");
656 uint64_t addr = qdict_get_int(qdict, "val");
657 Error *errp = NULL;
659 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
660 hmp_handle_error(mon, &errp);
663 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
665 uint32_t size = qdict_get_int(qdict, "size");
666 const char *filename = qdict_get_str(qdict, "filename");
667 uint64_t addr = qdict_get_int(qdict, "val");
668 Error *errp = NULL;
670 qmp_pmemsave(addr, size, filename, &errp);
671 hmp_handle_error(mon, &errp);
674 static void hmp_cont_cb(void *opaque, int err)
676 if (!err) {
677 qmp_cont(NULL);
681 static bool key_is_missing(const BlockInfo *bdev)
683 return (bdev->inserted && bdev->inserted->encryption_key_missing);
686 void hmp_cont(Monitor *mon, const QDict *qdict)
688 BlockInfoList *bdev_list, *bdev;
689 Error *errp = NULL;
691 bdev_list = qmp_query_block(NULL);
692 for (bdev = bdev_list; bdev; bdev = bdev->next) {
693 if (key_is_missing(bdev->value)) {
694 monitor_read_block_device_key(mon, bdev->value->device,
695 hmp_cont_cb, NULL);
696 goto out;
700 qmp_cont(&errp);
701 hmp_handle_error(mon, &errp);
703 out:
704 qapi_free_BlockInfoList(bdev_list);
707 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
709 qmp_system_wakeup(NULL);
712 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
714 Error *errp = NULL;
716 qmp_inject_nmi(&errp);
717 hmp_handle_error(mon, &errp);
720 void hmp_set_link(Monitor *mon, const QDict *qdict)
722 const char *name = qdict_get_str(qdict, "name");
723 int up = qdict_get_bool(qdict, "up");
724 Error *errp = NULL;
726 qmp_set_link(name, up, &errp);
727 hmp_handle_error(mon, &errp);
730 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
732 const char *device = qdict_get_str(qdict, "device");
733 const char *password = qdict_get_str(qdict, "password");
734 Error *errp = NULL;
736 qmp_block_passwd(device, password, &errp);
737 hmp_handle_error(mon, &errp);
740 void hmp_balloon(Monitor *mon, const QDict *qdict)
742 int64_t value = qdict_get_int(qdict, "value");
743 Error *errp = NULL;
745 qmp_balloon(value, &errp);
746 if (error_is_set(&errp)) {
747 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
748 error_free(errp);
752 void hmp_block_resize(Monitor *mon, const QDict *qdict)
754 const char *device = qdict_get_str(qdict, "device");
755 int64_t size = qdict_get_int(qdict, "size");
756 Error *errp = NULL;
758 qmp_block_resize(device, size, &errp);
759 hmp_handle_error(mon, &errp);
762 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
764 const char *device = qdict_get_str(qdict, "device");
765 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
766 const char *format = qdict_get_try_str(qdict, "format");
767 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
768 enum NewImageMode mode;
769 Error *errp = NULL;
771 if (!filename) {
772 /* In the future, if 'snapshot-file' is not specified, the snapshot
773 will be taken internally. Today it's actually required. */
774 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
775 hmp_handle_error(mon, &errp);
776 return;
779 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
780 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
781 true, mode, &errp);
782 hmp_handle_error(mon, &errp);
785 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
787 qmp_migrate_cancel(NULL);
790 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
792 double value = qdict_get_double(qdict, "value");
793 qmp_migrate_set_downtime(value, NULL);
796 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
798 int64_t value = qdict_get_int(qdict, "value");
799 Error *err = NULL;
801 qmp_migrate_set_cache_size(value, &err);
802 if (err) {
803 monitor_printf(mon, "%s\n", error_get_pretty(err));
804 error_free(err);
805 return;
809 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
811 int64_t value = qdict_get_int(qdict, "value");
812 qmp_migrate_set_speed(value, NULL);
815 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
817 const char *cap = qdict_get_str(qdict, "capability");
818 bool state = qdict_get_bool(qdict, "state");
819 Error *err = NULL;
820 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
821 int i;
823 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
824 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
825 caps->value = g_malloc0(sizeof(*caps->value));
826 caps->value->capability = i;
827 caps->value->state = state;
828 caps->next = NULL;
829 qmp_migrate_set_capabilities(caps, &err);
830 break;
834 if (i == MIGRATION_CAPABILITY_MAX) {
835 error_set(&err, QERR_INVALID_PARAMETER, cap);
838 qapi_free_MigrationCapabilityStatusList(caps);
840 if (err) {
841 monitor_printf(mon, "migrate_set_parameter: %s\n",
842 error_get_pretty(err));
843 error_free(err);
847 void hmp_set_password(Monitor *mon, const QDict *qdict)
849 const char *protocol = qdict_get_str(qdict, "protocol");
850 const char *password = qdict_get_str(qdict, "password");
851 const char *connected = qdict_get_try_str(qdict, "connected");
852 Error *err = NULL;
854 qmp_set_password(protocol, password, !!connected, connected, &err);
855 hmp_handle_error(mon, &err);
858 void hmp_expire_password(Monitor *mon, const QDict *qdict)
860 const char *protocol = qdict_get_str(qdict, "protocol");
861 const char *whenstr = qdict_get_str(qdict, "time");
862 Error *err = NULL;
864 qmp_expire_password(protocol, whenstr, &err);
865 hmp_handle_error(mon, &err);
868 void hmp_eject(Monitor *mon, const QDict *qdict)
870 int force = qdict_get_try_bool(qdict, "force", 0);
871 const char *device = qdict_get_str(qdict, "device");
872 Error *err = NULL;
874 qmp_eject(device, true, force, &err);
875 hmp_handle_error(mon, &err);
878 static void hmp_change_read_arg(Monitor *mon, const char *password,
879 void *opaque)
881 qmp_change_vnc_password(password, NULL);
882 monitor_read_command(mon, 1);
885 void hmp_change(Monitor *mon, const QDict *qdict)
887 const char *device = qdict_get_str(qdict, "device");
888 const char *target = qdict_get_str(qdict, "target");
889 const char *arg = qdict_get_try_str(qdict, "arg");
890 Error *err = NULL;
892 if (strcmp(device, "vnc") == 0 &&
893 (strcmp(target, "passwd") == 0 ||
894 strcmp(target, "password") == 0)) {
895 if (!arg) {
896 monitor_read_password(mon, hmp_change_read_arg, NULL);
897 return;
901 qmp_change(device, target, !!arg, arg, &err);
902 if (error_is_set(&err) &&
903 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
904 error_free(err);
905 monitor_read_block_device_key(mon, device, NULL, NULL);
906 return;
908 hmp_handle_error(mon, &err);
911 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
913 Error *err = NULL;
915 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
916 qdict_get_int(qdict, "bps"),
917 qdict_get_int(qdict, "bps_rd"),
918 qdict_get_int(qdict, "bps_wr"),
919 qdict_get_int(qdict, "iops"),
920 qdict_get_int(qdict, "iops_rd"),
921 qdict_get_int(qdict, "iops_wr"), &err);
922 hmp_handle_error(mon, &err);
925 void hmp_block_stream(Monitor *mon, const QDict *qdict)
927 Error *error = NULL;
928 const char *device = qdict_get_str(qdict, "device");
929 const char *base = qdict_get_try_str(qdict, "base");
930 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
932 qmp_block_stream(device, base != NULL, base,
933 qdict_haskey(qdict, "speed"), speed,
934 BLOCKDEV_ON_ERROR_REPORT, true, &error);
936 hmp_handle_error(mon, &error);
939 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
941 Error *error = NULL;
942 const char *device = qdict_get_str(qdict, "device");
943 int64_t value = qdict_get_int(qdict, "speed");
945 qmp_block_job_set_speed(device, value, &error);
947 hmp_handle_error(mon, &error);
950 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
952 Error *error = NULL;
953 const char *device = qdict_get_str(qdict, "device");
954 bool force = qdict_get_try_bool(qdict, "force", 0);
956 qmp_block_job_cancel(device, true, force, &error);
958 hmp_handle_error(mon, &error);
961 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
963 Error *error = NULL;
964 const char *device = qdict_get_str(qdict, "device");
966 qmp_block_job_pause(device, &error);
968 hmp_handle_error(mon, &error);
971 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
973 Error *error = NULL;
974 const char *device = qdict_get_str(qdict, "device");
976 qmp_block_job_resume(device, &error);
978 hmp_handle_error(mon, &error);
981 typedef struct MigrationStatus
983 QEMUTimer *timer;
984 Monitor *mon;
985 bool is_block_migration;
986 } MigrationStatus;
988 static void hmp_migrate_status_cb(void *opaque)
990 MigrationStatus *status = opaque;
991 MigrationInfo *info;
993 info = qmp_query_migrate(NULL);
994 if (!info->has_status || strcmp(info->status, "active") == 0) {
995 if (info->has_disk) {
996 int progress;
998 if (info->disk->remaining) {
999 progress = info->disk->transferred * 100 / info->disk->total;
1000 } else {
1001 progress = 100;
1004 monitor_printf(status->mon, "Completed %d %%\r", progress);
1005 monitor_flush(status->mon);
1008 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1009 } else {
1010 if (status->is_block_migration) {
1011 monitor_printf(status->mon, "\n");
1013 monitor_resume(status->mon);
1014 qemu_del_timer(status->timer);
1015 g_free(status);
1018 qapi_free_MigrationInfo(info);
1021 void hmp_migrate(Monitor *mon, const QDict *qdict)
1023 int detach = qdict_get_try_bool(qdict, "detach", 0);
1024 int blk = qdict_get_try_bool(qdict, "blk", 0);
1025 int inc = qdict_get_try_bool(qdict, "inc", 0);
1026 const char *uri = qdict_get_str(qdict, "uri");
1027 Error *err = NULL;
1029 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1030 if (err) {
1031 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1032 error_free(err);
1033 return;
1036 if (!detach) {
1037 MigrationStatus *status;
1039 if (monitor_suspend(mon) < 0) {
1040 monitor_printf(mon, "terminal does not allow synchronous "
1041 "migration, continuing detached\n");
1042 return;
1045 status = g_malloc0(sizeof(*status));
1046 status->mon = mon;
1047 status->is_block_migration = blk || inc;
1048 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1049 status);
1050 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1054 void hmp_device_del(Monitor *mon, const QDict *qdict)
1056 const char *id = qdict_get_str(qdict, "id");
1057 Error *err = NULL;
1059 qmp_device_del(id, &err);
1060 hmp_handle_error(mon, &err);
1063 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1065 Error *errp = NULL;
1066 int paging = qdict_get_try_bool(qdict, "paging", 0);
1067 const char *file = qdict_get_str(qdict, "filename");
1068 bool has_begin = qdict_haskey(qdict, "begin");
1069 bool has_length = qdict_haskey(qdict, "length");
1070 int64_t begin = 0;
1071 int64_t length = 0;
1072 char *prot;
1074 if (has_begin) {
1075 begin = qdict_get_int(qdict, "begin");
1077 if (has_length) {
1078 length = qdict_get_int(qdict, "length");
1081 prot = g_strconcat("file:", file, NULL);
1083 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1084 &errp);
1085 hmp_handle_error(mon, &errp);
1086 g_free(prot);
1089 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1091 Error *err = NULL;
1092 QemuOpts *opts;
1094 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1095 if (error_is_set(&err)) {
1096 goto out;
1099 netdev_add(opts, &err);
1100 if (error_is_set(&err)) {
1101 qemu_opts_del(opts);
1104 out:
1105 hmp_handle_error(mon, &err);
1108 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1110 const char *id = qdict_get_str(qdict, "id");
1111 Error *err = NULL;
1113 qmp_netdev_del(id, &err);
1114 hmp_handle_error(mon, &err);
1117 void hmp_getfd(Monitor *mon, const QDict *qdict)
1119 const char *fdname = qdict_get_str(qdict, "fdname");
1120 Error *errp = NULL;
1122 qmp_getfd(fdname, &errp);
1123 hmp_handle_error(mon, &errp);
1126 void hmp_closefd(Monitor *mon, const QDict *qdict)
1128 const char *fdname = qdict_get_str(qdict, "fdname");
1129 Error *errp = NULL;
1131 qmp_closefd(fdname, &errp);
1132 hmp_handle_error(mon, &errp);
1135 void hmp_send_key(Monitor *mon, const QDict *qdict)
1137 const char *keys = qdict_get_str(qdict, "keys");
1138 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1139 int has_hold_time = qdict_haskey(qdict, "hold-time");
1140 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1141 Error *err = NULL;
1142 char keyname_buf[16];
1143 char *separator;
1144 int keyname_len;
1146 while (1) {
1147 separator = strchr(keys, '-');
1148 keyname_len = separator ? separator - keys : strlen(keys);
1149 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1151 /* Be compatible with old interface, convert user inputted "<" */
1152 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1153 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1154 keyname_len = 4;
1156 keyname_buf[keyname_len] = 0;
1158 keylist = g_malloc0(sizeof(*keylist));
1159 keylist->value = g_malloc0(sizeof(*keylist->value));
1161 if (!head) {
1162 head = keylist;
1164 if (tmp) {
1165 tmp->next = keylist;
1167 tmp = keylist;
1169 if (strstart(keyname_buf, "0x", NULL)) {
1170 char *endp;
1171 int value = strtoul(keyname_buf, &endp, 0);
1172 if (*endp != '\0') {
1173 goto err_out;
1175 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1176 keylist->value->number = value;
1177 } else {
1178 int idx = index_from_key(keyname_buf);
1179 if (idx == Q_KEY_CODE_MAX) {
1180 goto err_out;
1182 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1183 keylist->value->qcode = idx;
1186 if (!separator) {
1187 break;
1189 keys = separator + 1;
1192 qmp_send_key(head, has_hold_time, hold_time, &err);
1193 hmp_handle_error(mon, &err);
1195 out:
1196 qapi_free_KeyValueList(head);
1197 return;
1199 err_out:
1200 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1201 goto out;
1204 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1206 const char *filename = qdict_get_str(qdict, "filename");
1207 Error *err = NULL;
1209 qmp_screendump(filename, &err);
1210 hmp_handle_error(mon, &err);