qcow2: handle_alloc(): Get rid of nb_clusters parameter
[qemu/kevin.git] / hmp.c
blobdbe9b9043e8c8dbd994682a0063bf9150a45c4da
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 "char/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"
26 static void hmp_handle_error(Monitor *mon, Error **errp)
28 if (error_is_set(errp)) {
29 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
30 error_free(*errp);
34 void hmp_info_name(Monitor *mon, const QDict *qdict)
36 NameInfo *info;
38 info = qmp_query_name(NULL);
39 if (info->has_name) {
40 monitor_printf(mon, "%s\n", info->name);
42 qapi_free_NameInfo(info);
45 void hmp_info_version(Monitor *mon, const QDict *qdict)
47 VersionInfo *info;
49 info = qmp_query_version(NULL);
51 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
52 info->qemu.major, info->qemu.minor, info->qemu.micro,
53 info->package);
55 qapi_free_VersionInfo(info);
58 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
60 KvmInfo *info;
62 info = qmp_query_kvm(NULL);
63 monitor_printf(mon, "kvm support: ");
64 if (info->present) {
65 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
66 } else {
67 monitor_printf(mon, "not compiled\n");
70 qapi_free_KvmInfo(info);
73 void hmp_info_status(Monitor *mon, const QDict *qdict)
75 StatusInfo *info;
77 info = qmp_query_status(NULL);
79 monitor_printf(mon, "VM status: %s%s",
80 info->running ? "running" : "paused",
81 info->singlestep ? " (single step mode)" : "");
83 if (!info->running && info->status != RUN_STATE_PAUSED) {
84 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
87 monitor_printf(mon, "\n");
89 qapi_free_StatusInfo(info);
92 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
94 UuidInfo *info;
96 info = qmp_query_uuid(NULL);
97 monitor_printf(mon, "%s\n", info->UUID);
98 qapi_free_UuidInfo(info);
101 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
103 ChardevInfoList *char_info, *info;
105 char_info = qmp_query_chardev(NULL);
106 for (info = char_info; info; info = info->next) {
107 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
108 info->value->filename);
111 qapi_free_ChardevInfoList(char_info);
114 void hmp_info_mice(Monitor *mon, const QDict *qdict)
116 MouseInfoList *mice_list, *mouse;
118 mice_list = qmp_query_mice(NULL);
119 if (!mice_list) {
120 monitor_printf(mon, "No mouse devices connected\n");
121 return;
124 for (mouse = mice_list; mouse; mouse = mouse->next) {
125 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
126 mouse->value->current ? '*' : ' ',
127 mouse->value->index, mouse->value->name,
128 mouse->value->absolute ? " (absolute)" : "");
131 qapi_free_MouseInfoList(mice_list);
134 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
136 MigrationInfo *info;
137 MigrationCapabilityStatusList *caps, *cap;
139 info = qmp_query_migrate(NULL);
140 caps = qmp_query_migrate_capabilities(NULL);
142 /* do not display parameters during setup */
143 if (info->has_status && caps) {
144 monitor_printf(mon, "capabilities: ");
145 for (cap = caps; cap; cap = cap->next) {
146 monitor_printf(mon, "%s: %s ",
147 MigrationCapability_lookup[cap->value->capability],
148 cap->value->state ? "on" : "off");
150 monitor_printf(mon, "\n");
153 if (info->has_status) {
154 monitor_printf(mon, "Migration status: %s\n", info->status);
155 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
156 info->total_time);
157 if (info->has_expected_downtime) {
158 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
159 info->expected_downtime);
161 if (info->has_downtime) {
162 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
163 info->downtime);
167 if (info->has_ram) {
168 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
169 info->ram->transferred >> 10);
170 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
171 info->ram->remaining >> 10);
172 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
173 info->ram->total >> 10);
174 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
175 info->ram->duplicate);
176 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
177 info->ram->skipped);
178 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
179 info->ram->normal);
180 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
181 info->ram->normal_bytes >> 10);
182 if (info->ram->dirty_pages_rate) {
183 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
184 info->ram->dirty_pages_rate);
188 if (info->has_disk) {
189 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
190 info->disk->transferred >> 10);
191 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
192 info->disk->remaining >> 10);
193 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
194 info->disk->total >> 10);
197 if (info->has_xbzrle_cache) {
198 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
199 info->xbzrle_cache->cache_size);
200 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
201 info->xbzrle_cache->bytes >> 10);
202 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
203 info->xbzrle_cache->pages);
204 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
205 info->xbzrle_cache->cache_miss);
206 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
207 info->xbzrle_cache->overflow);
210 qapi_free_MigrationInfo(info);
211 qapi_free_MigrationCapabilityStatusList(caps);
214 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
216 MigrationCapabilityStatusList *caps, *cap;
218 caps = qmp_query_migrate_capabilities(NULL);
220 if (caps) {
221 monitor_printf(mon, "capabilities: ");
222 for (cap = caps; cap; cap = cap->next) {
223 monitor_printf(mon, "%s: %s ",
224 MigrationCapability_lookup[cap->value->capability],
225 cap->value->state ? "on" : "off");
227 monitor_printf(mon, "\n");
230 qapi_free_MigrationCapabilityStatusList(caps);
233 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
235 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
236 qmp_query_migrate_cache_size(NULL) >> 10);
239 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
241 CpuInfoList *cpu_list, *cpu;
243 cpu_list = qmp_query_cpus(NULL);
245 for (cpu = cpu_list; cpu; cpu = cpu->next) {
246 int active = ' ';
248 if (cpu->value->CPU == monitor_get_cpu_index()) {
249 active = '*';
252 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
254 if (cpu->value->has_pc) {
255 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
257 if (cpu->value->has_nip) {
258 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
260 if (cpu->value->has_npc) {
261 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
263 if (cpu->value->has_PC) {
264 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
267 if (cpu->value->halted) {
268 monitor_printf(mon, " (halted)");
271 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
274 qapi_free_CpuInfoList(cpu_list);
277 void hmp_info_block(Monitor *mon, const QDict *qdict)
279 BlockInfoList *block_list, *info;
281 block_list = qmp_query_block(NULL);
283 for (info = block_list; info; info = info->next) {
284 monitor_printf(mon, "%s: removable=%d",
285 info->value->device, info->value->removable);
287 if (info->value->removable) {
288 monitor_printf(mon, " locked=%d", info->value->locked);
289 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
292 if (info->value->has_io_status) {
293 monitor_printf(mon, " io-status=%s",
294 BlockDeviceIoStatus_lookup[info->value->io_status]);
297 if (info->value->has_inserted) {
298 monitor_printf(mon, " file=");
299 monitor_print_filename(mon, info->value->inserted->file);
301 if (info->value->inserted->has_backing_file) {
302 monitor_printf(mon, " backing_file=");
303 monitor_print_filename(mon, info->value->inserted->backing_file);
304 monitor_printf(mon, " backing_file_depth=%" PRId64,
305 info->value->inserted->backing_file_depth);
307 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
308 info->value->inserted->ro,
309 info->value->inserted->drv,
310 info->value->inserted->encrypted);
312 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
313 " bps_wr=%" PRId64 " iops=%" PRId64
314 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
315 info->value->inserted->bps,
316 info->value->inserted->bps_rd,
317 info->value->inserted->bps_wr,
318 info->value->inserted->iops,
319 info->value->inserted->iops_rd,
320 info->value->inserted->iops_wr);
321 } else {
322 monitor_printf(mon, " [not inserted]");
325 monitor_printf(mon, "\n");
328 qapi_free_BlockInfoList(block_list);
331 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
333 BlockStatsList *stats_list, *stats;
335 stats_list = qmp_query_blockstats(NULL);
337 for (stats = stats_list; stats; stats = stats->next) {
338 if (!stats->value->has_device) {
339 continue;
342 monitor_printf(mon, "%s:", stats->value->device);
343 monitor_printf(mon, " rd_bytes=%" PRId64
344 " wr_bytes=%" PRId64
345 " rd_operations=%" PRId64
346 " wr_operations=%" PRId64
347 " flush_operations=%" PRId64
348 " wr_total_time_ns=%" PRId64
349 " rd_total_time_ns=%" PRId64
350 " flush_total_time_ns=%" PRId64
351 "\n",
352 stats->value->stats->rd_bytes,
353 stats->value->stats->wr_bytes,
354 stats->value->stats->rd_operations,
355 stats->value->stats->wr_operations,
356 stats->value->stats->flush_operations,
357 stats->value->stats->wr_total_time_ns,
358 stats->value->stats->rd_total_time_ns,
359 stats->value->stats->flush_total_time_ns);
362 qapi_free_BlockStatsList(stats_list);
365 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
367 VncInfo *info;
368 Error *err = NULL;
369 VncClientInfoList *client;
371 info = qmp_query_vnc(&err);
372 if (err) {
373 monitor_printf(mon, "%s\n", error_get_pretty(err));
374 error_free(err);
375 return;
378 if (!info->enabled) {
379 monitor_printf(mon, "Server: disabled\n");
380 goto out;
383 monitor_printf(mon, "Server:\n");
384 if (info->has_host && info->has_service) {
385 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
387 if (info->has_auth) {
388 monitor_printf(mon, " auth: %s\n", info->auth);
391 if (!info->has_clients || info->clients == NULL) {
392 monitor_printf(mon, "Client: none\n");
393 } else {
394 for (client = info->clients; client; client = client->next) {
395 monitor_printf(mon, "Client:\n");
396 monitor_printf(mon, " address: %s:%s\n",
397 client->value->host, client->value->service);
398 monitor_printf(mon, " x509_dname: %s\n",
399 client->value->x509_dname ?
400 client->value->x509_dname : "none");
401 monitor_printf(mon, " username: %s\n",
402 client->value->has_sasl_username ?
403 client->value->sasl_username : "none");
407 out:
408 qapi_free_VncInfo(info);
411 void hmp_info_spice(Monitor *mon, const QDict *qdict)
413 SpiceChannelList *chan;
414 SpiceInfo *info;
416 info = qmp_query_spice(NULL);
418 if (!info->enabled) {
419 monitor_printf(mon, "Server: disabled\n");
420 goto out;
423 monitor_printf(mon, "Server:\n");
424 if (info->has_port) {
425 monitor_printf(mon, " address: %s:%" PRId64 "\n",
426 info->host, info->port);
428 if (info->has_tls_port) {
429 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
430 info->host, info->tls_port);
432 monitor_printf(mon, " migrated: %s\n",
433 info->migrated ? "true" : "false");
434 monitor_printf(mon, " auth: %s\n", info->auth);
435 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
436 monitor_printf(mon, " mouse-mode: %s\n",
437 SpiceQueryMouseMode_lookup[info->mouse_mode]);
439 if (!info->has_channels || info->channels == NULL) {
440 monitor_printf(mon, "Channels: none\n");
441 } else {
442 for (chan = info->channels; chan; chan = chan->next) {
443 monitor_printf(mon, "Channel:\n");
444 monitor_printf(mon, " address: %s:%s%s\n",
445 chan->value->host, chan->value->port,
446 chan->value->tls ? " [tls]" : "");
447 monitor_printf(mon, " session: %" PRId64 "\n",
448 chan->value->connection_id);
449 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
450 chan->value->channel_type, chan->value->channel_id);
454 out:
455 qapi_free_SpiceInfo(info);
458 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
460 BalloonInfo *info;
461 Error *err = NULL;
463 info = qmp_query_balloon(&err);
464 if (err) {
465 monitor_printf(mon, "%s\n", error_get_pretty(err));
466 error_free(err);
467 return;
470 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
472 qapi_free_BalloonInfo(info);
475 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
477 PciMemoryRegionList *region;
479 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
480 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
481 dev->slot, dev->function);
482 monitor_printf(mon, " ");
484 if (dev->class_info.has_desc) {
485 monitor_printf(mon, "%s", dev->class_info.desc);
486 } else {
487 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
490 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
491 dev->id.vendor, dev->id.device);
493 if (dev->has_irq) {
494 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
497 if (dev->has_pci_bridge) {
498 monitor_printf(mon, " BUS %" PRId64 ".\n",
499 dev->pci_bridge->bus.number);
500 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
501 dev->pci_bridge->bus.secondary);
502 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
503 dev->pci_bridge->bus.subordinate);
505 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
506 dev->pci_bridge->bus.io_range->base,
507 dev->pci_bridge->bus.io_range->limit);
509 monitor_printf(mon,
510 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
511 dev->pci_bridge->bus.memory_range->base,
512 dev->pci_bridge->bus.memory_range->limit);
514 monitor_printf(mon, " prefetchable memory range "
515 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
516 dev->pci_bridge->bus.prefetchable_range->base,
517 dev->pci_bridge->bus.prefetchable_range->limit);
520 for (region = dev->regions; region; region = region->next) {
521 uint64_t addr, size;
523 addr = region->value->address;
524 size = region->value->size;
526 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
528 if (!strcmp(region->value->type, "io")) {
529 monitor_printf(mon, "I/O at 0x%04" PRIx64
530 " [0x%04" PRIx64 "].\n",
531 addr, addr + size - 1);
532 } else {
533 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
534 " [0x%08" PRIx64 "].\n",
535 region->value->mem_type_64 ? 64 : 32,
536 region->value->prefetch ? " prefetchable" : "",
537 addr, addr + size - 1);
541 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
543 if (dev->has_pci_bridge) {
544 if (dev->pci_bridge->has_devices) {
545 PciDeviceInfoList *cdev;
546 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
547 hmp_info_pci_device(mon, cdev->value);
553 void hmp_info_pci(Monitor *mon, const QDict *qdict)
555 PciInfoList *info_list, *info;
556 Error *err = NULL;
558 info_list = qmp_query_pci(&err);
559 if (err) {
560 monitor_printf(mon, "PCI devices not supported\n");
561 error_free(err);
562 return;
565 for (info = info_list; info; info = info->next) {
566 PciDeviceInfoList *dev;
568 for (dev = info->value->devices; dev; dev = dev->next) {
569 hmp_info_pci_device(mon, dev->value);
573 qapi_free_PciInfoList(info_list);
576 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
578 BlockJobInfoList *list;
579 Error *err = NULL;
581 list = qmp_query_block_jobs(&err);
582 assert(!err);
584 if (!list) {
585 monitor_printf(mon, "No active jobs\n");
586 return;
589 while (list) {
590 if (strcmp(list->value->type, "stream") == 0) {
591 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
592 " of %" PRId64 " bytes, speed limit %" PRId64
593 " bytes/s\n",
594 list->value->device,
595 list->value->offset,
596 list->value->len,
597 list->value->speed);
598 } else {
599 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
600 " of %" PRId64 " bytes, speed limit %" PRId64
601 " bytes/s\n",
602 list->value->type,
603 list->value->device,
604 list->value->offset,
605 list->value->len,
606 list->value->speed);
608 list = list->next;
612 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
614 TPMInfoList *info_list, *info;
615 Error *err = NULL;
616 unsigned int c = 0;
617 TPMPassthroughOptions *tpo;
619 info_list = qmp_query_tpm(&err);
620 if (err) {
621 monitor_printf(mon, "TPM device not supported\n");
622 error_free(err);
623 return;
626 if (info_list) {
627 monitor_printf(mon, "TPM device:\n");
630 for (info = info_list; info; info = info->next) {
631 TPMInfo *ti = info->value;
632 monitor_printf(mon, " tpm%d: model=%s\n",
633 c, TpmModel_lookup[ti->model]);
635 monitor_printf(mon, " \\ %s: type=%s",
636 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
638 switch (ti->options->kind) {
639 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
640 tpo = ti->options->passthrough;
641 monitor_printf(mon, "%s%s%s%s",
642 tpo->has_path ? ",path=" : "",
643 tpo->has_path ? tpo->path : "",
644 tpo->has_cancel_path ? ",cancel-path=" : "",
645 tpo->has_cancel_path ? tpo->cancel_path : "");
646 break;
647 case TPM_TYPE_OPTIONS_KIND_MAX:
648 break;
650 monitor_printf(mon, "\n");
651 c++;
653 qapi_free_TPMInfoList(info_list);
656 void hmp_quit(Monitor *mon, const QDict *qdict)
658 monitor_suspend(mon);
659 qmp_quit(NULL);
662 void hmp_stop(Monitor *mon, const QDict *qdict)
664 qmp_stop(NULL);
667 void hmp_system_reset(Monitor *mon, const QDict *qdict)
669 qmp_system_reset(NULL);
672 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
674 qmp_system_powerdown(NULL);
677 void hmp_cpu(Monitor *mon, const QDict *qdict)
679 int64_t cpu_index;
681 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
682 use it are converted to the QAPI */
683 cpu_index = qdict_get_int(qdict, "index");
684 if (monitor_set_cpu(cpu_index) < 0) {
685 monitor_printf(mon, "invalid CPU index\n");
689 void hmp_memsave(Monitor *mon, const QDict *qdict)
691 uint32_t size = qdict_get_int(qdict, "size");
692 const char *filename = qdict_get_str(qdict, "filename");
693 uint64_t addr = qdict_get_int(qdict, "val");
694 Error *errp = NULL;
696 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
697 hmp_handle_error(mon, &errp);
700 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
702 uint32_t size = qdict_get_int(qdict, "size");
703 const char *filename = qdict_get_str(qdict, "filename");
704 uint64_t addr = qdict_get_int(qdict, "val");
705 Error *errp = NULL;
707 qmp_pmemsave(addr, size, filename, &errp);
708 hmp_handle_error(mon, &errp);
711 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
713 const char *chardev = qdict_get_str(qdict, "device");
714 const char *data = qdict_get_str(qdict, "data");
715 Error *errp = NULL;
717 qmp_ringbuf_write(chardev, data, false, 0, &errp);
719 hmp_handle_error(mon, &errp);
722 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
724 uint32_t size = qdict_get_int(qdict, "size");
725 const char *chardev = qdict_get_str(qdict, "device");
726 char *data;
727 Error *errp = NULL;
728 int i;
730 data = qmp_ringbuf_read(chardev, size, false, 0, &errp);
731 if (errp) {
732 monitor_printf(mon, "%s\n", error_get_pretty(errp));
733 error_free(errp);
734 return;
737 for (i = 0; data[i]; i++) {
738 unsigned char ch = data[i];
740 if (ch == '\\') {
741 monitor_printf(mon, "\\\\");
742 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
743 monitor_printf(mon, "\\u%04X", ch);
744 } else {
745 monitor_printf(mon, "%c", ch);
749 monitor_printf(mon, "\n");
750 g_free(data);
753 void hmp_query_cpu_max(Monitor *mon, const QDict *qdict)
755 int cpu_max;
757 cpu_max = qmp_query_cpu_max(NULL);
758 monitor_printf(mon, "Maximum number of CPUs is %d\n", cpu_max);
761 static void hmp_cont_cb(void *opaque, int err)
763 if (!err) {
764 qmp_cont(NULL);
768 static bool key_is_missing(const BlockInfo *bdev)
770 return (bdev->inserted && bdev->inserted->encryption_key_missing);
773 void hmp_cont(Monitor *mon, const QDict *qdict)
775 BlockInfoList *bdev_list, *bdev;
776 Error *errp = NULL;
778 bdev_list = qmp_query_block(NULL);
779 for (bdev = bdev_list; bdev; bdev = bdev->next) {
780 if (key_is_missing(bdev->value)) {
781 monitor_read_block_device_key(mon, bdev->value->device,
782 hmp_cont_cb, NULL);
783 goto out;
787 qmp_cont(&errp);
788 hmp_handle_error(mon, &errp);
790 out:
791 qapi_free_BlockInfoList(bdev_list);
794 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
796 qmp_system_wakeup(NULL);
799 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
801 Error *errp = NULL;
803 qmp_inject_nmi(&errp);
804 hmp_handle_error(mon, &errp);
807 void hmp_set_link(Monitor *mon, const QDict *qdict)
809 const char *name = qdict_get_str(qdict, "name");
810 int up = qdict_get_bool(qdict, "up");
811 Error *errp = NULL;
813 qmp_set_link(name, up, &errp);
814 hmp_handle_error(mon, &errp);
817 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
819 const char *device = qdict_get_str(qdict, "device");
820 const char *password = qdict_get_str(qdict, "password");
821 Error *errp = NULL;
823 qmp_block_passwd(device, password, &errp);
824 hmp_handle_error(mon, &errp);
827 void hmp_balloon(Monitor *mon, const QDict *qdict)
829 int64_t value = qdict_get_int(qdict, "value");
830 Error *errp = NULL;
832 qmp_balloon(value, &errp);
833 if (error_is_set(&errp)) {
834 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
835 error_free(errp);
839 void hmp_block_resize(Monitor *mon, const QDict *qdict)
841 const char *device = qdict_get_str(qdict, "device");
842 int64_t size = qdict_get_int(qdict, "size");
843 Error *errp = NULL;
845 qmp_block_resize(device, size, &errp);
846 hmp_handle_error(mon, &errp);
849 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
851 const char *device = qdict_get_str(qdict, "device");
852 const char *filename = qdict_get_str(qdict, "target");
853 const char *format = qdict_get_try_str(qdict, "format");
854 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
855 int full = qdict_get_try_bool(qdict, "full", 0);
856 enum NewImageMode mode;
857 Error *errp = NULL;
859 if (!filename) {
860 error_set(&errp, QERR_MISSING_PARAMETER, "target");
861 hmp_handle_error(mon, &errp);
862 return;
865 if (reuse) {
866 mode = NEW_IMAGE_MODE_EXISTING;
867 } else {
868 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
871 qmp_drive_mirror(device, filename, !!format, format,
872 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
873 true, mode, false, 0, false, 0, false, 0,
874 false, 0, false, 0, &errp);
875 hmp_handle_error(mon, &errp);
878 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
880 const char *device = qdict_get_str(qdict, "device");
881 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
882 const char *format = qdict_get_try_str(qdict, "format");
883 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
884 enum NewImageMode mode;
885 Error *errp = NULL;
887 if (!filename) {
888 /* In the future, if 'snapshot-file' is not specified, the snapshot
889 will be taken internally. Today it's actually required. */
890 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
891 hmp_handle_error(mon, &errp);
892 return;
895 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
896 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
897 true, mode, &errp);
898 hmp_handle_error(mon, &errp);
901 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
903 qmp_migrate_cancel(NULL);
906 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
908 double value = qdict_get_double(qdict, "value");
909 qmp_migrate_set_downtime(value, NULL);
912 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
914 int64_t value = qdict_get_int(qdict, "value");
915 Error *err = NULL;
917 qmp_migrate_set_cache_size(value, &err);
918 if (err) {
919 monitor_printf(mon, "%s\n", error_get_pretty(err));
920 error_free(err);
921 return;
925 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
927 int64_t value = qdict_get_int(qdict, "value");
928 qmp_migrate_set_speed(value, NULL);
931 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
933 const char *cap = qdict_get_str(qdict, "capability");
934 bool state = qdict_get_bool(qdict, "state");
935 Error *err = NULL;
936 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
937 int i;
939 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
940 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
941 caps->value = g_malloc0(sizeof(*caps->value));
942 caps->value->capability = i;
943 caps->value->state = state;
944 caps->next = NULL;
945 qmp_migrate_set_capabilities(caps, &err);
946 break;
950 if (i == MIGRATION_CAPABILITY_MAX) {
951 error_set(&err, QERR_INVALID_PARAMETER, cap);
954 qapi_free_MigrationCapabilityStatusList(caps);
956 if (err) {
957 monitor_printf(mon, "migrate_set_capability: %s\n",
958 error_get_pretty(err));
959 error_free(err);
963 void hmp_set_password(Monitor *mon, const QDict *qdict)
965 const char *protocol = qdict_get_str(qdict, "protocol");
966 const char *password = qdict_get_str(qdict, "password");
967 const char *connected = qdict_get_try_str(qdict, "connected");
968 Error *err = NULL;
970 qmp_set_password(protocol, password, !!connected, connected, &err);
971 hmp_handle_error(mon, &err);
974 void hmp_expire_password(Monitor *mon, const QDict *qdict)
976 const char *protocol = qdict_get_str(qdict, "protocol");
977 const char *whenstr = qdict_get_str(qdict, "time");
978 Error *err = NULL;
980 qmp_expire_password(protocol, whenstr, &err);
981 hmp_handle_error(mon, &err);
984 void hmp_eject(Monitor *mon, const QDict *qdict)
986 int force = qdict_get_try_bool(qdict, "force", 0);
987 const char *device = qdict_get_str(qdict, "device");
988 Error *err = NULL;
990 qmp_eject(device, true, force, &err);
991 hmp_handle_error(mon, &err);
994 static void hmp_change_read_arg(Monitor *mon, const char *password,
995 void *opaque)
997 qmp_change_vnc_password(password, NULL);
998 monitor_read_command(mon, 1);
1001 void hmp_change(Monitor *mon, const QDict *qdict)
1003 const char *device = qdict_get_str(qdict, "device");
1004 const char *target = qdict_get_str(qdict, "target");
1005 const char *arg = qdict_get_try_str(qdict, "arg");
1006 Error *err = NULL;
1008 if (strcmp(device, "vnc") == 0 &&
1009 (strcmp(target, "passwd") == 0 ||
1010 strcmp(target, "password") == 0)) {
1011 if (!arg) {
1012 monitor_read_password(mon, hmp_change_read_arg, NULL);
1013 return;
1017 qmp_change(device, target, !!arg, arg, &err);
1018 if (error_is_set(&err) &&
1019 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1020 error_free(err);
1021 monitor_read_block_device_key(mon, device, NULL, NULL);
1022 return;
1024 hmp_handle_error(mon, &err);
1027 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1029 Error *err = NULL;
1031 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1032 qdict_get_int(qdict, "bps"),
1033 qdict_get_int(qdict, "bps_rd"),
1034 qdict_get_int(qdict, "bps_wr"),
1035 qdict_get_int(qdict, "iops"),
1036 qdict_get_int(qdict, "iops_rd"),
1037 qdict_get_int(qdict, "iops_wr"), &err);
1038 hmp_handle_error(mon, &err);
1041 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1043 Error *error = NULL;
1044 const char *device = qdict_get_str(qdict, "device");
1045 const char *base = qdict_get_try_str(qdict, "base");
1046 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1048 qmp_block_stream(device, base != NULL, base,
1049 qdict_haskey(qdict, "speed"), speed,
1050 BLOCKDEV_ON_ERROR_REPORT, true, &error);
1052 hmp_handle_error(mon, &error);
1055 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1057 Error *error = NULL;
1058 const char *device = qdict_get_str(qdict, "device");
1059 int64_t value = qdict_get_int(qdict, "speed");
1061 qmp_block_job_set_speed(device, value, &error);
1063 hmp_handle_error(mon, &error);
1066 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1068 Error *error = NULL;
1069 const char *device = qdict_get_str(qdict, "device");
1070 bool force = qdict_get_try_bool(qdict, "force", 0);
1072 qmp_block_job_cancel(device, true, force, &error);
1074 hmp_handle_error(mon, &error);
1077 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1079 Error *error = NULL;
1080 const char *device = qdict_get_str(qdict, "device");
1082 qmp_block_job_pause(device, &error);
1084 hmp_handle_error(mon, &error);
1087 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1089 Error *error = NULL;
1090 const char *device = qdict_get_str(qdict, "device");
1092 qmp_block_job_resume(device, &error);
1094 hmp_handle_error(mon, &error);
1097 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1099 Error *error = NULL;
1100 const char *device = qdict_get_str(qdict, "device");
1102 qmp_block_job_complete(device, &error);
1104 hmp_handle_error(mon, &error);
1107 typedef struct MigrationStatus
1109 QEMUTimer *timer;
1110 Monitor *mon;
1111 bool is_block_migration;
1112 } MigrationStatus;
1114 static void hmp_migrate_status_cb(void *opaque)
1116 MigrationStatus *status = opaque;
1117 MigrationInfo *info;
1119 info = qmp_query_migrate(NULL);
1120 if (!info->has_status || strcmp(info->status, "active") == 0) {
1121 if (info->has_disk) {
1122 int progress;
1124 if (info->disk->remaining) {
1125 progress = info->disk->transferred * 100 / info->disk->total;
1126 } else {
1127 progress = 100;
1130 monitor_printf(status->mon, "Completed %d %%\r", progress);
1131 monitor_flush(status->mon);
1134 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1135 } else {
1136 if (status->is_block_migration) {
1137 monitor_printf(status->mon, "\n");
1139 monitor_resume(status->mon);
1140 qemu_del_timer(status->timer);
1141 g_free(status);
1144 qapi_free_MigrationInfo(info);
1147 void hmp_migrate(Monitor *mon, const QDict *qdict)
1149 int detach = qdict_get_try_bool(qdict, "detach", 0);
1150 int blk = qdict_get_try_bool(qdict, "blk", 0);
1151 int inc = qdict_get_try_bool(qdict, "inc", 0);
1152 const char *uri = qdict_get_str(qdict, "uri");
1153 Error *err = NULL;
1155 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1156 if (err) {
1157 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1158 error_free(err);
1159 return;
1162 if (!detach) {
1163 MigrationStatus *status;
1165 if (monitor_suspend(mon) < 0) {
1166 monitor_printf(mon, "terminal does not allow synchronous "
1167 "migration, continuing detached\n");
1168 return;
1171 status = g_malloc0(sizeof(*status));
1172 status->mon = mon;
1173 status->is_block_migration = blk || inc;
1174 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1175 status);
1176 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1180 void hmp_device_del(Monitor *mon, const QDict *qdict)
1182 const char *id = qdict_get_str(qdict, "id");
1183 Error *err = NULL;
1185 qmp_device_del(id, &err);
1186 hmp_handle_error(mon, &err);
1189 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1191 Error *errp = NULL;
1192 int paging = qdict_get_try_bool(qdict, "paging", 0);
1193 const char *file = qdict_get_str(qdict, "filename");
1194 bool has_begin = qdict_haskey(qdict, "begin");
1195 bool has_length = qdict_haskey(qdict, "length");
1196 int64_t begin = 0;
1197 int64_t length = 0;
1198 char *prot;
1200 if (has_begin) {
1201 begin = qdict_get_int(qdict, "begin");
1203 if (has_length) {
1204 length = qdict_get_int(qdict, "length");
1207 prot = g_strconcat("file:", file, NULL);
1209 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1210 &errp);
1211 hmp_handle_error(mon, &errp);
1212 g_free(prot);
1215 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1217 Error *err = NULL;
1218 QemuOpts *opts;
1220 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1221 if (error_is_set(&err)) {
1222 goto out;
1225 netdev_add(opts, &err);
1226 if (error_is_set(&err)) {
1227 qemu_opts_del(opts);
1230 out:
1231 hmp_handle_error(mon, &err);
1234 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1236 const char *id = qdict_get_str(qdict, "id");
1237 Error *err = NULL;
1239 qmp_netdev_del(id, &err);
1240 hmp_handle_error(mon, &err);
1243 void hmp_getfd(Monitor *mon, const QDict *qdict)
1245 const char *fdname = qdict_get_str(qdict, "fdname");
1246 Error *errp = NULL;
1248 qmp_getfd(fdname, &errp);
1249 hmp_handle_error(mon, &errp);
1252 void hmp_closefd(Monitor *mon, const QDict *qdict)
1254 const char *fdname = qdict_get_str(qdict, "fdname");
1255 Error *errp = NULL;
1257 qmp_closefd(fdname, &errp);
1258 hmp_handle_error(mon, &errp);
1261 void hmp_send_key(Monitor *mon, const QDict *qdict)
1263 const char *keys = qdict_get_str(qdict, "keys");
1264 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1265 int has_hold_time = qdict_haskey(qdict, "hold-time");
1266 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1267 Error *err = NULL;
1268 char keyname_buf[16];
1269 char *separator;
1270 int keyname_len;
1272 while (1) {
1273 separator = strchr(keys, '-');
1274 keyname_len = separator ? separator - keys : strlen(keys);
1275 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1277 /* Be compatible with old interface, convert user inputted "<" */
1278 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1279 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1280 keyname_len = 4;
1282 keyname_buf[keyname_len] = 0;
1284 keylist = g_malloc0(sizeof(*keylist));
1285 keylist->value = g_malloc0(sizeof(*keylist->value));
1287 if (!head) {
1288 head = keylist;
1290 if (tmp) {
1291 tmp->next = keylist;
1293 tmp = keylist;
1295 if (strstart(keyname_buf, "0x", NULL)) {
1296 char *endp;
1297 int value = strtoul(keyname_buf, &endp, 0);
1298 if (*endp != '\0') {
1299 goto err_out;
1301 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1302 keylist->value->number = value;
1303 } else {
1304 int idx = index_from_key(keyname_buf);
1305 if (idx == Q_KEY_CODE_MAX) {
1306 goto err_out;
1308 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1309 keylist->value->qcode = idx;
1312 if (!separator) {
1313 break;
1315 keys = separator + 1;
1318 qmp_send_key(head, has_hold_time, hold_time, &err);
1319 hmp_handle_error(mon, &err);
1321 out:
1322 qapi_free_KeyValueList(head);
1323 return;
1325 err_out:
1326 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1327 goto out;
1330 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1332 const char *filename = qdict_get_str(qdict, "filename");
1333 Error *err = NULL;
1335 qmp_screendump(filename, &err);
1336 hmp_handle_error(mon, &err);
1339 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1341 const char *uri = qdict_get_str(qdict, "uri");
1342 int writable = qdict_get_try_bool(qdict, "writable", 0);
1343 int all = qdict_get_try_bool(qdict, "all", 0);
1344 Error *local_err = NULL;
1345 BlockInfoList *block_list, *info;
1346 SocketAddress *addr;
1348 if (writable && !all) {
1349 error_setg(&local_err, "-w only valid together with -a");
1350 goto exit;
1353 /* First check if the address is valid and start the server. */
1354 addr = socket_parse(uri, &local_err);
1355 if (local_err != NULL) {
1356 goto exit;
1359 qmp_nbd_server_start(addr, &local_err);
1360 qapi_free_SocketAddress(addr);
1361 if (local_err != NULL) {
1362 goto exit;
1365 if (!all) {
1366 return;
1369 /* Then try adding all block devices. If one fails, close all and
1370 * exit.
1372 block_list = qmp_query_block(NULL);
1374 for (info = block_list; info; info = info->next) {
1375 if (!info->value->has_inserted) {
1376 continue;
1379 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1381 if (local_err != NULL) {
1382 qmp_nbd_server_stop(NULL);
1383 break;
1387 qapi_free_BlockInfoList(block_list);
1389 exit:
1390 hmp_handle_error(mon, &local_err);
1393 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1395 const char *device = qdict_get_str(qdict, "device");
1396 int writable = qdict_get_try_bool(qdict, "writable", 0);
1397 Error *local_err = NULL;
1399 qmp_nbd_server_add(device, true, writable, &local_err);
1401 if (local_err != NULL) {
1402 hmp_handle_error(mon, &local_err);
1406 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1408 Error *errp = NULL;
1410 qmp_nbd_server_stop(&errp);
1411 hmp_handle_error(mon, &errp);
1414 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1416 const char *args = qdict_get_str(qdict, "args");
1417 Error *err = NULL;
1418 QemuOpts *opts;
1420 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1421 if (opts == NULL) {
1422 error_setg(&err, "Parsing chardev args failed");
1423 } else {
1424 qemu_chr_new_from_opts(opts, NULL, &err);
1426 hmp_handle_error(mon, &err);
1429 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1431 Error *local_err = NULL;
1433 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1434 hmp_handle_error(mon, &local_err);