2
0

hmp-cmds.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Human Monitor Interface commands
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "exec/address-spaces.h"
  17. #include "exec/gdbstub.h"
  18. #include "exec/ioport.h"
  19. #include "monitor/hmp.h"
  20. #include "qemu/help_option.h"
  21. #include "monitor/monitor-internal.h"
  22. #include "qapi/error.h"
  23. #include "qapi/qapi-commands-control.h"
  24. #include "qapi/qapi-commands-misc.h"
  25. #include "qapi/qmp/qdict.h"
  26. #include "qapi/qmp/qerror.h"
  27. #include "qemu/cutils.h"
  28. #include "hw/intc/intc.h"
  29. #include "qemu/log.h"
  30. #include "sysemu/sysemu.h"
  31. bool hmp_handle_error(Monitor *mon, Error *err)
  32. {
  33. if (err) {
  34. error_reportf_err(err, "Error: ");
  35. return true;
  36. }
  37. return false;
  38. }
  39. /*
  40. * Split @str at comma.
  41. * A null @str defaults to "".
  42. */
  43. strList *hmp_split_at_comma(const char *str)
  44. {
  45. char **split = g_strsplit(str ?: "", ",", -1);
  46. strList *res = NULL;
  47. strList **tail = &res;
  48. int i;
  49. for (i = 0; split[i]; i++) {
  50. QAPI_LIST_APPEND(tail, split[i]);
  51. }
  52. g_free(split);
  53. return res;
  54. }
  55. void hmp_info_name(Monitor *mon, const QDict *qdict)
  56. {
  57. NameInfo *info;
  58. info = qmp_query_name(NULL);
  59. if (info->name) {
  60. monitor_printf(mon, "%s\n", info->name);
  61. }
  62. qapi_free_NameInfo(info);
  63. }
  64. void hmp_info_version(Monitor *mon, const QDict *qdict)
  65. {
  66. VersionInfo *info;
  67. info = qmp_query_version(NULL);
  68. monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
  69. info->qemu->major, info->qemu->minor, info->qemu->micro,
  70. info->package);
  71. qapi_free_VersionInfo(info);
  72. }
  73. static int hmp_info_pic_foreach(Object *obj, void *opaque)
  74. {
  75. InterruptStatsProvider *intc;
  76. InterruptStatsProviderClass *k;
  77. Monitor *mon = opaque;
  78. if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
  79. intc = INTERRUPT_STATS_PROVIDER(obj);
  80. k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
  81. if (k->print_info) {
  82. k->print_info(intc, mon);
  83. } else {
  84. monitor_printf(mon, "Interrupt controller information not available for %s.\n",
  85. object_get_typename(obj));
  86. }
  87. }
  88. return 0;
  89. }
  90. void hmp_info_pic(Monitor *mon, const QDict *qdict)
  91. {
  92. object_child_foreach_recursive(object_get_root(),
  93. hmp_info_pic_foreach, mon);
  94. }
  95. void hmp_quit(Monitor *mon, const QDict *qdict)
  96. {
  97. monitor_suspend(mon);
  98. qmp_quit(NULL);
  99. }
  100. void hmp_stop(Monitor *mon, const QDict *qdict)
  101. {
  102. qmp_stop(NULL);
  103. }
  104. void hmp_sync_profile(Monitor *mon, const QDict *qdict)
  105. {
  106. const char *op = qdict_get_try_str(qdict, "op");
  107. if (op == NULL) {
  108. bool on = qsp_is_enabled();
  109. monitor_printf(mon, "sync-profile is %s\n", on ? "on" : "off");
  110. return;
  111. }
  112. if (!strcmp(op, "on")) {
  113. qsp_enable();
  114. } else if (!strcmp(op, "off")) {
  115. qsp_disable();
  116. } else if (!strcmp(op, "reset")) {
  117. qsp_reset();
  118. } else {
  119. Error *err = NULL;
  120. error_setg(&err, QERR_INVALID_PARAMETER, op);
  121. hmp_handle_error(mon, err);
  122. }
  123. }
  124. void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
  125. {
  126. Error *err = NULL;
  127. qmp_x_exit_preconfig(&err);
  128. hmp_handle_error(mon, err);
  129. }
  130. void hmp_cpu(Monitor *mon, const QDict *qdict)
  131. {
  132. int64_t cpu_index;
  133. /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
  134. use it are converted to the QAPI */
  135. cpu_index = qdict_get_int(qdict, "index");
  136. if (monitor_set_cpu(mon, cpu_index) < 0) {
  137. monitor_printf(mon, "invalid CPU index\n");
  138. }
  139. }
  140. void hmp_cont(Monitor *mon, const QDict *qdict)
  141. {
  142. Error *err = NULL;
  143. qmp_cont(&err);
  144. hmp_handle_error(mon, err);
  145. }
  146. void hmp_change(Monitor *mon, const QDict *qdict)
  147. {
  148. const char *device = qdict_get_str(qdict, "device");
  149. const char *target = qdict_get_str(qdict, "target");
  150. const char *arg = qdict_get_try_str(qdict, "arg");
  151. const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
  152. bool force = qdict_get_try_bool(qdict, "force", false);
  153. Error *err = NULL;
  154. #ifdef CONFIG_VNC
  155. if (strcmp(device, "vnc") == 0) {
  156. hmp_change_vnc(mon, device, target, arg, read_only, force, &err);
  157. } else
  158. #endif
  159. {
  160. hmp_change_medium(mon, device, target, arg, read_only, force, &err);
  161. }
  162. hmp_handle_error(mon, err);
  163. }
  164. #ifdef CONFIG_POSIX
  165. void hmp_getfd(Monitor *mon, const QDict *qdict)
  166. {
  167. const char *fdname = qdict_get_str(qdict, "fdname");
  168. Error *err = NULL;
  169. qmp_getfd(fdname, &err);
  170. hmp_handle_error(mon, err);
  171. }
  172. #endif
  173. void hmp_closefd(Monitor *mon, const QDict *qdict)
  174. {
  175. const char *fdname = qdict_get_str(qdict, "fdname");
  176. Error *err = NULL;
  177. qmp_closefd(fdname, &err);
  178. hmp_handle_error(mon, err);
  179. }
  180. void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
  181. {
  182. IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
  183. IOThreadInfoList *info;
  184. IOThreadInfo *value;
  185. for (info = info_list; info; info = info->next) {
  186. value = info->value;
  187. monitor_printf(mon, "%s:\n", value->id);
  188. monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id);
  189. monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
  190. monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow);
  191. monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink);
  192. monitor_printf(mon, " aio-max-batch=%" PRId64 "\n",
  193. value->aio_max_batch);
  194. }
  195. qapi_free_IOThreadInfoList(info_list);
  196. }
  197. void hmp_help(Monitor *mon, const QDict *qdict)
  198. {
  199. hmp_help_cmd(mon, qdict_get_try_str(qdict, "name"));
  200. }
  201. void hmp_info_help(Monitor *mon, const QDict *qdict)
  202. {
  203. hmp_help_cmd(mon, "info");
  204. }
  205. void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
  206. {
  207. int64_t max = qdict_get_try_int(qdict, "max", 10);
  208. bool mean = qdict_get_try_bool(qdict, "mean", false);
  209. bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
  210. enum QSPSortBy sort_by;
  211. sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
  212. qsp_report(max, sort_by, coalesce);
  213. }
  214. void hmp_info_history(Monitor *mon, const QDict *qdict)
  215. {
  216. MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
  217. int i;
  218. const char *str;
  219. if (!hmp_mon->rs) {
  220. return;
  221. }
  222. i = 0;
  223. for(;;) {
  224. str = readline_get_history(hmp_mon->rs, i);
  225. if (!str) {
  226. break;
  227. }
  228. monitor_printf(mon, "%d: '%s'\n", i, str);
  229. i++;
  230. }
  231. }
  232. void hmp_logfile(Monitor *mon, const QDict *qdict)
  233. {
  234. Error *err = NULL;
  235. if (!qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err)) {
  236. error_report_err(err);
  237. }
  238. }
  239. void hmp_log(Monitor *mon, const QDict *qdict)
  240. {
  241. int mask;
  242. const char *items = qdict_get_str(qdict, "items");
  243. Error *err = NULL;
  244. if (!strcmp(items, "none")) {
  245. mask = 0;
  246. } else {
  247. mask = qemu_str_to_log_mask(items);
  248. if (!mask) {
  249. hmp_help_cmd(mon, "log");
  250. return;
  251. }
  252. }
  253. if (!qemu_set_log(mask, &err)) {
  254. error_report_err(err);
  255. }
  256. }
  257. void hmp_gdbserver(Monitor *mon, const QDict *qdict)
  258. {
  259. const char *device = qdict_get_try_str(qdict, "device");
  260. if (!device) {
  261. device = "tcp::" DEFAULT_GDBSTUB_PORT;
  262. }
  263. if (gdbserver_start(device) < 0) {
  264. monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
  265. device);
  266. } else if (strcmp(device, "none") == 0) {
  267. monitor_printf(mon, "Disabled gdbserver\n");
  268. } else {
  269. monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
  270. device);
  271. }
  272. }
  273. void hmp_print(Monitor *mon, const QDict *qdict)
  274. {
  275. int format = qdict_get_int(qdict, "format");
  276. hwaddr val = qdict_get_int(qdict, "val");
  277. switch(format) {
  278. case 'o':
  279. monitor_printf(mon, "%#" HWADDR_PRIo, val);
  280. break;
  281. case 'x':
  282. monitor_printf(mon, "%#" HWADDR_PRIx, val);
  283. break;
  284. case 'u':
  285. monitor_printf(mon, "%" HWADDR_PRIu, val);
  286. break;
  287. default:
  288. case 'd':
  289. monitor_printf(mon, "%" HWADDR_PRId, val);
  290. break;
  291. case 'c':
  292. monitor_printc(mon, val);
  293. break;
  294. }
  295. monitor_printf(mon, "\n");
  296. }
  297. void hmp_sum(Monitor *mon, const QDict *qdict)
  298. {
  299. uint32_t addr;
  300. uint16_t sum;
  301. uint32_t start = qdict_get_int(qdict, "start");
  302. uint32_t size = qdict_get_int(qdict, "size");
  303. sum = 0;
  304. for(addr = start; addr < (start + size); addr++) {
  305. uint8_t val = address_space_ldub(&address_space_memory, addr,
  306. MEMTXATTRS_UNSPECIFIED, NULL);
  307. /* BSD sum algorithm ('sum' Unix command) */
  308. sum = (sum >> 1) | (sum << 15);
  309. sum += val;
  310. }
  311. monitor_printf(mon, "%05d\n", sum);
  312. }
  313. void hmp_ioport_read(Monitor *mon, const QDict *qdict)
  314. {
  315. int size = qdict_get_int(qdict, "size");
  316. int addr = qdict_get_int(qdict, "addr");
  317. int has_index = qdict_haskey(qdict, "index");
  318. uint32_t val;
  319. int suffix;
  320. if (has_index) {
  321. int index = qdict_get_int(qdict, "index");
  322. cpu_outb(addr & IOPORTS_MASK, index & 0xff);
  323. addr++;
  324. }
  325. addr &= 0xffff;
  326. switch(size) {
  327. default:
  328. case 1:
  329. val = cpu_inb(addr);
  330. suffix = 'b';
  331. break;
  332. case 2:
  333. val = cpu_inw(addr);
  334. suffix = 'w';
  335. break;
  336. case 4:
  337. val = cpu_inl(addr);
  338. suffix = 'l';
  339. break;
  340. }
  341. monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
  342. suffix, addr, size * 2, val);
  343. }
  344. void hmp_ioport_write(Monitor *mon, const QDict *qdict)
  345. {
  346. int size = qdict_get_int(qdict, "size");
  347. int addr = qdict_get_int(qdict, "addr");
  348. int val = qdict_get_int(qdict, "val");
  349. addr &= IOPORTS_MASK;
  350. switch (size) {
  351. default:
  352. case 1:
  353. cpu_outb(addr, val);
  354. break;
  355. case 2:
  356. cpu_outw(addr, val);
  357. break;
  358. case 4:
  359. cpu_outl(addr, val);
  360. break;
  361. }
  362. }
  363. void hmp_boot_set(Monitor *mon, const QDict *qdict)
  364. {
  365. Error *local_err = NULL;
  366. const char *bootdevice = qdict_get_str(qdict, "bootdevice");
  367. qemu_boot_set(bootdevice, &local_err);
  368. if (local_err) {
  369. error_report_err(local_err);
  370. } else {
  371. monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
  372. }
  373. }
  374. void hmp_info_mtree(Monitor *mon, const QDict *qdict)
  375. {
  376. bool flatview = qdict_get_try_bool(qdict, "flatview", false);
  377. bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
  378. bool owner = qdict_get_try_bool(qdict, "owner", false);
  379. bool disabled = qdict_get_try_bool(qdict, "disabled", false);
  380. mtree_info(flatview, dispatch_tree, owner, disabled);
  381. }