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