Merge remote-tracking branch 'bonzini/iommu-for-anthony' into staging
[qemu/ar7.git] / hmp.c
blobdc4d8d453fe2a2af48048f4fd397c24e492bee19
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 "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"
26 #include "qemu-io.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));
32 error_free(*errp);
36 void hmp_info_name(Monitor *mon, const QDict *qdict)
38 NameInfo *info;
40 info = qmp_query_name(NULL);
41 if (info->has_name) {
42 monitor_printf(mon, "%s\n", info->name);
44 qapi_free_NameInfo(info);
47 void hmp_info_version(Monitor *mon, const QDict *qdict)
49 VersionInfo *info;
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,
55 info->package);
57 qapi_free_VersionInfo(info);
60 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
62 KvmInfo *info;
64 info = qmp_query_kvm(NULL);
65 monitor_printf(mon, "kvm support: ");
66 if (info->present) {
67 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
68 } else {
69 monitor_printf(mon, "not compiled\n");
72 qapi_free_KvmInfo(info);
75 void hmp_info_status(Monitor *mon, const QDict *qdict)
77 StatusInfo *info;
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)
96 UuidInfo *info;
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);
121 if (!mice_list) {
122 monitor_printf(mon, "No mouse devices connected\n");
123 return;
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)
138 MigrationInfo *info;
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",
158 info->total_time);
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",
165 info->downtime);
169 if (info->has_ram) {
170 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
171 info->ram->transferred >> 10);
172 monitor_printf(mon, "throughput: %0.2f mbps\n",
173 info->ram->mbps);
174 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
175 info->ram->remaining >> 10);
176 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
177 info->ram->total >> 10);
178 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
179 info->ram->duplicate);
180 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
181 info->ram->skipped);
182 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
183 info->ram->normal);
184 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
185 info->ram->normal_bytes >> 10);
186 if (info->ram->dirty_pages_rate) {
187 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
188 info->ram->dirty_pages_rate);
192 if (info->has_disk) {
193 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
194 info->disk->transferred >> 10);
195 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
196 info->disk->remaining >> 10);
197 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
198 info->disk->total >> 10);
201 if (info->has_xbzrle_cache) {
202 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
203 info->xbzrle_cache->cache_size);
204 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
205 info->xbzrle_cache->bytes >> 10);
206 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
207 info->xbzrle_cache->pages);
208 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
209 info->xbzrle_cache->cache_miss);
210 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
211 info->xbzrle_cache->overflow);
214 qapi_free_MigrationInfo(info);
215 qapi_free_MigrationCapabilityStatusList(caps);
218 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
220 MigrationCapabilityStatusList *caps, *cap;
222 caps = qmp_query_migrate_capabilities(NULL);
224 if (caps) {
225 monitor_printf(mon, "capabilities: ");
226 for (cap = caps; cap; cap = cap->next) {
227 monitor_printf(mon, "%s: %s ",
228 MigrationCapability_lookup[cap->value->capability],
229 cap->value->state ? "on" : "off");
231 monitor_printf(mon, "\n");
234 qapi_free_MigrationCapabilityStatusList(caps);
237 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
239 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
240 qmp_query_migrate_cache_size(NULL) >> 10);
243 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
245 CpuInfoList *cpu_list, *cpu;
247 cpu_list = qmp_query_cpus(NULL);
249 for (cpu = cpu_list; cpu; cpu = cpu->next) {
250 int active = ' ';
252 if (cpu->value->CPU == monitor_get_cpu_index()) {
253 active = '*';
256 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
258 if (cpu->value->has_pc) {
259 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
261 if (cpu->value->has_nip) {
262 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
264 if (cpu->value->has_npc) {
265 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
267 if (cpu->value->has_PC) {
268 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
271 if (cpu->value->halted) {
272 monitor_printf(mon, " (halted)");
275 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
278 qapi_free_CpuInfoList(cpu_list);
281 void hmp_info_block(Monitor *mon, const QDict *qdict)
283 BlockInfoList *block_list, *info;
284 ImageInfo *image_info;
285 const char *device = qdict_get_try_str(qdict, "device");
286 bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
288 block_list = qmp_query_block(NULL);
290 for (info = block_list; info; info = info->next) {
291 if (device && strcmp(device, info->value->device)) {
292 continue;
295 if (info != block_list) {
296 monitor_printf(mon, "\n");
299 monitor_printf(mon, "%s", info->value->device);
300 if (info->value->has_inserted) {
301 monitor_printf(mon, ": %s (%s%s%s)\n",
302 info->value->inserted->file,
303 info->value->inserted->drv,
304 info->value->inserted->ro ? ", read-only" : "",
305 info->value->inserted->encrypted ? ", encrypted" : "");
306 } else {
307 monitor_printf(mon, ": [not inserted]\n");
310 if (info->value->has_io_status && info->value->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
311 monitor_printf(mon, " I/O status: %s\n",
312 BlockDeviceIoStatus_lookup[info->value->io_status]);
315 if (info->value->removable) {
316 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
317 info->value->locked ? "" : "not ",
318 info->value->tray_open ? "open" : "closed");
322 if (!info->value->has_inserted) {
323 continue;
326 if (info->value->inserted->has_backing_file) {
327 monitor_printf(mon,
328 " Backing file: %s "
329 "(chain depth: %" PRId64 ")\n",
330 info->value->inserted->backing_file,
331 info->value->inserted->backing_file_depth);
334 if (info->value->inserted->bps
335 || info->value->inserted->bps_rd
336 || info->value->inserted->bps_wr
337 || info->value->inserted->iops
338 || info->value->inserted->iops_rd
339 || info->value->inserted->iops_wr)
341 monitor_printf(mon, " I/O throttling: bps=%" PRId64
342 " bps_rd=%" PRId64 " bps_wr=%" PRId64
343 " iops=%" PRId64 " iops_rd=%" PRId64
344 " iops_wr=%" PRId64 "\n",
345 info->value->inserted->bps,
346 info->value->inserted->bps_rd,
347 info->value->inserted->bps_wr,
348 info->value->inserted->iops,
349 info->value->inserted->iops_rd,
350 info->value->inserted->iops_wr);
353 if (verbose) {
354 monitor_printf(mon, "\nImages:\n");
355 image_info = info->value->inserted->image;
356 while (1) {
357 bdrv_image_info_dump((fprintf_function)monitor_printf,
358 mon, image_info);
359 if (image_info->has_backing_image) {
360 image_info = image_info->backing_image;
361 } else {
362 break;
368 qapi_free_BlockInfoList(block_list);
371 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
373 BlockStatsList *stats_list, *stats;
375 stats_list = qmp_query_blockstats(NULL);
377 for (stats = stats_list; stats; stats = stats->next) {
378 if (!stats->value->has_device) {
379 continue;
382 monitor_printf(mon, "%s:", stats->value->device);
383 monitor_printf(mon, " rd_bytes=%" PRId64
384 " wr_bytes=%" PRId64
385 " rd_operations=%" PRId64
386 " wr_operations=%" PRId64
387 " flush_operations=%" PRId64
388 " wr_total_time_ns=%" PRId64
389 " rd_total_time_ns=%" PRId64
390 " flush_total_time_ns=%" PRId64
391 "\n",
392 stats->value->stats->rd_bytes,
393 stats->value->stats->wr_bytes,
394 stats->value->stats->rd_operations,
395 stats->value->stats->wr_operations,
396 stats->value->stats->flush_operations,
397 stats->value->stats->wr_total_time_ns,
398 stats->value->stats->rd_total_time_ns,
399 stats->value->stats->flush_total_time_ns);
402 qapi_free_BlockStatsList(stats_list);
405 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
407 VncInfo *info;
408 Error *err = NULL;
409 VncClientInfoList *client;
411 info = qmp_query_vnc(&err);
412 if (err) {
413 monitor_printf(mon, "%s\n", error_get_pretty(err));
414 error_free(err);
415 return;
418 if (!info->enabled) {
419 monitor_printf(mon, "Server: disabled\n");
420 goto out;
423 monitor_printf(mon, "Server:\n");
424 if (info->has_host && info->has_service) {
425 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
427 if (info->has_auth) {
428 monitor_printf(mon, " auth: %s\n", info->auth);
431 if (!info->has_clients || info->clients == NULL) {
432 monitor_printf(mon, "Client: none\n");
433 } else {
434 for (client = info->clients; client; client = client->next) {
435 monitor_printf(mon, "Client:\n");
436 monitor_printf(mon, " address: %s:%s\n",
437 client->value->host, client->value->service);
438 monitor_printf(mon, " x509_dname: %s\n",
439 client->value->x509_dname ?
440 client->value->x509_dname : "none");
441 monitor_printf(mon, " username: %s\n",
442 client->value->has_sasl_username ?
443 client->value->sasl_username : "none");
447 out:
448 qapi_free_VncInfo(info);
451 void hmp_info_spice(Monitor *mon, const QDict *qdict)
453 SpiceChannelList *chan;
454 SpiceInfo *info;
456 info = qmp_query_spice(NULL);
458 if (!info->enabled) {
459 monitor_printf(mon, "Server: disabled\n");
460 goto out;
463 monitor_printf(mon, "Server:\n");
464 if (info->has_port) {
465 monitor_printf(mon, " address: %s:%" PRId64 "\n",
466 info->host, info->port);
468 if (info->has_tls_port) {
469 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
470 info->host, info->tls_port);
472 monitor_printf(mon, " migrated: %s\n",
473 info->migrated ? "true" : "false");
474 monitor_printf(mon, " auth: %s\n", info->auth);
475 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
476 monitor_printf(mon, " mouse-mode: %s\n",
477 SpiceQueryMouseMode_lookup[info->mouse_mode]);
479 if (!info->has_channels || info->channels == NULL) {
480 monitor_printf(mon, "Channels: none\n");
481 } else {
482 for (chan = info->channels; chan; chan = chan->next) {
483 monitor_printf(mon, "Channel:\n");
484 monitor_printf(mon, " address: %s:%s%s\n",
485 chan->value->host, chan->value->port,
486 chan->value->tls ? " [tls]" : "");
487 monitor_printf(mon, " session: %" PRId64 "\n",
488 chan->value->connection_id);
489 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
490 chan->value->channel_type, chan->value->channel_id);
494 out:
495 qapi_free_SpiceInfo(info);
498 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
500 BalloonInfo *info;
501 Error *err = NULL;
503 info = qmp_query_balloon(&err);
504 if (err) {
505 monitor_printf(mon, "%s\n", error_get_pretty(err));
506 error_free(err);
507 return;
510 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
512 qapi_free_BalloonInfo(info);
515 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
517 PciMemoryRegionList *region;
519 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
520 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
521 dev->slot, dev->function);
522 monitor_printf(mon, " ");
524 if (dev->class_info.has_desc) {
525 monitor_printf(mon, "%s", dev->class_info.desc);
526 } else {
527 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
530 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
531 dev->id.vendor, dev->id.device);
533 if (dev->has_irq) {
534 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
537 if (dev->has_pci_bridge) {
538 monitor_printf(mon, " BUS %" PRId64 ".\n",
539 dev->pci_bridge->bus.number);
540 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
541 dev->pci_bridge->bus.secondary);
542 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
543 dev->pci_bridge->bus.subordinate);
545 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
546 dev->pci_bridge->bus.io_range->base,
547 dev->pci_bridge->bus.io_range->limit);
549 monitor_printf(mon,
550 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
551 dev->pci_bridge->bus.memory_range->base,
552 dev->pci_bridge->bus.memory_range->limit);
554 monitor_printf(mon, " prefetchable memory range "
555 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
556 dev->pci_bridge->bus.prefetchable_range->base,
557 dev->pci_bridge->bus.prefetchable_range->limit);
560 for (region = dev->regions; region; region = region->next) {
561 uint64_t addr, size;
563 addr = region->value->address;
564 size = region->value->size;
566 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
568 if (!strcmp(region->value->type, "io")) {
569 monitor_printf(mon, "I/O at 0x%04" PRIx64
570 " [0x%04" PRIx64 "].\n",
571 addr, addr + size - 1);
572 } else {
573 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
574 " [0x%08" PRIx64 "].\n",
575 region->value->mem_type_64 ? 64 : 32,
576 region->value->prefetch ? " prefetchable" : "",
577 addr, addr + size - 1);
581 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
583 if (dev->has_pci_bridge) {
584 if (dev->pci_bridge->has_devices) {
585 PciDeviceInfoList *cdev;
586 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
587 hmp_info_pci_device(mon, cdev->value);
593 void hmp_info_pci(Monitor *mon, const QDict *qdict)
595 PciInfoList *info_list, *info;
596 Error *err = NULL;
598 info_list = qmp_query_pci(&err);
599 if (err) {
600 monitor_printf(mon, "PCI devices not supported\n");
601 error_free(err);
602 return;
605 for (info = info_list; info; info = info->next) {
606 PciDeviceInfoList *dev;
608 for (dev = info->value->devices; dev; dev = dev->next) {
609 hmp_info_pci_device(mon, dev->value);
613 qapi_free_PciInfoList(info_list);
616 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
618 BlockJobInfoList *list;
619 Error *err = NULL;
621 list = qmp_query_block_jobs(&err);
622 assert(!err);
624 if (!list) {
625 monitor_printf(mon, "No active jobs\n");
626 return;
629 while (list) {
630 if (strcmp(list->value->type, "stream") == 0) {
631 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
632 " of %" PRId64 " bytes, speed limit %" PRId64
633 " bytes/s\n",
634 list->value->device,
635 list->value->offset,
636 list->value->len,
637 list->value->speed);
638 } else {
639 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
640 " of %" PRId64 " bytes, speed limit %" PRId64
641 " bytes/s\n",
642 list->value->type,
643 list->value->device,
644 list->value->offset,
645 list->value->len,
646 list->value->speed);
648 list = list->next;
652 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
654 TPMInfoList *info_list, *info;
655 Error *err = NULL;
656 unsigned int c = 0;
657 TPMPassthroughOptions *tpo;
659 info_list = qmp_query_tpm(&err);
660 if (err) {
661 monitor_printf(mon, "TPM device not supported\n");
662 error_free(err);
663 return;
666 if (info_list) {
667 monitor_printf(mon, "TPM device:\n");
670 for (info = info_list; info; info = info->next) {
671 TPMInfo *ti = info->value;
672 monitor_printf(mon, " tpm%d: model=%s\n",
673 c, TpmModel_lookup[ti->model]);
675 monitor_printf(mon, " \\ %s: type=%s",
676 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
678 switch (ti->options->kind) {
679 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
680 tpo = ti->options->passthrough;
681 monitor_printf(mon, "%s%s%s%s",
682 tpo->has_path ? ",path=" : "",
683 tpo->has_path ? tpo->path : "",
684 tpo->has_cancel_path ? ",cancel-path=" : "",
685 tpo->has_cancel_path ? tpo->cancel_path : "");
686 break;
687 case TPM_TYPE_OPTIONS_KIND_MAX:
688 break;
690 monitor_printf(mon, "\n");
691 c++;
693 qapi_free_TPMInfoList(info_list);
696 void hmp_quit(Monitor *mon, const QDict *qdict)
698 monitor_suspend(mon);
699 qmp_quit(NULL);
702 void hmp_stop(Monitor *mon, const QDict *qdict)
704 qmp_stop(NULL);
707 void hmp_system_reset(Monitor *mon, const QDict *qdict)
709 qmp_system_reset(NULL);
712 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
714 qmp_system_powerdown(NULL);
717 void hmp_cpu(Monitor *mon, const QDict *qdict)
719 int64_t cpu_index;
721 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
722 use it are converted to the QAPI */
723 cpu_index = qdict_get_int(qdict, "index");
724 if (monitor_set_cpu(cpu_index) < 0) {
725 monitor_printf(mon, "invalid CPU index\n");
729 void hmp_memsave(Monitor *mon, const QDict *qdict)
731 uint32_t size = qdict_get_int(qdict, "size");
732 const char *filename = qdict_get_str(qdict, "filename");
733 uint64_t addr = qdict_get_int(qdict, "val");
734 Error *errp = NULL;
736 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
737 hmp_handle_error(mon, &errp);
740 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
742 uint32_t size = qdict_get_int(qdict, "size");
743 const char *filename = qdict_get_str(qdict, "filename");
744 uint64_t addr = qdict_get_int(qdict, "val");
745 Error *errp = NULL;
747 qmp_pmemsave(addr, size, filename, &errp);
748 hmp_handle_error(mon, &errp);
751 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
753 const char *chardev = qdict_get_str(qdict, "device");
754 const char *data = qdict_get_str(qdict, "data");
755 Error *errp = NULL;
757 qmp_ringbuf_write(chardev, data, false, 0, &errp);
759 hmp_handle_error(mon, &errp);
762 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
764 uint32_t size = qdict_get_int(qdict, "size");
765 const char *chardev = qdict_get_str(qdict, "device");
766 char *data;
767 Error *errp = NULL;
768 int i;
770 data = qmp_ringbuf_read(chardev, size, false, 0, &errp);
771 if (errp) {
772 monitor_printf(mon, "%s\n", error_get_pretty(errp));
773 error_free(errp);
774 return;
777 for (i = 0; data[i]; i++) {
778 unsigned char ch = data[i];
780 if (ch == '\\') {
781 monitor_printf(mon, "\\\\");
782 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
783 monitor_printf(mon, "\\u%04X", ch);
784 } else {
785 monitor_printf(mon, "%c", ch);
789 monitor_printf(mon, "\n");
790 g_free(data);
793 static void hmp_cont_cb(void *opaque, int err)
795 if (!err) {
796 qmp_cont(NULL);
800 static bool key_is_missing(const BlockInfo *bdev)
802 return (bdev->inserted && bdev->inserted->encryption_key_missing);
805 void hmp_cont(Monitor *mon, const QDict *qdict)
807 BlockInfoList *bdev_list, *bdev;
808 Error *errp = NULL;
810 bdev_list = qmp_query_block(NULL);
811 for (bdev = bdev_list; bdev; bdev = bdev->next) {
812 if (key_is_missing(bdev->value)) {
813 monitor_read_block_device_key(mon, bdev->value->device,
814 hmp_cont_cb, NULL);
815 goto out;
819 qmp_cont(&errp);
820 hmp_handle_error(mon, &errp);
822 out:
823 qapi_free_BlockInfoList(bdev_list);
826 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
828 qmp_system_wakeup(NULL);
831 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
833 Error *errp = NULL;
835 qmp_inject_nmi(&errp);
836 hmp_handle_error(mon, &errp);
839 void hmp_set_link(Monitor *mon, const QDict *qdict)
841 const char *name = qdict_get_str(qdict, "name");
842 int up = qdict_get_bool(qdict, "up");
843 Error *errp = NULL;
845 qmp_set_link(name, up, &errp);
846 hmp_handle_error(mon, &errp);
849 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
851 const char *device = qdict_get_str(qdict, "device");
852 const char *password = qdict_get_str(qdict, "password");
853 Error *errp = NULL;
855 qmp_block_passwd(device, password, &errp);
856 hmp_handle_error(mon, &errp);
859 void hmp_balloon(Monitor *mon, const QDict *qdict)
861 int64_t value = qdict_get_int(qdict, "value");
862 Error *errp = NULL;
864 qmp_balloon(value, &errp);
865 if (error_is_set(&errp)) {
866 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
867 error_free(errp);
871 void hmp_block_resize(Monitor *mon, const QDict *qdict)
873 const char *device = qdict_get_str(qdict, "device");
874 int64_t size = qdict_get_int(qdict, "size");
875 Error *errp = NULL;
877 qmp_block_resize(device, size, &errp);
878 hmp_handle_error(mon, &errp);
881 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
883 const char *device = qdict_get_str(qdict, "device");
884 const char *filename = qdict_get_str(qdict, "target");
885 const char *format = qdict_get_try_str(qdict, "format");
886 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
887 int full = qdict_get_try_bool(qdict, "full", 0);
888 enum NewImageMode mode;
889 Error *errp = NULL;
891 if (!filename) {
892 error_set(&errp, QERR_MISSING_PARAMETER, "target");
893 hmp_handle_error(mon, &errp);
894 return;
897 if (reuse) {
898 mode = NEW_IMAGE_MODE_EXISTING;
899 } else {
900 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
903 qmp_drive_mirror(device, filename, !!format, format,
904 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
905 true, mode, false, 0, false, 0, false, 0,
906 false, 0, false, 0, &errp);
907 hmp_handle_error(mon, &errp);
910 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
912 const char *device = qdict_get_str(qdict, "device");
913 const char *filename = qdict_get_str(qdict, "target");
914 const char *format = qdict_get_try_str(qdict, "format");
915 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
916 int full = qdict_get_try_bool(qdict, "full", 0);
917 enum NewImageMode mode;
918 Error *errp = NULL;
920 if (!filename) {
921 error_set(&errp, QERR_MISSING_PARAMETER, "target");
922 hmp_handle_error(mon, &errp);
923 return;
926 if (reuse) {
927 mode = NEW_IMAGE_MODE_EXISTING;
928 } else {
929 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
932 qmp_drive_backup(device, filename, !!format, format,
933 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
934 true, mode, false, 0, false, 0, false, 0, &errp);
935 hmp_handle_error(mon, &errp);
938 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
940 const char *device = qdict_get_str(qdict, "device");
941 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
942 const char *format = qdict_get_try_str(qdict, "format");
943 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
944 enum NewImageMode mode;
945 Error *errp = NULL;
947 if (!filename) {
948 /* In the future, if 'snapshot-file' is not specified, the snapshot
949 will be taken internally. Today it's actually required. */
950 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
951 hmp_handle_error(mon, &errp);
952 return;
955 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
956 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
957 true, mode, &errp);
958 hmp_handle_error(mon, &errp);
961 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
963 qmp_migrate_cancel(NULL);
966 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
968 double value = qdict_get_double(qdict, "value");
969 qmp_migrate_set_downtime(value, NULL);
972 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
974 int64_t value = qdict_get_int(qdict, "value");
975 Error *err = NULL;
977 qmp_migrate_set_cache_size(value, &err);
978 if (err) {
979 monitor_printf(mon, "%s\n", error_get_pretty(err));
980 error_free(err);
981 return;
985 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
987 int64_t value = qdict_get_int(qdict, "value");
988 qmp_migrate_set_speed(value, NULL);
991 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
993 const char *cap = qdict_get_str(qdict, "capability");
994 bool state = qdict_get_bool(qdict, "state");
995 Error *err = NULL;
996 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
997 int i;
999 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
1000 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1001 caps->value = g_malloc0(sizeof(*caps->value));
1002 caps->value->capability = i;
1003 caps->value->state = state;
1004 caps->next = NULL;
1005 qmp_migrate_set_capabilities(caps, &err);
1006 break;
1010 if (i == MIGRATION_CAPABILITY_MAX) {
1011 error_set(&err, QERR_INVALID_PARAMETER, cap);
1014 qapi_free_MigrationCapabilityStatusList(caps);
1016 if (err) {
1017 monitor_printf(mon, "migrate_set_capability: %s\n",
1018 error_get_pretty(err));
1019 error_free(err);
1023 void hmp_set_password(Monitor *mon, const QDict *qdict)
1025 const char *protocol = qdict_get_str(qdict, "protocol");
1026 const char *password = qdict_get_str(qdict, "password");
1027 const char *connected = qdict_get_try_str(qdict, "connected");
1028 Error *err = NULL;
1030 qmp_set_password(protocol, password, !!connected, connected, &err);
1031 hmp_handle_error(mon, &err);
1034 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1036 const char *protocol = qdict_get_str(qdict, "protocol");
1037 const char *whenstr = qdict_get_str(qdict, "time");
1038 Error *err = NULL;
1040 qmp_expire_password(protocol, whenstr, &err);
1041 hmp_handle_error(mon, &err);
1044 void hmp_eject(Monitor *mon, const QDict *qdict)
1046 int force = qdict_get_try_bool(qdict, "force", 0);
1047 const char *device = qdict_get_str(qdict, "device");
1048 Error *err = NULL;
1050 qmp_eject(device, true, force, &err);
1051 hmp_handle_error(mon, &err);
1054 static void hmp_change_read_arg(Monitor *mon, const char *password,
1055 void *opaque)
1057 qmp_change_vnc_password(password, NULL);
1058 monitor_read_command(mon, 1);
1061 void hmp_change(Monitor *mon, const QDict *qdict)
1063 const char *device = qdict_get_str(qdict, "device");
1064 const char *target = qdict_get_str(qdict, "target");
1065 const char *arg = qdict_get_try_str(qdict, "arg");
1066 Error *err = NULL;
1068 if (strcmp(device, "vnc") == 0 &&
1069 (strcmp(target, "passwd") == 0 ||
1070 strcmp(target, "password") == 0)) {
1071 if (!arg) {
1072 monitor_read_password(mon, hmp_change_read_arg, NULL);
1073 return;
1077 qmp_change(device, target, !!arg, arg, &err);
1078 if (error_is_set(&err) &&
1079 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1080 error_free(err);
1081 monitor_read_block_device_key(mon, device, NULL, NULL);
1082 return;
1084 hmp_handle_error(mon, &err);
1087 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1089 Error *err = NULL;
1091 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1092 qdict_get_int(qdict, "bps"),
1093 qdict_get_int(qdict, "bps_rd"),
1094 qdict_get_int(qdict, "bps_wr"),
1095 qdict_get_int(qdict, "iops"),
1096 qdict_get_int(qdict, "iops_rd"),
1097 qdict_get_int(qdict, "iops_wr"), &err);
1098 hmp_handle_error(mon, &err);
1101 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1103 Error *error = NULL;
1104 const char *device = qdict_get_str(qdict, "device");
1105 const char *base = qdict_get_try_str(qdict, "base");
1106 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1108 qmp_block_stream(device, base != NULL, base,
1109 qdict_haskey(qdict, "speed"), speed,
1110 BLOCKDEV_ON_ERROR_REPORT, true, &error);
1112 hmp_handle_error(mon, &error);
1115 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1117 Error *error = NULL;
1118 const char *device = qdict_get_str(qdict, "device");
1119 int64_t value = qdict_get_int(qdict, "speed");
1121 qmp_block_job_set_speed(device, value, &error);
1123 hmp_handle_error(mon, &error);
1126 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1128 Error *error = NULL;
1129 const char *device = qdict_get_str(qdict, "device");
1130 bool force = qdict_get_try_bool(qdict, "force", 0);
1132 qmp_block_job_cancel(device, true, force, &error);
1134 hmp_handle_error(mon, &error);
1137 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1139 Error *error = NULL;
1140 const char *device = qdict_get_str(qdict, "device");
1142 qmp_block_job_pause(device, &error);
1144 hmp_handle_error(mon, &error);
1147 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1149 Error *error = NULL;
1150 const char *device = qdict_get_str(qdict, "device");
1152 qmp_block_job_resume(device, &error);
1154 hmp_handle_error(mon, &error);
1157 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1159 Error *error = NULL;
1160 const char *device = qdict_get_str(qdict, "device");
1162 qmp_block_job_complete(device, &error);
1164 hmp_handle_error(mon, &error);
1167 typedef struct MigrationStatus
1169 QEMUTimer *timer;
1170 Monitor *mon;
1171 bool is_block_migration;
1172 } MigrationStatus;
1174 static void hmp_migrate_status_cb(void *opaque)
1176 MigrationStatus *status = opaque;
1177 MigrationInfo *info;
1179 info = qmp_query_migrate(NULL);
1180 if (!info->has_status || strcmp(info->status, "active") == 0) {
1181 if (info->has_disk) {
1182 int progress;
1184 if (info->disk->remaining) {
1185 progress = info->disk->transferred * 100 / info->disk->total;
1186 } else {
1187 progress = 100;
1190 monitor_printf(status->mon, "Completed %d %%\r", progress);
1191 monitor_flush(status->mon);
1194 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1195 } else {
1196 if (status->is_block_migration) {
1197 monitor_printf(status->mon, "\n");
1199 monitor_resume(status->mon);
1200 qemu_del_timer(status->timer);
1201 g_free(status);
1204 qapi_free_MigrationInfo(info);
1207 void hmp_migrate(Monitor *mon, const QDict *qdict)
1209 int detach = qdict_get_try_bool(qdict, "detach", 0);
1210 int blk = qdict_get_try_bool(qdict, "blk", 0);
1211 int inc = qdict_get_try_bool(qdict, "inc", 0);
1212 const char *uri = qdict_get_str(qdict, "uri");
1213 Error *err = NULL;
1215 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1216 if (err) {
1217 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1218 error_free(err);
1219 return;
1222 if (!detach) {
1223 MigrationStatus *status;
1225 if (monitor_suspend(mon) < 0) {
1226 monitor_printf(mon, "terminal does not allow synchronous "
1227 "migration, continuing detached\n");
1228 return;
1231 status = g_malloc0(sizeof(*status));
1232 status->mon = mon;
1233 status->is_block_migration = blk || inc;
1234 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1235 status);
1236 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1240 void hmp_device_del(Monitor *mon, const QDict *qdict)
1242 const char *id = qdict_get_str(qdict, "id");
1243 Error *err = NULL;
1245 qmp_device_del(id, &err);
1246 hmp_handle_error(mon, &err);
1249 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1251 Error *errp = NULL;
1252 int paging = qdict_get_try_bool(qdict, "paging", 0);
1253 const char *file = qdict_get_str(qdict, "filename");
1254 bool has_begin = qdict_haskey(qdict, "begin");
1255 bool has_length = qdict_haskey(qdict, "length");
1256 int64_t begin = 0;
1257 int64_t length = 0;
1258 char *prot;
1260 if (has_begin) {
1261 begin = qdict_get_int(qdict, "begin");
1263 if (has_length) {
1264 length = qdict_get_int(qdict, "length");
1267 prot = g_strconcat("file:", file, NULL);
1269 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1270 &errp);
1271 hmp_handle_error(mon, &errp);
1272 g_free(prot);
1275 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1277 Error *err = NULL;
1278 QemuOpts *opts;
1280 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1281 if (error_is_set(&err)) {
1282 goto out;
1285 netdev_add(opts, &err);
1286 if (error_is_set(&err)) {
1287 qemu_opts_del(opts);
1290 out:
1291 hmp_handle_error(mon, &err);
1294 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1296 const char *id = qdict_get_str(qdict, "id");
1297 Error *err = NULL;
1299 qmp_netdev_del(id, &err);
1300 hmp_handle_error(mon, &err);
1303 void hmp_getfd(Monitor *mon, const QDict *qdict)
1305 const char *fdname = qdict_get_str(qdict, "fdname");
1306 Error *errp = NULL;
1308 qmp_getfd(fdname, &errp);
1309 hmp_handle_error(mon, &errp);
1312 void hmp_closefd(Monitor *mon, const QDict *qdict)
1314 const char *fdname = qdict_get_str(qdict, "fdname");
1315 Error *errp = NULL;
1317 qmp_closefd(fdname, &errp);
1318 hmp_handle_error(mon, &errp);
1321 void hmp_send_key(Monitor *mon, const QDict *qdict)
1323 const char *keys = qdict_get_str(qdict, "keys");
1324 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1325 int has_hold_time = qdict_haskey(qdict, "hold-time");
1326 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1327 Error *err = NULL;
1328 char keyname_buf[16];
1329 char *separator;
1330 int keyname_len;
1332 while (1) {
1333 separator = strchr(keys, '-');
1334 keyname_len = separator ? separator - keys : strlen(keys);
1335 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1337 /* Be compatible with old interface, convert user inputted "<" */
1338 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1339 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1340 keyname_len = 4;
1342 keyname_buf[keyname_len] = 0;
1344 keylist = g_malloc0(sizeof(*keylist));
1345 keylist->value = g_malloc0(sizeof(*keylist->value));
1347 if (!head) {
1348 head = keylist;
1350 if (tmp) {
1351 tmp->next = keylist;
1353 tmp = keylist;
1355 if (strstart(keyname_buf, "0x", NULL)) {
1356 char *endp;
1357 int value = strtoul(keyname_buf, &endp, 0);
1358 if (*endp != '\0') {
1359 goto err_out;
1361 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1362 keylist->value->number = value;
1363 } else {
1364 int idx = index_from_key(keyname_buf);
1365 if (idx == Q_KEY_CODE_MAX) {
1366 goto err_out;
1368 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1369 keylist->value->qcode = idx;
1372 if (!separator) {
1373 break;
1375 keys = separator + 1;
1378 qmp_send_key(head, has_hold_time, hold_time, &err);
1379 hmp_handle_error(mon, &err);
1381 out:
1382 qapi_free_KeyValueList(head);
1383 return;
1385 err_out:
1386 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1387 goto out;
1390 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1392 const char *filename = qdict_get_str(qdict, "filename");
1393 Error *err = NULL;
1395 qmp_screendump(filename, &err);
1396 hmp_handle_error(mon, &err);
1399 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1401 const char *uri = qdict_get_str(qdict, "uri");
1402 int writable = qdict_get_try_bool(qdict, "writable", 0);
1403 int all = qdict_get_try_bool(qdict, "all", 0);
1404 Error *local_err = NULL;
1405 BlockInfoList *block_list, *info;
1406 SocketAddress *addr;
1408 if (writable && !all) {
1409 error_setg(&local_err, "-w only valid together with -a");
1410 goto exit;
1413 /* First check if the address is valid and start the server. */
1414 addr = socket_parse(uri, &local_err);
1415 if (local_err != NULL) {
1416 goto exit;
1419 qmp_nbd_server_start(addr, &local_err);
1420 qapi_free_SocketAddress(addr);
1421 if (local_err != NULL) {
1422 goto exit;
1425 if (!all) {
1426 return;
1429 /* Then try adding all block devices. If one fails, close all and
1430 * exit.
1432 block_list = qmp_query_block(NULL);
1434 for (info = block_list; info; info = info->next) {
1435 if (!info->value->has_inserted) {
1436 continue;
1439 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1441 if (local_err != NULL) {
1442 qmp_nbd_server_stop(NULL);
1443 break;
1447 qapi_free_BlockInfoList(block_list);
1449 exit:
1450 hmp_handle_error(mon, &local_err);
1453 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1455 const char *device = qdict_get_str(qdict, "device");
1456 int writable = qdict_get_try_bool(qdict, "writable", 0);
1457 Error *local_err = NULL;
1459 qmp_nbd_server_add(device, true, writable, &local_err);
1461 if (local_err != NULL) {
1462 hmp_handle_error(mon, &local_err);
1466 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1468 Error *errp = NULL;
1470 qmp_nbd_server_stop(&errp);
1471 hmp_handle_error(mon, &errp);
1474 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1476 const char *args = qdict_get_str(qdict, "args");
1477 Error *err = NULL;
1478 QemuOpts *opts;
1480 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1481 if (opts == NULL) {
1482 error_setg(&err, "Parsing chardev args failed");
1483 } else {
1484 qemu_chr_new_from_opts(opts, NULL, &err);
1486 hmp_handle_error(mon, &err);
1489 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1491 Error *local_err = NULL;
1493 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1494 hmp_handle_error(mon, &local_err);
1497 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1499 BlockDriverState *bs;
1500 const char* device = qdict_get_str(qdict, "device");
1501 const char* command = qdict_get_str(qdict, "command");
1502 Error *err = NULL;
1504 bs = bdrv_find(device);
1505 if (bs) {
1506 qemuio_command(bs, command);
1507 } else {
1508 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1511 hmp_handle_error(mon, &err);