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