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