hmp-cmds.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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/ioport.h"
  18. #include "exec/gdbstub.h"
  19. #include "gdbstub/enums.h"
  20. #include "monitor/hmp.h"
  21. #include "qemu/help_option.h"
  22. #include "monitor/monitor-internal.h"
  23. #include "qapi/error.h"
  24. #include "qapi/qapi-commands-control.h"
  25. #include "qapi/qapi-commands-machine.h"
  26. #include "qapi/qapi-commands-misc.h"
  27. #include "qobject/qdict.h"
  28. #include "qemu/cutils.h"
  29. #include "qemu/log.h"
  30. #include "system/system.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. void hmp_quit(Monitor *mon, const QDict *qdict)
  74. {
  75. monitor_suspend(mon);
  76. qmp_quit(NULL);
  77. }
  78. void hmp_stop(Monitor *mon, const QDict *qdict)
  79. {
  80. qmp_stop(NULL);
  81. }
  82. void hmp_sync_profile(Monitor *mon, const QDict *qdict)
  83. {
  84. const char *op = qdict_get_try_str(qdict, "op");
  85. if (op == NULL) {
  86. bool on = qsp_is_enabled();
  87. monitor_printf(mon, "sync-profile is %s\n", on ? "on" : "off");
  88. return;
  89. }
  90. if (!strcmp(op, "on")) {
  91. qsp_enable();
  92. } else if (!strcmp(op, "off")) {
  93. qsp_disable();
  94. } else if (!strcmp(op, "reset")) {
  95. qsp_reset();
  96. } else {
  97. Error *err = NULL;
  98. error_setg(&err, "invalid parameter '%s',"
  99. " expecting 'on', 'off', or 'reset'", op);
  100. hmp_handle_error(mon, err);
  101. }
  102. }
  103. void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
  104. {
  105. Error *err = NULL;
  106. qmp_x_exit_preconfig(&err);
  107. hmp_handle_error(mon, err);
  108. }
  109. void hmp_cpu(Monitor *mon, const QDict *qdict)
  110. {
  111. int64_t cpu_index;
  112. /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
  113. use it are converted to the QAPI */
  114. cpu_index = qdict_get_int(qdict, "index");
  115. if (monitor_set_cpu(mon, cpu_index) < 0) {
  116. monitor_printf(mon, "invalid CPU index\n");
  117. }
  118. }
  119. void hmp_cont(Monitor *mon, const QDict *qdict)
  120. {
  121. Error *err = NULL;
  122. qmp_cont(&err);
  123. hmp_handle_error(mon, err);
  124. }
  125. void hmp_change(Monitor *mon, const QDict *qdict)
  126. {
  127. const char *device = qdict_get_str(qdict, "device");
  128. const char *target = qdict_get_str(qdict, "target");
  129. const char *arg = qdict_get_try_str(qdict, "arg");
  130. const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
  131. bool force = qdict_get_try_bool(qdict, "force", false);
  132. Error *err = NULL;
  133. #ifdef CONFIG_VNC
  134. if (strcmp(device, "vnc") == 0) {
  135. hmp_change_vnc(mon, device, target, arg, read_only, force, &err);
  136. } else
  137. #endif
  138. {
  139. hmp_change_medium(mon, device, target, arg, read_only, force, &err);
  140. }
  141. hmp_handle_error(mon, err);
  142. }
  143. #ifdef CONFIG_POSIX
  144. void hmp_getfd(Monitor *mon, const QDict *qdict)
  145. {
  146. const char *fdname = qdict_get_str(qdict, "fdname");
  147. Error *err = NULL;
  148. qmp_getfd(fdname, &err);
  149. hmp_handle_error(mon, err);
  150. }
  151. #endif
  152. void hmp_closefd(Monitor *mon, const QDict *qdict)
  153. {
  154. const char *fdname = qdict_get_str(qdict, "fdname");
  155. Error *err = NULL;
  156. qmp_closefd(fdname, &err);
  157. hmp_handle_error(mon, err);
  158. }
  159. void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
  160. {
  161. IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
  162. IOThreadInfoList *info;
  163. IOThreadInfo *value;
  164. for (info = info_list; info; info = info->next) {
  165. value = info->value;
  166. monitor_printf(mon, "%s:\n", value->id);
  167. monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id);
  168. monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
  169. monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow);
  170. monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink);
  171. monitor_printf(mon, " aio-max-batch=%" PRId64 "\n",
  172. value->aio_max_batch);
  173. }
  174. qapi_free_IOThreadInfoList(info_list);
  175. }
  176. void hmp_help(Monitor *mon, const QDict *qdict)
  177. {
  178. hmp_help_cmd(mon, qdict_get_try_str(qdict, "name"));
  179. }
  180. void hmp_info_help(Monitor *mon, const QDict *qdict)
  181. {
  182. hmp_help_cmd(mon, "info");
  183. }
  184. void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
  185. {
  186. int64_t max = qdict_get_try_int(qdict, "max", 10);
  187. bool mean = qdict_get_try_bool(qdict, "mean", false);
  188. bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
  189. enum QSPSortBy sort_by;
  190. sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
  191. qsp_report(max, sort_by, coalesce);
  192. }
  193. void hmp_info_history(Monitor *mon, const QDict *qdict)
  194. {
  195. MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
  196. int i;
  197. const char *str;
  198. if (!hmp_mon->rs) {
  199. return;
  200. }
  201. i = 0;
  202. for(;;) {
  203. str = readline_get_history(hmp_mon->rs, i);
  204. if (!str) {
  205. break;
  206. }
  207. monitor_printf(mon, "%d: '%s'\n", i, str);
  208. i++;
  209. }
  210. }
  211. void hmp_logfile(Monitor *mon, const QDict *qdict)
  212. {
  213. Error *err = NULL;
  214. if (!qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err)) {
  215. error_report_err(err);
  216. }
  217. }
  218. void hmp_log(Monitor *mon, const QDict *qdict)
  219. {
  220. int mask;
  221. const char *items = qdict_get_str(qdict, "items");
  222. Error *err = NULL;
  223. if (!strcmp(items, "none")) {
  224. mask = 0;
  225. } else {
  226. mask = qemu_str_to_log_mask(items);
  227. if (!mask) {
  228. hmp_help_cmd(mon, "log");
  229. return;
  230. }
  231. }
  232. if (!qemu_set_log(mask, &err)) {
  233. error_report_err(err);
  234. }
  235. }
  236. void hmp_gdbserver(Monitor *mon, const QDict *qdict)
  237. {
  238. const char *device = qdict_get_try_str(qdict, "device");
  239. if (!device) {
  240. device = "tcp::" DEFAULT_GDBSTUB_PORT;
  241. }
  242. if (!gdbserver_start(device, &error_warn)) {
  243. monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
  244. device);
  245. } else if (strcmp(device, "none") == 0) {
  246. monitor_printf(mon, "Disabled gdbserver\n");
  247. } else {
  248. monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
  249. device);
  250. }
  251. }
  252. void hmp_print(Monitor *mon, const QDict *qdict)
  253. {
  254. int format = qdict_get_int(qdict, "format");
  255. hwaddr val = qdict_get_int(qdict, "val");
  256. switch(format) {
  257. case 'o':
  258. monitor_printf(mon, "%#" HWADDR_PRIo, val);
  259. break;
  260. case 'x':
  261. monitor_printf(mon, "%#" HWADDR_PRIx, val);
  262. break;
  263. case 'u':
  264. monitor_printf(mon, "%" HWADDR_PRIu, val);
  265. break;
  266. default:
  267. case 'd':
  268. monitor_printf(mon, "%" HWADDR_PRId, val);
  269. break;
  270. case 'c':
  271. monitor_printc(mon, val);
  272. break;
  273. }
  274. monitor_printf(mon, "\n");
  275. }
  276. void hmp_sum(Monitor *mon, const QDict *qdict)
  277. {
  278. uint32_t addr;
  279. uint16_t sum;
  280. uint32_t start = qdict_get_int(qdict, "start");
  281. uint32_t size = qdict_get_int(qdict, "size");
  282. sum = 0;
  283. for(addr = start; addr < (start + size); addr++) {
  284. uint8_t val = address_space_ldub(&address_space_memory, addr,
  285. MEMTXATTRS_UNSPECIFIED, NULL);
  286. /* BSD sum algorithm ('sum' Unix command) */
  287. sum = (sum >> 1) | (sum << 15);
  288. sum += val;
  289. }
  290. monitor_printf(mon, "%05d\n", sum);
  291. }
  292. void hmp_ioport_read(Monitor *mon, const QDict *qdict)
  293. {
  294. int size = qdict_get_int(qdict, "size");
  295. int addr = qdict_get_int(qdict, "addr");
  296. int has_index = qdict_haskey(qdict, "index");
  297. uint32_t val;
  298. int suffix;
  299. if (has_index) {
  300. int index = qdict_get_int(qdict, "index");
  301. cpu_outb(addr & IOPORTS_MASK, index & 0xff);
  302. addr++;
  303. }
  304. addr &= 0xffff;
  305. switch(size) {
  306. default:
  307. case 1:
  308. val = cpu_inb(addr);
  309. suffix = 'b';
  310. break;
  311. case 2:
  312. val = cpu_inw(addr);
  313. suffix = 'w';
  314. break;
  315. case 4:
  316. val = cpu_inl(addr);
  317. suffix = 'l';
  318. break;
  319. }
  320. monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
  321. suffix, addr, size * 2, val);
  322. }
  323. void hmp_ioport_write(Monitor *mon, const QDict *qdict)
  324. {
  325. int size = qdict_get_int(qdict, "size");
  326. int addr = qdict_get_int(qdict, "addr");
  327. int val = qdict_get_int(qdict, "val");
  328. addr &= IOPORTS_MASK;
  329. switch (size) {
  330. default:
  331. case 1:
  332. cpu_outb(addr, val);
  333. break;
  334. case 2:
  335. cpu_outw(addr, val);
  336. break;
  337. case 4:
  338. cpu_outl(addr, val);
  339. break;
  340. }
  341. }
  342. void hmp_boot_set(Monitor *mon, const QDict *qdict)
  343. {
  344. Error *local_err = NULL;
  345. const char *bootdevice = qdict_get_str(qdict, "bootdevice");
  346. qemu_boot_set(bootdevice, &local_err);
  347. if (local_err) {
  348. error_report_err(local_err);
  349. } else {
  350. monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
  351. }
  352. }
  353. void hmp_info_mtree(Monitor *mon, const QDict *qdict)
  354. {
  355. bool flatview = qdict_get_try_bool(qdict, "flatview", false);
  356. bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
  357. bool owner = qdict_get_try_bool(qdict, "owner", false);
  358. bool disabled = qdict_get_try_bool(qdict, "disabled", false);
  359. mtree_info(flatview, dispatch_tree, owner, disabled);
  360. }
  361. #if defined(CONFIG_FDT)
  362. void hmp_dumpdtb(Monitor *mon, const QDict *qdict)
  363. {
  364. const char *filename = qdict_get_str(qdict, "filename");
  365. Error *local_err = NULL;
  366. qmp_dumpdtb(filename, &local_err);
  367. if (hmp_handle_error(mon, local_err)) {
  368. return;
  369. }
  370. monitor_printf(mon, "DTB dumped to '%s'\n", filename);
  371. }
  372. #endif