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