]> git.proxmox.com Git - mirror_qemu.git/blame - hmp.c
block/hmp: Factor out print_block_info()
[mirror_qemu.git] / hmp.c
CommitLineData
48a32bed
AL
1/*
2 * Human Monitor Interface
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
6b620ca3
PB
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.
48a32bed
AL
14 */
15
16#include "hmp.h"
1422e32d 17#include "net/net.h"
dccfcd0e 18#include "sysemu/char.h"
1de7afc9
PB
19#include "qemu/option.h"
20#include "qemu/timer.h"
48a32bed 21#include "qmp-commands.h"
1de7afc9 22#include "qemu/sockets.h"
83c9089e 23#include "monitor/monitor.h"
cff8b2c6 24#include "qapi/opts-visitor.h"
eb1539b2
HT
25#include "qapi/string-output-visitor.h"
26#include "qapi-visit.h"
28ecbaee 27#include "ui/console.h"
bd093a36 28#include "block/qapi.h"
587da2c3 29#include "qemu-io.h"
48a32bed 30
0cfd6a9a
LC
31static void hmp_handle_error(Monitor *mon, Error **errp)
32{
415168e0
MA
33 assert(errp);
34 if (*errp) {
0cfd6a9a
LC
35 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
36 error_free(*errp);
37 }
38}
39
84f2d0ea 40void hmp_info_name(Monitor *mon, const QDict *qdict)
48a32bed
AL
41{
42 NameInfo *info;
43
44 info = qmp_query_name(NULL);
45 if (info->has_name) {
46 monitor_printf(mon, "%s\n", info->name);
47 }
48 qapi_free_NameInfo(info);
49}
b9c15f16 50
84f2d0ea 51void hmp_info_version(Monitor *mon, const QDict *qdict)
b9c15f16
LC
52{
53 VersionInfo *info;
54
55 info = qmp_query_version(NULL);
56
57 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
58 info->qemu.major, info->qemu.minor, info->qemu.micro,
59 info->package);
60
61 qapi_free_VersionInfo(info);
62}
292a2602 63
84f2d0ea 64void hmp_info_kvm(Monitor *mon, const QDict *qdict)
292a2602
LC
65{
66 KvmInfo *info;
67
68 info = qmp_query_kvm(NULL);
69 monitor_printf(mon, "kvm support: ");
70 if (info->present) {
71 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
72 } else {
73 monitor_printf(mon, "not compiled\n");
74 }
75
76 qapi_free_KvmInfo(info);
77}
78
84f2d0ea 79void hmp_info_status(Monitor *mon, const QDict *qdict)
1fa9a5e4
LC
80{
81 StatusInfo *info;
82
83 info = qmp_query_status(NULL);
84
85 monitor_printf(mon, "VM status: %s%s",
86 info->running ? "running" : "paused",
87 info->singlestep ? " (single step mode)" : "");
88
89 if (!info->running && info->status != RUN_STATE_PAUSED) {
90 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
91 }
92
93 monitor_printf(mon, "\n");
94
95 qapi_free_StatusInfo(info);
96}
97
84f2d0ea 98void hmp_info_uuid(Monitor *mon, const QDict *qdict)
efab767e
LC
99{
100 UuidInfo *info;
101
102 info = qmp_query_uuid(NULL);
103 monitor_printf(mon, "%s\n", info->UUID);
104 qapi_free_UuidInfo(info);
105}
c5a415a0 106
84f2d0ea 107void hmp_info_chardev(Monitor *mon, const QDict *qdict)
c5a415a0
LC
108{
109 ChardevInfoList *char_info, *info;
110
111 char_info = qmp_query_chardev(NULL);
112 for (info = char_info; info; info = info->next) {
113 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
114 info->value->filename);
115 }
116
117 qapi_free_ChardevInfoList(char_info);
118}
7a7f325e 119
84f2d0ea 120void hmp_info_mice(Monitor *mon, const QDict *qdict)
e235cec3
LC
121{
122 MouseInfoList *mice_list, *mouse;
123
124 mice_list = qmp_query_mice(NULL);
125 if (!mice_list) {
126 monitor_printf(mon, "No mouse devices connected\n");
127 return;
128 }
129
130 for (mouse = mice_list; mouse; mouse = mouse->next) {
131 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
132 mouse->value->current ? '*' : ' ',
133 mouse->value->index, mouse->value->name,
134 mouse->value->absolute ? " (absolute)" : "");
135 }
136
137 qapi_free_MouseInfoList(mice_list);
138}
139
84f2d0ea 140void hmp_info_migrate(Monitor *mon, const QDict *qdict)
791e7c82
LC
141{
142 MigrationInfo *info;
bbf6da32 143 MigrationCapabilityStatusList *caps, *cap;
791e7c82
LC
144
145 info = qmp_query_migrate(NULL);
bbf6da32
OW
146 caps = qmp_query_migrate_capabilities(NULL);
147
148 /* do not display parameters during setup */
149 if (info->has_status && caps) {
150 monitor_printf(mon, "capabilities: ");
151 for (cap = caps; cap; cap = cap->next) {
152 monitor_printf(mon, "%s: %s ",
153 MigrationCapability_lookup[cap->value->capability],
154 cap->value->state ? "on" : "off");
155 }
156 monitor_printf(mon, "\n");
157 }
791e7c82
LC
158
159 if (info->has_status) {
160 monitor_printf(mon, "Migration status: %s\n", info->status);
7aa939af
JQ
161 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
162 info->total_time);
2c52ddf1
JQ
163 if (info->has_expected_downtime) {
164 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
165 info->expected_downtime);
166 }
9c5a9fcf
JQ
167 if (info->has_downtime) {
168 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
169 info->downtime);
170 }
ed4fbd10
MH
171 if (info->has_setup_time) {
172 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
173 info->setup_time);
174 }
791e7c82
LC
175 }
176
177 if (info->has_ram) {
178 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
179 info->ram->transferred >> 10);
7e114f8c
MH
180 monitor_printf(mon, "throughput: %0.2f mbps\n",
181 info->ram->mbps);
791e7c82
LC
182 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
183 info->ram->remaining >> 10);
184 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
185 info->ram->total >> 10);
004d4c10
OW
186 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
187 info->ram->duplicate);
f1c72795
PL
188 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
189 info->ram->skipped);
004d4c10
OW
190 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
191 info->ram->normal);
192 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
193 info->ram->normal_bytes >> 10);
58570ed8
C
194 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
195 info->ram->dirty_sync_count);
8d017193
JQ
196 if (info->ram->dirty_pages_rate) {
197 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
198 info->ram->dirty_pages_rate);
199 }
791e7c82
LC
200 }
201
202 if (info->has_disk) {
203 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
204 info->disk->transferred >> 10);
205 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
206 info->disk->remaining >> 10);
207 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
208 info->disk->total >> 10);
209 }
210
f36d55af
OW
211 if (info->has_xbzrle_cache) {
212 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
213 info->xbzrle_cache->cache_size);
214 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
215 info->xbzrle_cache->bytes >> 10);
216 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
217 info->xbzrle_cache->pages);
218 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
219 info->xbzrle_cache->cache_miss);
8bc39233
C
220 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
221 info->xbzrle_cache->cache_miss_rate);
f36d55af
OW
222 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
223 info->xbzrle_cache->overflow);
224 }
225
791e7c82 226 qapi_free_MigrationInfo(info);
bbf6da32
OW
227 qapi_free_MigrationCapabilityStatusList(caps);
228}
229
84f2d0ea 230void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
bbf6da32
OW
231{
232 MigrationCapabilityStatusList *caps, *cap;
233
234 caps = qmp_query_migrate_capabilities(NULL);
235
236 if (caps) {
237 monitor_printf(mon, "capabilities: ");
238 for (cap = caps; cap; cap = cap->next) {
239 monitor_printf(mon, "%s: %s ",
240 MigrationCapability_lookup[cap->value->capability],
241 cap->value->state ? "on" : "off");
242 }
243 monitor_printf(mon, "\n");
244 }
245
246 qapi_free_MigrationCapabilityStatusList(caps);
791e7c82
LC
247}
248
84f2d0ea 249void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
9e1ba4cc
OW
250{
251 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
252 qmp_query_migrate_cache_size(NULL) >> 10);
253}
254
84f2d0ea 255void hmp_info_cpus(Monitor *mon, const QDict *qdict)
de0b36b6
LC
256{
257 CpuInfoList *cpu_list, *cpu;
258
259 cpu_list = qmp_query_cpus(NULL);
260
261 for (cpu = cpu_list; cpu; cpu = cpu->next) {
262 int active = ' ';
263
264 if (cpu->value->CPU == monitor_get_cpu_index()) {
265 active = '*';
266 }
267
852bef0e 268 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
de0b36b6
LC
269
270 if (cpu->value->has_pc) {
852bef0e 271 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
de0b36b6
LC
272 }
273 if (cpu->value->has_nip) {
852bef0e 274 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
de0b36b6
LC
275 }
276 if (cpu->value->has_npc) {
852bef0e 277 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
de0b36b6
LC
278 }
279 if (cpu->value->has_PC) {
852bef0e 280 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
de0b36b6
LC
281 }
282
283 if (cpu->value->halted) {
284 monitor_printf(mon, " (halted)");
285 }
286
287 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
288 }
289
290 qapi_free_CpuInfoList(cpu_list);
291}
292
289b276c
KW
293static void print_block_info(Monitor *mon, BlockInfo *info,
294 BlockDeviceInfo *inserted, bool verbose)
b2023818 295{
bd093a36 296 ImageInfo *image_info;
b2023818 297
289b276c
KW
298 monitor_printf(mon, "%s", info->device);
299 if (inserted) {
300 monitor_printf(mon, ": %s (%s%s%s)\n",
301 inserted->file,
302 inserted->drv,
303 inserted->ro ? ", read-only" : "",
304 inserted->encrypted ? ", encrypted" : "");
305 } else {
306 monitor_printf(mon, ": [not inserted]\n");
307 }
b2023818 308
289b276c
KW
309 if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
310 monitor_printf(mon, " I/O status: %s\n",
311 BlockDeviceIoStatus_lookup[info->io_status]);
312 }
b2023818 313
289b276c
KW
314 if (info->removable) {
315 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
316 info->locked ? "" : "not ",
317 info->tray_open ? "open" : "closed");
318 }
fbe2e26c 319
b2023818 320
289b276c
KW
321 if (!inserted) {
322 return;
323 }
b2023818 324
289b276c
KW
325 monitor_printf(mon, " Cache mode: %s%s%s\n",
326 inserted->cache->writeback ? "writeback" : "writethrough",
327 inserted->cache->direct ? ", direct" : "",
328 inserted->cache->no_flush ? ", ignore flushes" : "");
329
330 if (inserted->has_backing_file) {
331 monitor_printf(mon,
332 " Backing file: %s "
333 "(chain depth: %" PRId64 ")\n",
334 inserted->backing_file,
335 inserted->backing_file_depth);
336 }
727f005e 337
289b276c
KW
338 if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
339 monitor_printf(mon, " Detect zeroes: %s\n",
340 BlockdevDetectZeroesOptions_lookup[inserted->detect_zeroes]);
341 }
fbe2e26c 342
289b276c
KW
343 if (inserted->bps || inserted->bps_rd || inserted->bps_wr ||
344 inserted->iops || inserted->iops_rd || inserted->iops_wr)
345 {
346 monitor_printf(mon, " I/O throttling: bps=%" PRId64
347 " bps_rd=%" PRId64 " bps_wr=%" PRId64
348 " bps_max=%" PRId64
349 " bps_rd_max=%" PRId64
350 " bps_wr_max=%" PRId64
351 " iops=%" PRId64 " iops_rd=%" PRId64
352 " iops_wr=%" PRId64
353 " iops_max=%" PRId64
354 " iops_rd_max=%" PRId64
355 " iops_wr_max=%" PRId64
356 " iops_size=%" PRId64 "\n",
357 inserted->bps,
358 inserted->bps_rd,
359 inserted->bps_wr,
360 inserted->bps_max,
361 inserted->bps_rd_max,
362 inserted->bps_wr_max,
363 inserted->iops,
364 inserted->iops_rd,
365 inserted->iops_wr,
366 inserted->iops_max,
367 inserted->iops_rd_max,
368 inserted->iops_wr_max,
369 inserted->iops_size);
370 }
fbe2e26c 371
289b276c
KW
372 if (verbose) {
373 monitor_printf(mon, "\nImages:\n");
374 image_info = inserted->image;
375 while (1) {
376 bdrv_image_info_dump((fprintf_function)monitor_printf,
377 mon, image_info);
378 if (image_info->has_backing_image) {
379 image_info = image_info->backing_image;
380 } else {
381 break;
382 }
383 }
384 }
385}
9e193c5a 386
289b276c
KW
387void hmp_info_block(Monitor *mon, const QDict *qdict)
388{
389 BlockInfoList *block_list, *info;
390 const char *device = qdict_get_try_str(qdict, "device");
391 bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
9e193c5a 392
289b276c 393 block_list = qmp_query_block(false);
fbe2e26c 394
289b276c
KW
395 for (info = block_list; info; info = info->next) {
396 if (device && strcmp(device, info->value->device)) {
397 continue;
465bee1d
PL
398 }
399
289b276c
KW
400 if (info != block_list) {
401 monitor_printf(mon, "\n");
fbe2e26c 402 }
bd093a36 403
289b276c
KW
404 print_block_info(mon, info->value, info->value->has_inserted
405 ? info->value->inserted : NULL,
406 verbose);
b2023818
LC
407 }
408
409 qapi_free_BlockInfoList(block_list);
410}
411
84f2d0ea 412void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
f11f57e4
LC
413{
414 BlockStatsList *stats_list, *stats;
415
f71eaa74 416 stats_list = qmp_query_blockstats(false, false, NULL);
f11f57e4
LC
417
418 for (stats = stats_list; stats; stats = stats->next) {
419 if (!stats->value->has_device) {
420 continue;
421 }
422
423 monitor_printf(mon, "%s:", stats->value->device);
424 monitor_printf(mon, " rd_bytes=%" PRId64
425 " wr_bytes=%" PRId64
426 " rd_operations=%" PRId64
427 " wr_operations=%" PRId64
428 " flush_operations=%" PRId64
429 " wr_total_time_ns=%" PRId64
430 " rd_total_time_ns=%" PRId64
431 " flush_total_time_ns=%" PRId64
432 "\n",
433 stats->value->stats->rd_bytes,
434 stats->value->stats->wr_bytes,
435 stats->value->stats->rd_operations,
436 stats->value->stats->wr_operations,
437 stats->value->stats->flush_operations,
438 stats->value->stats->wr_total_time_ns,
439 stats->value->stats->rd_total_time_ns,
440 stats->value->stats->flush_total_time_ns);
441 }
442
443 qapi_free_BlockStatsList(stats_list);
444}
445
84f2d0ea 446void hmp_info_vnc(Monitor *mon, const QDict *qdict)
2b54aa87
LC
447{
448 VncInfo *info;
449 Error *err = NULL;
450 VncClientInfoList *client;
451
452 info = qmp_query_vnc(&err);
453 if (err) {
454 monitor_printf(mon, "%s\n", error_get_pretty(err));
455 error_free(err);
456 return;
457 }
458
459 if (!info->enabled) {
460 monitor_printf(mon, "Server: disabled\n");
461 goto out;
462 }
463
464 monitor_printf(mon, "Server:\n");
465 if (info->has_host && info->has_service) {
466 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
467 }
468 if (info->has_auth) {
469 monitor_printf(mon, " auth: %s\n", info->auth);
470 }
471
472 if (!info->has_clients || info->clients == NULL) {
473 monitor_printf(mon, "Client: none\n");
474 } else {
475 for (client = info->clients; client; client = client->next) {
476 monitor_printf(mon, "Client:\n");
477 monitor_printf(mon, " address: %s:%s\n",
a589569f
WX
478 client->value->base->host,
479 client->value->base->service);
2b54aa87
LC
480 monitor_printf(mon, " x509_dname: %s\n",
481 client->value->x509_dname ?
482 client->value->x509_dname : "none");
483 monitor_printf(mon, " username: %s\n",
484 client->value->has_sasl_username ?
485 client->value->sasl_username : "none");
486 }
487 }
488
489out:
490 qapi_free_VncInfo(info);
491}
492
84f2d0ea 493void hmp_info_spice(Monitor *mon, const QDict *qdict)
d1f29646
LC
494{
495 SpiceChannelList *chan;
496 SpiceInfo *info;
497
498 info = qmp_query_spice(NULL);
499
500 if (!info->enabled) {
501 monitor_printf(mon, "Server: disabled\n");
502 goto out;
503 }
504
505 monitor_printf(mon, "Server:\n");
506 if (info->has_port) {
507 monitor_printf(mon, " address: %s:%" PRId64 "\n",
508 info->host, info->port);
509 }
510 if (info->has_tls_port) {
511 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
512 info->host, info->tls_port);
513 }
61c4efe2
YH
514 monitor_printf(mon, " migrated: %s\n",
515 info->migrated ? "true" : "false");
d1f29646
LC
516 monitor_printf(mon, " auth: %s\n", info->auth);
517 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
4efee029
AL
518 monitor_printf(mon, " mouse-mode: %s\n",
519 SpiceQueryMouseMode_lookup[info->mouse_mode]);
d1f29646
LC
520
521 if (!info->has_channels || info->channels == NULL) {
522 monitor_printf(mon, "Channels: none\n");
523 } else {
524 for (chan = info->channels; chan; chan = chan->next) {
525 monitor_printf(mon, "Channel:\n");
526 monitor_printf(mon, " address: %s:%s%s\n",
a589569f 527 chan->value->base->host, chan->value->base->port,
d1f29646
LC
528 chan->value->tls ? " [tls]" : "");
529 monitor_printf(mon, " session: %" PRId64 "\n",
530 chan->value->connection_id);
531 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
532 chan->value->channel_type, chan->value->channel_id);
533 }
534 }
535
536out:
537 qapi_free_SpiceInfo(info);
538}
539
84f2d0ea 540void hmp_info_balloon(Monitor *mon, const QDict *qdict)
96637bcd
LC
541{
542 BalloonInfo *info;
543 Error *err = NULL;
544
545 info = qmp_query_balloon(&err);
546 if (err) {
547 monitor_printf(mon, "%s\n", error_get_pretty(err));
548 error_free(err);
549 return;
550 }
551
01ceb97e 552 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
96637bcd
LC
553
554 qapi_free_BalloonInfo(info);
555}
556
79627472
LC
557static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
558{
559 PciMemoryRegionList *region;
560
561 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
562 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
563 dev->slot, dev->function);
564 monitor_printf(mon, " ");
565
566 if (dev->class_info.has_desc) {
567 monitor_printf(mon, "%s", dev->class_info.desc);
568 } else {
6f88009e 569 monitor_printf(mon, "Class %04" PRId64, dev->class_info.q_class);
79627472
LC
570 }
571
572 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
573 dev->id.vendor, dev->id.device);
574
575 if (dev->has_irq) {
576 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
577 }
578
579 if (dev->has_pci_bridge) {
580 monitor_printf(mon, " BUS %" PRId64 ".\n",
581 dev->pci_bridge->bus.number);
582 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
583 dev->pci_bridge->bus.secondary);
584 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
585 dev->pci_bridge->bus.subordinate);
586
587 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
588 dev->pci_bridge->bus.io_range->base,
589 dev->pci_bridge->bus.io_range->limit);
590
591 monitor_printf(mon,
592 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
593 dev->pci_bridge->bus.memory_range->base,
594 dev->pci_bridge->bus.memory_range->limit);
595
596 monitor_printf(mon, " prefetchable memory range "
597 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
598 dev->pci_bridge->bus.prefetchable_range->base,
599 dev->pci_bridge->bus.prefetchable_range->limit);
600 }
601
602 for (region = dev->regions; region; region = region->next) {
603 uint64_t addr, size;
604
605 addr = region->value->address;
606 size = region->value->size;
607
608 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
609
610 if (!strcmp(region->value->type, "io")) {
611 monitor_printf(mon, "I/O at 0x%04" PRIx64
612 " [0x%04" PRIx64 "].\n",
613 addr, addr + size - 1);
614 } else {
615 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
616 " [0x%08" PRIx64 "].\n",
617 region->value->mem_type_64 ? 64 : 32,
618 region->value->prefetch ? " prefetchable" : "",
619 addr, addr + size - 1);
620 }
621 }
622
623 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
624
625 if (dev->has_pci_bridge) {
626 if (dev->pci_bridge->has_devices) {
627 PciDeviceInfoList *cdev;
628 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
629 hmp_info_pci_device(mon, cdev->value);
630 }
631 }
632 }
633}
634
84f2d0ea 635void hmp_info_pci(Monitor *mon, const QDict *qdict)
79627472 636{
f46cee37 637 PciInfoList *info_list, *info;
79627472
LC
638 Error *err = NULL;
639
f46cee37 640 info_list = qmp_query_pci(&err);
79627472
LC
641 if (err) {
642 monitor_printf(mon, "PCI devices not supported\n");
643 error_free(err);
644 return;
645 }
646
f46cee37 647 for (info = info_list; info; info = info->next) {
79627472
LC
648 PciDeviceInfoList *dev;
649
650 for (dev = info->value->devices; dev; dev = dev->next) {
651 hmp_info_pci_device(mon, dev->value);
652 }
653 }
654
f46cee37 655 qapi_free_PciInfoList(info_list);
79627472
LC
656}
657
84f2d0ea 658void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
fb5458cd
SH
659{
660 BlockJobInfoList *list;
661 Error *err = NULL;
662
663 list = qmp_query_block_jobs(&err);
664 assert(!err);
665
666 if (!list) {
667 monitor_printf(mon, "No active jobs\n");
668 return;
669 }
670
671 while (list) {
672 if (strcmp(list->value->type, "stream") == 0) {
673 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
674 " of %" PRId64 " bytes, speed limit %" PRId64
675 " bytes/s\n",
676 list->value->device,
677 list->value->offset,
678 list->value->len,
679 list->value->speed);
680 } else {
681 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
682 " of %" PRId64 " bytes, speed limit %" PRId64
683 " bytes/s\n",
684 list->value->type,
685 list->value->device,
686 list->value->offset,
687 list->value->len,
688 list->value->speed);
689 }
690 list = list->next;
691 }
93bb1315
GA
692
693 qapi_free_BlockJobInfoList(list);
fb5458cd
SH
694}
695
d1a0cf73
SB
696void hmp_info_tpm(Monitor *mon, const QDict *qdict)
697{
698 TPMInfoList *info_list, *info;
699 Error *err = NULL;
700 unsigned int c = 0;
701 TPMPassthroughOptions *tpo;
702
703 info_list = qmp_query_tpm(&err);
704 if (err) {
705 monitor_printf(mon, "TPM device not supported\n");
706 error_free(err);
707 return;
708 }
709
710 if (info_list) {
711 monitor_printf(mon, "TPM device:\n");
712 }
713
714 for (info = info_list; info; info = info->next) {
715 TPMInfo *ti = info->value;
716 monitor_printf(mon, " tpm%d: model=%s\n",
717 c, TpmModel_lookup[ti->model]);
718
719 monitor_printf(mon, " \\ %s: type=%s",
88ca7bcf 720 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
d1a0cf73 721
88ca7bcf
CB
722 switch (ti->options->kind) {
723 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
724 tpo = ti->options->passthrough;
d1a0cf73
SB
725 monitor_printf(mon, "%s%s%s%s",
726 tpo->has_path ? ",path=" : "",
727 tpo->has_path ? tpo->path : "",
728 tpo->has_cancel_path ? ",cancel-path=" : "",
729 tpo->has_cancel_path ? tpo->cancel_path : "");
730 break;
731 case TPM_TYPE_OPTIONS_KIND_MAX:
732 break;
733 }
734 monitor_printf(mon, "\n");
735 c++;
736 }
737 qapi_free_TPMInfoList(info_list);
738}
739
7a7f325e
LC
740void hmp_quit(Monitor *mon, const QDict *qdict)
741{
742 monitor_suspend(mon);
743 qmp_quit(NULL);
744}
5f158f21
LC
745
746void hmp_stop(Monitor *mon, const QDict *qdict)
747{
748 qmp_stop(NULL);
749}
38d22653
LC
750
751void hmp_system_reset(Monitor *mon, const QDict *qdict)
752{
753 qmp_system_reset(NULL);
754}
5bc465e4
LC
755
756void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
757{
758 qmp_system_powerdown(NULL);
759}
755f1968
LC
760
761void hmp_cpu(Monitor *mon, const QDict *qdict)
762{
763 int64_t cpu_index;
764
765 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
766 use it are converted to the QAPI */
767 cpu_index = qdict_get_int(qdict, "index");
768 if (monitor_set_cpu(cpu_index) < 0) {
769 monitor_printf(mon, "invalid CPU index\n");
770 }
771}
0cfd6a9a
LC
772
773void hmp_memsave(Monitor *mon, const QDict *qdict)
774{
775 uint32_t size = qdict_get_int(qdict, "size");
776 const char *filename = qdict_get_str(qdict, "filename");
777 uint64_t addr = qdict_get_int(qdict, "val");
e940f543 778 Error *err = NULL;
0cfd6a9a 779
e940f543
MA
780 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &err);
781 hmp_handle_error(mon, &err);
0cfd6a9a 782}
6d3962bf
LC
783
784void hmp_pmemsave(Monitor *mon, const QDict *qdict)
785{
786 uint32_t size = qdict_get_int(qdict, "size");
787 const char *filename = qdict_get_str(qdict, "filename");
788 uint64_t addr = qdict_get_int(qdict, "val");
e940f543 789 Error *err = NULL;
6d3962bf 790
e940f543
MA
791 qmp_pmemsave(addr, size, filename, &err);
792 hmp_handle_error(mon, &err);
1f590cf9
LL
793}
794
3949e594 795void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1f590cf9 796{
1f590cf9
LL
797 const char *chardev = qdict_get_str(qdict, "device");
798 const char *data = qdict_get_str(qdict, "data");
e940f543 799 Error *err = NULL;
1f590cf9 800
e940f543 801 qmp_ringbuf_write(chardev, data, false, 0, &err);
1f590cf9 802
e940f543 803 hmp_handle_error(mon, &err);
6d3962bf 804}
e42e818b 805
3949e594 806void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
49b6d722
LL
807{
808 uint32_t size = qdict_get_int(qdict, "size");
809 const char *chardev = qdict_get_str(qdict, "device");
3ab651fc 810 char *data;
e940f543 811 Error *err = NULL;
543f3412 812 int i;
49b6d722 813
e940f543
MA
814 data = qmp_ringbuf_read(chardev, size, false, 0, &err);
815 if (err) {
816 monitor_printf(mon, "%s\n", error_get_pretty(err));
817 error_free(err);
49b6d722
LL
818 return;
819 }
820
543f3412
MA
821 for (i = 0; data[i]; i++) {
822 unsigned char ch = data[i];
823
824 if (ch == '\\') {
825 monitor_printf(mon, "\\\\");
826 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
827 monitor_printf(mon, "\\u%04X", ch);
828 } else {
829 monitor_printf(mon, "%c", ch);
830 }
831
832 }
833 monitor_printf(mon, "\n");
3ab651fc 834 g_free(data);
49b6d722
LL
835}
836
e42e818b
LC
837static void hmp_cont_cb(void *opaque, int err)
838{
e42e818b 839 if (!err) {
8b7f6fbb 840 qmp_cont(NULL);
e42e818b
LC
841 }
842}
843
8b7f6fbb
LC
844static bool key_is_missing(const BlockInfo *bdev)
845{
846 return (bdev->inserted && bdev->inserted->encryption_key_missing);
847}
848
e42e818b
LC
849void hmp_cont(Monitor *mon, const QDict *qdict)
850{
8b7f6fbb 851 BlockInfoList *bdev_list, *bdev;
e940f543 852 Error *err = NULL;
e42e818b 853
8b7f6fbb
LC
854 bdev_list = qmp_query_block(NULL);
855 for (bdev = bdev_list; bdev; bdev = bdev->next) {
856 if (key_is_missing(bdev->value)) {
857 monitor_read_block_device_key(mon, bdev->value->device,
858 hmp_cont_cb, NULL);
859 goto out;
e42e818b 860 }
e42e818b 861 }
8b7f6fbb 862
e940f543
MA
863 qmp_cont(&err);
864 hmp_handle_error(mon, &err);
8b7f6fbb
LC
865
866out:
867 qapi_free_BlockInfoList(bdev_list);
e42e818b 868}
ab49ab5c 869
9b9df25a
GH
870void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
871{
872 qmp_system_wakeup(NULL);
873}
874
ab49ab5c
LC
875void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
876{
e940f543 877 Error *err = NULL;
ab49ab5c 878
e940f543
MA
879 qmp_inject_nmi(&err);
880 hmp_handle_error(mon, &err);
ab49ab5c 881}
4b37156c
LC
882
883void hmp_set_link(Monitor *mon, const QDict *qdict)
884{
885 const char *name = qdict_get_str(qdict, "name");
886 int up = qdict_get_bool(qdict, "up");
e940f543 887 Error *err = NULL;
4b37156c 888
e940f543
MA
889 qmp_set_link(name, up, &err);
890 hmp_handle_error(mon, &err);
4b37156c 891}
a4dea8a9
LC
892
893void hmp_block_passwd(Monitor *mon, const QDict *qdict)
894{
895 const char *device = qdict_get_str(qdict, "device");
896 const char *password = qdict_get_str(qdict, "password");
e940f543 897 Error *err = NULL;
a4dea8a9 898
e940f543
MA
899 qmp_block_passwd(true, device, false, NULL, password, &err);
900 hmp_handle_error(mon, &err);
a4dea8a9 901}
d72f3264
LC
902
903void hmp_balloon(Monitor *mon, const QDict *qdict)
904{
905 int64_t value = qdict_get_int(qdict, "value");
e940f543 906 Error *err = NULL;
d72f3264 907
e940f543
MA
908 qmp_balloon(value, &err);
909 if (err) {
910 monitor_printf(mon, "balloon: %s\n", error_get_pretty(err));
911 error_free(err);
d72f3264
LC
912 }
913}
5e7caacb
LC
914
915void hmp_block_resize(Monitor *mon, const QDict *qdict)
916{
917 const char *device = qdict_get_str(qdict, "device");
918 int64_t size = qdict_get_int(qdict, "size");
e940f543 919 Error *err = NULL;
5e7caacb 920
e940f543
MA
921 qmp_block_resize(true, device, false, NULL, size, &err);
922 hmp_handle_error(mon, &err);
5e7caacb 923}
6106e249 924
d9b902db
PB
925void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
926{
927 const char *device = qdict_get_str(qdict, "device");
928 const char *filename = qdict_get_str(qdict, "target");
929 const char *format = qdict_get_try_str(qdict, "format");
930 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
931 int full = qdict_get_try_bool(qdict, "full", 0);
932 enum NewImageMode mode;
e940f543 933 Error *err = NULL;
d9b902db
PB
934
935 if (!filename) {
e940f543
MA
936 error_set(&err, QERR_MISSING_PARAMETER, "target");
937 hmp_handle_error(mon, &err);
d9b902db
PB
938 return;
939 }
940
941 if (reuse) {
942 mode = NEW_IMAGE_MODE_EXISTING;
943 } else {
944 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
945 }
946
947 qmp_drive_mirror(device, filename, !!format, format,
09158f00 948 false, NULL, false, NULL,
d9b902db 949 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
08e4ed6c 950 true, mode, false, 0, false, 0, false, 0,
e940f543
MA
951 false, 0, false, 0, &err);
952 hmp_handle_error(mon, &err);
d9b902db
PB
953}
954
de90930a
SH
955void hmp_drive_backup(Monitor *mon, const QDict *qdict)
956{
957 const char *device = qdict_get_str(qdict, "device");
958 const char *filename = qdict_get_str(qdict, "target");
959 const char *format = qdict_get_try_str(qdict, "format");
960 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
961 int full = qdict_get_try_bool(qdict, "full", 0);
962 enum NewImageMode mode;
e940f543 963 Error *err = NULL;
de90930a
SH
964
965 if (!filename) {
e940f543
MA
966 error_set(&err, QERR_MISSING_PARAMETER, "target");
967 hmp_handle_error(mon, &err);
de90930a
SH
968 return;
969 }
970
971 if (reuse) {
972 mode = NEW_IMAGE_MODE_EXISTING;
973 } else {
974 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
975 }
976
977 qmp_drive_backup(device, filename, !!format, format,
978 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
e940f543
MA
979 true, mode, false, 0, false, 0, false, 0, &err);
980 hmp_handle_error(mon, &err);
de90930a
SH
981}
982
6106e249
LC
983void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
984{
985 const char *device = qdict_get_str(qdict, "device");
986 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
987 const char *format = qdict_get_try_str(qdict, "format");
6cc2a415
PB
988 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
989 enum NewImageMode mode;
e940f543 990 Error *err = NULL;
6106e249
LC
991
992 if (!filename) {
993 /* In the future, if 'snapshot-file' is not specified, the snapshot
994 will be taken internally. Today it's actually required. */
e940f543
MA
995 error_set(&err, QERR_MISSING_PARAMETER, "snapshot-file");
996 hmp_handle_error(mon, &err);
6106e249
LC
997 return;
998 }
999
6cc2a415 1000 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
0901f67e
BC
1001 qmp_blockdev_snapshot_sync(true, device, false, NULL,
1002 filename, false, NULL,
1003 !!format, format,
e940f543
MA
1004 true, mode, &err);
1005 hmp_handle_error(mon, &err);
6106e249 1006}
6cdedb07 1007
775ca88e
WX
1008void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1009{
1010 const char *device = qdict_get_str(qdict, "device");
1011 const char *name = qdict_get_str(qdict, "name");
e940f543 1012 Error *err = NULL;
775ca88e 1013
e940f543
MA
1014 qmp_blockdev_snapshot_internal_sync(device, name, &err);
1015 hmp_handle_error(mon, &err);
775ca88e
WX
1016}
1017
7a4ed2ee
WX
1018void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1019{
1020 const char *device = qdict_get_str(qdict, "device");
1021 const char *name = qdict_get_str(qdict, "name");
1022 const char *id = qdict_get_try_str(qdict, "id");
e940f543 1023 Error *err = NULL;
7a4ed2ee
WX
1024
1025 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
e940f543
MA
1026 true, name, &err);
1027 hmp_handle_error(mon, &err);
7a4ed2ee
WX
1028}
1029
6cdedb07
LC
1030void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1031{
1032 qmp_migrate_cancel(NULL);
1033}
4f0a993b
LC
1034
1035void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1036{
1037 double value = qdict_get_double(qdict, "value");
1038 qmp_migrate_set_downtime(value, NULL);
1039}
3dc85383 1040
9e1ba4cc
OW
1041void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1042{
1043 int64_t value = qdict_get_int(qdict, "value");
1044 Error *err = NULL;
1045
1046 qmp_migrate_set_cache_size(value, &err);
1047 if (err) {
1048 monitor_printf(mon, "%s\n", error_get_pretty(err));
1049 error_free(err);
1050 return;
1051 }
1052}
1053
3dc85383
LC
1054void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1055{
1056 int64_t value = qdict_get_int(qdict, "value");
1057 qmp_migrate_set_speed(value, NULL);
1058}
fbf796fd 1059
00458433
OW
1060void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1061{
1062 const char *cap = qdict_get_str(qdict, "capability");
1063 bool state = qdict_get_bool(qdict, "state");
1064 Error *err = NULL;
1065 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1066 int i;
1067
1068 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
1069 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1070 caps->value = g_malloc0(sizeof(*caps->value));
1071 caps->value->capability = i;
1072 caps->value->state = state;
1073 caps->next = NULL;
1074 qmp_migrate_set_capabilities(caps, &err);
1075 break;
1076 }
1077 }
1078
1079 if (i == MIGRATION_CAPABILITY_MAX) {
1080 error_set(&err, QERR_INVALID_PARAMETER, cap);
1081 }
1082
1083 qapi_free_MigrationCapabilityStatusList(caps);
1084
1085 if (err) {
a31ca017 1086 monitor_printf(mon, "migrate_set_capability: %s\n",
00458433
OW
1087 error_get_pretty(err));
1088 error_free(err);
1089 }
1090}
1091
fbf796fd
LC
1092void hmp_set_password(Monitor *mon, const QDict *qdict)
1093{
1094 const char *protocol = qdict_get_str(qdict, "protocol");
1095 const char *password = qdict_get_str(qdict, "password");
1096 const char *connected = qdict_get_try_str(qdict, "connected");
1097 Error *err = NULL;
1098
1099 qmp_set_password(protocol, password, !!connected, connected, &err);
1100 hmp_handle_error(mon, &err);
1101}
9ad5372d
LC
1102
1103void hmp_expire_password(Monitor *mon, const QDict *qdict)
1104{
1105 const char *protocol = qdict_get_str(qdict, "protocol");
1106 const char *whenstr = qdict_get_str(qdict, "time");
1107 Error *err = NULL;
1108
1109 qmp_expire_password(protocol, whenstr, &err);
1110 hmp_handle_error(mon, &err);
1111}
c245b6a3
LC
1112
1113void hmp_eject(Monitor *mon, const QDict *qdict)
1114{
1115 int force = qdict_get_try_bool(qdict, "force", 0);
1116 const char *device = qdict_get_str(qdict, "device");
1117 Error *err = NULL;
1118
1119 qmp_eject(device, true, force, &err);
1120 hmp_handle_error(mon, &err);
1121}
333a96ec 1122
c60bf339
SH
1123static void hmp_change_read_arg(void *opaque, const char *password,
1124 void *readline_opaque)
333a96ec
LC
1125{
1126 qmp_change_vnc_password(password, NULL);
c60bf339 1127 monitor_read_command(opaque, 1);
333a96ec
LC
1128}
1129
333a96ec
LC
1130void hmp_change(Monitor *mon, const QDict *qdict)
1131{
1132 const char *device = qdict_get_str(qdict, "device");
1133 const char *target = qdict_get_str(qdict, "target");
1134 const char *arg = qdict_get_try_str(qdict, "arg");
1135 Error *err = NULL;
1136
1137 if (strcmp(device, "vnc") == 0 &&
1138 (strcmp(target, "passwd") == 0 ||
1139 strcmp(target, "password") == 0)) {
1140 if (!arg) {
1141 monitor_read_password(mon, hmp_change_read_arg, NULL);
1142 return;
1143 }
1144 }
1145
1146 qmp_change(device, target, !!arg, arg, &err);
84d18f06 1147 if (err &&
ab878ddf 1148 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
eef5ad10
LC
1149 error_free(err);
1150 monitor_read_block_device_key(mon, device, NULL, NULL);
333a96ec
LC
1151 return;
1152 }
1153 hmp_handle_error(mon, &err);
1154}
80047da5
LC
1155
1156void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1157{
1158 Error *err = NULL;
1159
1160 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1161 qdict_get_int(qdict, "bps"),
1162 qdict_get_int(qdict, "bps_rd"),
1163 qdict_get_int(qdict, "bps_wr"),
1164 qdict_get_int(qdict, "iops"),
1165 qdict_get_int(qdict, "iops_rd"),
3e9fab69
BC
1166 qdict_get_int(qdict, "iops_wr"),
1167 false, /* no burst max via HMP */
1168 0,
1169 false,
1170 0,
1171 false,
1172 0,
1173 false,
1174 0,
1175 false,
1176 0,
1177 false,
2024c1df
BC
1178 0,
1179 false, /* No default I/O size */
3e9fab69 1180 0, &err);
80047da5
LC
1181 hmp_handle_error(mon, &err);
1182}
12bd451f
SH
1183
1184void hmp_block_stream(Monitor *mon, const QDict *qdict)
1185{
1186 Error *error = NULL;
1187 const char *device = qdict_get_str(qdict, "device");
1188 const char *base = qdict_get_try_str(qdict, "base");
c83c66c3 1189 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
12bd451f 1190
13d8cc51 1191 qmp_block_stream(device, base != NULL, base, false, NULL,
1d809098 1192 qdict_haskey(qdict, "speed"), speed,
46663e5e 1193 true, BLOCKDEV_ON_ERROR_REPORT, &error);
12bd451f
SH
1194
1195 hmp_handle_error(mon, &error);
1196}
2d47c6e9
SH
1197
1198void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1199{
1200 Error *error = NULL;
1201 const char *device = qdict_get_str(qdict, "device");
c6db2395 1202 int64_t value = qdict_get_int(qdict, "speed");
2d47c6e9
SH
1203
1204 qmp_block_job_set_speed(device, value, &error);
1205
1206 hmp_handle_error(mon, &error);
1207}
370521a1
SH
1208
1209void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1210{
1211 Error *error = NULL;
1212 const char *device = qdict_get_str(qdict, "device");
6e37fb81 1213 bool force = qdict_get_try_bool(qdict, "force", 0);
370521a1 1214
6e37fb81
PB
1215 qmp_block_job_cancel(device, true, force, &error);
1216
1217 hmp_handle_error(mon, &error);
1218}
1219
1220void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1221{
1222 Error *error = NULL;
1223 const char *device = qdict_get_str(qdict, "device");
1224
1225 qmp_block_job_pause(device, &error);
1226
1227 hmp_handle_error(mon, &error);
1228}
1229
1230void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1231{
1232 Error *error = NULL;
1233 const char *device = qdict_get_str(qdict, "device");
1234
1235 qmp_block_job_resume(device, &error);
370521a1
SH
1236
1237 hmp_handle_error(mon, &error);
1238}
e1c37d0e 1239
aeae883b
PB
1240void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1241{
1242 Error *error = NULL;
1243 const char *device = qdict_get_str(qdict, "device");
1244
1245 qmp_block_job_complete(device, &error);
1246
1247 hmp_handle_error(mon, &error);
1248}
1249
e1c37d0e
LC
1250typedef struct MigrationStatus
1251{
1252 QEMUTimer *timer;
1253 Monitor *mon;
1254 bool is_block_migration;
1255} MigrationStatus;
1256
1257static void hmp_migrate_status_cb(void *opaque)
1258{
1259 MigrationStatus *status = opaque;
1260 MigrationInfo *info;
1261
1262 info = qmp_query_migrate(NULL);
dde3a218
SA
1263 if (!info->has_status || strcmp(info->status, "active") == 0 ||
1264 strcmp(info->status, "setup") == 0) {
e1c37d0e
LC
1265 if (info->has_disk) {
1266 int progress;
1267
1268 if (info->disk->remaining) {
1269 progress = info->disk->transferred * 100 / info->disk->total;
1270 } else {
1271 progress = 100;
1272 }
1273
1274 monitor_printf(status->mon, "Completed %d %%\r", progress);
1275 monitor_flush(status->mon);
1276 }
1277
bc72ad67 1278 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
e1c37d0e
LC
1279 } else {
1280 if (status->is_block_migration) {
1281 monitor_printf(status->mon, "\n");
1282 }
1283 monitor_resume(status->mon);
bc72ad67 1284 timer_del(status->timer);
e1c37d0e
LC
1285 g_free(status);
1286 }
1287
1288 qapi_free_MigrationInfo(info);
1289}
1290
1291void hmp_migrate(Monitor *mon, const QDict *qdict)
1292{
1293 int detach = qdict_get_try_bool(qdict, "detach", 0);
1294 int blk = qdict_get_try_bool(qdict, "blk", 0);
1295 int inc = qdict_get_try_bool(qdict, "inc", 0);
1296 const char *uri = qdict_get_str(qdict, "uri");
1297 Error *err = NULL;
1298
1299 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1300 if (err) {
1301 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1302 error_free(err);
1303 return;
1304 }
1305
1306 if (!detach) {
1307 MigrationStatus *status;
1308
1309 if (monitor_suspend(mon) < 0) {
1310 monitor_printf(mon, "terminal does not allow synchronous "
1311 "migration, continuing detached\n");
1312 return;
1313 }
1314
1315 status = g_malloc0(sizeof(*status));
1316 status->mon = mon;
1317 status->is_block_migration = blk || inc;
bc72ad67 1318 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
e1c37d0e 1319 status);
bc72ad67 1320 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
e1c37d0e
LC
1321 }
1322}
a15fef21
LC
1323
1324void hmp_device_del(Monitor *mon, const QDict *qdict)
1325{
1326 const char *id = qdict_get_str(qdict, "id");
1327 Error *err = NULL;
1328
1329 qmp_device_del(id, &err);
1330 hmp_handle_error(mon, &err);
1331}
783e9b48
WC
1332
1333void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1334{
e940f543 1335 Error *err = NULL;
783e9b48 1336 int paging = qdict_get_try_bool(qdict, "paging", 0);
1b7a0f75
QN
1337 int zlib = qdict_get_try_bool(qdict, "zlib", 0);
1338 int lzo = qdict_get_try_bool(qdict, "lzo", 0);
1339 int snappy = qdict_get_try_bool(qdict, "snappy", 0);
75363769 1340 const char *file = qdict_get_str(qdict, "filename");
783e9b48
WC
1341 bool has_begin = qdict_haskey(qdict, "begin");
1342 bool has_length = qdict_haskey(qdict, "length");
1343 int64_t begin = 0;
1344 int64_t length = 0;
b53ccc30 1345 enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
75363769 1346 char *prot;
783e9b48 1347
1b7a0f75 1348 if (zlib + lzo + snappy > 1) {
e940f543
MA
1349 error_setg(&err, "only one of '-z|-l|-s' can be set");
1350 hmp_handle_error(mon, &err);
1b7a0f75
QN
1351 return;
1352 }
1353
1354 if (zlib) {
1355 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1356 }
1357
1358 if (lzo) {
1359 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1360 }
1361
1362 if (snappy) {
1363 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1364 }
1365
783e9b48
WC
1366 if (has_begin) {
1367 begin = qdict_get_int(qdict, "begin");
1368 }
1369 if (has_length) {
1370 length = qdict_get_int(qdict, "length");
1371 }
1372
75363769
LC
1373 prot = g_strconcat("file:", file, NULL);
1374
1375 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
e940f543
MA
1376 true, dump_format, &err);
1377 hmp_handle_error(mon, &err);
75363769 1378 g_free(prot);
783e9b48 1379}
928059a3
LC
1380
1381void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1382{
1383 Error *err = NULL;
1384 QemuOpts *opts;
1385
1386 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
84d18f06 1387 if (err) {
928059a3
LC
1388 goto out;
1389 }
1390
1391 netdev_add(opts, &err);
84d18f06 1392 if (err) {
928059a3
LC
1393 qemu_opts_del(opts);
1394 }
1395
1396out:
1397 hmp_handle_error(mon, &err);
1398}
5f964155
LC
1399
1400void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1401{
1402 const char *id = qdict_get_str(qdict, "id");
1403 Error *err = NULL;
1404
1405 qmp_netdev_del(id, &err);
1406 hmp_handle_error(mon, &err);
1407}
208c9d1b 1408
cff8b2c6
PB
1409void hmp_object_add(Monitor *mon, const QDict *qdict)
1410{
1411 Error *err = NULL;
f9f3a5ec 1412 Error *err_end = NULL;
cff8b2c6
PB
1413 QemuOpts *opts;
1414 char *type = NULL;
1415 char *id = NULL;
1416 void *dummy = NULL;
1417 OptsVisitor *ov;
1418 QDict *pdict;
1419
1420 opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1421 if (err) {
1422 goto out;
1423 }
1424
1425 ov = opts_visitor_new(opts);
1426 pdict = qdict_clone_shallow(qdict);
1427
1428 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
1429 if (err) {
1430 goto out_clean;
1431 }
1432
1433 qdict_del(pdict, "qom-type");
1434 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
1435 if (err) {
f9f3a5ec 1436 goto out_end;
cff8b2c6
PB
1437 }
1438
1439 qdict_del(pdict, "id");
1440 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
1441 if (err) {
f9f3a5ec 1442 goto out_end;
cff8b2c6
PB
1443 }
1444
1445 object_add(type, id, pdict, opts_get_visitor(ov), &err);
f9f3a5ec
MA
1446
1447out_end:
1448 visit_end_struct(opts_get_visitor(ov), &err_end);
1449 if (!err && err_end) {
cff8b2c6
PB
1450 qmp_object_del(id, NULL);
1451 }
f9f3a5ec 1452 error_propagate(&err, err_end);
cff8b2c6
PB
1453out_clean:
1454 opts_visitor_cleanup(ov);
1455
1456 QDECREF(pdict);
1457 qemu_opts_del(opts);
1458 g_free(id);
1459 g_free(type);
1460 g_free(dummy);
1461
1462out:
1463 hmp_handle_error(mon, &err);
1464}
1465
208c9d1b
CB
1466void hmp_getfd(Monitor *mon, const QDict *qdict)
1467{
1468 const char *fdname = qdict_get_str(qdict, "fdname");
e940f543 1469 Error *err = NULL;
208c9d1b 1470
e940f543
MA
1471 qmp_getfd(fdname, &err);
1472 hmp_handle_error(mon, &err);
208c9d1b
CB
1473}
1474
1475void hmp_closefd(Monitor *mon, const QDict *qdict)
1476{
1477 const char *fdname = qdict_get_str(qdict, "fdname");
e940f543 1478 Error *err = NULL;
208c9d1b 1479
e940f543
MA
1480 qmp_closefd(fdname, &err);
1481 hmp_handle_error(mon, &err);
208c9d1b 1482}
e4c8f004
AK
1483
1484void hmp_send_key(Monitor *mon, const QDict *qdict)
1485{
1486 const char *keys = qdict_get_str(qdict, "keys");
9f328977 1487 KeyValueList *keylist, *head = NULL, *tmp = NULL;
e4c8f004
AK
1488 int has_hold_time = qdict_haskey(qdict, "hold-time");
1489 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1490 Error *err = NULL;
1491 char keyname_buf[16];
1492 char *separator;
9f328977 1493 int keyname_len;
e4c8f004
AK
1494
1495 while (1) {
1496 separator = strchr(keys, '-');
1497 keyname_len = separator ? separator - keys : strlen(keys);
1498 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1499
1500 /* Be compatible with old interface, convert user inputted "<" */
1501 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1502 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1503 keyname_len = 4;
1504 }
1505 keyname_buf[keyname_len] = 0;
1506
e4c8f004 1507 keylist = g_malloc0(sizeof(*keylist));
9f328977 1508 keylist->value = g_malloc0(sizeof(*keylist->value));
e4c8f004
AK
1509
1510 if (!head) {
1511 head = keylist;
1512 }
1513 if (tmp) {
1514 tmp->next = keylist;
1515 }
1516 tmp = keylist;
1517
9f328977
LC
1518 if (strstart(keyname_buf, "0x", NULL)) {
1519 char *endp;
1520 int value = strtoul(keyname_buf, &endp, 0);
1521 if (*endp != '\0') {
1522 goto err_out;
1523 }
1524 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1525 keylist->value->number = value;
1526 } else {
1527 int idx = index_from_key(keyname_buf);
1528 if (idx == Q_KEY_CODE_MAX) {
1529 goto err_out;
1530 }
1531 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1532 keylist->value->qcode = idx;
1533 }
1534
e4c8f004
AK
1535 if (!separator) {
1536 break;
1537 }
1538 keys = separator + 1;
1539 }
1540
9f328977 1541 qmp_send_key(head, has_hold_time, hold_time, &err);
e4c8f004 1542 hmp_handle_error(mon, &err);
9f328977
LC
1543
1544out:
1545 qapi_free_KeyValueList(head);
1546 return;
1547
1548err_out:
1549 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1550 goto out;
e4c8f004 1551}
ad39cf6d
LC
1552
1553void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1554{
1555 const char *filename = qdict_get_str(qdict, "filename");
1556 Error *err = NULL;
1557
1558 qmp_screendump(filename, &err);
1559 hmp_handle_error(mon, &err);
1560}
4057725f
PB
1561
1562void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1563{
1564 const char *uri = qdict_get_str(qdict, "uri");
1565 int writable = qdict_get_try_bool(qdict, "writable", 0);
1566 int all = qdict_get_try_bool(qdict, "all", 0);
1567 Error *local_err = NULL;
1568 BlockInfoList *block_list, *info;
1569 SocketAddress *addr;
1570
1571 if (writable && !all) {
1572 error_setg(&local_err, "-w only valid together with -a");
1573 goto exit;
1574 }
1575
1576 /* First check if the address is valid and start the server. */
1577 addr = socket_parse(uri, &local_err);
1578 if (local_err != NULL) {
1579 goto exit;
1580 }
1581
1582 qmp_nbd_server_start(addr, &local_err);
1583 qapi_free_SocketAddress(addr);
1584 if (local_err != NULL) {
1585 goto exit;
1586 }
1587
1588 if (!all) {
1589 return;
1590 }
1591
1592 /* Then try adding all block devices. If one fails, close all and
1593 * exit.
1594 */
1595 block_list = qmp_query_block(NULL);
1596
1597 for (info = block_list; info; info = info->next) {
1598 if (!info->value->has_inserted) {
1599 continue;
1600 }
1601
1602 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1603
1604 if (local_err != NULL) {
1605 qmp_nbd_server_stop(NULL);
1606 break;
1607 }
1608 }
1609
1610 qapi_free_BlockInfoList(block_list);
1611
1612exit:
1613 hmp_handle_error(mon, &local_err);
1614}
1615
1616void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1617{
1618 const char *device = qdict_get_str(qdict, "device");
1619 int writable = qdict_get_try_bool(qdict, "writable", 0);
1620 Error *local_err = NULL;
1621
1622 qmp_nbd_server_add(device, true, writable, &local_err);
1623
1624 if (local_err != NULL) {
1625 hmp_handle_error(mon, &local_err);
1626 }
1627}
1628
1629void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1630{
e940f543 1631 Error *err = NULL;
4057725f 1632
e940f543
MA
1633 qmp_nbd_server_stop(&err);
1634 hmp_handle_error(mon, &err);
4057725f 1635}
f1088908 1636
abf23329
JH
1637void hmp_cpu_add(Monitor *mon, const QDict *qdict)
1638{
1639 int cpuid;
1640 Error *err = NULL;
1641
1642 cpuid = qdict_get_int(qdict, "id");
1643 qmp_cpu_add(cpuid, &err);
1644 hmp_handle_error(mon, &err);
1645}
1646
f1088908
GH
1647void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1648{
1649 const char *args = qdict_get_str(qdict, "args");
1650 Error *err = NULL;
1651 QemuOpts *opts;
1652
1653 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1654 if (opts == NULL) {
312fd5f2 1655 error_setg(&err, "Parsing chardev args failed");
f1088908
GH
1656 } else {
1657 qemu_chr_new_from_opts(opts, NULL, &err);
1658 }
1659 hmp_handle_error(mon, &err);
1660}
1661
1662void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1663{
1664 Error *local_err = NULL;
1665
1666 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1667 hmp_handle_error(mon, &local_err);
1668}
587da2c3
KW
1669
1670void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1671{
1672 BlockDriverState *bs;
1673 const char* device = qdict_get_str(qdict, "device");
1674 const char* command = qdict_get_str(qdict, "command");
1675 Error *err = NULL;
1676
1677 bs = bdrv_find(device);
1678 if (bs) {
1679 qemuio_command(bs, command);
1680 } else {
1681 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1682 }
1683
1684 hmp_handle_error(mon, &err);
1685}
ab2d0531
PB
1686
1687void hmp_object_del(Monitor *mon, const QDict *qdict)
1688{
1689 const char *id = qdict_get_str(qdict, "id");
1690 Error *err = NULL;
1691
1692 qmp_object_del(id, &err);
1693 hmp_handle_error(mon, &err);
1694}
eb1539b2
HT
1695
1696void hmp_info_memdev(Monitor *mon, const QDict *qdict)
1697{
1698 Error *err = NULL;
1699 MemdevList *memdev_list = qmp_query_memdev(&err);
1700 MemdevList *m = memdev_list;
1701 StringOutputVisitor *ov;
976620ac 1702 char *str;
eb1539b2
HT
1703 int i = 0;
1704
1705
1706 while (m) {
1707 ov = string_output_visitor_new(false);
1708 visit_type_uint16List(string_output_get_visitor(ov),
1709 &m->value->host_nodes, NULL, NULL);
8f4e5ac3 1710 monitor_printf(mon, "memory backend: %d\n", i);
eb1539b2
HT
1711 monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
1712 monitor_printf(mon, " merge: %s\n",
1713 m->value->merge ? "true" : "false");
1714 monitor_printf(mon, " dump: %s\n",
1715 m->value->dump ? "true" : "false");
1716 monitor_printf(mon, " prealloc: %s\n",
1717 m->value->prealloc ? "true" : "false");
1718 monitor_printf(mon, " policy: %s\n",
1719 HostMemPolicy_lookup[m->value->policy]);
976620ac
CF
1720 str = string_output_get_string(ov);
1721 monitor_printf(mon, " host nodes: %s\n", str);
eb1539b2 1722
976620ac 1723 g_free(str);
eb1539b2
HT
1724 string_output_visitor_cleanup(ov);
1725 m = m->next;
1726 i++;
1727 }
1728
1729 monitor_printf(mon, "\n");
ecaf54a0
CF
1730
1731 qapi_free_MemdevList(memdev_list);
eb1539b2 1732}
a631892f
ZG
1733
1734void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
1735{
1736 Error *err = NULL;
1737 MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
1738 MemoryDeviceInfoList *info;
1739 MemoryDeviceInfo *value;
1740 PCDIMMDeviceInfo *di;
1741
1742 for (info = info_list; info; info = info->next) {
1743 value = info->value;
1744
1745 if (value) {
1746 switch (value->kind) {
1747 case MEMORY_DEVICE_INFO_KIND_DIMM:
1748 di = value->dimm;
1749
1750 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
1751 MemoryDeviceInfoKind_lookup[value->kind],
1752 di->id ? di->id : "");
1753 monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
1754 monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
1755 monitor_printf(mon, " node: %" PRId64 "\n", di->node);
1756 monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
1757 monitor_printf(mon, " memdev: %s\n", di->memdev);
1758 monitor_printf(mon, " hotplugged: %s\n",
1759 di->hotplugged ? "true" : "false");
1760 monitor_printf(mon, " hotpluggable: %s\n",
1761 di->hotpluggable ? "true" : "false");
1762 break;
1763 default:
1764 break;
1765 }
1766 }
1767 }
1768
1769 qapi_free_MemoryDeviceInfoList(info_list);
1770}