2
0

cflow.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Control Flow plugin
  3. *
  4. * This plugin will track changes to control flow and detect where
  5. * instructions fault.
  6. *
  7. * Copyright (c) 2024 Linaro Ltd
  8. *
  9. * SPDX-License-Identifier: GPL-2.0-or-later
  10. */
  11. #include <glib.h>
  12. #include <inttypes.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <qemu-plugin.h>
  18. QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
  19. typedef enum {
  20. SORT_HOTTEST, /* hottest branch insn */
  21. SORT_EXCEPTION, /* most early exits */
  22. SORT_POPDEST, /* most destinations (usually ret's) */
  23. } ReportType;
  24. ReportType report = SORT_HOTTEST;
  25. int topn = 10;
  26. typedef struct {
  27. uint64_t daddr;
  28. uint64_t dcount;
  29. } DestData;
  30. /* A node is an address where we can go to multiple places */
  31. typedef struct {
  32. GMutex lock;
  33. /* address of the branch point */
  34. uint64_t addr;
  35. /* array of DestData */
  36. GArray *dests;
  37. /* early exit/fault count */
  38. uint64_t early_exit;
  39. /* jump destination count */
  40. uint64_t dest_count;
  41. /* instruction data */
  42. char *insn_disas;
  43. /* symbol? */
  44. const char *symbol;
  45. /* times translated as last in block? */
  46. int last_count;
  47. /* times translated in the middle of block? */
  48. int mid_count;
  49. } NodeData;
  50. typedef enum {
  51. /* last insn in block, expected flow control */
  52. LAST_INSN = (1 << 0),
  53. /* mid-block insn, can only be an exception */
  54. EXCP_INSN = (1 << 1),
  55. /* multiple disassembly, may have changed */
  56. MULT_INSN = (1 << 2),
  57. } InsnTypes;
  58. typedef struct {
  59. /* address of the branch point */
  60. uint64_t addr;
  61. /* disassembly */
  62. char *insn_disas;
  63. /* symbol? */
  64. const char *symbol;
  65. /* types */
  66. InsnTypes type_flag;
  67. } InsnData;
  68. /* We use this to track the current execution state */
  69. typedef struct {
  70. /* address of current translated block */
  71. uint64_t tb_pc;
  72. /* address of end of block */
  73. uint64_t end_block;
  74. /* next pc after end of block */
  75. uint64_t pc_after_block;
  76. /* address of last executed PC */
  77. uint64_t last_pc;
  78. } VCPUScoreBoard;
  79. /* descriptors for accessing the above scoreboard */
  80. static qemu_plugin_u64 tb_pc;
  81. static qemu_plugin_u64 end_block;
  82. static qemu_plugin_u64 pc_after_block;
  83. static qemu_plugin_u64 last_pc;
  84. static GMutex node_lock;
  85. static GHashTable *nodes;
  86. struct qemu_plugin_scoreboard *state;
  87. /* SORT_HOTTEST */
  88. static gint hottest(gconstpointer a, gconstpointer b)
  89. {
  90. NodeData *na = (NodeData *) a;
  91. NodeData *nb = (NodeData *) b;
  92. return na->dest_count > nb->dest_count ? -1 :
  93. na->dest_count == nb->dest_count ? 0 : 1;
  94. }
  95. static gint exception(gconstpointer a, gconstpointer b)
  96. {
  97. NodeData *na = (NodeData *) a;
  98. NodeData *nb = (NodeData *) b;
  99. return na->early_exit > nb->early_exit ? -1 :
  100. na->early_exit == nb->early_exit ? 0 : 1;
  101. }
  102. static gint popular(gconstpointer a, gconstpointer b)
  103. {
  104. NodeData *na = (NodeData *) a;
  105. NodeData *nb = (NodeData *) b;
  106. return na->dests->len > nb->dests->len ? -1 :
  107. na->dests->len == nb->dests->len ? 0 : 1;
  108. }
  109. /* Filter out non-branches - returns true to remove entry */
  110. static gboolean filter_non_branches(gpointer key, gpointer value,
  111. gpointer user_data)
  112. {
  113. NodeData *node = (NodeData *) value;
  114. return node->dest_count == 0;
  115. }
  116. static void plugin_exit(qemu_plugin_id_t id, void *p)
  117. {
  118. g_autoptr(GString) result = g_string_new("collected ");
  119. GList *data;
  120. GCompareFunc sort = &hottest;
  121. int i = 0;
  122. g_mutex_lock(&node_lock);
  123. g_string_append_printf(result, "%d control flow nodes in the hash table\n",
  124. g_hash_table_size(nodes));
  125. /* remove all nodes that didn't branch */
  126. g_hash_table_foreach_remove(nodes, filter_non_branches, NULL);
  127. data = g_hash_table_get_values(nodes);
  128. switch (report) {
  129. case SORT_HOTTEST:
  130. sort = &hottest;
  131. break;
  132. case SORT_EXCEPTION:
  133. sort = &exception;
  134. break;
  135. case SORT_POPDEST:
  136. sort = &popular;
  137. break;
  138. }
  139. data = g_list_sort(data, sort);
  140. for (GList *l = data;
  141. l != NULL && i < topn;
  142. l = l->next, i++) {
  143. NodeData *n = l->data;
  144. const char *type = n->mid_count ? "sync fault" : "branch";
  145. g_string_append_printf(result, " addr: 0x%"PRIx64 " %s: %s (%s)\n",
  146. n->addr, n->symbol, n->insn_disas, type);
  147. if (n->early_exit) {
  148. g_string_append_printf(result, " early exits %"PRId64"\n",
  149. n->early_exit);
  150. }
  151. g_string_append_printf(result, " branches %"PRId64"\n",
  152. n->dest_count);
  153. for (int j = 0; j < n->dests->len; j++) {
  154. DestData *dd = &g_array_index(n->dests, DestData, j);
  155. g_string_append_printf(result, " to 0x%"PRIx64" (%"PRId64")\n",
  156. dd->daddr, dd->dcount);
  157. }
  158. }
  159. qemu_plugin_outs(result->str);
  160. g_mutex_unlock(&node_lock);
  161. }
  162. static void plugin_init(void)
  163. {
  164. g_mutex_init(&node_lock);
  165. nodes = g_hash_table_new(g_int64_hash, g_int64_equal);
  166. state = qemu_plugin_scoreboard_new(sizeof(VCPUScoreBoard));
  167. /* score board declarations */
  168. tb_pc = qemu_plugin_scoreboard_u64_in_struct(state, VCPUScoreBoard, tb_pc);
  169. end_block = qemu_plugin_scoreboard_u64_in_struct(state, VCPUScoreBoard,
  170. end_block);
  171. pc_after_block = qemu_plugin_scoreboard_u64_in_struct(state, VCPUScoreBoard,
  172. pc_after_block);
  173. last_pc = qemu_plugin_scoreboard_u64_in_struct(state, VCPUScoreBoard,
  174. last_pc);
  175. }
  176. static NodeData *create_node(uint64_t addr)
  177. {
  178. NodeData *node = g_new0(NodeData, 1);
  179. g_mutex_init(&node->lock);
  180. node->addr = addr;
  181. node->dests = g_array_new(true, true, sizeof(DestData));
  182. return node;
  183. }
  184. static NodeData *fetch_node(uint64_t addr, bool create_if_not_found)
  185. {
  186. NodeData *node = NULL;
  187. g_mutex_lock(&node_lock);
  188. node = (NodeData *) g_hash_table_lookup(nodes, &addr);
  189. if (!node && create_if_not_found) {
  190. node = create_node(addr);
  191. g_hash_table_insert(nodes, &node->addr, node);
  192. }
  193. g_mutex_unlock(&node_lock);
  194. return node;
  195. }
  196. /*
  197. * Called when we detect a non-linear execution (pc !=
  198. * pc_after_block). This could be due to a fault causing some sort of
  199. * exit exception (if last_pc != block_end) or just a taken branch.
  200. */
  201. static void vcpu_tb_branched_exec(unsigned int cpu_index, void *udata)
  202. {
  203. uint64_t lpc = qemu_plugin_u64_get(last_pc, cpu_index);
  204. uint64_t ebpc = qemu_plugin_u64_get(end_block, cpu_index);
  205. uint64_t npc = qemu_plugin_u64_get(pc_after_block, cpu_index);
  206. uint64_t pc = qemu_plugin_u64_get(tb_pc, cpu_index);
  207. /* return early for address 0 */
  208. if (!lpc) {
  209. return;
  210. }
  211. NodeData *node = fetch_node(lpc, true);
  212. DestData *data = NULL;
  213. bool early_exit = (lpc != ebpc);
  214. GArray *dests;
  215. /* the condition should never hit */
  216. g_assert(pc != npc);
  217. g_mutex_lock(&node->lock);
  218. if (early_exit) {
  219. fprintf(stderr, "%s: pc=%"PRIx64", epbc=%"PRIx64
  220. " npc=%"PRIx64", lpc=%"PRIx64"\n",
  221. __func__, pc, ebpc, npc, lpc);
  222. node->early_exit++;
  223. if (!node->mid_count) {
  224. /* count now as we've only just allocated */
  225. node->mid_count++;
  226. }
  227. }
  228. dests = node->dests;
  229. for (int i = 0; i < dests->len; i++) {
  230. if (g_array_index(dests, DestData, i).daddr == pc) {
  231. data = &g_array_index(dests, DestData, i);
  232. }
  233. }
  234. /* we've never seen this before, allocate a new entry */
  235. if (!data) {
  236. DestData new_entry = { .daddr = pc };
  237. g_array_append_val(dests, new_entry);
  238. data = &g_array_index(dests, DestData, dests->len - 1);
  239. g_assert(data->daddr == pc);
  240. }
  241. data->dcount++;
  242. node->dest_count++;
  243. g_mutex_unlock(&node->lock);
  244. }
  245. /*
  246. * At the start of each block we need to resolve two things:
  247. *
  248. * - is last_pc == block_end, if not we had an early exit
  249. * - is start of block last_pc + insn width, if not we jumped
  250. *
  251. * Once those are dealt with we can instrument the rest of the
  252. * instructions for their execution.
  253. *
  254. */
  255. static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
  256. {
  257. uint64_t pc = qemu_plugin_tb_vaddr(tb);
  258. size_t insns = qemu_plugin_tb_n_insns(tb);
  259. struct qemu_plugin_insn *first_insn = qemu_plugin_tb_get_insn(tb, 0);
  260. struct qemu_plugin_insn *last_insn = qemu_plugin_tb_get_insn(tb, insns - 1);
  261. /*
  262. * check if we are executing linearly after the last block. We can
  263. * handle both early block exits and normal branches in the
  264. * callback if we hit it.
  265. */
  266. qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
  267. tb, QEMU_PLUGIN_INLINE_STORE_U64, tb_pc, pc);
  268. qemu_plugin_register_vcpu_tb_exec_cond_cb(
  269. tb, vcpu_tb_branched_exec, QEMU_PLUGIN_CB_NO_REGS,
  270. QEMU_PLUGIN_COND_NE, pc_after_block, pc, NULL);
  271. /*
  272. * Now we can set start/end for this block so the next block can
  273. * check where we are at. Do this on the first instruction and not
  274. * the TB so we don't get mixed up with above.
  275. */
  276. qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(first_insn,
  277. QEMU_PLUGIN_INLINE_STORE_U64,
  278. end_block, qemu_plugin_insn_vaddr(last_insn));
  279. qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(first_insn,
  280. QEMU_PLUGIN_INLINE_STORE_U64,
  281. pc_after_block,
  282. qemu_plugin_insn_vaddr(last_insn) +
  283. qemu_plugin_insn_size(last_insn));
  284. for (int idx = 0; idx < qemu_plugin_tb_n_insns(tb); ++idx) {
  285. struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, idx);
  286. uint64_t ipc = qemu_plugin_insn_vaddr(insn);
  287. /*
  288. * If this is a potential branch point check if we could grab
  289. * the disassembly for it. If it is the last instruction
  290. * always create an entry.
  291. */
  292. NodeData *node = fetch_node(ipc, last_insn);
  293. if (node) {
  294. g_mutex_lock(&node->lock);
  295. if (!node->insn_disas) {
  296. node->insn_disas = qemu_plugin_insn_disas(insn);
  297. }
  298. if (!node->symbol) {
  299. node->symbol = qemu_plugin_insn_symbol(insn);
  300. }
  301. if (last_insn == insn) {
  302. node->last_count++;
  303. } else {
  304. node->mid_count++;
  305. }
  306. g_mutex_unlock(&node->lock);
  307. }
  308. /* Store the PC of what we are about to execute */
  309. qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(insn,
  310. QEMU_PLUGIN_INLINE_STORE_U64,
  311. last_pc, ipc);
  312. }
  313. }
  314. QEMU_PLUGIN_EXPORT
  315. int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
  316. int argc, char **argv)
  317. {
  318. for (int i = 0; i < argc; i++) {
  319. char *opt = argv[i];
  320. g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
  321. if (g_strcmp0(tokens[0], "sort") == 0) {
  322. if (g_strcmp0(tokens[1], "hottest") == 0) {
  323. report = SORT_HOTTEST;
  324. } else if (g_strcmp0(tokens[1], "early") == 0) {
  325. report = SORT_EXCEPTION;
  326. } else if (g_strcmp0(tokens[1], "exceptions") == 0) {
  327. report = SORT_POPDEST;
  328. } else {
  329. fprintf(stderr, "failed to parse: %s\n", tokens[1]);
  330. return -1;
  331. }
  332. } else {
  333. fprintf(stderr, "option parsing failed: %s\n", opt);
  334. return -1;
  335. }
  336. }
  337. plugin_init();
  338. qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
  339. qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
  340. return 0;
  341. }