virtio-ccw: fix build breakage on windows
[qemu/ar7.git] / hmp.c
blob148a3fbbd626aba9bc5c2ca1eedc235daf743f37
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;
294 monitor_printf(mon, "%s: removable=%d",
295 info->value->device, info->value->removable);
297 if (info->value->removable) {
298 monitor_printf(mon, " locked=%d", info->value->locked);
299 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
302 if (info->value->has_io_status) {
303 monitor_printf(mon, " io-status=%s",
304 BlockDeviceIoStatus_lookup[info->value->io_status]);
307 if (info->value->has_inserted) {
308 monitor_printf(mon, " file=");
309 monitor_print_filename(mon, info->value->inserted->file);
311 if (info->value->inserted->has_backing_file) {
312 monitor_printf(mon, " backing_file=");
313 monitor_print_filename(mon, info->value->inserted->backing_file);
314 monitor_printf(mon, " backing_file_depth=%" PRId64,
315 info->value->inserted->backing_file_depth);
317 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
318 info->value->inserted->ro,
319 info->value->inserted->drv,
320 info->value->inserted->encrypted);
322 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
323 " bps_wr=%" PRId64 " iops=%" PRId64
324 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
325 info->value->inserted->bps,
326 info->value->inserted->bps_rd,
327 info->value->inserted->bps_wr,
328 info->value->inserted->iops,
329 info->value->inserted->iops_rd,
330 info->value->inserted->iops_wr);
332 if (verbose) {
333 monitor_printf(mon, " images:\n");
334 image_info = info->value->inserted->image;
335 while (1) {
336 bdrv_image_info_dump((fprintf_function)monitor_printf,
337 mon, image_info);
338 if (image_info->has_backing_image) {
339 image_info = image_info->backing_image;
340 } else {
341 break;
345 } else {
346 monitor_printf(mon, " [not inserted]");
349 monitor_printf(mon, "\n");
352 qapi_free_BlockInfoList(block_list);
355 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
357 BlockStatsList *stats_list, *stats;
359 stats_list = qmp_query_blockstats(NULL);
361 for (stats = stats_list; stats; stats = stats->next) {
362 if (!stats->value->has_device) {
363 continue;
366 monitor_printf(mon, "%s:", stats->value->device);
367 monitor_printf(mon, " rd_bytes=%" PRId64
368 " wr_bytes=%" PRId64
369 " rd_operations=%" PRId64
370 " wr_operations=%" PRId64
371 " flush_operations=%" PRId64
372 " wr_total_time_ns=%" PRId64
373 " rd_total_time_ns=%" PRId64
374 " flush_total_time_ns=%" PRId64
375 "\n",
376 stats->value->stats->rd_bytes,
377 stats->value->stats->wr_bytes,
378 stats->value->stats->rd_operations,
379 stats->value->stats->wr_operations,
380 stats->value->stats->flush_operations,
381 stats->value->stats->wr_total_time_ns,
382 stats->value->stats->rd_total_time_ns,
383 stats->value->stats->flush_total_time_ns);
386 qapi_free_BlockStatsList(stats_list);
389 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
391 VncInfo *info;
392 Error *err = NULL;
393 VncClientInfoList *client;
395 info = qmp_query_vnc(&err);
396 if (err) {
397 monitor_printf(mon, "%s\n", error_get_pretty(err));
398 error_free(err);
399 return;
402 if (!info->enabled) {
403 monitor_printf(mon, "Server: disabled\n");
404 goto out;
407 monitor_printf(mon, "Server:\n");
408 if (info->has_host && info->has_service) {
409 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
411 if (info->has_auth) {
412 monitor_printf(mon, " auth: %s\n", info->auth);
415 if (!info->has_clients || info->clients == NULL) {
416 monitor_printf(mon, "Client: none\n");
417 } else {
418 for (client = info->clients; client; client = client->next) {
419 monitor_printf(mon, "Client:\n");
420 monitor_printf(mon, " address: %s:%s\n",
421 client->value->host, client->value->service);
422 monitor_printf(mon, " x509_dname: %s\n",
423 client->value->x509_dname ?
424 client->value->x509_dname : "none");
425 monitor_printf(mon, " username: %s\n",
426 client->value->has_sasl_username ?
427 client->value->sasl_username : "none");
431 out:
432 qapi_free_VncInfo(info);
435 void hmp_info_spice(Monitor *mon, const QDict *qdict)
437 SpiceChannelList *chan;
438 SpiceInfo *info;
440 info = qmp_query_spice(NULL);
442 if (!info->enabled) {
443 monitor_printf(mon, "Server: disabled\n");
444 goto out;
447 monitor_printf(mon, "Server:\n");
448 if (info->has_port) {
449 monitor_printf(mon, " address: %s:%" PRId64 "\n",
450 info->host, info->port);
452 if (info->has_tls_port) {
453 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
454 info->host, info->tls_port);
456 monitor_printf(mon, " migrated: %s\n",
457 info->migrated ? "true" : "false");
458 monitor_printf(mon, " auth: %s\n", info->auth);
459 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
460 monitor_printf(mon, " mouse-mode: %s\n",
461 SpiceQueryMouseMode_lookup[info->mouse_mode]);
463 if (!info->has_channels || info->channels == NULL) {
464 monitor_printf(mon, "Channels: none\n");
465 } else {
466 for (chan = info->channels; chan; chan = chan->next) {
467 monitor_printf(mon, "Channel:\n");
468 monitor_printf(mon, " address: %s:%s%s\n",
469 chan->value->host, chan->value->port,
470 chan->value->tls ? " [tls]" : "");
471 monitor_printf(mon, " session: %" PRId64 "\n",
472 chan->value->connection_id);
473 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
474 chan->value->channel_type, chan->value->channel_id);
478 out:
479 qapi_free_SpiceInfo(info);
482 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
484 BalloonInfo *info;
485 Error *err = NULL;
487 info = qmp_query_balloon(&err);
488 if (err) {
489 monitor_printf(mon, "%s\n", error_get_pretty(err));
490 error_free(err);
491 return;
494 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
496 qapi_free_BalloonInfo(info);
499 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
501 PciMemoryRegionList *region;
503 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
504 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
505 dev->slot, dev->function);
506 monitor_printf(mon, " ");
508 if (dev->class_info.has_desc) {
509 monitor_printf(mon, "%s", dev->class_info.desc);
510 } else {
511 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
514 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
515 dev->id.vendor, dev->id.device);
517 if (dev->has_irq) {
518 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
521 if (dev->has_pci_bridge) {
522 monitor_printf(mon, " BUS %" PRId64 ".\n",
523 dev->pci_bridge->bus.number);
524 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
525 dev->pci_bridge->bus.secondary);
526 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
527 dev->pci_bridge->bus.subordinate);
529 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
530 dev->pci_bridge->bus.io_range->base,
531 dev->pci_bridge->bus.io_range->limit);
533 monitor_printf(mon,
534 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
535 dev->pci_bridge->bus.memory_range->base,
536 dev->pci_bridge->bus.memory_range->limit);
538 monitor_printf(mon, " prefetchable memory range "
539 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
540 dev->pci_bridge->bus.prefetchable_range->base,
541 dev->pci_bridge->bus.prefetchable_range->limit);
544 for (region = dev->regions; region; region = region->next) {
545 uint64_t addr, size;
547 addr = region->value->address;
548 size = region->value->size;
550 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
552 if (!strcmp(region->value->type, "io")) {
553 monitor_printf(mon, "I/O at 0x%04" PRIx64
554 " [0x%04" PRIx64 "].\n",
555 addr, addr + size - 1);
556 } else {
557 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
558 " [0x%08" PRIx64 "].\n",
559 region->value->mem_type_64 ? 64 : 32,
560 region->value->prefetch ? " prefetchable" : "",
561 addr, addr + size - 1);
565 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
567 if (dev->has_pci_bridge) {
568 if (dev->pci_bridge->has_devices) {
569 PciDeviceInfoList *cdev;
570 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
571 hmp_info_pci_device(mon, cdev->value);
577 void hmp_info_pci(Monitor *mon, const QDict *qdict)
579 PciInfoList *info_list, *info;
580 Error *err = NULL;
582 info_list = qmp_query_pci(&err);
583 if (err) {
584 monitor_printf(mon, "PCI devices not supported\n");
585 error_free(err);
586 return;
589 for (info = info_list; info; info = info->next) {
590 PciDeviceInfoList *dev;
592 for (dev = info->value->devices; dev; dev = dev->next) {
593 hmp_info_pci_device(mon, dev->value);
597 qapi_free_PciInfoList(info_list);
600 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
602 BlockJobInfoList *list;
603 Error *err = NULL;
605 list = qmp_query_block_jobs(&err);
606 assert(!err);
608 if (!list) {
609 monitor_printf(mon, "No active jobs\n");
610 return;
613 while (list) {
614 if (strcmp(list->value->type, "stream") == 0) {
615 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
616 " of %" PRId64 " bytes, speed limit %" PRId64
617 " bytes/s\n",
618 list->value->device,
619 list->value->offset,
620 list->value->len,
621 list->value->speed);
622 } else {
623 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
624 " of %" PRId64 " bytes, speed limit %" PRId64
625 " bytes/s\n",
626 list->value->type,
627 list->value->device,
628 list->value->offset,
629 list->value->len,
630 list->value->speed);
632 list = list->next;
636 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
638 TPMInfoList *info_list, *info;
639 Error *err = NULL;
640 unsigned int c = 0;
641 TPMPassthroughOptions *tpo;
643 info_list = qmp_query_tpm(&err);
644 if (err) {
645 monitor_printf(mon, "TPM device not supported\n");
646 error_free(err);
647 return;
650 if (info_list) {
651 monitor_printf(mon, "TPM device:\n");
654 for (info = info_list; info; info = info->next) {
655 TPMInfo *ti = info->value;
656 monitor_printf(mon, " tpm%d: model=%s\n",
657 c, TpmModel_lookup[ti->model]);
659 monitor_printf(mon, " \\ %s: type=%s",
660 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
662 switch (ti->options->kind) {
663 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
664 tpo = ti->options->passthrough;
665 monitor_printf(mon, "%s%s%s%s",
666 tpo->has_path ? ",path=" : "",
667 tpo->has_path ? tpo->path : "",
668 tpo->has_cancel_path ? ",cancel-path=" : "",
669 tpo->has_cancel_path ? tpo->cancel_path : "");
670 break;
671 case TPM_TYPE_OPTIONS_KIND_MAX:
672 break;
674 monitor_printf(mon, "\n");
675 c++;
677 qapi_free_TPMInfoList(info_list);
680 void hmp_quit(Monitor *mon, const QDict *qdict)
682 monitor_suspend(mon);
683 qmp_quit(NULL);
686 void hmp_stop(Monitor *mon, const QDict *qdict)
688 qmp_stop(NULL);
691 void hmp_system_reset(Monitor *mon, const QDict *qdict)
693 qmp_system_reset(NULL);
696 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
698 qmp_system_powerdown(NULL);
701 void hmp_cpu(Monitor *mon, const QDict *qdict)
703 int64_t cpu_index;
705 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
706 use it are converted to the QAPI */
707 cpu_index = qdict_get_int(qdict, "index");
708 if (monitor_set_cpu(cpu_index) < 0) {
709 monitor_printf(mon, "invalid CPU index\n");
713 void hmp_memsave(Monitor *mon, const QDict *qdict)
715 uint32_t size = qdict_get_int(qdict, "size");
716 const char *filename = qdict_get_str(qdict, "filename");
717 uint64_t addr = qdict_get_int(qdict, "val");
718 Error *errp = NULL;
720 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
721 hmp_handle_error(mon, &errp);
724 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
726 uint32_t size = qdict_get_int(qdict, "size");
727 const char *filename = qdict_get_str(qdict, "filename");
728 uint64_t addr = qdict_get_int(qdict, "val");
729 Error *errp = NULL;
731 qmp_pmemsave(addr, size, filename, &errp);
732 hmp_handle_error(mon, &errp);
735 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
737 const char *chardev = qdict_get_str(qdict, "device");
738 const char *data = qdict_get_str(qdict, "data");
739 Error *errp = NULL;
741 qmp_ringbuf_write(chardev, data, false, 0, &errp);
743 hmp_handle_error(mon, &errp);
746 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
748 uint32_t size = qdict_get_int(qdict, "size");
749 const char *chardev = qdict_get_str(qdict, "device");
750 char *data;
751 Error *errp = NULL;
752 int i;
754 data = qmp_ringbuf_read(chardev, size, false, 0, &errp);
755 if (errp) {
756 monitor_printf(mon, "%s\n", error_get_pretty(errp));
757 error_free(errp);
758 return;
761 for (i = 0; data[i]; i++) {
762 unsigned char ch = data[i];
764 if (ch == '\\') {
765 monitor_printf(mon, "\\\\");
766 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
767 monitor_printf(mon, "\\u%04X", ch);
768 } else {
769 monitor_printf(mon, "%c", ch);
773 monitor_printf(mon, "\n");
774 g_free(data);
777 static void hmp_cont_cb(void *opaque, int err)
779 if (!err) {
780 qmp_cont(NULL);
784 static bool key_is_missing(const BlockInfo *bdev)
786 return (bdev->inserted && bdev->inserted->encryption_key_missing);
789 void hmp_cont(Monitor *mon, const QDict *qdict)
791 BlockInfoList *bdev_list, *bdev;
792 Error *errp = NULL;
794 bdev_list = qmp_query_block(NULL);
795 for (bdev = bdev_list; bdev; bdev = bdev->next) {
796 if (key_is_missing(bdev->value)) {
797 monitor_read_block_device_key(mon, bdev->value->device,
798 hmp_cont_cb, NULL);
799 goto out;
803 qmp_cont(&errp);
804 hmp_handle_error(mon, &errp);
806 out:
807 qapi_free_BlockInfoList(bdev_list);
810 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
812 qmp_system_wakeup(NULL);
815 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
817 Error *errp = NULL;
819 qmp_inject_nmi(&errp);
820 hmp_handle_error(mon, &errp);
823 void hmp_set_link(Monitor *mon, const QDict *qdict)
825 const char *name = qdict_get_str(qdict, "name");
826 int up = qdict_get_bool(qdict, "up");
827 Error *errp = NULL;
829 qmp_set_link(name, up, &errp);
830 hmp_handle_error(mon, &errp);
833 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
835 const char *device = qdict_get_str(qdict, "device");
836 const char *password = qdict_get_str(qdict, "password");
837 Error *errp = NULL;
839 qmp_block_passwd(device, password, &errp);
840 hmp_handle_error(mon, &errp);
843 void hmp_balloon(Monitor *mon, const QDict *qdict)
845 int64_t value = qdict_get_int(qdict, "value");
846 Error *errp = NULL;
848 qmp_balloon(value, &errp);
849 if (error_is_set(&errp)) {
850 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
851 error_free(errp);
855 void hmp_block_resize(Monitor *mon, const QDict *qdict)
857 const char *device = qdict_get_str(qdict, "device");
858 int64_t size = qdict_get_int(qdict, "size");
859 Error *errp = NULL;
861 qmp_block_resize(device, size, &errp);
862 hmp_handle_error(mon, &errp);
865 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
867 const char *device = qdict_get_str(qdict, "device");
868 const char *filename = qdict_get_str(qdict, "target");
869 const char *format = qdict_get_try_str(qdict, "format");
870 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
871 int full = qdict_get_try_bool(qdict, "full", 0);
872 enum NewImageMode mode;
873 Error *errp = NULL;
875 if (!filename) {
876 error_set(&errp, QERR_MISSING_PARAMETER, "target");
877 hmp_handle_error(mon, &errp);
878 return;
881 if (reuse) {
882 mode = NEW_IMAGE_MODE_EXISTING;
883 } else {
884 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
887 qmp_drive_mirror(device, filename, !!format, format,
888 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
889 true, mode, false, 0, false, 0, false, 0,
890 false, 0, false, 0, &errp);
891 hmp_handle_error(mon, &errp);
894 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
896 const char *device = qdict_get_str(qdict, "device");
897 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
898 const char *format = qdict_get_try_str(qdict, "format");
899 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
900 enum NewImageMode mode;
901 Error *errp = NULL;
903 if (!filename) {
904 /* In the future, if 'snapshot-file' is not specified, the snapshot
905 will be taken internally. Today it's actually required. */
906 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
907 hmp_handle_error(mon, &errp);
908 return;
911 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
912 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
913 true, mode, &errp);
914 hmp_handle_error(mon, &errp);
917 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
919 qmp_migrate_cancel(NULL);
922 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
924 double value = qdict_get_double(qdict, "value");
925 qmp_migrate_set_downtime(value, NULL);
928 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
930 int64_t value = qdict_get_int(qdict, "value");
931 Error *err = NULL;
933 qmp_migrate_set_cache_size(value, &err);
934 if (err) {
935 monitor_printf(mon, "%s\n", error_get_pretty(err));
936 error_free(err);
937 return;
941 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
943 int64_t value = qdict_get_int(qdict, "value");
944 qmp_migrate_set_speed(value, NULL);
947 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
949 const char *cap = qdict_get_str(qdict, "capability");
950 bool state = qdict_get_bool(qdict, "state");
951 Error *err = NULL;
952 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
953 int i;
955 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
956 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
957 caps->value = g_malloc0(sizeof(*caps->value));
958 caps->value->capability = i;
959 caps->value->state = state;
960 caps->next = NULL;
961 qmp_migrate_set_capabilities(caps, &err);
962 break;
966 if (i == MIGRATION_CAPABILITY_MAX) {
967 error_set(&err, QERR_INVALID_PARAMETER, cap);
970 qapi_free_MigrationCapabilityStatusList(caps);
972 if (err) {
973 monitor_printf(mon, "migrate_set_capability: %s\n",
974 error_get_pretty(err));
975 error_free(err);
979 void hmp_set_password(Monitor *mon, const QDict *qdict)
981 const char *protocol = qdict_get_str(qdict, "protocol");
982 const char *password = qdict_get_str(qdict, "password");
983 const char *connected = qdict_get_try_str(qdict, "connected");
984 Error *err = NULL;
986 qmp_set_password(protocol, password, !!connected, connected, &err);
987 hmp_handle_error(mon, &err);
990 void hmp_expire_password(Monitor *mon, const QDict *qdict)
992 const char *protocol = qdict_get_str(qdict, "protocol");
993 const char *whenstr = qdict_get_str(qdict, "time");
994 Error *err = NULL;
996 qmp_expire_password(protocol, whenstr, &err);
997 hmp_handle_error(mon, &err);
1000 void hmp_eject(Monitor *mon, const QDict *qdict)
1002 int force = qdict_get_try_bool(qdict, "force", 0);
1003 const char *device = qdict_get_str(qdict, "device");
1004 Error *err = NULL;
1006 qmp_eject(device, true, force, &err);
1007 hmp_handle_error(mon, &err);
1010 static void hmp_change_read_arg(Monitor *mon, const char *password,
1011 void *opaque)
1013 qmp_change_vnc_password(password, NULL);
1014 monitor_read_command(mon, 1);
1017 void hmp_change(Monitor *mon, const QDict *qdict)
1019 const char *device = qdict_get_str(qdict, "device");
1020 const char *target = qdict_get_str(qdict, "target");
1021 const char *arg = qdict_get_try_str(qdict, "arg");
1022 Error *err = NULL;
1024 if (strcmp(device, "vnc") == 0 &&
1025 (strcmp(target, "passwd") == 0 ||
1026 strcmp(target, "password") == 0)) {
1027 if (!arg) {
1028 monitor_read_password(mon, hmp_change_read_arg, NULL);
1029 return;
1033 qmp_change(device, target, !!arg, arg, &err);
1034 if (error_is_set(&err) &&
1035 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1036 error_free(err);
1037 monitor_read_block_device_key(mon, device, NULL, NULL);
1038 return;
1040 hmp_handle_error(mon, &err);
1043 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1045 Error *err = NULL;
1047 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1048 qdict_get_int(qdict, "bps"),
1049 qdict_get_int(qdict, "bps_rd"),
1050 qdict_get_int(qdict, "bps_wr"),
1051 qdict_get_int(qdict, "iops"),
1052 qdict_get_int(qdict, "iops_rd"),
1053 qdict_get_int(qdict, "iops_wr"), &err);
1054 hmp_handle_error(mon, &err);
1057 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1059 Error *error = NULL;
1060 const char *device = qdict_get_str(qdict, "device");
1061 const char *base = qdict_get_try_str(qdict, "base");
1062 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1064 qmp_block_stream(device, base != NULL, base,
1065 qdict_haskey(qdict, "speed"), speed,
1066 BLOCKDEV_ON_ERROR_REPORT, true, &error);
1068 hmp_handle_error(mon, &error);
1071 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1073 Error *error = NULL;
1074 const char *device = qdict_get_str(qdict, "device");
1075 int64_t value = qdict_get_int(qdict, "speed");
1077 qmp_block_job_set_speed(device, value, &error);
1079 hmp_handle_error(mon, &error);
1082 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1084 Error *error = NULL;
1085 const char *device = qdict_get_str(qdict, "device");
1086 bool force = qdict_get_try_bool(qdict, "force", 0);
1088 qmp_block_job_cancel(device, true, force, &error);
1090 hmp_handle_error(mon, &error);
1093 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1095 Error *error = NULL;
1096 const char *device = qdict_get_str(qdict, "device");
1098 qmp_block_job_pause(device, &error);
1100 hmp_handle_error(mon, &error);
1103 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1105 Error *error = NULL;
1106 const char *device = qdict_get_str(qdict, "device");
1108 qmp_block_job_resume(device, &error);
1110 hmp_handle_error(mon, &error);
1113 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1115 Error *error = NULL;
1116 const char *device = qdict_get_str(qdict, "device");
1118 qmp_block_job_complete(device, &error);
1120 hmp_handle_error(mon, &error);
1123 typedef struct MigrationStatus
1125 QEMUTimer *timer;
1126 Monitor *mon;
1127 bool is_block_migration;
1128 } MigrationStatus;
1130 static void hmp_migrate_status_cb(void *opaque)
1132 MigrationStatus *status = opaque;
1133 MigrationInfo *info;
1135 info = qmp_query_migrate(NULL);
1136 if (!info->has_status || strcmp(info->status, "active") == 0) {
1137 if (info->has_disk) {
1138 int progress;
1140 if (info->disk->remaining) {
1141 progress = info->disk->transferred * 100 / info->disk->total;
1142 } else {
1143 progress = 100;
1146 monitor_printf(status->mon, "Completed %d %%\r", progress);
1147 monitor_flush(status->mon);
1150 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1151 } else {
1152 if (status->is_block_migration) {
1153 monitor_printf(status->mon, "\n");
1155 monitor_resume(status->mon);
1156 qemu_del_timer(status->timer);
1157 g_free(status);
1160 qapi_free_MigrationInfo(info);
1163 void hmp_migrate(Monitor *mon, const QDict *qdict)
1165 int detach = qdict_get_try_bool(qdict, "detach", 0);
1166 int blk = qdict_get_try_bool(qdict, "blk", 0);
1167 int inc = qdict_get_try_bool(qdict, "inc", 0);
1168 const char *uri = qdict_get_str(qdict, "uri");
1169 Error *err = NULL;
1171 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1172 if (err) {
1173 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1174 error_free(err);
1175 return;
1178 if (!detach) {
1179 MigrationStatus *status;
1181 if (monitor_suspend(mon) < 0) {
1182 monitor_printf(mon, "terminal does not allow synchronous "
1183 "migration, continuing detached\n");
1184 return;
1187 status = g_malloc0(sizeof(*status));
1188 status->mon = mon;
1189 status->is_block_migration = blk || inc;
1190 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1191 status);
1192 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1196 void hmp_device_del(Monitor *mon, const QDict *qdict)
1198 const char *id = qdict_get_str(qdict, "id");
1199 Error *err = NULL;
1201 qmp_device_del(id, &err);
1202 hmp_handle_error(mon, &err);
1205 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1207 Error *errp = NULL;
1208 int paging = qdict_get_try_bool(qdict, "paging", 0);
1209 const char *file = qdict_get_str(qdict, "filename");
1210 bool has_begin = qdict_haskey(qdict, "begin");
1211 bool has_length = qdict_haskey(qdict, "length");
1212 int64_t begin = 0;
1213 int64_t length = 0;
1214 char *prot;
1216 if (has_begin) {
1217 begin = qdict_get_int(qdict, "begin");
1219 if (has_length) {
1220 length = qdict_get_int(qdict, "length");
1223 prot = g_strconcat("file:", file, NULL);
1225 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1226 &errp);
1227 hmp_handle_error(mon, &errp);
1228 g_free(prot);
1231 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1233 Error *err = NULL;
1234 QemuOpts *opts;
1236 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1237 if (error_is_set(&err)) {
1238 goto out;
1241 netdev_add(opts, &err);
1242 if (error_is_set(&err)) {
1243 qemu_opts_del(opts);
1246 out:
1247 hmp_handle_error(mon, &err);
1250 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1252 const char *id = qdict_get_str(qdict, "id");
1253 Error *err = NULL;
1255 qmp_netdev_del(id, &err);
1256 hmp_handle_error(mon, &err);
1259 void hmp_getfd(Monitor *mon, const QDict *qdict)
1261 const char *fdname = qdict_get_str(qdict, "fdname");
1262 Error *errp = NULL;
1264 qmp_getfd(fdname, &errp);
1265 hmp_handle_error(mon, &errp);
1268 void hmp_closefd(Monitor *mon, const QDict *qdict)
1270 const char *fdname = qdict_get_str(qdict, "fdname");
1271 Error *errp = NULL;
1273 qmp_closefd(fdname, &errp);
1274 hmp_handle_error(mon, &errp);
1277 void hmp_send_key(Monitor *mon, const QDict *qdict)
1279 const char *keys = qdict_get_str(qdict, "keys");
1280 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1281 int has_hold_time = qdict_haskey(qdict, "hold-time");
1282 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1283 Error *err = NULL;
1284 char keyname_buf[16];
1285 char *separator;
1286 int keyname_len;
1288 while (1) {
1289 separator = strchr(keys, '-');
1290 keyname_len = separator ? separator - keys : strlen(keys);
1291 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1293 /* Be compatible with old interface, convert user inputted "<" */
1294 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1295 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1296 keyname_len = 4;
1298 keyname_buf[keyname_len] = 0;
1300 keylist = g_malloc0(sizeof(*keylist));
1301 keylist->value = g_malloc0(sizeof(*keylist->value));
1303 if (!head) {
1304 head = keylist;
1306 if (tmp) {
1307 tmp->next = keylist;
1309 tmp = keylist;
1311 if (strstart(keyname_buf, "0x", NULL)) {
1312 char *endp;
1313 int value = strtoul(keyname_buf, &endp, 0);
1314 if (*endp != '\0') {
1315 goto err_out;
1317 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1318 keylist->value->number = value;
1319 } else {
1320 int idx = index_from_key(keyname_buf);
1321 if (idx == Q_KEY_CODE_MAX) {
1322 goto err_out;
1324 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1325 keylist->value->qcode = idx;
1328 if (!separator) {
1329 break;
1331 keys = separator + 1;
1334 qmp_send_key(head, has_hold_time, hold_time, &err);
1335 hmp_handle_error(mon, &err);
1337 out:
1338 qapi_free_KeyValueList(head);
1339 return;
1341 err_out:
1342 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1343 goto out;
1346 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1348 const char *filename = qdict_get_str(qdict, "filename");
1349 Error *err = NULL;
1351 qmp_screendump(filename, &err);
1352 hmp_handle_error(mon, &err);
1355 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1357 const char *uri = qdict_get_str(qdict, "uri");
1358 int writable = qdict_get_try_bool(qdict, "writable", 0);
1359 int all = qdict_get_try_bool(qdict, "all", 0);
1360 Error *local_err = NULL;
1361 BlockInfoList *block_list, *info;
1362 SocketAddress *addr;
1364 if (writable && !all) {
1365 error_setg(&local_err, "-w only valid together with -a");
1366 goto exit;
1369 /* First check if the address is valid and start the server. */
1370 addr = socket_parse(uri, &local_err);
1371 if (local_err != NULL) {
1372 goto exit;
1375 qmp_nbd_server_start(addr, &local_err);
1376 qapi_free_SocketAddress(addr);
1377 if (local_err != NULL) {
1378 goto exit;
1381 if (!all) {
1382 return;
1385 /* Then try adding all block devices. If one fails, close all and
1386 * exit.
1388 block_list = qmp_query_block(NULL);
1390 for (info = block_list; info; info = info->next) {
1391 if (!info->value->has_inserted) {
1392 continue;
1395 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1397 if (local_err != NULL) {
1398 qmp_nbd_server_stop(NULL);
1399 break;
1403 qapi_free_BlockInfoList(block_list);
1405 exit:
1406 hmp_handle_error(mon, &local_err);
1409 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1411 const char *device = qdict_get_str(qdict, "device");
1412 int writable = qdict_get_try_bool(qdict, "writable", 0);
1413 Error *local_err = NULL;
1415 qmp_nbd_server_add(device, true, writable, &local_err);
1417 if (local_err != NULL) {
1418 hmp_handle_error(mon, &local_err);
1422 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1424 Error *errp = NULL;
1426 qmp_nbd_server_stop(&errp);
1427 hmp_handle_error(mon, &errp);
1430 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1432 const char *args = qdict_get_str(qdict, "args");
1433 Error *err = NULL;
1434 QemuOpts *opts;
1436 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1437 if (opts == NULL) {
1438 error_setg(&err, "Parsing chardev args failed");
1439 } else {
1440 qemu_chr_new_from_opts(opts, NULL, &err);
1442 hmp_handle_error(mon, &err);
1445 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1447 Error *local_err = NULL;
1449 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1450 hmp_handle_error(mon, &local_err);
1453 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1455 BlockDriverState *bs;
1456 const char* device = qdict_get_str(qdict, "device");
1457 const char* command = qdict_get_str(qdict, "command");
1458 Error *err = NULL;
1460 bs = bdrv_find(device);
1461 if (bs) {
1462 qemuio_command(bs, command);
1463 } else {
1464 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1467 hmp_handle_error(mon, &err);