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