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