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