libcacard: remove libcacard-specific CFLAGS and LIBS from global vars
[qemu.git] / hmp.c
blob903e0a1dd702f6b234b3112a78fb993545505557
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 "qapi/opts-visitor.h"
25 #include "ui/console.h"
26 #include "block/qapi.h"
27 #include "qemu-io.h"
29 static void hmp_handle_error(Monitor *mon, Error **errp)
31 if (error_is_set(errp)) {
32 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
33 error_free(*errp);
37 void hmp_info_name(Monitor *mon, const QDict *qdict)
39 NameInfo *info;
41 info = qmp_query_name(NULL);
42 if (info->has_name) {
43 monitor_printf(mon, "%s\n", info->name);
45 qapi_free_NameInfo(info);
48 void hmp_info_version(Monitor *mon, const QDict *qdict)
50 VersionInfo *info;
52 info = qmp_query_version(NULL);
54 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
55 info->qemu.major, info->qemu.minor, info->qemu.micro,
56 info->package);
58 qapi_free_VersionInfo(info);
61 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
63 KvmInfo *info;
65 info = qmp_query_kvm(NULL);
66 monitor_printf(mon, "kvm support: ");
67 if (info->present) {
68 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
69 } else {
70 monitor_printf(mon, "not compiled\n");
73 qapi_free_KvmInfo(info);
76 void hmp_info_status(Monitor *mon, const QDict *qdict)
78 StatusInfo *info;
80 info = qmp_query_status(NULL);
82 monitor_printf(mon, "VM status: %s%s",
83 info->running ? "running" : "paused",
84 info->singlestep ? " (single step mode)" : "");
86 if (!info->running && info->status != RUN_STATE_PAUSED) {
87 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
90 monitor_printf(mon, "\n");
92 qapi_free_StatusInfo(info);
95 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
97 UuidInfo *info;
99 info = qmp_query_uuid(NULL);
100 monitor_printf(mon, "%s\n", info->UUID);
101 qapi_free_UuidInfo(info);
104 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
106 ChardevInfoList *char_info, *info;
108 char_info = qmp_query_chardev(NULL);
109 for (info = char_info; info; info = info->next) {
110 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
111 info->value->filename);
114 qapi_free_ChardevInfoList(char_info);
117 void hmp_info_mice(Monitor *mon, const QDict *qdict)
119 MouseInfoList *mice_list, *mouse;
121 mice_list = qmp_query_mice(NULL);
122 if (!mice_list) {
123 monitor_printf(mon, "No mouse devices connected\n");
124 return;
127 for (mouse = mice_list; mouse; mouse = mouse->next) {
128 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
129 mouse->value->current ? '*' : ' ',
130 mouse->value->index, mouse->value->name,
131 mouse->value->absolute ? " (absolute)" : "");
134 qapi_free_MouseInfoList(mice_list);
137 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
139 MigrationInfo *info;
140 MigrationCapabilityStatusList *caps, *cap;
142 info = qmp_query_migrate(NULL);
143 caps = qmp_query_migrate_capabilities(NULL);
145 /* do not display parameters during setup */
146 if (info->has_status && caps) {
147 monitor_printf(mon, "capabilities: ");
148 for (cap = caps; cap; cap = cap->next) {
149 monitor_printf(mon, "%s: %s ",
150 MigrationCapability_lookup[cap->value->capability],
151 cap->value->state ? "on" : "off");
153 monitor_printf(mon, "\n");
156 if (info->has_status) {
157 monitor_printf(mon, "Migration status: %s\n", info->status);
158 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
159 info->total_time);
160 if (info->has_expected_downtime) {
161 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
162 info->expected_downtime);
164 if (info->has_downtime) {
165 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
166 info->downtime);
168 if (info->has_setup_time) {
169 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
170 info->setup_time);
174 if (info->has_ram) {
175 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
176 info->ram->transferred >> 10);
177 monitor_printf(mon, "throughput: %0.2f mbps\n",
178 info->ram->mbps);
179 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
180 info->ram->remaining >> 10);
181 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
182 info->ram->total >> 10);
183 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
184 info->ram->duplicate);
185 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
186 info->ram->skipped);
187 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
188 info->ram->normal);
189 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
190 info->ram->normal_bytes >> 10);
191 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
192 info->ram->dirty_sync_count);
193 if (info->ram->dirty_pages_rate) {
194 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
195 info->ram->dirty_pages_rate);
199 if (info->has_disk) {
200 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
201 info->disk->transferred >> 10);
202 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
203 info->disk->remaining >> 10);
204 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
205 info->disk->total >> 10);
208 if (info->has_xbzrle_cache) {
209 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
210 info->xbzrle_cache->cache_size);
211 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
212 info->xbzrle_cache->bytes >> 10);
213 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
214 info->xbzrle_cache->pages);
215 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
216 info->xbzrle_cache->cache_miss);
217 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
218 info->xbzrle_cache->cache_miss_rate);
219 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
220 info->xbzrle_cache->overflow);
223 qapi_free_MigrationInfo(info);
224 qapi_free_MigrationCapabilityStatusList(caps);
227 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
229 MigrationCapabilityStatusList *caps, *cap;
231 caps = qmp_query_migrate_capabilities(NULL);
233 if (caps) {
234 monitor_printf(mon, "capabilities: ");
235 for (cap = caps; cap; cap = cap->next) {
236 monitor_printf(mon, "%s: %s ",
237 MigrationCapability_lookup[cap->value->capability],
238 cap->value->state ? "on" : "off");
240 monitor_printf(mon, "\n");
243 qapi_free_MigrationCapabilityStatusList(caps);
246 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
248 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
249 qmp_query_migrate_cache_size(NULL) >> 10);
252 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
254 CpuInfoList *cpu_list, *cpu;
256 cpu_list = qmp_query_cpus(NULL);
258 for (cpu = cpu_list; cpu; cpu = cpu->next) {
259 int active = ' ';
261 if (cpu->value->CPU == monitor_get_cpu_index()) {
262 active = '*';
265 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
267 if (cpu->value->has_pc) {
268 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
270 if (cpu->value->has_nip) {
271 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
273 if (cpu->value->has_npc) {
274 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
276 if (cpu->value->has_PC) {
277 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
280 if (cpu->value->halted) {
281 monitor_printf(mon, " (halted)");
284 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
287 qapi_free_CpuInfoList(cpu_list);
290 void hmp_info_block(Monitor *mon, const QDict *qdict)
292 BlockInfoList *block_list, *info;
293 ImageInfo *image_info;
294 const char *device = qdict_get_try_str(qdict, "device");
295 bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
297 block_list = qmp_query_block(NULL);
299 for (info = block_list; info; info = info->next) {
300 if (device && strcmp(device, info->value->device)) {
301 continue;
304 if (info != block_list) {
305 monitor_printf(mon, "\n");
308 monitor_printf(mon, "%s", info->value->device);
309 if (info->value->has_inserted) {
310 monitor_printf(mon, ": %s (%s%s%s)\n",
311 info->value->inserted->file,
312 info->value->inserted->drv,
313 info->value->inserted->ro ? ", read-only" : "",
314 info->value->inserted->encrypted ? ", encrypted" : "");
315 } else {
316 monitor_printf(mon, ": [not inserted]\n");
319 if (info->value->has_io_status && info->value->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
320 monitor_printf(mon, " I/O status: %s\n",
321 BlockDeviceIoStatus_lookup[info->value->io_status]);
324 if (info->value->removable) {
325 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
326 info->value->locked ? "" : "not ",
327 info->value->tray_open ? "open" : "closed");
331 if (!info->value->has_inserted) {
332 continue;
335 if (info->value->inserted->has_backing_file) {
336 monitor_printf(mon,
337 " Backing file: %s "
338 "(chain depth: %" PRId64 ")\n",
339 info->value->inserted->backing_file,
340 info->value->inserted->backing_file_depth);
343 if (info->value->inserted->bps
344 || info->value->inserted->bps_rd
345 || info->value->inserted->bps_wr
346 || info->value->inserted->iops
347 || info->value->inserted->iops_rd
348 || info->value->inserted->iops_wr)
350 monitor_printf(mon, " I/O throttling: bps=%" PRId64
351 " bps_rd=%" PRId64 " bps_wr=%" PRId64
352 " bps_max=%" PRId64
353 " bps_rd_max=%" PRId64
354 " bps_wr_max=%" PRId64
355 " iops=%" PRId64 " iops_rd=%" PRId64
356 " iops_wr=%" PRId64
357 " iops_max=%" PRId64
358 " iops_rd_max=%" PRId64
359 " iops_wr_max=%" PRId64
360 " iops_size=%" PRId64 "\n",
361 info->value->inserted->bps,
362 info->value->inserted->bps_rd,
363 info->value->inserted->bps_wr,
364 info->value->inserted->bps_max,
365 info->value->inserted->bps_rd_max,
366 info->value->inserted->bps_wr_max,
367 info->value->inserted->iops,
368 info->value->inserted->iops_rd,
369 info->value->inserted->iops_wr,
370 info->value->inserted->iops_max,
371 info->value->inserted->iops_rd_max,
372 info->value->inserted->iops_wr_max,
373 info->value->inserted->iops_size);
376 if (verbose) {
377 monitor_printf(mon, "\nImages:\n");
378 image_info = info->value->inserted->image;
379 while (1) {
380 bdrv_image_info_dump((fprintf_function)monitor_printf,
381 mon, image_info);
382 if (image_info->has_backing_image) {
383 image_info = image_info->backing_image;
384 } else {
385 break;
391 qapi_free_BlockInfoList(block_list);
394 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
396 BlockStatsList *stats_list, *stats;
398 stats_list = qmp_query_blockstats(NULL);
400 for (stats = stats_list; stats; stats = stats->next) {
401 if (!stats->value->has_device) {
402 continue;
405 monitor_printf(mon, "%s:", stats->value->device);
406 monitor_printf(mon, " rd_bytes=%" PRId64
407 " wr_bytes=%" PRId64
408 " rd_operations=%" PRId64
409 " wr_operations=%" PRId64
410 " flush_operations=%" PRId64
411 " wr_total_time_ns=%" PRId64
412 " rd_total_time_ns=%" PRId64
413 " flush_total_time_ns=%" PRId64
414 "\n",
415 stats->value->stats->rd_bytes,
416 stats->value->stats->wr_bytes,
417 stats->value->stats->rd_operations,
418 stats->value->stats->wr_operations,
419 stats->value->stats->flush_operations,
420 stats->value->stats->wr_total_time_ns,
421 stats->value->stats->rd_total_time_ns,
422 stats->value->stats->flush_total_time_ns);
425 qapi_free_BlockStatsList(stats_list);
428 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
430 VncInfo *info;
431 Error *err = NULL;
432 VncClientInfoList *client;
434 info = qmp_query_vnc(&err);
435 if (err) {
436 monitor_printf(mon, "%s\n", error_get_pretty(err));
437 error_free(err);
438 return;
441 if (!info->enabled) {
442 monitor_printf(mon, "Server: disabled\n");
443 goto out;
446 monitor_printf(mon, "Server:\n");
447 if (info->has_host && info->has_service) {
448 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
450 if (info->has_auth) {
451 monitor_printf(mon, " auth: %s\n", info->auth);
454 if (!info->has_clients || info->clients == NULL) {
455 monitor_printf(mon, "Client: none\n");
456 } else {
457 for (client = info->clients; client; client = client->next) {
458 monitor_printf(mon, "Client:\n");
459 monitor_printf(mon, " address: %s:%s\n",
460 client->value->host, client->value->service);
461 monitor_printf(mon, " x509_dname: %s\n",
462 client->value->x509_dname ?
463 client->value->x509_dname : "none");
464 monitor_printf(mon, " username: %s\n",
465 client->value->has_sasl_username ?
466 client->value->sasl_username : "none");
470 out:
471 qapi_free_VncInfo(info);
474 void hmp_info_spice(Monitor *mon, const QDict *qdict)
476 SpiceChannelList *chan;
477 SpiceInfo *info;
479 info = qmp_query_spice(NULL);
481 if (!info->enabled) {
482 monitor_printf(mon, "Server: disabled\n");
483 goto out;
486 monitor_printf(mon, "Server:\n");
487 if (info->has_port) {
488 monitor_printf(mon, " address: %s:%" PRId64 "\n",
489 info->host, info->port);
491 if (info->has_tls_port) {
492 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
493 info->host, info->tls_port);
495 monitor_printf(mon, " migrated: %s\n",
496 info->migrated ? "true" : "false");
497 monitor_printf(mon, " auth: %s\n", info->auth);
498 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
499 monitor_printf(mon, " mouse-mode: %s\n",
500 SpiceQueryMouseMode_lookup[info->mouse_mode]);
502 if (!info->has_channels || info->channels == NULL) {
503 monitor_printf(mon, "Channels: none\n");
504 } else {
505 for (chan = info->channels; chan; chan = chan->next) {
506 monitor_printf(mon, "Channel:\n");
507 monitor_printf(mon, " address: %s:%s%s\n",
508 chan->value->host, chan->value->port,
509 chan->value->tls ? " [tls]" : "");
510 monitor_printf(mon, " session: %" PRId64 "\n",
511 chan->value->connection_id);
512 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
513 chan->value->channel_type, chan->value->channel_id);
517 out:
518 qapi_free_SpiceInfo(info);
521 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
523 BalloonInfo *info;
524 Error *err = NULL;
526 info = qmp_query_balloon(&err);
527 if (err) {
528 monitor_printf(mon, "%s\n", error_get_pretty(err));
529 error_free(err);
530 return;
533 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
535 qapi_free_BalloonInfo(info);
538 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
540 PciMemoryRegionList *region;
542 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
543 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
544 dev->slot, dev->function);
545 monitor_printf(mon, " ");
547 if (dev->class_info.has_desc) {
548 monitor_printf(mon, "%s", dev->class_info.desc);
549 } else {
550 monitor_printf(mon, "Class %04" PRId64, dev->class_info.q_class);
553 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
554 dev->id.vendor, dev->id.device);
556 if (dev->has_irq) {
557 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
560 if (dev->has_pci_bridge) {
561 monitor_printf(mon, " BUS %" PRId64 ".\n",
562 dev->pci_bridge->bus.number);
563 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
564 dev->pci_bridge->bus.secondary);
565 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
566 dev->pci_bridge->bus.subordinate);
568 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
569 dev->pci_bridge->bus.io_range->base,
570 dev->pci_bridge->bus.io_range->limit);
572 monitor_printf(mon,
573 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
574 dev->pci_bridge->bus.memory_range->base,
575 dev->pci_bridge->bus.memory_range->limit);
577 monitor_printf(mon, " prefetchable memory range "
578 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
579 dev->pci_bridge->bus.prefetchable_range->base,
580 dev->pci_bridge->bus.prefetchable_range->limit);
583 for (region = dev->regions; region; region = region->next) {
584 uint64_t addr, size;
586 addr = region->value->address;
587 size = region->value->size;
589 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
591 if (!strcmp(region->value->type, "io")) {
592 monitor_printf(mon, "I/O at 0x%04" PRIx64
593 " [0x%04" PRIx64 "].\n",
594 addr, addr + size - 1);
595 } else {
596 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
597 " [0x%08" PRIx64 "].\n",
598 region->value->mem_type_64 ? 64 : 32,
599 region->value->prefetch ? " prefetchable" : "",
600 addr, addr + size - 1);
604 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
606 if (dev->has_pci_bridge) {
607 if (dev->pci_bridge->has_devices) {
608 PciDeviceInfoList *cdev;
609 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
610 hmp_info_pci_device(mon, cdev->value);
616 void hmp_info_pci(Monitor *mon, const QDict *qdict)
618 PciInfoList *info_list, *info;
619 Error *err = NULL;
621 info_list = qmp_query_pci(&err);
622 if (err) {
623 monitor_printf(mon, "PCI devices not supported\n");
624 error_free(err);
625 return;
628 for (info = info_list; info; info = info->next) {
629 PciDeviceInfoList *dev;
631 for (dev = info->value->devices; dev; dev = dev->next) {
632 hmp_info_pci_device(mon, dev->value);
636 qapi_free_PciInfoList(info_list);
639 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
641 BlockJobInfoList *list;
642 Error *err = NULL;
644 list = qmp_query_block_jobs(&err);
645 assert(!err);
647 if (!list) {
648 monitor_printf(mon, "No active jobs\n");
649 return;
652 while (list) {
653 if (strcmp(list->value->type, "stream") == 0) {
654 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
655 " of %" PRId64 " bytes, speed limit %" PRId64
656 " bytes/s\n",
657 list->value->device,
658 list->value->offset,
659 list->value->len,
660 list->value->speed);
661 } else {
662 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
663 " of %" PRId64 " bytes, speed limit %" PRId64
664 " bytes/s\n",
665 list->value->type,
666 list->value->device,
667 list->value->offset,
668 list->value->len,
669 list->value->speed);
671 list = list->next;
675 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
677 TPMInfoList *info_list, *info;
678 Error *err = NULL;
679 unsigned int c = 0;
680 TPMPassthroughOptions *tpo;
682 info_list = qmp_query_tpm(&err);
683 if (err) {
684 monitor_printf(mon, "TPM device not supported\n");
685 error_free(err);
686 return;
689 if (info_list) {
690 monitor_printf(mon, "TPM device:\n");
693 for (info = info_list; info; info = info->next) {
694 TPMInfo *ti = info->value;
695 monitor_printf(mon, " tpm%d: model=%s\n",
696 c, TpmModel_lookup[ti->model]);
698 monitor_printf(mon, " \\ %s: type=%s",
699 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
701 switch (ti->options->kind) {
702 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
703 tpo = ti->options->passthrough;
704 monitor_printf(mon, "%s%s%s%s",
705 tpo->has_path ? ",path=" : "",
706 tpo->has_path ? tpo->path : "",
707 tpo->has_cancel_path ? ",cancel-path=" : "",
708 tpo->has_cancel_path ? tpo->cancel_path : "");
709 break;
710 case TPM_TYPE_OPTIONS_KIND_MAX:
711 break;
713 monitor_printf(mon, "\n");
714 c++;
716 qapi_free_TPMInfoList(info_list);
719 void hmp_quit(Monitor *mon, const QDict *qdict)
721 monitor_suspend(mon);
722 qmp_quit(NULL);
725 void hmp_stop(Monitor *mon, const QDict *qdict)
727 qmp_stop(NULL);
730 void hmp_system_reset(Monitor *mon, const QDict *qdict)
732 qmp_system_reset(NULL);
735 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
737 qmp_system_powerdown(NULL);
740 void hmp_cpu(Monitor *mon, const QDict *qdict)
742 int64_t cpu_index;
744 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
745 use it are converted to the QAPI */
746 cpu_index = qdict_get_int(qdict, "index");
747 if (monitor_set_cpu(cpu_index) < 0) {
748 monitor_printf(mon, "invalid CPU index\n");
752 void hmp_memsave(Monitor *mon, const QDict *qdict)
754 uint32_t size = qdict_get_int(qdict, "size");
755 const char *filename = qdict_get_str(qdict, "filename");
756 uint64_t addr = qdict_get_int(qdict, "val");
757 Error *errp = NULL;
759 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
760 hmp_handle_error(mon, &errp);
763 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
765 uint32_t size = qdict_get_int(qdict, "size");
766 const char *filename = qdict_get_str(qdict, "filename");
767 uint64_t addr = qdict_get_int(qdict, "val");
768 Error *errp = NULL;
770 qmp_pmemsave(addr, size, filename, &errp);
771 hmp_handle_error(mon, &errp);
774 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
776 const char *chardev = qdict_get_str(qdict, "device");
777 const char *data = qdict_get_str(qdict, "data");
778 Error *errp = NULL;
780 qmp_ringbuf_write(chardev, data, false, 0, &errp);
782 hmp_handle_error(mon, &errp);
785 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
787 uint32_t size = qdict_get_int(qdict, "size");
788 const char *chardev = qdict_get_str(qdict, "device");
789 char *data;
790 Error *errp = NULL;
791 int i;
793 data = qmp_ringbuf_read(chardev, size, false, 0, &errp);
794 if (errp) {
795 monitor_printf(mon, "%s\n", error_get_pretty(errp));
796 error_free(errp);
797 return;
800 for (i = 0; data[i]; i++) {
801 unsigned char ch = data[i];
803 if (ch == '\\') {
804 monitor_printf(mon, "\\\\");
805 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
806 monitor_printf(mon, "\\u%04X", ch);
807 } else {
808 monitor_printf(mon, "%c", ch);
812 monitor_printf(mon, "\n");
813 g_free(data);
816 static void hmp_cont_cb(void *opaque, int err)
818 if (!err) {
819 qmp_cont(NULL);
823 static bool key_is_missing(const BlockInfo *bdev)
825 return (bdev->inserted && bdev->inserted->encryption_key_missing);
828 void hmp_cont(Monitor *mon, const QDict *qdict)
830 BlockInfoList *bdev_list, *bdev;
831 Error *errp = NULL;
833 bdev_list = qmp_query_block(NULL);
834 for (bdev = bdev_list; bdev; bdev = bdev->next) {
835 if (key_is_missing(bdev->value)) {
836 monitor_read_block_device_key(mon, bdev->value->device,
837 hmp_cont_cb, NULL);
838 goto out;
842 qmp_cont(&errp);
843 hmp_handle_error(mon, &errp);
845 out:
846 qapi_free_BlockInfoList(bdev_list);
849 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
851 qmp_system_wakeup(NULL);
854 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
856 Error *errp = NULL;
858 qmp_inject_nmi(&errp);
859 hmp_handle_error(mon, &errp);
862 void hmp_set_link(Monitor *mon, const QDict *qdict)
864 const char *name = qdict_get_str(qdict, "name");
865 int up = qdict_get_bool(qdict, "up");
866 Error *errp = NULL;
868 qmp_set_link(name, up, &errp);
869 hmp_handle_error(mon, &errp);
872 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
874 const char *device = qdict_get_str(qdict, "device");
875 const char *password = qdict_get_str(qdict, "password");
876 Error *errp = NULL;
878 qmp_block_passwd(true, device, false, NULL, password, &errp);
879 hmp_handle_error(mon, &errp);
882 void hmp_balloon(Monitor *mon, const QDict *qdict)
884 int64_t value = qdict_get_int(qdict, "value");
885 Error *errp = NULL;
887 qmp_balloon(value, &errp);
888 if (errp) {
889 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
890 error_free(errp);
894 void hmp_block_resize(Monitor *mon, const QDict *qdict)
896 const char *device = qdict_get_str(qdict, "device");
897 int64_t size = qdict_get_int(qdict, "size");
898 Error *errp = NULL;
900 qmp_block_resize(true, device, false, NULL, size, &errp);
901 hmp_handle_error(mon, &errp);
904 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
906 const char *device = qdict_get_str(qdict, "device");
907 const char *filename = qdict_get_str(qdict, "target");
908 const char *format = qdict_get_try_str(qdict, "format");
909 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
910 int full = qdict_get_try_bool(qdict, "full", 0);
911 enum NewImageMode mode;
912 Error *errp = NULL;
914 if (!filename) {
915 error_set(&errp, QERR_MISSING_PARAMETER, "target");
916 hmp_handle_error(mon, &errp);
917 return;
920 if (reuse) {
921 mode = NEW_IMAGE_MODE_EXISTING;
922 } else {
923 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
926 qmp_drive_mirror(device, filename, !!format, format,
927 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
928 true, mode, false, 0, false, 0, false, 0,
929 false, 0, false, 0, &errp);
930 hmp_handle_error(mon, &errp);
933 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
935 const char *device = qdict_get_str(qdict, "device");
936 const char *filename = qdict_get_str(qdict, "target");
937 const char *format = qdict_get_try_str(qdict, "format");
938 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
939 int full = qdict_get_try_bool(qdict, "full", 0);
940 enum NewImageMode mode;
941 Error *errp = NULL;
943 if (!filename) {
944 error_set(&errp, QERR_MISSING_PARAMETER, "target");
945 hmp_handle_error(mon, &errp);
946 return;
949 if (reuse) {
950 mode = NEW_IMAGE_MODE_EXISTING;
951 } else {
952 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
955 qmp_drive_backup(device, filename, !!format, format,
956 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
957 true, mode, false, 0, false, 0, false, 0, &errp);
958 hmp_handle_error(mon, &errp);
961 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
963 const char *device = qdict_get_str(qdict, "device");
964 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
965 const char *format = qdict_get_try_str(qdict, "format");
966 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
967 enum NewImageMode mode;
968 Error *errp = NULL;
970 if (!filename) {
971 /* In the future, if 'snapshot-file' is not specified, the snapshot
972 will be taken internally. Today it's actually required. */
973 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
974 hmp_handle_error(mon, &errp);
975 return;
978 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
979 qmp_blockdev_snapshot_sync(true, device, false, NULL,
980 filename, false, NULL,
981 !!format, format,
982 true, mode, &errp);
983 hmp_handle_error(mon, &errp);
986 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
988 const char *device = qdict_get_str(qdict, "device");
989 const char *name = qdict_get_str(qdict, "name");
990 Error *errp = NULL;
992 qmp_blockdev_snapshot_internal_sync(device, name, &errp);
993 hmp_handle_error(mon, &errp);
996 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
998 const char *device = qdict_get_str(qdict, "device");
999 const char *name = qdict_get_str(qdict, "name");
1000 const char *id = qdict_get_try_str(qdict, "id");
1001 Error *errp = NULL;
1003 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1004 true, name, &errp);
1005 hmp_handle_error(mon, &errp);
1008 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1010 qmp_migrate_cancel(NULL);
1013 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1015 double value = qdict_get_double(qdict, "value");
1016 qmp_migrate_set_downtime(value, NULL);
1019 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1021 int64_t value = qdict_get_int(qdict, "value");
1022 Error *err = NULL;
1024 qmp_migrate_set_cache_size(value, &err);
1025 if (err) {
1026 monitor_printf(mon, "%s\n", error_get_pretty(err));
1027 error_free(err);
1028 return;
1032 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1034 int64_t value = qdict_get_int(qdict, "value");
1035 qmp_migrate_set_speed(value, NULL);
1038 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1040 const char *cap = qdict_get_str(qdict, "capability");
1041 bool state = qdict_get_bool(qdict, "state");
1042 Error *err = NULL;
1043 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1044 int i;
1046 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
1047 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1048 caps->value = g_malloc0(sizeof(*caps->value));
1049 caps->value->capability = i;
1050 caps->value->state = state;
1051 caps->next = NULL;
1052 qmp_migrate_set_capabilities(caps, &err);
1053 break;
1057 if (i == MIGRATION_CAPABILITY_MAX) {
1058 error_set(&err, QERR_INVALID_PARAMETER, cap);
1061 qapi_free_MigrationCapabilityStatusList(caps);
1063 if (err) {
1064 monitor_printf(mon, "migrate_set_capability: %s\n",
1065 error_get_pretty(err));
1066 error_free(err);
1070 void hmp_set_password(Monitor *mon, const QDict *qdict)
1072 const char *protocol = qdict_get_str(qdict, "protocol");
1073 const char *password = qdict_get_str(qdict, "password");
1074 const char *connected = qdict_get_try_str(qdict, "connected");
1075 Error *err = NULL;
1077 qmp_set_password(protocol, password, !!connected, connected, &err);
1078 hmp_handle_error(mon, &err);
1081 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1083 const char *protocol = qdict_get_str(qdict, "protocol");
1084 const char *whenstr = qdict_get_str(qdict, "time");
1085 Error *err = NULL;
1087 qmp_expire_password(protocol, whenstr, &err);
1088 hmp_handle_error(mon, &err);
1091 void hmp_eject(Monitor *mon, const QDict *qdict)
1093 int force = qdict_get_try_bool(qdict, "force", 0);
1094 const char *device = qdict_get_str(qdict, "device");
1095 Error *err = NULL;
1097 qmp_eject(device, true, force, &err);
1098 hmp_handle_error(mon, &err);
1101 static void hmp_change_read_arg(void *opaque, const char *password,
1102 void *readline_opaque)
1104 qmp_change_vnc_password(password, NULL);
1105 monitor_read_command(opaque, 1);
1108 void hmp_change(Monitor *mon, const QDict *qdict)
1110 const char *device = qdict_get_str(qdict, "device");
1111 const char *target = qdict_get_str(qdict, "target");
1112 const char *arg = qdict_get_try_str(qdict, "arg");
1113 Error *err = NULL;
1115 if (strcmp(device, "vnc") == 0 &&
1116 (strcmp(target, "passwd") == 0 ||
1117 strcmp(target, "password") == 0)) {
1118 if (!arg) {
1119 monitor_read_password(mon, hmp_change_read_arg, NULL);
1120 return;
1124 qmp_change(device, target, !!arg, arg, &err);
1125 if (err &&
1126 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1127 error_free(err);
1128 monitor_read_block_device_key(mon, device, NULL, NULL);
1129 return;
1131 hmp_handle_error(mon, &err);
1134 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1136 Error *err = NULL;
1138 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1139 qdict_get_int(qdict, "bps"),
1140 qdict_get_int(qdict, "bps_rd"),
1141 qdict_get_int(qdict, "bps_wr"),
1142 qdict_get_int(qdict, "iops"),
1143 qdict_get_int(qdict, "iops_rd"),
1144 qdict_get_int(qdict, "iops_wr"),
1145 false, /* no burst max via HMP */
1147 false,
1149 false,
1151 false,
1153 false,
1155 false,
1157 false, /* No default I/O size */
1158 0, &err);
1159 hmp_handle_error(mon, &err);
1162 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1164 Error *error = NULL;
1165 const char *device = qdict_get_str(qdict, "device");
1166 const char *base = qdict_get_try_str(qdict, "base");
1167 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1169 qmp_block_stream(device, base != NULL, base,
1170 qdict_haskey(qdict, "speed"), speed,
1171 true, BLOCKDEV_ON_ERROR_REPORT, &error);
1173 hmp_handle_error(mon, &error);
1176 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1178 Error *error = NULL;
1179 const char *device = qdict_get_str(qdict, "device");
1180 int64_t value = qdict_get_int(qdict, "speed");
1182 qmp_block_job_set_speed(device, value, &error);
1184 hmp_handle_error(mon, &error);
1187 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1189 Error *error = NULL;
1190 const char *device = qdict_get_str(qdict, "device");
1191 bool force = qdict_get_try_bool(qdict, "force", 0);
1193 qmp_block_job_cancel(device, true, force, &error);
1195 hmp_handle_error(mon, &error);
1198 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1200 Error *error = NULL;
1201 const char *device = qdict_get_str(qdict, "device");
1203 qmp_block_job_pause(device, &error);
1205 hmp_handle_error(mon, &error);
1208 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1210 Error *error = NULL;
1211 const char *device = qdict_get_str(qdict, "device");
1213 qmp_block_job_resume(device, &error);
1215 hmp_handle_error(mon, &error);
1218 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1220 Error *error = NULL;
1221 const char *device = qdict_get_str(qdict, "device");
1223 qmp_block_job_complete(device, &error);
1225 hmp_handle_error(mon, &error);
1228 typedef struct MigrationStatus
1230 QEMUTimer *timer;
1231 Monitor *mon;
1232 bool is_block_migration;
1233 } MigrationStatus;
1235 static void hmp_migrate_status_cb(void *opaque)
1237 MigrationStatus *status = opaque;
1238 MigrationInfo *info;
1240 info = qmp_query_migrate(NULL);
1241 if (!info->has_status || strcmp(info->status, "active") == 0 ||
1242 strcmp(info->status, "setup") == 0) {
1243 if (info->has_disk) {
1244 int progress;
1246 if (info->disk->remaining) {
1247 progress = info->disk->transferred * 100 / info->disk->total;
1248 } else {
1249 progress = 100;
1252 monitor_printf(status->mon, "Completed %d %%\r", progress);
1253 monitor_flush(status->mon);
1256 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1257 } else {
1258 if (status->is_block_migration) {
1259 monitor_printf(status->mon, "\n");
1261 monitor_resume(status->mon);
1262 timer_del(status->timer);
1263 g_free(status);
1266 qapi_free_MigrationInfo(info);
1269 void hmp_migrate(Monitor *mon, const QDict *qdict)
1271 int detach = qdict_get_try_bool(qdict, "detach", 0);
1272 int blk = qdict_get_try_bool(qdict, "blk", 0);
1273 int inc = qdict_get_try_bool(qdict, "inc", 0);
1274 const char *uri = qdict_get_str(qdict, "uri");
1275 Error *err = NULL;
1277 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1278 if (err) {
1279 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1280 error_free(err);
1281 return;
1284 if (!detach) {
1285 MigrationStatus *status;
1287 if (monitor_suspend(mon) < 0) {
1288 monitor_printf(mon, "terminal does not allow synchronous "
1289 "migration, continuing detached\n");
1290 return;
1293 status = g_malloc0(sizeof(*status));
1294 status->mon = mon;
1295 status->is_block_migration = blk || inc;
1296 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1297 status);
1298 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1302 void hmp_device_del(Monitor *mon, const QDict *qdict)
1304 const char *id = qdict_get_str(qdict, "id");
1305 Error *err = NULL;
1307 qmp_device_del(id, &err);
1308 hmp_handle_error(mon, &err);
1311 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1313 Error *errp = NULL;
1314 int paging = qdict_get_try_bool(qdict, "paging", 0);
1315 int zlib = qdict_get_try_bool(qdict, "zlib", 0);
1316 int lzo = qdict_get_try_bool(qdict, "lzo", 0);
1317 int snappy = qdict_get_try_bool(qdict, "snappy", 0);
1318 const char *file = qdict_get_str(qdict, "filename");
1319 bool has_begin = qdict_haskey(qdict, "begin");
1320 bool has_length = qdict_haskey(qdict, "length");
1321 int64_t begin = 0;
1322 int64_t length = 0;
1323 enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
1324 char *prot;
1326 if (zlib + lzo + snappy > 1) {
1327 error_setg(&errp, "only one of '-z|-l|-s' can be set");
1328 hmp_handle_error(mon, &errp);
1329 return;
1332 if (zlib) {
1333 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1336 if (lzo) {
1337 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1340 if (snappy) {
1341 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1344 if (has_begin) {
1345 begin = qdict_get_int(qdict, "begin");
1347 if (has_length) {
1348 length = qdict_get_int(qdict, "length");
1351 prot = g_strconcat("file:", file, NULL);
1353 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1354 true, dump_format, &errp);
1355 hmp_handle_error(mon, &errp);
1356 g_free(prot);
1359 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1361 Error *err = NULL;
1362 QemuOpts *opts;
1364 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1365 if (err) {
1366 goto out;
1369 netdev_add(opts, &err);
1370 if (err) {
1371 qemu_opts_del(opts);
1374 out:
1375 hmp_handle_error(mon, &err);
1378 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1380 const char *id = qdict_get_str(qdict, "id");
1381 Error *err = NULL;
1383 qmp_netdev_del(id, &err);
1384 hmp_handle_error(mon, &err);
1387 void hmp_object_add(Monitor *mon, const QDict *qdict)
1389 Error *err = NULL;
1390 QemuOpts *opts;
1391 char *type = NULL;
1392 char *id = NULL;
1393 void *dummy = NULL;
1394 OptsVisitor *ov;
1395 QDict *pdict;
1397 opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1398 if (err) {
1399 goto out;
1402 ov = opts_visitor_new(opts);
1403 pdict = qdict_clone_shallow(qdict);
1405 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
1406 if (err) {
1407 goto out_clean;
1410 qdict_del(pdict, "qom-type");
1411 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
1412 if (err) {
1413 goto out_clean;
1416 qdict_del(pdict, "id");
1417 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
1418 if (err) {
1419 goto out_clean;
1422 object_add(type, id, pdict, opts_get_visitor(ov), &err);
1423 if (err) {
1424 goto out_clean;
1426 visit_end_struct(opts_get_visitor(ov), &err);
1427 if (err) {
1428 qmp_object_del(id, NULL);
1431 out_clean:
1432 opts_visitor_cleanup(ov);
1434 QDECREF(pdict);
1435 qemu_opts_del(opts);
1436 g_free(id);
1437 g_free(type);
1438 g_free(dummy);
1440 out:
1441 hmp_handle_error(mon, &err);
1444 void hmp_getfd(Monitor *mon, const QDict *qdict)
1446 const char *fdname = qdict_get_str(qdict, "fdname");
1447 Error *errp = NULL;
1449 qmp_getfd(fdname, &errp);
1450 hmp_handle_error(mon, &errp);
1453 void hmp_closefd(Monitor *mon, const QDict *qdict)
1455 const char *fdname = qdict_get_str(qdict, "fdname");
1456 Error *errp = NULL;
1458 qmp_closefd(fdname, &errp);
1459 hmp_handle_error(mon, &errp);
1462 void hmp_send_key(Monitor *mon, const QDict *qdict)
1464 const char *keys = qdict_get_str(qdict, "keys");
1465 KeyValueList *keylist, *head = NULL, *tmp = NULL;
1466 int has_hold_time = qdict_haskey(qdict, "hold-time");
1467 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1468 Error *err = NULL;
1469 char keyname_buf[16];
1470 char *separator;
1471 int keyname_len;
1473 while (1) {
1474 separator = strchr(keys, '-');
1475 keyname_len = separator ? separator - keys : strlen(keys);
1476 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1478 /* Be compatible with old interface, convert user inputted "<" */
1479 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1480 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1481 keyname_len = 4;
1483 keyname_buf[keyname_len] = 0;
1485 keylist = g_malloc0(sizeof(*keylist));
1486 keylist->value = g_malloc0(sizeof(*keylist->value));
1488 if (!head) {
1489 head = keylist;
1491 if (tmp) {
1492 tmp->next = keylist;
1494 tmp = keylist;
1496 if (strstart(keyname_buf, "0x", NULL)) {
1497 char *endp;
1498 int value = strtoul(keyname_buf, &endp, 0);
1499 if (*endp != '\0') {
1500 goto err_out;
1502 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1503 keylist->value->number = value;
1504 } else {
1505 int idx = index_from_key(keyname_buf);
1506 if (idx == Q_KEY_CODE_MAX) {
1507 goto err_out;
1509 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1510 keylist->value->qcode = idx;
1513 if (!separator) {
1514 break;
1516 keys = separator + 1;
1519 qmp_send_key(head, has_hold_time, hold_time, &err);
1520 hmp_handle_error(mon, &err);
1522 out:
1523 qapi_free_KeyValueList(head);
1524 return;
1526 err_out:
1527 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1528 goto out;
1531 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1533 const char *filename = qdict_get_str(qdict, "filename");
1534 Error *err = NULL;
1536 qmp_screendump(filename, &err);
1537 hmp_handle_error(mon, &err);
1540 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1542 const char *uri = qdict_get_str(qdict, "uri");
1543 int writable = qdict_get_try_bool(qdict, "writable", 0);
1544 int all = qdict_get_try_bool(qdict, "all", 0);
1545 Error *local_err = NULL;
1546 BlockInfoList *block_list, *info;
1547 SocketAddress *addr;
1549 if (writable && !all) {
1550 error_setg(&local_err, "-w only valid together with -a");
1551 goto exit;
1554 /* First check if the address is valid and start the server. */
1555 addr = socket_parse(uri, &local_err);
1556 if (local_err != NULL) {
1557 goto exit;
1560 qmp_nbd_server_start(addr, &local_err);
1561 qapi_free_SocketAddress(addr);
1562 if (local_err != NULL) {
1563 goto exit;
1566 if (!all) {
1567 return;
1570 /* Then try adding all block devices. If one fails, close all and
1571 * exit.
1573 block_list = qmp_query_block(NULL);
1575 for (info = block_list; info; info = info->next) {
1576 if (!info->value->has_inserted) {
1577 continue;
1580 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1582 if (local_err != NULL) {
1583 qmp_nbd_server_stop(NULL);
1584 break;
1588 qapi_free_BlockInfoList(block_list);
1590 exit:
1591 hmp_handle_error(mon, &local_err);
1594 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1596 const char *device = qdict_get_str(qdict, "device");
1597 int writable = qdict_get_try_bool(qdict, "writable", 0);
1598 Error *local_err = NULL;
1600 qmp_nbd_server_add(device, true, writable, &local_err);
1602 if (local_err != NULL) {
1603 hmp_handle_error(mon, &local_err);
1607 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1609 Error *errp = NULL;
1611 qmp_nbd_server_stop(&errp);
1612 hmp_handle_error(mon, &errp);
1615 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
1617 int cpuid;
1618 Error *err = NULL;
1620 cpuid = qdict_get_int(qdict, "id");
1621 qmp_cpu_add(cpuid, &err);
1622 hmp_handle_error(mon, &err);
1625 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1627 const char *args = qdict_get_str(qdict, "args");
1628 Error *err = NULL;
1629 QemuOpts *opts;
1631 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1632 if (opts == NULL) {
1633 error_setg(&err, "Parsing chardev args failed");
1634 } else {
1635 qemu_chr_new_from_opts(opts, NULL, &err);
1637 hmp_handle_error(mon, &err);
1640 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1642 Error *local_err = NULL;
1644 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1645 hmp_handle_error(mon, &local_err);
1648 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1650 BlockDriverState *bs;
1651 const char* device = qdict_get_str(qdict, "device");
1652 const char* command = qdict_get_str(qdict, "command");
1653 Error *err = NULL;
1655 bs = bdrv_find(device);
1656 if (bs) {
1657 qemuio_command(bs, command);
1658 } else {
1659 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1662 hmp_handle_error(mon, &err);
1665 void hmp_object_del(Monitor *mon, const QDict *qdict)
1667 const char *id = qdict_get_str(qdict, "id");
1668 Error *err = NULL;
1670 qmp_object_del(id, &err);
1671 hmp_handle_error(mon, &err);