hmp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. #include "hmp.h"
  14. #include "qmp-commands.h"
  15. void hmp_info_name(Monitor *mon)
  16. {
  17. NameInfo *info;
  18. info = qmp_query_name(NULL);
  19. if (info->has_name) {
  20. monitor_printf(mon, "%s\n", info->name);
  21. }
  22. qapi_free_NameInfo(info);
  23. }
  24. void hmp_info_version(Monitor *mon)
  25. {
  26. VersionInfo *info;
  27. info = qmp_query_version(NULL);
  28. monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
  29. info->qemu.major, info->qemu.minor, info->qemu.micro,
  30. info->package);
  31. qapi_free_VersionInfo(info);
  32. }
  33. void hmp_info_kvm(Monitor *mon)
  34. {
  35. KvmInfo *info;
  36. info = qmp_query_kvm(NULL);
  37. monitor_printf(mon, "kvm support: ");
  38. if (info->present) {
  39. monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
  40. } else {
  41. monitor_printf(mon, "not compiled\n");
  42. }
  43. qapi_free_KvmInfo(info);
  44. }
  45. void hmp_info_status(Monitor *mon)
  46. {
  47. StatusInfo *info;
  48. info = qmp_query_status(NULL);
  49. monitor_printf(mon, "VM status: %s%s",
  50. info->running ? "running" : "paused",
  51. info->singlestep ? " (single step mode)" : "");
  52. if (!info->running && info->status != RUN_STATE_PAUSED) {
  53. monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
  54. }
  55. monitor_printf(mon, "\n");
  56. qapi_free_StatusInfo(info);
  57. }
  58. void hmp_info_uuid(Monitor *mon)
  59. {
  60. UuidInfo *info;
  61. info = qmp_query_uuid(NULL);
  62. monitor_printf(mon, "%s\n", info->UUID);
  63. qapi_free_UuidInfo(info);
  64. }
  65. void hmp_info_chardev(Monitor *mon)
  66. {
  67. ChardevInfoList *char_info, *info;
  68. char_info = qmp_query_chardev(NULL);
  69. for (info = char_info; info; info = info->next) {
  70. monitor_printf(mon, "%s: filename=%s\n", info->value->label,
  71. info->value->filename);
  72. }
  73. qapi_free_ChardevInfoList(char_info);
  74. }
  75. void hmp_info_mice(Monitor *mon)
  76. {
  77. MouseInfoList *mice_list, *mouse;
  78. mice_list = qmp_query_mice(NULL);
  79. if (!mice_list) {
  80. monitor_printf(mon, "No mouse devices connected\n");
  81. return;
  82. }
  83. for (mouse = mice_list; mouse; mouse = mouse->next) {
  84. monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
  85. mouse->value->current ? '*' : ' ',
  86. mouse->value->index, mouse->value->name,
  87. mouse->value->absolute ? " (absolute)" : "");
  88. }
  89. qapi_free_MouseInfoList(mice_list);
  90. }
  91. void hmp_info_migrate(Monitor *mon)
  92. {
  93. MigrationInfo *info;
  94. info = qmp_query_migrate(NULL);
  95. if (info->has_status) {
  96. monitor_printf(mon, "Migration status: %s\n", info->status);
  97. }
  98. if (info->has_ram) {
  99. monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
  100. info->ram->transferred >> 10);
  101. monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
  102. info->ram->remaining >> 10);
  103. monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
  104. info->ram->total >> 10);
  105. }
  106. if (info->has_disk) {
  107. monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
  108. info->disk->transferred >> 10);
  109. monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
  110. info->disk->remaining >> 10);
  111. monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
  112. info->disk->total >> 10);
  113. }
  114. qapi_free_MigrationInfo(info);
  115. }
  116. void hmp_info_cpus(Monitor *mon)
  117. {
  118. CpuInfoList *cpu_list, *cpu;
  119. cpu_list = qmp_query_cpus(NULL);
  120. for (cpu = cpu_list; cpu; cpu = cpu->next) {
  121. int active = ' ';
  122. if (cpu->value->CPU == monitor_get_cpu_index()) {
  123. active = '*';
  124. }
  125. monitor_printf(mon, "%c CPU #%" PRId64 ": ", active, cpu->value->CPU);
  126. if (cpu->value->has_pc) {
  127. monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
  128. }
  129. if (cpu->value->has_nip) {
  130. monitor_printf(mon, "nip=0x%016" PRIx64, cpu->value->nip);
  131. }
  132. if (cpu->value->has_npc) {
  133. monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
  134. monitor_printf(mon, "npc=0x%016" PRIx64, cpu->value->npc);
  135. }
  136. if (cpu->value->has_PC) {
  137. monitor_printf(mon, "PC=0x%016" PRIx64, cpu->value->PC);
  138. }
  139. if (cpu->value->halted) {
  140. monitor_printf(mon, " (halted)");
  141. }
  142. monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
  143. }
  144. qapi_free_CpuInfoList(cpu_list);
  145. }
  146. void hmp_info_block(Monitor *mon)
  147. {
  148. BlockInfoList *block_list, *info;
  149. block_list = qmp_query_block(NULL);
  150. for (info = block_list; info; info = info->next) {
  151. monitor_printf(mon, "%s: removable=%d",
  152. info->value->device, info->value->removable);
  153. if (info->value->removable) {
  154. monitor_printf(mon, " locked=%d", info->value->locked);
  155. monitor_printf(mon, " tray-open=%d", info->value->tray_open);
  156. }
  157. if (info->value->has_io_status) {
  158. monitor_printf(mon, " io-status=%s",
  159. BlockDeviceIoStatus_lookup[info->value->io_status]);
  160. }
  161. if (info->value->has_inserted) {
  162. monitor_printf(mon, " file=");
  163. monitor_print_filename(mon, info->value->inserted->file);
  164. if (info->value->inserted->has_backing_file) {
  165. monitor_printf(mon, " backing_file=");
  166. monitor_print_filename(mon, info->value->inserted->backing_file);
  167. }
  168. monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
  169. info->value->inserted->ro,
  170. info->value->inserted->drv,
  171. info->value->inserted->encrypted);
  172. } else {
  173. monitor_printf(mon, " [not inserted]");
  174. }
  175. monitor_printf(mon, "\n");
  176. }
  177. qapi_free_BlockInfoList(block_list);
  178. }
  179. void hmp_info_blockstats(Monitor *mon)
  180. {
  181. BlockStatsList *stats_list, *stats;
  182. stats_list = qmp_query_blockstats(NULL);
  183. for (stats = stats_list; stats; stats = stats->next) {
  184. if (!stats->value->has_device) {
  185. continue;
  186. }
  187. monitor_printf(mon, "%s:", stats->value->device);
  188. monitor_printf(mon, " rd_bytes=%" PRId64
  189. " wr_bytes=%" PRId64
  190. " rd_operations=%" PRId64
  191. " wr_operations=%" PRId64
  192. " flush_operations=%" PRId64
  193. " wr_total_time_ns=%" PRId64
  194. " rd_total_time_ns=%" PRId64
  195. " flush_total_time_ns=%" PRId64
  196. "\n",
  197. stats->value->stats->rd_bytes,
  198. stats->value->stats->wr_bytes,
  199. stats->value->stats->rd_operations,
  200. stats->value->stats->wr_operations,
  201. stats->value->stats->flush_operations,
  202. stats->value->stats->wr_total_time_ns,
  203. stats->value->stats->rd_total_time_ns,
  204. stats->value->stats->flush_total_time_ns);
  205. }
  206. qapi_free_BlockStatsList(stats_list);
  207. }
  208. void hmp_info_vnc(Monitor *mon)
  209. {
  210. VncInfo *info;
  211. Error *err = NULL;
  212. VncClientInfoList *client;
  213. info = qmp_query_vnc(&err);
  214. if (err) {
  215. monitor_printf(mon, "%s\n", error_get_pretty(err));
  216. error_free(err);
  217. return;
  218. }
  219. if (!info->enabled) {
  220. monitor_printf(mon, "Server: disabled\n");
  221. goto out;
  222. }
  223. monitor_printf(mon, "Server:\n");
  224. if (info->has_host && info->has_service) {
  225. monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
  226. }
  227. if (info->has_auth) {
  228. monitor_printf(mon, " auth: %s\n", info->auth);
  229. }
  230. if (!info->has_clients || info->clients == NULL) {
  231. monitor_printf(mon, "Client: none\n");
  232. } else {
  233. for (client = info->clients; client; client = client->next) {
  234. monitor_printf(mon, "Client:\n");
  235. monitor_printf(mon, " address: %s:%s\n",
  236. client->value->host, client->value->service);
  237. monitor_printf(mon, " x509_dname: %s\n",
  238. client->value->x509_dname ?
  239. client->value->x509_dname : "none");
  240. monitor_printf(mon, " username: %s\n",
  241. client->value->has_sasl_username ?
  242. client->value->sasl_username : "none");
  243. }
  244. }
  245. out:
  246. qapi_free_VncInfo(info);
  247. }
  248. void hmp_info_spice(Monitor *mon)
  249. {
  250. SpiceChannelList *chan;
  251. SpiceInfo *info;
  252. info = qmp_query_spice(NULL);
  253. if (!info->enabled) {
  254. monitor_printf(mon, "Server: disabled\n");
  255. goto out;
  256. }
  257. monitor_printf(mon, "Server:\n");
  258. if (info->has_port) {
  259. monitor_printf(mon, " address: %s:%" PRId64 "\n",
  260. info->host, info->port);
  261. }
  262. if (info->has_tls_port) {
  263. monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
  264. info->host, info->tls_port);
  265. }
  266. monitor_printf(mon, " auth: %s\n", info->auth);
  267. monitor_printf(mon, " compiled: %s\n", info->compiled_version);
  268. if (!info->has_channels || info->channels == NULL) {
  269. monitor_printf(mon, "Channels: none\n");
  270. } else {
  271. for (chan = info->channels; chan; chan = chan->next) {
  272. monitor_printf(mon, "Channel:\n");
  273. monitor_printf(mon, " address: %s:%s%s\n",
  274. chan->value->host, chan->value->port,
  275. chan->value->tls ? " [tls]" : "");
  276. monitor_printf(mon, " session: %" PRId64 "\n",
  277. chan->value->connection_id);
  278. monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
  279. chan->value->channel_type, chan->value->channel_id);
  280. }
  281. }
  282. out:
  283. qapi_free_SpiceInfo(info);
  284. }
  285. void hmp_info_balloon(Monitor *mon)
  286. {
  287. BalloonInfo *info;
  288. Error *err = NULL;
  289. info = qmp_query_balloon(&err);
  290. if (err) {
  291. monitor_printf(mon, "%s\n", error_get_pretty(err));
  292. error_free(err);
  293. return;
  294. }
  295. monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
  296. if (info->has_mem_swapped_in) {
  297. monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
  298. }
  299. if (info->has_mem_swapped_out) {
  300. monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
  301. }
  302. if (info->has_major_page_faults) {
  303. monitor_printf(mon, " major_page_faults=%" PRId64,
  304. info->major_page_faults);
  305. }
  306. if (info->has_minor_page_faults) {
  307. monitor_printf(mon, " minor_page_faults=%" PRId64,
  308. info->minor_page_faults);
  309. }
  310. if (info->has_free_mem) {
  311. monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
  312. }
  313. if (info->has_total_mem) {
  314. monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
  315. }
  316. monitor_printf(mon, "\n");
  317. qapi_free_BalloonInfo(info);
  318. }
  319. static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
  320. {
  321. PciMemoryRegionList *region;
  322. monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
  323. monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
  324. dev->slot, dev->function);
  325. monitor_printf(mon, " ");
  326. if (dev->class_info.has_desc) {
  327. monitor_printf(mon, "%s", dev->class_info.desc);
  328. } else {
  329. monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
  330. }
  331. monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
  332. dev->id.vendor, dev->id.device);
  333. if (dev->has_irq) {
  334. monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
  335. }
  336. if (dev->has_pci_bridge) {
  337. monitor_printf(mon, " BUS %" PRId64 ".\n",
  338. dev->pci_bridge->bus.number);
  339. monitor_printf(mon, " secondary bus %" PRId64 ".\n",
  340. dev->pci_bridge->bus.secondary);
  341. monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
  342. dev->pci_bridge->bus.subordinate);
  343. monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
  344. dev->pci_bridge->bus.io_range->base,
  345. dev->pci_bridge->bus.io_range->limit);
  346. monitor_printf(mon,
  347. " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
  348. dev->pci_bridge->bus.memory_range->base,
  349. dev->pci_bridge->bus.memory_range->limit);
  350. monitor_printf(mon, " prefetchable memory range "
  351. "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
  352. dev->pci_bridge->bus.prefetchable_range->base,
  353. dev->pci_bridge->bus.prefetchable_range->limit);
  354. }
  355. for (region = dev->regions; region; region = region->next) {
  356. uint64_t addr, size;
  357. addr = region->value->address;
  358. size = region->value->size;
  359. monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
  360. if (!strcmp(region->value->type, "io")) {
  361. monitor_printf(mon, "I/O at 0x%04" PRIx64
  362. " [0x%04" PRIx64 "].\n",
  363. addr, addr + size - 1);
  364. } else {
  365. monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
  366. " [0x%08" PRIx64 "].\n",
  367. region->value->mem_type_64 ? 64 : 32,
  368. region->value->prefetch ? " prefetchable" : "",
  369. addr, addr + size - 1);
  370. }
  371. }
  372. monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
  373. if (dev->has_pci_bridge) {
  374. if (dev->pci_bridge->has_devices) {
  375. PciDeviceInfoList *cdev;
  376. for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
  377. hmp_info_pci_device(mon, cdev->value);
  378. }
  379. }
  380. }
  381. }
  382. void hmp_info_pci(Monitor *mon)
  383. {
  384. PciInfoList *info;
  385. Error *err = NULL;
  386. info = qmp_query_pci(&err);
  387. if (err) {
  388. monitor_printf(mon, "PCI devices not supported\n");
  389. error_free(err);
  390. return;
  391. }
  392. for (; info; info = info->next) {
  393. PciDeviceInfoList *dev;
  394. for (dev = info->value->devices; dev; dev = dev->next) {
  395. hmp_info_pci_device(mon, dev->value);
  396. }
  397. }
  398. qapi_free_PciInfoList(info);
  399. }
  400. void hmp_quit(Monitor *mon, const QDict *qdict)
  401. {
  402. monitor_suspend(mon);
  403. qmp_quit(NULL);
  404. }
  405. void hmp_stop(Monitor *mon, const QDict *qdict)
  406. {
  407. qmp_stop(NULL);
  408. }
  409. void hmp_system_reset(Monitor *mon, const QDict *qdict)
  410. {
  411. qmp_system_reset(NULL);
  412. }
  413. void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
  414. {
  415. qmp_system_powerdown(NULL);
  416. }
  417. void hmp_cpu(Monitor *mon, const QDict *qdict)
  418. {
  419. int64_t cpu_index;
  420. /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
  421. use it are converted to the QAPI */
  422. cpu_index = qdict_get_int(qdict, "index");
  423. if (monitor_set_cpu(cpu_index) < 0) {
  424. monitor_printf(mon, "invalid CPU index\n");
  425. }
  426. }