Merge remote-tracking branch 'pmaydell/tcg-aarch64.next' into staging
[qemu/ar7.git] / hmp.c
blob494a9aa4593164679d6ad505d040c9a093163827
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, "remaining ram: %" PRIu64 " kbytes\n",
173 info->ram->remaining >> 10);
174 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
175 info->ram->total >> 10);
176 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
177 info->ram->duplicate);
178 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
179 info->ram->skipped);
180 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
181 info->ram->normal);
182 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
183 info->ram->normal_bytes >> 10);
184 if (info->ram->dirty_pages_rate) {
185 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
186 info->ram->dirty_pages_rate);
190 if (info->has_disk) {
191 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
192 info->disk->transferred >> 10);
193 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
194 info->disk->remaining >> 10);
195 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
196 info->disk->total >> 10);
199 if (info->has_xbzrle_cache) {
200 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
201 info->xbzrle_cache->cache_size);
202 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
203 info->xbzrle_cache->bytes >> 10);
204 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
205 info->xbzrle_cache->pages);
206 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
207 info->xbzrle_cache->cache_miss);
208 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
209 info->xbzrle_cache->overflow);
212 qapi_free_MigrationInfo(info);
213 qapi_free_MigrationCapabilityStatusList(caps);
216 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
218 MigrationCapabilityStatusList *caps, *cap;
220 caps = qmp_query_migrate_capabilities(NULL);
222 if (caps) {
223 monitor_printf(mon, "capabilities: ");
224 for (cap = caps; cap; cap = cap->next) {
225 monitor_printf(mon, "%s: %s ",
226 MigrationCapability_lookup[cap->value->capability],
227 cap->value->state ? "on" : "off");
229 monitor_printf(mon, "\n");
232 qapi_free_MigrationCapabilityStatusList(caps);
235 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
237 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
238 qmp_query_migrate_cache_size(NULL) >> 10);
241 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
243 CpuInfoList *cpu_list, *cpu;
245 cpu_list = qmp_query_cpus(NULL);
247 for (cpu = cpu_list; cpu; cpu = cpu->next) {
248 int active = ' ';
250 if (cpu->value->CPU == monitor_get_cpu_index()) {
251 active = '*';
254 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
256 if (cpu->value->has_pc) {
257 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
259 if (cpu->value->has_nip) {
260 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
262 if (cpu->value->has_npc) {
263 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
265 if (cpu->value->has_PC) {
266 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
269 if (cpu->value->halted) {
270 monitor_printf(mon, " (halted)");
273 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
276 qapi_free_CpuInfoList(cpu_list);
279 void hmp_info_block(Monitor *mon, const QDict *qdict)
281 BlockInfoList *block_list, *info;
282 ImageInfo *image_info;
283 const char *device = qdict_get_try_str(qdict, "device");
284 bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
286 block_list = qmp_query_block(NULL);
288 for (info = block_list; info; info = info->next) {
289 if (device && strcmp(device, info->value->device)) {
290 continue;
292 monitor_printf(mon, "%s: removable=%d",
293 info->value->device, info->value->removable);
295 if (info->value->removable) {
296 monitor_printf(mon, " locked=%d", info->value->locked);
297 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
300 if (info->value->has_io_status) {
301 monitor_printf(mon, " io-status=%s",
302 BlockDeviceIoStatus_lookup[info->value->io_status]);
305 if (info->value->has_inserted) {
306 monitor_printf(mon, " file=");
307 monitor_print_filename(mon, info->value->inserted->file);
309 if (info->value->inserted->has_backing_file) {
310 monitor_printf(mon, " backing_file=");
311 monitor_print_filename(mon, info->value->inserted->backing_file);
312 monitor_printf(mon, " backing_file_depth=%" PRId64,
313 info->value->inserted->backing_file_depth);
315 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
316 info->value->inserted->ro,
317 info->value->inserted->drv,
318 info->value->inserted->encrypted);
320 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
321 " bps_wr=%" PRId64 " iops=%" PRId64
322 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
323 info->value->inserted->bps,
324 info->value->inserted->bps_rd,
325 info->value->inserted->bps_wr,
326 info->value->inserted->iops,
327 info->value->inserted->iops_rd,
328 info->value->inserted->iops_wr);
330 if (verbose) {
331 monitor_printf(mon, " images:\n");
332 image_info = info->value->inserted->image;
333 while (1) {
334 bdrv_image_info_dump((fprintf_function)monitor_printf,
335 mon, image_info);
336 if (image_info->has_backing_image) {
337 image_info = image_info->backing_image;
338 } else {
339 break;
343 } else {
344 monitor_printf(mon, " [not inserted]");
347 monitor_printf(mon, "\n");
350 qapi_free_BlockInfoList(block_list);
353 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
355 BlockStatsList *stats_list, *stats;
357 stats_list = qmp_query_blockstats(NULL);
359 for (stats = stats_list; stats; stats = stats->next) {
360 if (!stats->value->has_device) {
361 continue;
364 monitor_printf(mon, "%s:", stats->value->device);
365 monitor_printf(mon, " rd_bytes=%" PRId64
366 " wr_bytes=%" PRId64
367 " rd_operations=%" PRId64
368 " wr_operations=%" PRId64
369 " flush_operations=%" PRId64
370 " wr_total_time_ns=%" PRId64
371 " rd_total_time_ns=%" PRId64
372 " flush_total_time_ns=%" PRId64
373 "\n",
374 stats->value->stats->rd_bytes,
375 stats->value->stats->wr_bytes,
376 stats->value->stats->rd_operations,
377 stats->value->stats->wr_operations,
378 stats->value->stats->flush_operations,
379 stats->value->stats->wr_total_time_ns,
380 stats->value->stats->rd_total_time_ns,
381 stats->value->stats->flush_total_time_ns);
384 qapi_free_BlockStatsList(stats_list);
387 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
389 VncInfo *info;
390 Error *err = NULL;
391 VncClientInfoList *client;
393 info = qmp_query_vnc(&err);
394 if (err) {
395 monitor_printf(mon, "%s\n", error_get_pretty(err));
396 error_free(err);
397 return;
400 if (!info->enabled) {
401 monitor_printf(mon, "Server: disabled\n");
402 goto out;
405 monitor_printf(mon, "Server:\n");
406 if (info->has_host && info->has_service) {
407 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
409 if (info->has_auth) {
410 monitor_printf(mon, " auth: %s\n", info->auth);
413 if (!info->has_clients || info->clients == NULL) {
414 monitor_printf(mon, "Client: none\n");
415 } else {
416 for (client = info->clients; client; client = client->next) {
417 monitor_printf(mon, "Client:\n");
418 monitor_printf(mon, " address: %s:%s\n",
419 client->value->host, client->value->service);
420 monitor_printf(mon, " x509_dname: %s\n",
421 client->value->x509_dname ?
422 client->value->x509_dname : "none");
423 monitor_printf(mon, " username: %s\n",
424 client->value->has_sasl_username ?
425 client->value->sasl_username : "none");
429 out:
430 qapi_free_VncInfo(info);
433 void hmp_info_spice(Monitor *mon, const QDict *qdict)
435 SpiceChannelList *chan;
436 SpiceInfo *info;
438 info = qmp_query_spice(NULL);
440 if (!info->enabled) {
441 monitor_printf(mon, "Server: disabled\n");
442 goto out;
445 monitor_printf(mon, "Server:\n");
446 if (info->has_port) {
447 monitor_printf(mon, " address: %s:%" PRId64 "\n",
448 info->host, info->port);
450 if (info->has_tls_port) {
451 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
452 info->host, info->tls_port);
454 monitor_printf(mon, " migrated: %s\n",
455 info->migrated ? "true" : "false");
456 monitor_printf(mon, " auth: %s\n", info->auth);
457 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
458 monitor_printf(mon, " mouse-mode: %s\n",
459 SpiceQueryMouseMode_lookup[info->mouse_mode]);
461 if (!info->has_channels || info->channels == NULL) {
462 monitor_printf(mon, "Channels: none\n");
463 } else {
464 for (chan = info->channels; chan; chan = chan->next) {
465 monitor_printf(mon, "Channel:\n");
466 monitor_printf(mon, " address: %s:%s%s\n",
467 chan->value->host, chan->value->port,
468 chan->value->tls ? " [tls]" : "");
469 monitor_printf(mon, " session: %" PRId64 "\n",
470 chan->value->connection_id);
471 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
472 chan->value->channel_type, chan->value->channel_id);
476 out:
477 qapi_free_SpiceInfo(info);
480 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
482 BalloonInfo *info;
483 Error *err = NULL;
485 info = qmp_query_balloon(&err);
486 if (err) {
487 monitor_printf(mon, "%s\n", error_get_pretty(err));
488 error_free(err);
489 return;
492 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
494 qapi_free_BalloonInfo(info);
497 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
499 PciMemoryRegionList *region;
501 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
502 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
503 dev->slot, dev->function);
504 monitor_printf(mon, " ");
506 if (dev->class_info.has_desc) {
507 monitor_printf(mon, "%s", dev->class_info.desc);
508 } else {
509 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
512 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
513 dev->id.vendor, dev->id.device);
515 if (dev->has_irq) {
516 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
519 if (dev->has_pci_bridge) {
520 monitor_printf(mon, " BUS %" PRId64 ".\n",
521 dev->pci_bridge->bus.number);
522 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
523 dev->pci_bridge->bus.secondary);
524 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
525 dev->pci_bridge->bus.subordinate);
527 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
528 dev->pci_bridge->bus.io_range->base,
529 dev->pci_bridge->bus.io_range->limit);
531 monitor_printf(mon,
532 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
533 dev->pci_bridge->bus.memory_range->base,
534 dev->pci_bridge->bus.memory_range->limit);
536 monitor_printf(mon, " prefetchable memory range "
537 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
538 dev->pci_bridge->bus.prefetchable_range->base,
539 dev->pci_bridge->bus.prefetchable_range->limit);
542 for (region = dev->regions; region; region = region->next) {
543 uint64_t addr, size;
545 addr = region->value->address;
546 size = region->value->size;
548 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
550 if (!strcmp(region->value->type, "io")) {
551 monitor_printf(mon, "I/O at 0x%04" PRIx64
552 " [0x%04" PRIx64 "].\n",
553 addr, addr + size - 1);
554 } else {
555 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
556 " [0x%08" PRIx64 "].\n",
557 region->value->mem_type_64 ? 64 : 32,
558 region->value->prefetch ? " prefetchable" : "",
559 addr, addr + size - 1);
563 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
565 if (dev->has_pci_bridge) {
566 if (dev->pci_bridge->has_devices) {
567 PciDeviceInfoList *cdev;
568 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
569 hmp_info_pci_device(mon, cdev->value);
575 void hmp_info_pci(Monitor *mon, const QDict *qdict)
577 PciInfoList *info_list, *info;
578 Error *err = NULL;
580 info_list = qmp_query_pci(&err);
581 if (err) {
582 monitor_printf(mon, "PCI devices not supported\n");
583 error_free(err);
584 return;
587 for (info = info_list; info; info = info->next) {
588 PciDeviceInfoList *dev;
590 for (dev = info->value->devices; dev; dev = dev->next) {
591 hmp_info_pci_device(mon, dev->value);
595 qapi_free_PciInfoList(info_list);
598 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
600 BlockJobInfoList *list;
601 Error *err = NULL;
603 list = qmp_query_block_jobs(&err);
604 assert(!err);
606 if (!list) {
607 monitor_printf(mon, "No active jobs\n");
608 return;
611 while (list) {
612 if (strcmp(list->value->type, "stream") == 0) {
613 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
614 " of %" PRId64 " bytes, speed limit %" PRId64
615 " bytes/s\n",
616 list->value->device,
617 list->value->offset,
618 list->value->len,
619 list->value->speed);
620 } else {
621 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
622 " of %" PRId64 " bytes, speed limit %" PRId64
623 " bytes/s\n",
624 list->value->type,
625 list->value->device,
626 list->value->offset,
627 list->value->len,
628 list->value->speed);
630 list = list->next;
634 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
636 TPMInfoList *info_list, *info;
637 Error *err = NULL;
638 unsigned int c = 0;
639 TPMPassthroughOptions *tpo;
641 info_list = qmp_query_tpm(&err);
642 if (err) {
643 monitor_printf(mon, "TPM device not supported\n");
644 error_free(err);
645 return;
648 if (info_list) {
649 monitor_printf(mon, "TPM device:\n");
652 for (info = info_list; info; info = info->next) {
653 TPMInfo *ti = info->value;
654 monitor_printf(mon, " tpm%d: model=%s\n",
655 c, TpmModel_lookup[ti->model]);
657 monitor_printf(mon, " \\ %s: type=%s",
658 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
660 switch (ti->options->kind) {
661 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
662 tpo = ti->options->passthrough;
663 monitor_printf(mon, "%s%s%s%s",
664 tpo->has_path ? ",path=" : "",
665 tpo->has_path ? tpo->path : "",
666 tpo->has_cancel_path ? ",cancel-path=" : "",
667 tpo->has_cancel_path ? tpo->cancel_path : "");
668 break;
669 case TPM_TYPE_OPTIONS_KIND_MAX:
670 break;
672 monitor_printf(mon, "\n");
673 c++;
675 qapi_free_TPMInfoList(info_list);
678 void hmp_quit(Monitor *mon, const QDict *qdict)
680 monitor_suspend(mon);
681 qmp_quit(NULL);
684 void hmp_stop(Monitor *mon, const QDict *qdict)
686 qmp_stop(NULL);
689 void hmp_system_reset(Monitor *mon, const QDict *qdict)
691 qmp_system_reset(NULL);
694 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
696 qmp_system_powerdown(NULL);
699 void hmp_cpu(Monitor *mon, const QDict *qdict)
701 int64_t cpu_index;
703 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
704 use it are converted to the QAPI */
705 cpu_index = qdict_get_int(qdict, "index");
706 if (monitor_set_cpu(cpu_index) < 0) {
707 monitor_printf(mon, "invalid CPU index\n");
711 void hmp_memsave(Monitor *mon, const QDict *qdict)
713 uint32_t size = qdict_get_int(qdict, "size");
714 const char *filename = qdict_get_str(qdict, "filename");
715 uint64_t addr = qdict_get_int(qdict, "val");
716 Error *errp = NULL;
718 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
719 hmp_handle_error(mon, &errp);
722 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
724 uint32_t size = qdict_get_int(qdict, "size");
725 const char *filename = qdict_get_str(qdict, "filename");
726 uint64_t addr = qdict_get_int(qdict, "val");
727 Error *errp = NULL;
729 qmp_pmemsave(addr, size, filename, &errp);
730 hmp_handle_error(mon, &errp);
733 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
735 const char *chardev = qdict_get_str(qdict, "device");
736 const char *data = qdict_get_str(qdict, "data");
737 Error *errp = NULL;
739 qmp_ringbuf_write(chardev, data, false, 0, &errp);
741 hmp_handle_error(mon, &errp);
744 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
746 uint32_t size = qdict_get_int(qdict, "size");
747 const char *chardev = qdict_get_str(qdict, "device");
748 char *data;
749 Error *errp = NULL;
750 int i;
752 data = qmp_ringbuf_read(chardev, size, false, 0, &errp);
753 if (errp) {
754 monitor_printf(mon, "%s\n", error_get_pretty(errp));
755 error_free(errp);
756 return;
759 for (i = 0; data[i]; i++) {
760 unsigned char ch = data[i];
762 if (ch == '\\') {
763 monitor_printf(mon, "\\\\");
764 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
765 monitor_printf(mon, "\\u%04X", ch);
766 } else {
767 monitor_printf(mon, "%c", ch);
771 monitor_printf(mon, "\n");
772 g_free(data);
775 static void hmp_cont_cb(void *opaque, int err)
777 if (!err) {
778 qmp_cont(NULL);
782 static bool key_is_missing(const BlockInfo *bdev)
784 return (bdev->inserted && bdev->inserted->encryption_key_missing);
787 void hmp_cont(Monitor *mon, const QDict *qdict)
789 BlockInfoList *bdev_list, *bdev;
790 Error *errp = NULL;
792 bdev_list = qmp_query_block(NULL);
793 for (bdev = bdev_list; bdev; bdev = bdev->next) {
794 if (key_is_missing(bdev->value)) {
795 monitor_read_block_device_key(mon, bdev->value->device,
796 hmp_cont_cb, NULL);
797 goto out;
801 qmp_cont(&errp);
802 hmp_handle_error(mon, &errp);
804 out:
805 qapi_free_BlockInfoList(bdev_list);
808 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
810 qmp_system_wakeup(NULL);
813 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
815 Error *errp = NULL;
817 qmp_inject_nmi(&errp);
818 hmp_handle_error(mon, &errp);
821 void hmp_set_link(Monitor *mon, const QDict *qdict)
823 const char *name = qdict_get_str(qdict, "name");
824 int up = qdict_get_bool(qdict, "up");
825 Error *errp = NULL;
827 qmp_set_link(name, up, &errp);
828 hmp_handle_error(mon, &errp);
831 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
833 const char *device = qdict_get_str(qdict, "device");
834 const char *password = qdict_get_str(qdict, "password");
835 Error *errp = NULL;
837 qmp_block_passwd(device, password, &errp);
838 hmp_handle_error(mon, &errp);
841 void hmp_balloon(Monitor *mon, const QDict *qdict)
843 int64_t value = qdict_get_int(qdict, "value");
844 Error *errp = NULL;
846 qmp_balloon(value, &errp);
847 if (error_is_set(&errp)) {
848 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
849 error_free(errp);
853 void hmp_block_resize(Monitor *mon, const QDict *qdict)
855 const char *device = qdict_get_str(qdict, "device");
856 int64_t size = qdict_get_int(qdict, "size");
857 Error *errp = NULL;
859 qmp_block_resize(device, size, &errp);
860 hmp_handle_error(mon, &errp);
863 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
865 const char *device = qdict_get_str(qdict, "device");
866 const char *filename = qdict_get_str(qdict, "target");
867 const char *format = qdict_get_try_str(qdict, "format");
868 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
869 int full = qdict_get_try_bool(qdict, "full", 0);
870 enum NewImageMode mode;
871 Error *errp = NULL;
873 if (!filename) {
874 error_set(&errp, QERR_MISSING_PARAMETER, "target");
875 hmp_handle_error(mon, &errp);
876 return;
879 if (reuse) {
880 mode = NEW_IMAGE_MODE_EXISTING;
881 } else {
882 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
885 qmp_drive_mirror(device, filename, !!format, format,
886 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
887 true, mode, false, 0, false, 0, false, 0,
888 false, 0, false, 0, &errp);
889 hmp_handle_error(mon, &errp);
892 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
894 const char *device = qdict_get_str(qdict, "device");
895 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
896 const char *format = qdict_get_try_str(qdict, "format");
897 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
898 enum NewImageMode mode;
899 Error *errp = NULL;
901 if (!filename) {
902 /* In the future, if 'snapshot-file' is not specified, the snapshot
903 will be taken internally. Today it's actually required. */
904 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
905 hmp_handle_error(mon, &errp);
906 return;
909 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
910 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
911 true, mode, &errp);
912 hmp_handle_error(mon, &errp);
915 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
917 qmp_migrate_cancel(NULL);
920 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
922 double value = qdict_get_double(qdict, "value");
923 qmp_migrate_set_downtime(value, NULL);
926 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
928 int64_t value = qdict_get_int(qdict, "value");
929 Error *err = NULL;
931 qmp_migrate_set_cache_size(value, &err);
932 if (err) {
933 monitor_printf(mon, "%s\n", error_get_pretty(err));
934 error_free(err);
935 return;
939 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
941 int64_t value = qdict_get_int(qdict, "value");
942 qmp_migrate_set_speed(value, NULL);
945 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
947 const char *cap = qdict_get_str(qdict, "capability");
948 bool state = qdict_get_bool(qdict, "state");
949 Error *err = NULL;
950 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
951 int i;
953 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
954 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
955 caps->value = g_malloc0(sizeof(*caps->value));
956 caps->value->capability = i;
957 caps->value->state = state;
958 caps->next = NULL;
959 qmp_migrate_set_capabilities(caps, &err);
960 break;
964 if (i == MIGRATION_CAPABILITY_MAX) {
965 error_set(&err, QERR_INVALID_PARAMETER, cap);
968 qapi_free_MigrationCapabilityStatusList(caps);
970 if (err) {
971 monitor_printf(mon, "migrate_set_capability: %s\n",
972 error_get_pretty(err));
973 error_free(err);
977 void hmp_set_password(Monitor *mon, const QDict *qdict)
979 const char *protocol = qdict_get_str(qdict, "protocol");
980 const char *password = qdict_get_str(qdict, "password");
981 const char *connected = qdict_get_try_str(qdict, "connected");
982 Error *err = NULL;
984 qmp_set_password(protocol, password, !!connected, connected, &err);
985 hmp_handle_error(mon, &err);
988 void hmp_expire_password(Monitor *mon, const QDict *qdict)
990 const char *protocol = qdict_get_str(qdict, "protocol");
991 const char *whenstr = qdict_get_str(qdict, "time");
992 Error *err = NULL;
994 qmp_expire_password(protocol, whenstr, &err);
995 hmp_handle_error(mon, &err);
998 void hmp_eject(Monitor *mon, const QDict *qdict)
1000 int force = qdict_get_try_bool(qdict, "force", 0);
1001 const char *device = qdict_get_str(qdict, "device");
1002 Error *err = NULL;
1004 qmp_eject(device, true, force, &err);
1005 hmp_handle_error(mon, &err);
1008 static void hmp_change_read_arg(Monitor *mon, const char *password,
1009 void *opaque)
1011 qmp_change_vnc_password(password, NULL);
1012 monitor_read_command(mon, 1);
1015 void hmp_change(Monitor *mon, const QDict *qdict)
1017 const char *device = qdict_get_str(qdict, "device");
1018 const char *target = qdict_get_str(qdict, "target");
1019 const char *arg = qdict_get_try_str(qdict, "arg");
1020 Error *err = NULL;
1022 if (strcmp(device, "vnc") == 0 &&
1023 (strcmp(target, "passwd") == 0 ||
1024 strcmp(target, "password") == 0)) {
1025 if (!arg) {
1026 monitor_read_password(mon, hmp_change_read_arg, NULL);
1027 return;
1031 qmp_change(device, target, !!arg, arg, &err);
1032 if (error_is_set(&err) &&
1033 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1034 error_free(err);
1035 monitor_read_block_device_key(mon, device, NULL, NULL);
1036 return;
1038 hmp_handle_error(mon, &err);
1041 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1043 Error *err = NULL;
1045 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1046 qdict_get_int(qdict, "bps"),
1047 qdict_get_int(qdict, "bps_rd"),
1048 qdict_get_int(qdict, "bps_wr"),
1049 qdict_get_int(qdict, "iops"),
1050 qdict_get_int(qdict, "iops_rd"),
1051 qdict_get_int(qdict, "iops_wr"), &err);
1052 hmp_handle_error(mon, &err);
1055 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1057 Error *error = NULL;
1058 const char *device = qdict_get_str(qdict, "device");
1059 const char *base = qdict_get_try_str(qdict, "base");
1060 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1062 qmp_block_stream(device, base != NULL, base,
1063 qdict_haskey(qdict, "speed"), speed,
1064 BLOCKDEV_ON_ERROR_REPORT, true, &error);
1066 hmp_handle_error(mon, &error);
1069 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1071 Error *error = NULL;
1072 const char *device = qdict_get_str(qdict, "device");
1073 int64_t value = qdict_get_int(qdict, "speed");
1075 qmp_block_job_set_speed(device, value, &error);
1077 hmp_handle_error(mon, &error);
1080 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1082 Error *error = NULL;
1083 const char *device = qdict_get_str(qdict, "device");
1084 bool force = qdict_get_try_bool(qdict, "force", 0);
1086 qmp_block_job_cancel(device, true, force, &error);
1088 hmp_handle_error(mon, &error);
1091 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1093 Error *error = NULL;
1094 const char *device = qdict_get_str(qdict, "device");
1096 qmp_block_job_pause(device, &error);
1098 hmp_handle_error(mon, &error);
1101 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1103 Error *error = NULL;
1104 const char *device = qdict_get_str(qdict, "device");
1106 qmp_block_job_resume(device, &error);
1108 hmp_handle_error(mon, &error);
1111 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1113 Error *error = NULL;
1114 const char *device = qdict_get_str(qdict, "device");
1116 qmp_block_job_complete(device, &error);
1118 hmp_handle_error(mon, &error);
1121 typedef struct MigrationStatus
1123 QEMUTimer *timer;
1124 Monitor *mon;
1125 bool is_block_migration;
1126 } MigrationStatus;
1128 static void hmp_migrate_status_cb(void *opaque)
1130 MigrationStatus *status = opaque;
1131 MigrationInfo *info;
1133 info = qmp_query_migrate(NULL);
1134 if (!info->has_status || strcmp(info->status, "active") == 0) {
1135 if (info->has_disk) {
1136 int progress;
1138 if (info->disk->remaining) {
1139 progress = info->disk->transferred * 100 / info->disk->total;
1140 } else {
1141 progress = 100;
1144 monitor_printf(status->mon, "Completed %d %%\r", progress);
1145 monitor_flush(status->mon);
1148 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1149 } else {
1150 if (status->is_block_migration) {
1151 monitor_printf(status->mon, "\n");
1153 monitor_resume(status->mon);
1154 qemu_del_timer(status->timer);
1155 g_free(status);
1158 qapi_free_MigrationInfo(info);
1161 void hmp_migrate(Monitor *mon, const QDict *qdict)
1163 int detach = qdict_get_try_bool(qdict, "detach", 0);
1164 int blk = qdict_get_try_bool(qdict, "blk", 0);
1165 int inc = qdict_get_try_bool(qdict, "inc", 0);
1166 const char *uri = qdict_get_str(qdict, "uri");
1167 Error *err = NULL;
1169 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1170 if (err) {
1171 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1172 error_free(err);
1173 return;
1176 if (!detach) {
1177 MigrationStatus *status;
1179 if (monitor_suspend(mon) < 0) {
1180 monitor_printf(mon, "terminal does not allow synchronous "
1181 "migration, continuing detached\n");
1182 return;
1185 status = g_malloc0(sizeof(*status));
1186 status->mon = mon;
1187 status->is_block_migration = blk || inc;
1188 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1189 status);
1190 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1194 void hmp_device_del(Monitor *mon, const QDict *qdict)
1196 const char *id = qdict_get_str(qdict, "id");
1197 Error *err = NULL;
1199 qmp_device_del(id, &err);
1200 hmp_handle_error(mon, &err);
1203 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1205 Error *errp = NULL;
1206 int paging = qdict_get_try_bool(qdict, "paging", 0);
1207 const char *file = qdict_get_str(qdict, "filename");
1208 bool has_begin = qdict_haskey(qdict, "begin");
1209 bool has_length = qdict_haskey(qdict, "length");
1210 int64_t begin = 0;
1211 int64_t length = 0;
1212 char *prot;
1214 if (has_begin) {
1215 begin = qdict_get_int(qdict, "begin");
1217 if (has_length) {
1218 length = qdict_get_int(qdict, "length");
1221 prot = g_strconcat("file:", file, NULL);
1223 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1224 &errp);
1225 hmp_handle_error(mon, &errp);
1226 g_free(prot);
1229 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1231 Error *err = NULL;
1232 QemuOpts *opts;
1234 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1235 if (error_is_set(&err)) {
1236 goto out;
1239 netdev_add(opts, &err);
1240 if (error_is_set(&err)) {
1241 qemu_opts_del(opts);
1244 out:
1245 hmp_handle_error(mon, &err);
1248 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1250 const char *id = qdict_get_str(qdict, "id");
1251 Error *err = NULL;
1253 qmp_netdev_del(id, &err);
1254 hmp_handle_error(mon, &err);
1257 void hmp_getfd(Monitor *mon, const QDict *qdict)
1259 const char *fdname = qdict_get_str(qdict, "fdname");
1260 Error *errp = NULL;
1262 qmp_getfd(fdname, &errp);
1263 hmp_handle_error(mon, &errp);
1266 void hmp_closefd(Monitor *mon, const QDict *qdict)
1268 const char *fdname = qdict_get_str(qdict, "fdname");
1269 Error *errp = NULL;
1271 qmp_closefd(fdname, &errp);
1272 hmp_handle_error(mon, &errp);
1275 void hmp_send_key(Monitor *mon, const QDict *qdict)
1277 const char *keys = qdict_get_str(qdict, "keys");
1278 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1279 int has_hold_time = qdict_haskey(qdict, "hold-time");
1280 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1281 Error *err = NULL;
1282 char keyname_buf[16];
1283 char *separator;
1284 int keyname_len;
1286 while (1) {
1287 separator = strchr(keys, '-');
1288 keyname_len = separator ? separator - keys : strlen(keys);
1289 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1291 /* Be compatible with old interface, convert user inputted "<" */
1292 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1293 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1294 keyname_len = 4;
1296 keyname_buf[keyname_len] = 0;
1298 keylist = g_malloc0(sizeof(*keylist));
1299 keylist->value = g_malloc0(sizeof(*keylist->value));
1301 if (!head) {
1302 head = keylist;
1304 if (tmp) {
1305 tmp->next = keylist;
1307 tmp = keylist;
1309 if (strstart(keyname_buf, "0x", NULL)) {
1310 char *endp;
1311 int value = strtoul(keyname_buf, &endp, 0);
1312 if (*endp != '\0') {
1313 goto err_out;
1315 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1316 keylist->value->number = value;
1317 } else {
1318 int idx = index_from_key(keyname_buf);
1319 if (idx == Q_KEY_CODE_MAX) {
1320 goto err_out;
1322 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1323 keylist->value->qcode = idx;
1326 if (!separator) {
1327 break;
1329 keys = separator + 1;
1332 qmp_send_key(head, has_hold_time, hold_time, &err);
1333 hmp_handle_error(mon, &err);
1335 out:
1336 qapi_free_KeyValueList(head);
1337 return;
1339 err_out:
1340 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1341 goto out;
1344 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1346 const char *filename = qdict_get_str(qdict, "filename");
1347 Error *err = NULL;
1349 qmp_screendump(filename, &err);
1350 hmp_handle_error(mon, &err);
1353 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1355 const char *uri = qdict_get_str(qdict, "uri");
1356 int writable = qdict_get_try_bool(qdict, "writable", 0);
1357 int all = qdict_get_try_bool(qdict, "all", 0);
1358 Error *local_err = NULL;
1359 BlockInfoList *block_list, *info;
1360 SocketAddress *addr;
1362 if (writable && !all) {
1363 error_setg(&local_err, "-w only valid together with -a");
1364 goto exit;
1367 /* First check if the address is valid and start the server. */
1368 addr = socket_parse(uri, &local_err);
1369 if (local_err != NULL) {
1370 goto exit;
1373 qmp_nbd_server_start(addr, &local_err);
1374 qapi_free_SocketAddress(addr);
1375 if (local_err != NULL) {
1376 goto exit;
1379 if (!all) {
1380 return;
1383 /* Then try adding all block devices. If one fails, close all and
1384 * exit.
1386 block_list = qmp_query_block(NULL);
1388 for (info = block_list; info; info = info->next) {
1389 if (!info->value->has_inserted) {
1390 continue;
1393 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1395 if (local_err != NULL) {
1396 qmp_nbd_server_stop(NULL);
1397 break;
1401 qapi_free_BlockInfoList(block_list);
1403 exit:
1404 hmp_handle_error(mon, &local_err);
1407 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1409 const char *device = qdict_get_str(qdict, "device");
1410 int writable = qdict_get_try_bool(qdict, "writable", 0);
1411 Error *local_err = NULL;
1413 qmp_nbd_server_add(device, true, writable, &local_err);
1415 if (local_err != NULL) {
1416 hmp_handle_error(mon, &local_err);
1420 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1422 Error *errp = NULL;
1424 qmp_nbd_server_stop(&errp);
1425 hmp_handle_error(mon, &errp);
1428 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1430 const char *args = qdict_get_str(qdict, "args");
1431 Error *err = NULL;
1432 QemuOpts *opts;
1434 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1435 if (opts == NULL) {
1436 error_setg(&err, "Parsing chardev args failed");
1437 } else {
1438 qemu_chr_new_from_opts(opts, NULL, &err);
1440 hmp_handle_error(mon, &err);
1443 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1445 Error *local_err = NULL;
1447 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1448 hmp_handle_error(mon, &local_err);
1451 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1453 BlockDriverState *bs;
1454 const char* device = qdict_get_str(qdict, "device");
1455 const char* command = qdict_get_str(qdict, "command");
1456 Error *err = NULL;
1458 bs = bdrv_find(device);
1459 if (bs) {
1460 qemuio_command(bs, command);
1461 } else {
1462 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1465 hmp_handle_error(mon, &err);