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