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