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