]> git.proxmox.com Git - mirror_qemu.git/blame - hmp.c
virtio-console: no need to remove char handlers explicitly
[mirror_qemu.git] / hmp.c
CommitLineData
48a32bed
AL
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 */
13
14#include "hmp.h"
15#include "qmp-commands.h"
16
0cfd6a9a
LC
17static void hmp_handle_error(Monitor *mon, Error **errp)
18{
19 if (error_is_set(errp)) {
20 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
21 error_free(*errp);
22 }
23}
24
48a32bed
AL
25void hmp_info_name(Monitor *mon)
26{
27 NameInfo *info;
28
29 info = qmp_query_name(NULL);
30 if (info->has_name) {
31 monitor_printf(mon, "%s\n", info->name);
32 }
33 qapi_free_NameInfo(info);
34}
b9c15f16
LC
35
36void hmp_info_version(Monitor *mon)
37{
38 VersionInfo *info;
39
40 info = qmp_query_version(NULL);
41
42 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
43 info->qemu.major, info->qemu.minor, info->qemu.micro,
44 info->package);
45
46 qapi_free_VersionInfo(info);
47}
292a2602
LC
48
49void hmp_info_kvm(Monitor *mon)
50{
51 KvmInfo *info;
52
53 info = qmp_query_kvm(NULL);
54 monitor_printf(mon, "kvm support: ");
55 if (info->present) {
56 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
57 } else {
58 monitor_printf(mon, "not compiled\n");
59 }
60
61 qapi_free_KvmInfo(info);
62}
63
1fa9a5e4
LC
64void hmp_info_status(Monitor *mon)
65{
66 StatusInfo *info;
67
68 info = qmp_query_status(NULL);
69
70 monitor_printf(mon, "VM status: %s%s",
71 info->running ? "running" : "paused",
72 info->singlestep ? " (single step mode)" : "");
73
74 if (!info->running && info->status != RUN_STATE_PAUSED) {
75 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
76 }
77
78 monitor_printf(mon, "\n");
79
80 qapi_free_StatusInfo(info);
81}
82
efab767e
LC
83void hmp_info_uuid(Monitor *mon)
84{
85 UuidInfo *info;
86
87 info = qmp_query_uuid(NULL);
88 monitor_printf(mon, "%s\n", info->UUID);
89 qapi_free_UuidInfo(info);
90}
c5a415a0
LC
91
92void hmp_info_chardev(Monitor *mon)
93{
94 ChardevInfoList *char_info, *info;
95
96 char_info = qmp_query_chardev(NULL);
97 for (info = char_info; info; info = info->next) {
98 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
99 info->value->filename);
100 }
101
102 qapi_free_ChardevInfoList(char_info);
103}
7a7f325e 104
e235cec3
LC
105void hmp_info_mice(Monitor *mon)
106{
107 MouseInfoList *mice_list, *mouse;
108
109 mice_list = qmp_query_mice(NULL);
110 if (!mice_list) {
111 monitor_printf(mon, "No mouse devices connected\n");
112 return;
113 }
114
115 for (mouse = mice_list; mouse; mouse = mouse->next) {
116 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
117 mouse->value->current ? '*' : ' ',
118 mouse->value->index, mouse->value->name,
119 mouse->value->absolute ? " (absolute)" : "");
120 }
121
122 qapi_free_MouseInfoList(mice_list);
123}
124
791e7c82
LC
125void hmp_info_migrate(Monitor *mon)
126{
127 MigrationInfo *info;
128
129 info = qmp_query_migrate(NULL);
130
131 if (info->has_status) {
132 monitor_printf(mon, "Migration status: %s\n", info->status);
133 }
134
135 if (info->has_ram) {
136 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
137 info->ram->transferred >> 10);
138 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
139 info->ram->remaining >> 10);
140 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
141 info->ram->total >> 10);
142 }
143
144 if (info->has_disk) {
145 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
146 info->disk->transferred >> 10);
147 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
148 info->disk->remaining >> 10);
149 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
150 info->disk->total >> 10);
151 }
152
153 qapi_free_MigrationInfo(info);
154}
155
de0b36b6
LC
156void hmp_info_cpus(Monitor *mon)
157{
158 CpuInfoList *cpu_list, *cpu;
159
160 cpu_list = qmp_query_cpus(NULL);
161
162 for (cpu = cpu_list; cpu; cpu = cpu->next) {
163 int active = ' ';
164
165 if (cpu->value->CPU == monitor_get_cpu_index()) {
166 active = '*';
167 }
168
169 monitor_printf(mon, "%c CPU #%" PRId64 ": ", active, cpu->value->CPU);
170
171 if (cpu->value->has_pc) {
172 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
173 }
174 if (cpu->value->has_nip) {
175 monitor_printf(mon, "nip=0x%016" PRIx64, cpu->value->nip);
176 }
177 if (cpu->value->has_npc) {
178 monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
179 monitor_printf(mon, "npc=0x%016" PRIx64, cpu->value->npc);
180 }
181 if (cpu->value->has_PC) {
182 monitor_printf(mon, "PC=0x%016" PRIx64, cpu->value->PC);
183 }
184
185 if (cpu->value->halted) {
186 monitor_printf(mon, " (halted)");
187 }
188
189 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
190 }
191
192 qapi_free_CpuInfoList(cpu_list);
193}
194
b2023818
LC
195void hmp_info_block(Monitor *mon)
196{
197 BlockInfoList *block_list, *info;
198
199 block_list = qmp_query_block(NULL);
200
201 for (info = block_list; info; info = info->next) {
202 monitor_printf(mon, "%s: removable=%d",
203 info->value->device, info->value->removable);
204
205 if (info->value->removable) {
206 monitor_printf(mon, " locked=%d", info->value->locked);
207 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
208 }
209
210 if (info->value->has_io_status) {
211 monitor_printf(mon, " io-status=%s",
212 BlockDeviceIoStatus_lookup[info->value->io_status]);
213 }
214
215 if (info->value->has_inserted) {
216 monitor_printf(mon, " file=");
217 monitor_print_filename(mon, info->value->inserted->file);
218
219 if (info->value->inserted->has_backing_file) {
220 monitor_printf(mon, " backing_file=");
221 monitor_print_filename(mon, info->value->inserted->backing_file);
222 }
223 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
224 info->value->inserted->ro,
225 info->value->inserted->drv,
226 info->value->inserted->encrypted);
727f005e
ZYW
227
228 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
229 " bps_wr=%" PRId64 " iops=%" PRId64
230 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
231 info->value->inserted->bps,
232 info->value->inserted->bps_rd,
233 info->value->inserted->bps_wr,
234 info->value->inserted->iops,
235 info->value->inserted->iops_rd,
236 info->value->inserted->iops_wr);
b2023818
LC
237 } else {
238 monitor_printf(mon, " [not inserted]");
239 }
240
241 monitor_printf(mon, "\n");
242 }
243
244 qapi_free_BlockInfoList(block_list);
245}
246
f11f57e4
LC
247void hmp_info_blockstats(Monitor *mon)
248{
249 BlockStatsList *stats_list, *stats;
250
251 stats_list = qmp_query_blockstats(NULL);
252
253 for (stats = stats_list; stats; stats = stats->next) {
254 if (!stats->value->has_device) {
255 continue;
256 }
257
258 monitor_printf(mon, "%s:", stats->value->device);
259 monitor_printf(mon, " rd_bytes=%" PRId64
260 " wr_bytes=%" PRId64
261 " rd_operations=%" PRId64
262 " wr_operations=%" PRId64
263 " flush_operations=%" PRId64
264 " wr_total_time_ns=%" PRId64
265 " rd_total_time_ns=%" PRId64
266 " flush_total_time_ns=%" PRId64
267 "\n",
268 stats->value->stats->rd_bytes,
269 stats->value->stats->wr_bytes,
270 stats->value->stats->rd_operations,
271 stats->value->stats->wr_operations,
272 stats->value->stats->flush_operations,
273 stats->value->stats->wr_total_time_ns,
274 stats->value->stats->rd_total_time_ns,
275 stats->value->stats->flush_total_time_ns);
276 }
277
278 qapi_free_BlockStatsList(stats_list);
279}
280
2b54aa87
LC
281void hmp_info_vnc(Monitor *mon)
282{
283 VncInfo *info;
284 Error *err = NULL;
285 VncClientInfoList *client;
286
287 info = qmp_query_vnc(&err);
288 if (err) {
289 monitor_printf(mon, "%s\n", error_get_pretty(err));
290 error_free(err);
291 return;
292 }
293
294 if (!info->enabled) {
295 monitor_printf(mon, "Server: disabled\n");
296 goto out;
297 }
298
299 monitor_printf(mon, "Server:\n");
300 if (info->has_host && info->has_service) {
301 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
302 }
303 if (info->has_auth) {
304 monitor_printf(mon, " auth: %s\n", info->auth);
305 }
306
307 if (!info->has_clients || info->clients == NULL) {
308 monitor_printf(mon, "Client: none\n");
309 } else {
310 for (client = info->clients; client; client = client->next) {
311 monitor_printf(mon, "Client:\n");
312 monitor_printf(mon, " address: %s:%s\n",
313 client->value->host, client->value->service);
314 monitor_printf(mon, " x509_dname: %s\n",
315 client->value->x509_dname ?
316 client->value->x509_dname : "none");
317 monitor_printf(mon, " username: %s\n",
318 client->value->has_sasl_username ?
319 client->value->sasl_username : "none");
320 }
321 }
322
323out:
324 qapi_free_VncInfo(info);
325}
326
d1f29646
LC
327void hmp_info_spice(Monitor *mon)
328{
329 SpiceChannelList *chan;
330 SpiceInfo *info;
331
332 info = qmp_query_spice(NULL);
333
334 if (!info->enabled) {
335 monitor_printf(mon, "Server: disabled\n");
336 goto out;
337 }
338
339 monitor_printf(mon, "Server:\n");
340 if (info->has_port) {
341 monitor_printf(mon, " address: %s:%" PRId64 "\n",
342 info->host, info->port);
343 }
344 if (info->has_tls_port) {
345 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
346 info->host, info->tls_port);
347 }
348 monitor_printf(mon, " auth: %s\n", info->auth);
349 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
350
351 if (!info->has_channels || info->channels == NULL) {
352 monitor_printf(mon, "Channels: none\n");
353 } else {
354 for (chan = info->channels; chan; chan = chan->next) {
355 monitor_printf(mon, "Channel:\n");
356 monitor_printf(mon, " address: %s:%s%s\n",
357 chan->value->host, chan->value->port,
358 chan->value->tls ? " [tls]" : "");
359 monitor_printf(mon, " session: %" PRId64 "\n",
360 chan->value->connection_id);
361 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
362 chan->value->channel_type, chan->value->channel_id);
363 }
364 }
365
366out:
367 qapi_free_SpiceInfo(info);
368}
369
96637bcd
LC
370void hmp_info_balloon(Monitor *mon)
371{
372 BalloonInfo *info;
373 Error *err = NULL;
374
375 info = qmp_query_balloon(&err);
376 if (err) {
377 monitor_printf(mon, "%s\n", error_get_pretty(err));
378 error_free(err);
379 return;
380 }
381
382 monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
383 if (info->has_mem_swapped_in) {
384 monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
385 }
386 if (info->has_mem_swapped_out) {
387 monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
388 }
389 if (info->has_major_page_faults) {
390 monitor_printf(mon, " major_page_faults=%" PRId64,
391 info->major_page_faults);
392 }
393 if (info->has_minor_page_faults) {
394 monitor_printf(mon, " minor_page_faults=%" PRId64,
395 info->minor_page_faults);
396 }
397 if (info->has_free_mem) {
398 monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
399 }
400 if (info->has_total_mem) {
401 monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
402 }
403
404 monitor_printf(mon, "\n");
405
406 qapi_free_BalloonInfo(info);
407}
408
79627472
LC
409static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
410{
411 PciMemoryRegionList *region;
412
413 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
414 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
415 dev->slot, dev->function);
416 monitor_printf(mon, " ");
417
418 if (dev->class_info.has_desc) {
419 monitor_printf(mon, "%s", dev->class_info.desc);
420 } else {
421 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
422 }
423
424 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
425 dev->id.vendor, dev->id.device);
426
427 if (dev->has_irq) {
428 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
429 }
430
431 if (dev->has_pci_bridge) {
432 monitor_printf(mon, " BUS %" PRId64 ".\n",
433 dev->pci_bridge->bus.number);
434 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
435 dev->pci_bridge->bus.secondary);
436 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
437 dev->pci_bridge->bus.subordinate);
438
439 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
440 dev->pci_bridge->bus.io_range->base,
441 dev->pci_bridge->bus.io_range->limit);
442
443 monitor_printf(mon,
444 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
445 dev->pci_bridge->bus.memory_range->base,
446 dev->pci_bridge->bus.memory_range->limit);
447
448 monitor_printf(mon, " prefetchable memory range "
449 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
450 dev->pci_bridge->bus.prefetchable_range->base,
451 dev->pci_bridge->bus.prefetchable_range->limit);
452 }
453
454 for (region = dev->regions; region; region = region->next) {
455 uint64_t addr, size;
456
457 addr = region->value->address;
458 size = region->value->size;
459
460 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
461
462 if (!strcmp(region->value->type, "io")) {
463 monitor_printf(mon, "I/O at 0x%04" PRIx64
464 " [0x%04" PRIx64 "].\n",
465 addr, addr + size - 1);
466 } else {
467 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
468 " [0x%08" PRIx64 "].\n",
469 region->value->mem_type_64 ? 64 : 32,
470 region->value->prefetch ? " prefetchable" : "",
471 addr, addr + size - 1);
472 }
473 }
474
475 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
476
477 if (dev->has_pci_bridge) {
478 if (dev->pci_bridge->has_devices) {
479 PciDeviceInfoList *cdev;
480 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
481 hmp_info_pci_device(mon, cdev->value);
482 }
483 }
484 }
485}
486
487void hmp_info_pci(Monitor *mon)
488{
f46cee37 489 PciInfoList *info_list, *info;
79627472
LC
490 Error *err = NULL;
491
f46cee37 492 info_list = qmp_query_pci(&err);
79627472
LC
493 if (err) {
494 monitor_printf(mon, "PCI devices not supported\n");
495 error_free(err);
496 return;
497 }
498
f46cee37 499 for (info = info_list; info; info = info->next) {
79627472
LC
500 PciDeviceInfoList *dev;
501
502 for (dev = info->value->devices; dev; dev = dev->next) {
503 hmp_info_pci_device(mon, dev->value);
504 }
505 }
506
f46cee37 507 qapi_free_PciInfoList(info_list);
79627472
LC
508}
509
7a7f325e
LC
510void hmp_quit(Monitor *mon, const QDict *qdict)
511{
512 monitor_suspend(mon);
513 qmp_quit(NULL);
514}
5f158f21
LC
515
516void hmp_stop(Monitor *mon, const QDict *qdict)
517{
518 qmp_stop(NULL);
519}
38d22653
LC
520
521void hmp_system_reset(Monitor *mon, const QDict *qdict)
522{
523 qmp_system_reset(NULL);
524}
5bc465e4
LC
525
526void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
527{
528 qmp_system_powerdown(NULL);
529}
755f1968
LC
530
531void hmp_cpu(Monitor *mon, const QDict *qdict)
532{
533 int64_t cpu_index;
534
535 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
536 use it are converted to the QAPI */
537 cpu_index = qdict_get_int(qdict, "index");
538 if (monitor_set_cpu(cpu_index) < 0) {
539 monitor_printf(mon, "invalid CPU index\n");
540 }
541}
0cfd6a9a
LC
542
543void hmp_memsave(Monitor *mon, const QDict *qdict)
544{
545 uint32_t size = qdict_get_int(qdict, "size");
546 const char *filename = qdict_get_str(qdict, "filename");
547 uint64_t addr = qdict_get_int(qdict, "val");
548 Error *errp = NULL;
549
550 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
551 hmp_handle_error(mon, &errp);
552}
6d3962bf
LC
553
554void hmp_pmemsave(Monitor *mon, const QDict *qdict)
555{
556 uint32_t size = qdict_get_int(qdict, "size");
557 const char *filename = qdict_get_str(qdict, "filename");
558 uint64_t addr = qdict_get_int(qdict, "val");
559 Error *errp = NULL;
560
561 qmp_pmemsave(addr, size, filename, &errp);
562 hmp_handle_error(mon, &errp);
563}
e42e818b
LC
564
565static void hmp_cont_cb(void *opaque, int err)
566{
567 Monitor *mon = opaque;
568
569 if (!err) {
570 hmp_cont(mon, NULL);
571 }
572}
573
574void hmp_cont(Monitor *mon, const QDict *qdict)
575{
576 Error *errp = NULL;
577
578 qmp_cont(&errp);
579 if (error_is_set(&errp)) {
580 if (error_is_type(errp, QERR_DEVICE_ENCRYPTED)) {
581 const char *device;
582
583 /* The device is encrypted. Ask the user for the password
584 and retry */
585
586 device = error_get_field(errp, "device");
587 assert(device != NULL);
588
589 monitor_read_block_device_key(mon, device, hmp_cont_cb, mon);
590 error_free(errp);
591 return;
592 }
593 hmp_handle_error(mon, &errp);
594 }
595}
ab49ab5c
LC
596
597void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
598{
599 Error *errp = NULL;
600
601 qmp_inject_nmi(&errp);
602 hmp_handle_error(mon, &errp);
603}
4b37156c
LC
604
605void hmp_set_link(Monitor *mon, const QDict *qdict)
606{
607 const char *name = qdict_get_str(qdict, "name");
608 int up = qdict_get_bool(qdict, "up");
609 Error *errp = NULL;
610
611 qmp_set_link(name, up, &errp);
612 hmp_handle_error(mon, &errp);
613}
a4dea8a9
LC
614
615void hmp_block_passwd(Monitor *mon, const QDict *qdict)
616{
617 const char *device = qdict_get_str(qdict, "device");
618 const char *password = qdict_get_str(qdict, "password");
619 Error *errp = NULL;
620
621 qmp_block_passwd(device, password, &errp);
622 hmp_handle_error(mon, &errp);
623}
d72f3264
LC
624
625void hmp_balloon(Monitor *mon, const QDict *qdict)
626{
627 int64_t value = qdict_get_int(qdict, "value");
628 Error *errp = NULL;
629
630 qmp_balloon(value, &errp);
631 if (error_is_set(&errp)) {
632 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
633 error_free(errp);
634 }
635}
5e7caacb
LC
636
637void hmp_block_resize(Monitor *mon, const QDict *qdict)
638{
639 const char *device = qdict_get_str(qdict, "device");
640 int64_t size = qdict_get_int(qdict, "size");
641 Error *errp = NULL;
642
643 qmp_block_resize(device, size, &errp);
644 hmp_handle_error(mon, &errp);
645}
6106e249
LC
646
647void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
648{
649 const char *device = qdict_get_str(qdict, "device");
650 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
651 const char *format = qdict_get_try_str(qdict, "format");
652 Error *errp = NULL;
653
654 if (!filename) {
655 /* In the future, if 'snapshot-file' is not specified, the snapshot
656 will be taken internally. Today it's actually required. */
657 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
658 hmp_handle_error(mon, &errp);
659 return;
660 }
661
662 qmp_blockdev_snapshot_sync(device, filename, !!format, format, &errp);
663 hmp_handle_error(mon, &errp);
664}
6cdedb07
LC
665
666void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
667{
668 qmp_migrate_cancel(NULL);
669}
4f0a993b
LC
670
671void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
672{
673 double value = qdict_get_double(qdict, "value");
674 qmp_migrate_set_downtime(value, NULL);
675}
3dc85383
LC
676
677void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
678{
679 int64_t value = qdict_get_int(qdict, "value");
680 qmp_migrate_set_speed(value, NULL);
681}