blob: 9df84e3e222679a64f4b42e729fd597ceea81891 [file] [log] [blame]
Anthony Liguori48a32be2011-09-02 12:34:48 -05001/*
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
Anthony Liguori48a32be2011-09-02 12:34:48 -050014 */
15
16#include "hmp.h"
Luiz Capitulino928059a2012-04-18 17:34:15 -030017#include "net.h"
18#include "qemu-option.h"
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -020019#include "qemu-timer.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050020#include "qmp-commands.h"
Paolo Bonzini37003ad2012-06-07 04:02:21 +020021#include "monitor.h"
Amos Konge4c8f002012-08-31 10:56:26 +080022#include "console.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050023
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -020024static void hmp_handle_error(Monitor *mon, Error **errp)
25{
26 if (error_is_set(errp)) {
27 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
28 error_free(*errp);
29 }
30}
31
Anthony Liguori48a32be2011-09-02 12:34:48 -050032void hmp_info_name(Monitor *mon)
33{
34 NameInfo *info;
35
36 info = qmp_query_name(NULL);
37 if (info->has_name) {
38 monitor_printf(mon, "%s\n", info->name);
39 }
40 qapi_free_NameInfo(info);
41}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030042
43void hmp_info_version(Monitor *mon)
44{
45 VersionInfo *info;
46
47 info = qmp_query_version(NULL);
48
49 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
50 info->qemu.major, info->qemu.minor, info->qemu.micro,
51 info->package);
52
53 qapi_free_VersionInfo(info);
54}
Luiz Capitulino292a2602011-09-12 15:10:53 -030055
56void hmp_info_kvm(Monitor *mon)
57{
58 KvmInfo *info;
59
60 info = qmp_query_kvm(NULL);
61 monitor_printf(mon, "kvm support: ");
62 if (info->present) {
63 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
64 } else {
65 monitor_printf(mon, "not compiled\n");
66 }
67
68 qapi_free_KvmInfo(info);
69}
70
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -030071void hmp_info_status(Monitor *mon)
72{
73 StatusInfo *info;
74
75 info = qmp_query_status(NULL);
76
77 monitor_printf(mon, "VM status: %s%s",
78 info->running ? "running" : "paused",
79 info->singlestep ? " (single step mode)" : "");
80
81 if (!info->running && info->status != RUN_STATE_PAUSED) {
82 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
83 }
84
85 monitor_printf(mon, "\n");
86
87 qapi_free_StatusInfo(info);
88}
89
Luiz Capitulinoefab7672011-09-13 17:16:25 -030090void hmp_info_uuid(Monitor *mon)
91{
92 UuidInfo *info;
93
94 info = qmp_query_uuid(NULL);
95 monitor_printf(mon, "%s\n", info->UUID);
96 qapi_free_UuidInfo(info);
97}
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -030098
99void hmp_info_chardev(Monitor *mon)
100{
101 ChardevInfoList *char_info, *info;
102
103 char_info = qmp_query_chardev(NULL);
104 for (info = char_info; info; info = info->next) {
105 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
106 info->value->filename);
107 }
108
109 qapi_free_ChardevInfoList(char_info);
110}
Luiz Capitulino7a7f3252011-09-15 14:20:28 -0300111
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300112void hmp_info_mice(Monitor *mon)
113{
114 MouseInfoList *mice_list, *mouse;
115
116 mice_list = qmp_query_mice(NULL);
117 if (!mice_list) {
118 monitor_printf(mon, "No mouse devices connected\n");
119 return;
120 }
121
122 for (mouse = mice_list; mouse; mouse = mouse->next) {
123 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
124 mouse->value->current ? '*' : ' ',
125 mouse->value->index, mouse->value->name,
126 mouse->value->absolute ? " (absolute)" : "");
127 }
128
129 qapi_free_MouseInfoList(mice_list);
130}
131
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300132void hmp_info_migrate(Monitor *mon)
133{
134 MigrationInfo *info;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300135 MigrationCapabilityStatusList *caps, *cap;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300136
137 info = qmp_query_migrate(NULL);
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300138 caps = qmp_query_migrate_capabilities(NULL);
139
140 /* do not display parameters during setup */
141 if (info->has_status && caps) {
142 monitor_printf(mon, "capabilities: ");
143 for (cap = caps; cap; cap = cap->next) {
144 monitor_printf(mon, "%s: %s ",
145 MigrationCapability_lookup[cap->value->capability],
146 cap->value->state ? "on" : "off");
147 }
148 monitor_printf(mon, "\n");
149 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300150
151 if (info->has_status) {
152 monitor_printf(mon, "Migration status: %s\n", info->status);
Juan Quintela7aa939a2012-08-18 13:17:10 +0200153 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
154 info->total_time);
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200155 if (info->has_expected_downtime) {
156 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
157 info->expected_downtime);
158 }
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200159 if (info->has_downtime) {
160 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
161 info->downtime);
162 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300163 }
164
165 if (info->has_ram) {
166 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
167 info->ram->transferred >> 10);
168 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
169 info->ram->remaining >> 10);
170 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
171 info->ram->total >> 10);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300172 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
173 info->ram->duplicate);
174 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
175 info->ram->normal);
176 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
177 info->ram->normal_bytes >> 10);
Juan Quintela8d017192012-08-13 12:31:25 +0200178 if (info->ram->dirty_pages_rate) {
179 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
180 info->ram->dirty_pages_rate);
181 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300182 }
183
184 if (info->has_disk) {
185 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
186 info->disk->transferred >> 10);
187 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
188 info->disk->remaining >> 10);
189 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
190 info->disk->total >> 10);
191 }
192
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300193 if (info->has_xbzrle_cache) {
194 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
195 info->xbzrle_cache->cache_size);
196 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
197 info->xbzrle_cache->bytes >> 10);
198 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
199 info->xbzrle_cache->pages);
200 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
201 info->xbzrle_cache->cache_miss);
202 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
203 info->xbzrle_cache->overflow);
204 }
205
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300206 qapi_free_MigrationInfo(info);
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300207 qapi_free_MigrationCapabilityStatusList(caps);
208}
209
210void hmp_info_migrate_capabilities(Monitor *mon)
211{
212 MigrationCapabilityStatusList *caps, *cap;
213
214 caps = qmp_query_migrate_capabilities(NULL);
215
216 if (caps) {
217 monitor_printf(mon, "capabilities: ");
218 for (cap = caps; cap; cap = cap->next) {
219 monitor_printf(mon, "%s: %s ",
220 MigrationCapability_lookup[cap->value->capability],
221 cap->value->state ? "on" : "off");
222 }
223 monitor_printf(mon, "\n");
224 }
225
226 qapi_free_MigrationCapabilityStatusList(caps);
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300227}
228
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300229void hmp_info_migrate_cache_size(Monitor *mon)
230{
231 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
232 qmp_query_migrate_cache_size(NULL) >> 10);
233}
234
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300235void hmp_info_cpus(Monitor *mon)
236{
237 CpuInfoList *cpu_list, *cpu;
238
239 cpu_list = qmp_query_cpus(NULL);
240
241 for (cpu = cpu_list; cpu; cpu = cpu->next) {
242 int active = ' ';
243
244 if (cpu->value->CPU == monitor_get_cpu_index()) {
245 active = '*';
246 }
247
Aurelien Jarno852bef02012-10-19 23:19:19 +0200248 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300249
250 if (cpu->value->has_pc) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200251 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300252 }
253 if (cpu->value->has_nip) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200254 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300255 }
256 if (cpu->value->has_npc) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200257 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300258 }
259 if (cpu->value->has_PC) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200260 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300261 }
262
263 if (cpu->value->halted) {
264 monitor_printf(mon, " (halted)");
265 }
266
267 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
268 }
269
270 qapi_free_CpuInfoList(cpu_list);
271}
272
Luiz Capitulinob2023812011-09-21 17:16:47 -0300273void hmp_info_block(Monitor *mon)
274{
275 BlockInfoList *block_list, *info;
276
277 block_list = qmp_query_block(NULL);
278
279 for (info = block_list; info; info = info->next) {
280 monitor_printf(mon, "%s: removable=%d",
281 info->value->device, info->value->removable);
282
283 if (info->value->removable) {
284 monitor_printf(mon, " locked=%d", info->value->locked);
285 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
286 }
287
288 if (info->value->has_io_status) {
289 monitor_printf(mon, " io-status=%s",
290 BlockDeviceIoStatus_lookup[info->value->io_status]);
291 }
292
293 if (info->value->has_inserted) {
294 monitor_printf(mon, " file=");
295 monitor_print_filename(mon, info->value->inserted->file);
296
297 if (info->value->inserted->has_backing_file) {
298 monitor_printf(mon, " backing_file=");
299 monitor_print_filename(mon, info->value->inserted->backing_file);
Benoît Canet75115d92012-08-02 10:22:49 +0200300 monitor_printf(mon, " backing_file_depth=%" PRId64,
301 info->value->inserted->backing_file_depth);
Luiz Capitulinob2023812011-09-21 17:16:47 -0300302 }
303 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
304 info->value->inserted->ro,
305 info->value->inserted->drv,
306 info->value->inserted->encrypted);
Zhi Yong Wu727f0052011-11-08 13:00:31 +0800307
308 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
309 " bps_wr=%" PRId64 " iops=%" PRId64
310 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
311 info->value->inserted->bps,
312 info->value->inserted->bps_rd,
313 info->value->inserted->bps_wr,
314 info->value->inserted->iops,
315 info->value->inserted->iops_rd,
316 info->value->inserted->iops_wr);
Luiz Capitulinob2023812011-09-21 17:16:47 -0300317 } else {
318 monitor_printf(mon, " [not inserted]");
319 }
320
321 monitor_printf(mon, "\n");
322 }
323
324 qapi_free_BlockInfoList(block_list);
325}
326
Luiz Capitulinof11f57e2011-09-22 15:56:36 -0300327void hmp_info_blockstats(Monitor *mon)
328{
329 BlockStatsList *stats_list, *stats;
330
331 stats_list = qmp_query_blockstats(NULL);
332
333 for (stats = stats_list; stats; stats = stats->next) {
334 if (!stats->value->has_device) {
335 continue;
336 }
337
338 monitor_printf(mon, "%s:", stats->value->device);
339 monitor_printf(mon, " rd_bytes=%" PRId64
340 " wr_bytes=%" PRId64
341 " rd_operations=%" PRId64
342 " wr_operations=%" PRId64
343 " flush_operations=%" PRId64
344 " wr_total_time_ns=%" PRId64
345 " rd_total_time_ns=%" PRId64
346 " flush_total_time_ns=%" PRId64
347 "\n",
348 stats->value->stats->rd_bytes,
349 stats->value->stats->wr_bytes,
350 stats->value->stats->rd_operations,
351 stats->value->stats->wr_operations,
352 stats->value->stats->flush_operations,
353 stats->value->stats->wr_total_time_ns,
354 stats->value->stats->rd_total_time_ns,
355 stats->value->stats->flush_total_time_ns);
356 }
357
358 qapi_free_BlockStatsList(stats_list);
359}
360
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200361void hmp_info_vnc(Monitor *mon)
362{
363 VncInfo *info;
364 Error *err = NULL;
365 VncClientInfoList *client;
366
367 info = qmp_query_vnc(&err);
368 if (err) {
369 monitor_printf(mon, "%s\n", error_get_pretty(err));
370 error_free(err);
371 return;
372 }
373
374 if (!info->enabled) {
375 monitor_printf(mon, "Server: disabled\n");
376 goto out;
377 }
378
379 monitor_printf(mon, "Server:\n");
380 if (info->has_host && info->has_service) {
381 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
382 }
383 if (info->has_auth) {
384 monitor_printf(mon, " auth: %s\n", info->auth);
385 }
386
387 if (!info->has_clients || info->clients == NULL) {
388 monitor_printf(mon, "Client: none\n");
389 } else {
390 for (client = info->clients; client; client = client->next) {
391 monitor_printf(mon, "Client:\n");
392 monitor_printf(mon, " address: %s:%s\n",
393 client->value->host, client->value->service);
394 monitor_printf(mon, " x509_dname: %s\n",
395 client->value->x509_dname ?
396 client->value->x509_dname : "none");
397 monitor_printf(mon, " username: %s\n",
398 client->value->has_sasl_username ?
399 client->value->sasl_username : "none");
400 }
401 }
402
403out:
404 qapi_free_VncInfo(info);
405}
406
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200407void hmp_info_spice(Monitor *mon)
408{
409 SpiceChannelList *chan;
410 SpiceInfo *info;
411
412 info = qmp_query_spice(NULL);
413
414 if (!info->enabled) {
415 monitor_printf(mon, "Server: disabled\n");
416 goto out;
417 }
418
419 monitor_printf(mon, "Server:\n");
420 if (info->has_port) {
421 monitor_printf(mon, " address: %s:%" PRId64 "\n",
422 info->host, info->port);
423 }
424 if (info->has_tls_port) {
425 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
426 info->host, info->tls_port);
427 }
Yonit Halperin61c4efe2012-08-21 11:51:58 +0300428 monitor_printf(mon, " migrated: %s\n",
429 info->migrated ? "true" : "false");
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200430 monitor_printf(mon, " auth: %s\n", info->auth);
431 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
Alon Levy4efee022012-03-29 23:23:14 +0200432 monitor_printf(mon, " mouse-mode: %s\n",
433 SpiceQueryMouseMode_lookup[info->mouse_mode]);
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200434
435 if (!info->has_channels || info->channels == NULL) {
436 monitor_printf(mon, "Channels: none\n");
437 } else {
438 for (chan = info->channels; chan; chan = chan->next) {
439 monitor_printf(mon, "Channel:\n");
440 monitor_printf(mon, " address: %s:%s%s\n",
441 chan->value->host, chan->value->port,
442 chan->value->tls ? " [tls]" : "");
443 monitor_printf(mon, " session: %" PRId64 "\n",
444 chan->value->connection_id);
445 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
446 chan->value->channel_type, chan->value->channel_id);
447 }
448 }
449
450out:
451 qapi_free_SpiceInfo(info);
452}
453
Luiz Capitulino96637bc2011-10-21 11:41:37 -0200454void hmp_info_balloon(Monitor *mon)
455{
456 BalloonInfo *info;
457 Error *err = NULL;
458
459 info = qmp_query_balloon(&err);
460 if (err) {
461 monitor_printf(mon, "%s\n", error_get_pretty(err));
462 error_free(err);
463 return;
464 }
465
466 monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
467 if (info->has_mem_swapped_in) {
468 monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
469 }
470 if (info->has_mem_swapped_out) {
471 monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
472 }
473 if (info->has_major_page_faults) {
474 monitor_printf(mon, " major_page_faults=%" PRId64,
475 info->major_page_faults);
476 }
477 if (info->has_minor_page_faults) {
478 monitor_printf(mon, " minor_page_faults=%" PRId64,
479 info->minor_page_faults);
480 }
481 if (info->has_free_mem) {
482 monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
483 }
484 if (info->has_total_mem) {
485 monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
486 }
487
488 monitor_printf(mon, "\n");
489
490 qapi_free_BalloonInfo(info);
491}
492
Luiz Capitulino79627472011-10-21 14:15:33 -0200493static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
494{
495 PciMemoryRegionList *region;
496
497 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
498 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
499 dev->slot, dev->function);
500 monitor_printf(mon, " ");
501
502 if (dev->class_info.has_desc) {
503 monitor_printf(mon, "%s", dev->class_info.desc);
504 } else {
505 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
506 }
507
508 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
509 dev->id.vendor, dev->id.device);
510
511 if (dev->has_irq) {
512 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
513 }
514
515 if (dev->has_pci_bridge) {
516 monitor_printf(mon, " BUS %" PRId64 ".\n",
517 dev->pci_bridge->bus.number);
518 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
519 dev->pci_bridge->bus.secondary);
520 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
521 dev->pci_bridge->bus.subordinate);
522
523 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
524 dev->pci_bridge->bus.io_range->base,
525 dev->pci_bridge->bus.io_range->limit);
526
527 monitor_printf(mon,
528 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
529 dev->pci_bridge->bus.memory_range->base,
530 dev->pci_bridge->bus.memory_range->limit);
531
532 monitor_printf(mon, " prefetchable memory range "
533 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
534 dev->pci_bridge->bus.prefetchable_range->base,
535 dev->pci_bridge->bus.prefetchable_range->limit);
536 }
537
538 for (region = dev->regions; region; region = region->next) {
539 uint64_t addr, size;
540
541 addr = region->value->address;
542 size = region->value->size;
543
544 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
545
546 if (!strcmp(region->value->type, "io")) {
547 monitor_printf(mon, "I/O at 0x%04" PRIx64
548 " [0x%04" PRIx64 "].\n",
549 addr, addr + size - 1);
550 } else {
551 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
552 " [0x%08" PRIx64 "].\n",
553 region->value->mem_type_64 ? 64 : 32,
554 region->value->prefetch ? " prefetchable" : "",
555 addr, addr + size - 1);
556 }
557 }
558
559 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
560
561 if (dev->has_pci_bridge) {
562 if (dev->pci_bridge->has_devices) {
563 PciDeviceInfoList *cdev;
564 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
565 hmp_info_pci_device(mon, cdev->value);
566 }
567 }
568 }
569}
570
571void hmp_info_pci(Monitor *mon)
572{
Stefan Bergerf46cee32012-01-11 10:51:52 -0500573 PciInfoList *info_list, *info;
Luiz Capitulino79627472011-10-21 14:15:33 -0200574 Error *err = NULL;
575
Stefan Bergerf46cee32012-01-11 10:51:52 -0500576 info_list = qmp_query_pci(&err);
Luiz Capitulino79627472011-10-21 14:15:33 -0200577 if (err) {
578 monitor_printf(mon, "PCI devices not supported\n");
579 error_free(err);
580 return;
581 }
582
Stefan Bergerf46cee32012-01-11 10:51:52 -0500583 for (info = info_list; info; info = info->next) {
Luiz Capitulino79627472011-10-21 14:15:33 -0200584 PciDeviceInfoList *dev;
585
586 for (dev = info->value->devices; dev; dev = dev->next) {
587 hmp_info_pci_device(mon, dev->value);
588 }
589 }
590
Stefan Bergerf46cee32012-01-11 10:51:52 -0500591 qapi_free_PciInfoList(info_list);
Luiz Capitulino79627472011-10-21 14:15:33 -0200592}
593
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +0000594void hmp_info_block_jobs(Monitor *mon)
595{
596 BlockJobInfoList *list;
597 Error *err = NULL;
598
599 list = qmp_query_block_jobs(&err);
600 assert(!err);
601
602 if (!list) {
603 monitor_printf(mon, "No active jobs\n");
604 return;
605 }
606
607 while (list) {
608 if (strcmp(list->value->type, "stream") == 0) {
609 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
610 " of %" PRId64 " bytes, speed limit %" PRId64
611 " bytes/s\n",
612 list->value->device,
613 list->value->offset,
614 list->value->len,
615 list->value->speed);
616 } else {
617 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
618 " of %" PRId64 " bytes, speed limit %" PRId64
619 " bytes/s\n",
620 list->value->type,
621 list->value->device,
622 list->value->offset,
623 list->value->len,
624 list->value->speed);
625 }
626 list = list->next;
627 }
628}
629
Luiz Capitulino7a7f3252011-09-15 14:20:28 -0300630void hmp_quit(Monitor *mon, const QDict *qdict)
631{
632 monitor_suspend(mon);
633 qmp_quit(NULL);
634}
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300635
636void hmp_stop(Monitor *mon, const QDict *qdict)
637{
638 qmp_stop(NULL);
639}
Luiz Capitulino38d22652011-09-15 14:41:46 -0300640
641void hmp_system_reset(Monitor *mon, const QDict *qdict)
642{
643 qmp_system_reset(NULL);
644}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300645
646void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
647{
648 qmp_system_powerdown(NULL);
649}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300650
651void hmp_cpu(Monitor *mon, const QDict *qdict)
652{
653 int64_t cpu_index;
654
655 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
656 use it are converted to the QAPI */
657 cpu_index = qdict_get_int(qdict, "index");
658 if (monitor_set_cpu(cpu_index) < 0) {
659 monitor_printf(mon, "invalid CPU index\n");
660 }
661}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200662
663void hmp_memsave(Monitor *mon, const QDict *qdict)
664{
665 uint32_t size = qdict_get_int(qdict, "size");
666 const char *filename = qdict_get_str(qdict, "filename");
667 uint64_t addr = qdict_get_int(qdict, "val");
668 Error *errp = NULL;
669
670 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
671 hmp_handle_error(mon, &errp);
672}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200673
674void hmp_pmemsave(Monitor *mon, const QDict *qdict)
675{
676 uint32_t size = qdict_get_int(qdict, "size");
677 const char *filename = qdict_get_str(qdict, "filename");
678 uint64_t addr = qdict_get_int(qdict, "val");
679 Error *errp = NULL;
680
681 qmp_pmemsave(addr, size, filename, &errp);
682 hmp_handle_error(mon, &errp);
683}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200684
685static void hmp_cont_cb(void *opaque, int err)
686{
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200687 if (!err) {
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300688 qmp_cont(NULL);
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200689 }
690}
691
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300692static bool key_is_missing(const BlockInfo *bdev)
693{
694 return (bdev->inserted && bdev->inserted->encryption_key_missing);
695}
696
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200697void hmp_cont(Monitor *mon, const QDict *qdict)
698{
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300699 BlockInfoList *bdev_list, *bdev;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200700 Error *errp = NULL;
701
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300702 bdev_list = qmp_query_block(NULL);
703 for (bdev = bdev_list; bdev; bdev = bdev->next) {
704 if (key_is_missing(bdev->value)) {
705 monitor_read_block_device_key(mon, bdev->value->device,
706 hmp_cont_cb, NULL);
707 goto out;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200708 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200709 }
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300710
711 qmp_cont(&errp);
712 hmp_handle_error(mon, &errp);
713
714out:
715 qapi_free_BlockInfoList(bdev_list);
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200716}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200717
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100718void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
719{
720 qmp_system_wakeup(NULL);
721}
722
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200723void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
724{
725 Error *errp = NULL;
726
727 qmp_inject_nmi(&errp);
728 hmp_handle_error(mon, &errp);
729}
Luiz Capitulino4b371562011-11-23 13:11:55 -0200730
731void hmp_set_link(Monitor *mon, const QDict *qdict)
732{
733 const char *name = qdict_get_str(qdict, "name");
734 int up = qdict_get_bool(qdict, "up");
735 Error *errp = NULL;
736
737 qmp_set_link(name, up, &errp);
738 hmp_handle_error(mon, &errp);
739}
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200740
741void hmp_block_passwd(Monitor *mon, const QDict *qdict)
742{
743 const char *device = qdict_get_str(qdict, "device");
744 const char *password = qdict_get_str(qdict, "password");
745 Error *errp = NULL;
746
747 qmp_block_passwd(device, password, &errp);
748 hmp_handle_error(mon, &errp);
749}
Luiz Capitulinod72f3262011-11-25 14:38:09 -0200750
751void hmp_balloon(Monitor *mon, const QDict *qdict)
752{
753 int64_t value = qdict_get_int(qdict, "value");
754 Error *errp = NULL;
755
756 qmp_balloon(value, &errp);
757 if (error_is_set(&errp)) {
758 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
759 error_free(errp);
760 }
761}
Luiz Capitulino5e7caac2011-11-25 14:57:10 -0200762
763void hmp_block_resize(Monitor *mon, const QDict *qdict)
764{
765 const char *device = qdict_get_str(qdict, "device");
766 int64_t size = qdict_get_int(qdict, "size");
767 Error *errp = NULL;
768
769 qmp_block_resize(device, size, &errp);
770 hmp_handle_error(mon, &errp);
771}
Luiz Capitulino6106e242011-11-25 16:15:19 -0200772
773void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
774{
775 const char *device = qdict_get_str(qdict, "device");
776 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
777 const char *format = qdict_get_try_str(qdict, "format");
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100778 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
779 enum NewImageMode mode;
Luiz Capitulino6106e242011-11-25 16:15:19 -0200780 Error *errp = NULL;
781
782 if (!filename) {
783 /* In the future, if 'snapshot-file' is not specified, the snapshot
784 will be taken internally. Today it's actually required. */
785 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
786 hmp_handle_error(mon, &errp);
787 return;
788 }
789
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100790 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
791 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
792 true, mode, &errp);
Luiz Capitulino6106e242011-11-25 16:15:19 -0200793 hmp_handle_error(mon, &errp);
794}
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200795
796void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
797{
798 qmp_migrate_cancel(NULL);
799}
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200800
801void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
802{
803 double value = qdict_get_double(qdict, "value");
804 qmp_migrate_set_downtime(value, NULL);
805}
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200806
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300807void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
808{
809 int64_t value = qdict_get_int(qdict, "value");
810 Error *err = NULL;
811
812 qmp_migrate_set_cache_size(value, &err);
813 if (err) {
814 monitor_printf(mon, "%s\n", error_get_pretty(err));
815 error_free(err);
816 return;
817 }
818}
819
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200820void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
821{
822 int64_t value = qdict_get_int(qdict, "value");
823 qmp_migrate_set_speed(value, NULL);
824}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200825
Orit Wasserman00458432012-08-06 21:42:48 +0300826void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
827{
828 const char *cap = qdict_get_str(qdict, "capability");
829 bool state = qdict_get_bool(qdict, "state");
830 Error *err = NULL;
831 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
832 int i;
833
834 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
835 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
836 caps->value = g_malloc0(sizeof(*caps->value));
837 caps->value->capability = i;
838 caps->value->state = state;
839 caps->next = NULL;
840 qmp_migrate_set_capabilities(caps, &err);
841 break;
842 }
843 }
844
845 if (i == MIGRATION_CAPABILITY_MAX) {
846 error_set(&err, QERR_INVALID_PARAMETER, cap);
847 }
848
849 qapi_free_MigrationCapabilityStatusList(caps);
850
851 if (err) {
852 monitor_printf(mon, "migrate_set_parameter: %s\n",
853 error_get_pretty(err));
854 error_free(err);
855 }
856}
857
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200858void hmp_set_password(Monitor *mon, const QDict *qdict)
859{
860 const char *protocol = qdict_get_str(qdict, "protocol");
861 const char *password = qdict_get_str(qdict, "password");
862 const char *connected = qdict_get_try_str(qdict, "connected");
863 Error *err = NULL;
864
865 qmp_set_password(protocol, password, !!connected, connected, &err);
866 hmp_handle_error(mon, &err);
867}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200868
869void hmp_expire_password(Monitor *mon, const QDict *qdict)
870{
871 const char *protocol = qdict_get_str(qdict, "protocol");
872 const char *whenstr = qdict_get_str(qdict, "time");
873 Error *err = NULL;
874
875 qmp_expire_password(protocol, whenstr, &err);
876 hmp_handle_error(mon, &err);
877}
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -0200878
879void hmp_eject(Monitor *mon, const QDict *qdict)
880{
881 int force = qdict_get_try_bool(qdict, "force", 0);
882 const char *device = qdict_get_str(qdict, "device");
883 Error *err = NULL;
884
885 qmp_eject(device, true, force, &err);
886 hmp_handle_error(mon, &err);
887}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200888
889static void hmp_change_read_arg(Monitor *mon, const char *password,
890 void *opaque)
891{
892 qmp_change_vnc_password(password, NULL);
893 monitor_read_command(mon, 1);
894}
895
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200896void hmp_change(Monitor *mon, const QDict *qdict)
897{
898 const char *device = qdict_get_str(qdict, "device");
899 const char *target = qdict_get_str(qdict, "target");
900 const char *arg = qdict_get_try_str(qdict, "arg");
901 Error *err = NULL;
902
903 if (strcmp(device, "vnc") == 0 &&
904 (strcmp(target, "passwd") == 0 ||
905 strcmp(target, "password") == 0)) {
906 if (!arg) {
907 monitor_read_password(mon, hmp_change_read_arg, NULL);
908 return;
909 }
910 }
911
912 qmp_change(device, target, !!arg, arg, &err);
Luiz Capitulinoab878dd2012-08-06 15:55:22 -0300913 if (error_is_set(&err) &&
914 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
Luiz Capitulinoeef5ad12012-08-06 15:49:34 -0300915 error_free(err);
916 monitor_read_block_device_key(mon, device, NULL, NULL);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200917 return;
918 }
919 hmp_handle_error(mon, &err);
920}
Luiz Capitulino80047da2011-12-14 16:49:14 -0200921
922void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
923{
924 Error *err = NULL;
925
926 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
927 qdict_get_int(qdict, "bps"),
928 qdict_get_int(qdict, "bps_rd"),
929 qdict_get_int(qdict, "bps_wr"),
930 qdict_get_int(qdict, "iops"),
931 qdict_get_int(qdict, "iops_rd"),
932 qdict_get_int(qdict, "iops_wr"), &err);
933 hmp_handle_error(mon, &err);
934}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +0000935
936void hmp_block_stream(Monitor *mon, const QDict *qdict)
937{
938 Error *error = NULL;
939 const char *device = qdict_get_str(qdict, "device");
940 const char *base = qdict_get_try_str(qdict, "base");
Stefan Hajnoczic83c66c2012-04-25 16:51:03 +0100941 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +0000942
Stefan Hajnoczic83c66c2012-04-25 16:51:03 +0100943 qmp_block_stream(device, base != NULL, base,
Paolo Bonzini1d809092012-09-28 17:22:59 +0200944 qdict_haskey(qdict, "speed"), speed,
945 BLOCKDEV_ON_ERROR_REPORT, true, &error);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +0000946
947 hmp_handle_error(mon, &error);
948}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +0000949
950void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
951{
952 Error *error = NULL;
953 const char *device = qdict_get_str(qdict, "device");
Paolo Bonzinic6db2392012-05-08 16:51:56 +0200954 int64_t value = qdict_get_int(qdict, "speed");
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +0000955
956 qmp_block_job_set_speed(device, value, &error);
957
958 hmp_handle_error(mon, &error);
959}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +0000960
961void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
962{
963 Error *error = NULL;
964 const char *device = qdict_get_str(qdict, "device");
Paolo Bonzini6e37fb82012-09-28 17:22:51 +0200965 bool force = qdict_get_try_bool(qdict, "force", 0);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +0000966
Paolo Bonzini6e37fb82012-09-28 17:22:51 +0200967 qmp_block_job_cancel(device, true, force, &error);
968
969 hmp_handle_error(mon, &error);
970}
971
972void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
973{
974 Error *error = NULL;
975 const char *device = qdict_get_str(qdict, "device");
976
977 qmp_block_job_pause(device, &error);
978
979 hmp_handle_error(mon, &error);
980}
981
982void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
983{
984 Error *error = NULL;
985 const char *device = qdict_get_str(qdict, "device");
986
987 qmp_block_job_resume(device, &error);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +0000988
989 hmp_handle_error(mon, &error);
990}
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -0200991
992typedef struct MigrationStatus
993{
994 QEMUTimer *timer;
995 Monitor *mon;
996 bool is_block_migration;
997} MigrationStatus;
998
999static void hmp_migrate_status_cb(void *opaque)
1000{
1001 MigrationStatus *status = opaque;
1002 MigrationInfo *info;
1003
1004 info = qmp_query_migrate(NULL);
1005 if (!info->has_status || strcmp(info->status, "active") == 0) {
1006 if (info->has_disk) {
1007 int progress;
1008
1009 if (info->disk->remaining) {
1010 progress = info->disk->transferred * 100 / info->disk->total;
1011 } else {
1012 progress = 100;
1013 }
1014
1015 monitor_printf(status->mon, "Completed %d %%\r", progress);
1016 monitor_flush(status->mon);
1017 }
1018
1019 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1020 } else {
1021 if (status->is_block_migration) {
1022 monitor_printf(status->mon, "\n");
1023 }
1024 monitor_resume(status->mon);
1025 qemu_del_timer(status->timer);
1026 g_free(status);
1027 }
1028
1029 qapi_free_MigrationInfo(info);
1030}
1031
1032void hmp_migrate(Monitor *mon, const QDict *qdict)
1033{
1034 int detach = qdict_get_try_bool(qdict, "detach", 0);
1035 int blk = qdict_get_try_bool(qdict, "blk", 0);
1036 int inc = qdict_get_try_bool(qdict, "inc", 0);
1037 const char *uri = qdict_get_str(qdict, "uri");
1038 Error *err = NULL;
1039
1040 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1041 if (err) {
1042 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1043 error_free(err);
1044 return;
1045 }
1046
1047 if (!detach) {
1048 MigrationStatus *status;
1049
1050 if (monitor_suspend(mon) < 0) {
1051 monitor_printf(mon, "terminal does not allow synchronous "
1052 "migration, continuing detached\n");
1053 return;
1054 }
1055
1056 status = g_malloc0(sizeof(*status));
1057 status->mon = mon;
1058 status->is_block_migration = blk || inc;
1059 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1060 status);
1061 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1062 }
1063}
Luiz Capitulinoa15fef22012-03-29 12:38:50 -03001064
1065void hmp_device_del(Monitor *mon, const QDict *qdict)
1066{
1067 const char *id = qdict_get_str(qdict, "id");
1068 Error *err = NULL;
1069
1070 qmp_device_del(id, &err);
1071 hmp_handle_error(mon, &err);
1072}
Wen Congyang783e9b42012-05-07 12:10:47 +08001073
1074void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1075{
1076 Error *errp = NULL;
1077 int paging = qdict_get_try_bool(qdict, "paging", 0);
Luiz Capitulino75363762012-09-21 13:53:00 -03001078 const char *file = qdict_get_str(qdict, "filename");
Wen Congyang783e9b42012-05-07 12:10:47 +08001079 bool has_begin = qdict_haskey(qdict, "begin");
1080 bool has_length = qdict_haskey(qdict, "length");
1081 int64_t begin = 0;
1082 int64_t length = 0;
Luiz Capitulino75363762012-09-21 13:53:00 -03001083 char *prot;
Wen Congyang783e9b42012-05-07 12:10:47 +08001084
1085 if (has_begin) {
1086 begin = qdict_get_int(qdict, "begin");
1087 }
1088 if (has_length) {
1089 length = qdict_get_int(qdict, "length");
1090 }
1091
Luiz Capitulino75363762012-09-21 13:53:00 -03001092 prot = g_strconcat("file:", file, NULL);
1093
1094 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
Wen Congyang783e9b42012-05-07 12:10:47 +08001095 &errp);
1096 hmp_handle_error(mon, &errp);
Luiz Capitulino75363762012-09-21 13:53:00 -03001097 g_free(prot);
Wen Congyang783e9b42012-05-07 12:10:47 +08001098}
Luiz Capitulino928059a2012-04-18 17:34:15 -03001099
1100void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1101{
1102 Error *err = NULL;
1103 QemuOpts *opts;
1104
1105 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1106 if (error_is_set(&err)) {
1107 goto out;
1108 }
1109
1110 netdev_add(opts, &err);
1111 if (error_is_set(&err)) {
1112 qemu_opts_del(opts);
1113 }
1114
1115out:
1116 hmp_handle_error(mon, &err);
1117}
Luiz Capitulino5f964152012-04-16 14:36:32 -03001118
1119void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1120{
1121 const char *id = qdict_get_str(qdict, "id");
1122 Error *err = NULL;
1123
1124 qmp_netdev_del(id, &err);
1125 hmp_handle_error(mon, &err);
1126}
Corey Bryant208c9d12012-06-22 14:36:09 -04001127
1128void hmp_getfd(Monitor *mon, const QDict *qdict)
1129{
1130 const char *fdname = qdict_get_str(qdict, "fdname");
1131 Error *errp = NULL;
1132
1133 qmp_getfd(fdname, &errp);
1134 hmp_handle_error(mon, &errp);
1135}
1136
1137void hmp_closefd(Monitor *mon, const QDict *qdict)
1138{
1139 const char *fdname = qdict_get_str(qdict, "fdname");
1140 Error *errp = NULL;
1141
1142 qmp_closefd(fdname, &errp);
1143 hmp_handle_error(mon, &errp);
1144}
Amos Konge4c8f002012-08-31 10:56:26 +08001145
1146void hmp_send_key(Monitor *mon, const QDict *qdict)
1147{
1148 const char *keys = qdict_get_str(qdict, "keys");
Luiz Capitulino9f328972012-09-20 14:19:47 -03001149 KeyValueList *keylist, *head = NULL, *tmp = NULL;
Amos Konge4c8f002012-08-31 10:56:26 +08001150 int has_hold_time = qdict_haskey(qdict, "hold-time");
1151 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1152 Error *err = NULL;
1153 char keyname_buf[16];
1154 char *separator;
Luiz Capitulino9f328972012-09-20 14:19:47 -03001155 int keyname_len;
Amos Konge4c8f002012-08-31 10:56:26 +08001156
1157 while (1) {
1158 separator = strchr(keys, '-');
1159 keyname_len = separator ? separator - keys : strlen(keys);
1160 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1161
1162 /* Be compatible with old interface, convert user inputted "<" */
1163 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1164 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1165 keyname_len = 4;
1166 }
1167 keyname_buf[keyname_len] = 0;
1168
Amos Konge4c8f002012-08-31 10:56:26 +08001169 keylist = g_malloc0(sizeof(*keylist));
Luiz Capitulino9f328972012-09-20 14:19:47 -03001170 keylist->value = g_malloc0(sizeof(*keylist->value));
Amos Konge4c8f002012-08-31 10:56:26 +08001171
1172 if (!head) {
1173 head = keylist;
1174 }
1175 if (tmp) {
1176 tmp->next = keylist;
1177 }
1178 tmp = keylist;
1179
Luiz Capitulino9f328972012-09-20 14:19:47 -03001180 if (strstart(keyname_buf, "0x", NULL)) {
1181 char *endp;
1182 int value = strtoul(keyname_buf, &endp, 0);
1183 if (*endp != '\0') {
1184 goto err_out;
1185 }
1186 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1187 keylist->value->number = value;
1188 } else {
1189 int idx = index_from_key(keyname_buf);
1190 if (idx == Q_KEY_CODE_MAX) {
1191 goto err_out;
1192 }
1193 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1194 keylist->value->qcode = idx;
1195 }
1196
Amos Konge4c8f002012-08-31 10:56:26 +08001197 if (!separator) {
1198 break;
1199 }
1200 keys = separator + 1;
1201 }
1202
Luiz Capitulino9f328972012-09-20 14:19:47 -03001203 qmp_send_key(head, has_hold_time, hold_time, &err);
Amos Konge4c8f002012-08-31 10:56:26 +08001204 hmp_handle_error(mon, &err);
Luiz Capitulino9f328972012-09-20 14:19:47 -03001205
1206out:
1207 qapi_free_KeyValueList(head);
1208 return;
1209
1210err_out:
1211 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1212 goto out;
Amos Konge4c8f002012-08-31 10:56:26 +08001213}
Luiz Capitulinoad39cf62012-05-24 13:48:23 -03001214
1215void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1216{
1217 const char *filename = qdict_get_str(qdict, "filename");
1218 Error *err = NULL;
1219
1220 qmp_screendump(filename, &err);
1221 hmp_handle_error(mon, &err);
1222}