execlog.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (C) 2021, Alexandre Iooss <erdnaxe@crans.org>
  3. *
  4. * Log instruction execution with memory access and register changes
  5. *
  6. * License: GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include <glib.h>
  10. #include <inttypes.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <qemu-plugin.h>
  16. typedef struct {
  17. struct qemu_plugin_register *handle;
  18. GByteArray *last;
  19. GByteArray *new;
  20. const char *name;
  21. } Register;
  22. typedef struct CPU {
  23. /* Store last executed instruction on each vCPU as a GString */
  24. GString *last_exec;
  25. /* Ptr array of Register */
  26. GPtrArray *registers;
  27. } CPU;
  28. QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
  29. static GArray *cpus;
  30. static GRWLock expand_array_lock;
  31. static GPtrArray *imatches;
  32. static GArray *amatches;
  33. static GPtrArray *rmatches;
  34. static bool disas_assist;
  35. static GMutex add_reg_name_lock;
  36. static GPtrArray *all_reg_names;
  37. static CPU *get_cpu(int vcpu_index)
  38. {
  39. CPU *c;
  40. g_rw_lock_reader_lock(&expand_array_lock);
  41. c = &g_array_index(cpus, CPU, vcpu_index);
  42. g_rw_lock_reader_unlock(&expand_array_lock);
  43. return c;
  44. }
  45. /**
  46. * Add memory read or write information to current instruction log
  47. */
  48. static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t info,
  49. uint64_t vaddr, void *udata)
  50. {
  51. CPU *c = get_cpu(cpu_index);
  52. GString *s = c->last_exec;
  53. /* Find vCPU in array */
  54. /* Indicate type of memory access */
  55. if (qemu_plugin_mem_is_store(info)) {
  56. g_string_append(s, ", store");
  57. } else {
  58. g_string_append(s, ", load");
  59. }
  60. /* If full system emulation log physical address and device name */
  61. struct qemu_plugin_hwaddr *hwaddr = qemu_plugin_get_hwaddr(info, vaddr);
  62. if (hwaddr) {
  63. uint64_t addr = qemu_plugin_hwaddr_phys_addr(hwaddr);
  64. const char *name = qemu_plugin_hwaddr_device_name(hwaddr);
  65. g_string_append_printf(s, ", 0x%08"PRIx64", %s", addr, name);
  66. } else {
  67. g_string_append_printf(s, ", 0x%08"PRIx64, vaddr);
  68. }
  69. }
  70. /**
  71. * Log instruction execution, outputting the last one.
  72. *
  73. * vcpu_insn_exec() is a copy and paste of vcpu_insn_exec_with_regs()
  74. * without the checking of register values when we've attempted to
  75. * optimise with disas_assist.
  76. */
  77. static void insn_check_regs(CPU *cpu)
  78. {
  79. for (int n = 0; n < cpu->registers->len; n++) {
  80. Register *reg = cpu->registers->pdata[n];
  81. int sz;
  82. g_byte_array_set_size(reg->new, 0);
  83. sz = qemu_plugin_read_register(reg->handle, reg->new);
  84. g_assert(sz == reg->last->len);
  85. if (memcmp(reg->last->data, reg->new->data, sz)) {
  86. GByteArray *temp = reg->last;
  87. g_string_append_printf(cpu->last_exec, ", %s -> 0x", reg->name);
  88. /* TODO: handle BE properly */
  89. for (int i = sz - 1; i >= 0; i--) {
  90. g_string_append_printf(cpu->last_exec, "%02x",
  91. reg->new->data[i]);
  92. }
  93. reg->last = reg->new;
  94. reg->new = temp;
  95. }
  96. }
  97. }
  98. /* Log last instruction while checking registers */
  99. static void vcpu_insn_exec_with_regs(unsigned int cpu_index, void *udata)
  100. {
  101. CPU *cpu = get_cpu(cpu_index);
  102. /* Print previous instruction in cache */
  103. if (cpu->last_exec->len) {
  104. if (cpu->registers) {
  105. insn_check_regs(cpu);
  106. }
  107. qemu_plugin_outs(cpu->last_exec->str);
  108. qemu_plugin_outs("\n");
  109. }
  110. /* Store new instruction in cache */
  111. /* vcpu_mem will add memory access information to last_exec */
  112. g_string_printf(cpu->last_exec, "%u, ", cpu_index);
  113. g_string_append(cpu->last_exec, (char *)udata);
  114. }
  115. /* Log last instruction while checking registers, ignore next */
  116. static void vcpu_insn_exec_only_regs(unsigned int cpu_index, void *udata)
  117. {
  118. CPU *cpu = get_cpu(cpu_index);
  119. /* Print previous instruction in cache */
  120. if (cpu->last_exec->len) {
  121. if (cpu->registers) {
  122. insn_check_regs(cpu);
  123. }
  124. qemu_plugin_outs(cpu->last_exec->str);
  125. qemu_plugin_outs("\n");
  126. }
  127. /* reset */
  128. cpu->last_exec->len = 0;
  129. }
  130. /* Log last instruction without checking regs, setup next */
  131. static void vcpu_insn_exec(unsigned int cpu_index, void *udata)
  132. {
  133. CPU *cpu = get_cpu(cpu_index);
  134. /* Print previous instruction in cache */
  135. if (cpu->last_exec->len) {
  136. qemu_plugin_outs(cpu->last_exec->str);
  137. qemu_plugin_outs("\n");
  138. }
  139. /* Store new instruction in cache */
  140. /* vcpu_mem will add memory access information to last_exec */
  141. g_string_printf(cpu->last_exec, "%u, ", cpu_index);
  142. g_string_append(cpu->last_exec, (char *)udata);
  143. }
  144. /**
  145. * On translation block new translation
  146. *
  147. * QEMU convert code by translation block (TB). By hooking here we can then hook
  148. * a callback on each instruction and memory access.
  149. */
  150. static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
  151. {
  152. struct qemu_plugin_insn *insn;
  153. bool skip = (imatches || amatches);
  154. bool check_regs_this = rmatches;
  155. bool check_regs_next = false;
  156. size_t n_insns = qemu_plugin_tb_n_insns(tb);
  157. for (size_t i = 0; i < n_insns; i++) {
  158. char *insn_disas;
  159. uint64_t insn_vaddr;
  160. /*
  161. * `insn` is shared between translations in QEMU, copy needed data here.
  162. * `output` is never freed as it might be used multiple times during
  163. * the emulation lifetime.
  164. * We only consider the first 32 bits of the instruction, this may be
  165. * a limitation for CISC architectures.
  166. */
  167. insn = qemu_plugin_tb_get_insn(tb, i);
  168. insn_disas = qemu_plugin_insn_disas(insn);
  169. insn_vaddr = qemu_plugin_insn_vaddr(insn);
  170. /*
  171. * If we are filtering we better check out if we have any
  172. * hits. The skip "latches" so we can track memory accesses
  173. * after the instruction we care about. Also enable register
  174. * checking on the next instruction.
  175. */
  176. if (skip && imatches) {
  177. int j;
  178. for (j = 0; j < imatches->len && skip; j++) {
  179. char *m = g_ptr_array_index(imatches, j);
  180. if (g_str_has_prefix(insn_disas, m)) {
  181. skip = false;
  182. check_regs_next = rmatches;
  183. }
  184. }
  185. }
  186. if (skip && amatches) {
  187. int j;
  188. for (j = 0; j < amatches->len && skip; j++) {
  189. uint64_t v = g_array_index(amatches, uint64_t, j);
  190. if (v == insn_vaddr) {
  191. skip = false;
  192. }
  193. }
  194. }
  195. /*
  196. * Check the disassembly to see if a register we care about
  197. * will be affected by this instruction. This relies on the
  198. * dissembler doing something sensible for the registers we
  199. * care about.
  200. */
  201. if (disas_assist && rmatches) {
  202. check_regs_next = false;
  203. gchar *args = g_strstr_len(insn_disas, -1, " ");
  204. for (int n = 0; n < all_reg_names->len; n++) {
  205. gchar *reg = g_ptr_array_index(all_reg_names, n);
  206. if (g_strrstr(args, reg)) {
  207. check_regs_next = true;
  208. skip = false;
  209. }
  210. }
  211. }
  212. /*
  213. * We now have 3 choices:
  214. *
  215. * - Log insn
  216. * - Log insn while checking registers
  217. * - Don't log this insn but check if last insn changed registers
  218. */
  219. if (skip) {
  220. if (check_regs_this) {
  221. qemu_plugin_register_vcpu_insn_exec_cb(insn,
  222. vcpu_insn_exec_only_regs,
  223. QEMU_PLUGIN_CB_R_REGS,
  224. NULL);
  225. }
  226. } else {
  227. uint32_t insn_opcode = 0;
  228. qemu_plugin_insn_data(insn, &insn_opcode, sizeof(insn_opcode));
  229. char *output = g_strdup_printf("0x%"PRIx64", 0x%"PRIx32", \"%s\"",
  230. insn_vaddr, insn_opcode, insn_disas);
  231. /* Register callback on memory read or write */
  232. qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
  233. QEMU_PLUGIN_CB_NO_REGS,
  234. QEMU_PLUGIN_MEM_RW, NULL);
  235. /* Register callback on instruction */
  236. if (check_regs_this) {
  237. qemu_plugin_register_vcpu_insn_exec_cb(
  238. insn, vcpu_insn_exec_with_regs,
  239. QEMU_PLUGIN_CB_R_REGS,
  240. output);
  241. } else {
  242. qemu_plugin_register_vcpu_insn_exec_cb(
  243. insn, vcpu_insn_exec,
  244. QEMU_PLUGIN_CB_NO_REGS,
  245. output);
  246. }
  247. /* reset skip */
  248. skip = (imatches || amatches);
  249. }
  250. /* set regs for next */
  251. if (disas_assist && rmatches) {
  252. check_regs_this = check_regs_next;
  253. }
  254. g_free(insn_disas);
  255. }
  256. }
  257. static Register *init_vcpu_register(qemu_plugin_reg_descriptor *desc)
  258. {
  259. Register *reg = g_new0(Register, 1);
  260. g_autofree gchar *lower = g_utf8_strdown(desc->name, -1);
  261. int r;
  262. reg->handle = desc->handle;
  263. reg->name = g_intern_string(lower);
  264. reg->last = g_byte_array_new();
  265. reg->new = g_byte_array_new();
  266. /* read the initial value */
  267. r = qemu_plugin_read_register(reg->handle, reg->last);
  268. g_assert(r > 0);
  269. return reg;
  270. }
  271. /*
  272. * g_pattern_match_string has been deprecated in Glib since 2.70 and
  273. * will complain about it if you try to use it. Fortunately the
  274. * signature of both functions is the same making it easy to work
  275. * around.
  276. */
  277. static inline
  278. gboolean g_pattern_spec_match_string_qemu(GPatternSpec *pspec,
  279. const gchar *string)
  280. {
  281. #if GLIB_CHECK_VERSION(2, 70, 0)
  282. return g_pattern_spec_match_string(pspec, string);
  283. #else
  284. return g_pattern_match_string(pspec, string);
  285. #endif
  286. };
  287. #define g_pattern_spec_match_string(p, s) g_pattern_spec_match_string_qemu(p, s)
  288. static GPtrArray *registers_init(int vcpu_index)
  289. {
  290. g_autoptr(GPtrArray) registers = g_ptr_array_new();
  291. g_autoptr(GArray) reg_list = qemu_plugin_get_registers();
  292. if (rmatches && reg_list->len) {
  293. /*
  294. * Go through each register in the complete list and
  295. * see if we want to track it.
  296. */
  297. for (int r = 0; r < reg_list->len; r++) {
  298. qemu_plugin_reg_descriptor *rd = &g_array_index(
  299. reg_list, qemu_plugin_reg_descriptor, r);
  300. for (int p = 0; p < rmatches->len; p++) {
  301. g_autoptr(GPatternSpec) pat = g_pattern_spec_new(rmatches->pdata[p]);
  302. g_autofree gchar *rd_lower = g_utf8_strdown(rd->name, -1);
  303. if (g_pattern_spec_match_string(pat, rd->name) ||
  304. g_pattern_spec_match_string(pat, rd_lower)) {
  305. Register *reg = init_vcpu_register(rd);
  306. g_ptr_array_add(registers, reg);
  307. /* we need a list of regnames at TB translation time */
  308. if (disas_assist) {
  309. g_mutex_lock(&add_reg_name_lock);
  310. if (!g_ptr_array_find(all_reg_names, reg->name, NULL)) {
  311. g_ptr_array_add(all_reg_names, (gpointer)reg->name);
  312. }
  313. g_mutex_unlock(&add_reg_name_lock);
  314. }
  315. }
  316. }
  317. }
  318. }
  319. return registers->len ? g_steal_pointer(&registers) : NULL;
  320. }
  321. /*
  322. * Initialise a new vcpu/thread with:
  323. * - last_exec tracking data
  324. * - list of tracked registers
  325. * - initial value of registers
  326. *
  327. * As we could have multiple threads trying to do this we need to
  328. * serialise the expansion under a lock.
  329. */
  330. static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index)
  331. {
  332. CPU *c;
  333. g_rw_lock_writer_lock(&expand_array_lock);
  334. if (vcpu_index >= cpus->len) {
  335. g_array_set_size(cpus, vcpu_index + 1);
  336. }
  337. g_rw_lock_writer_unlock(&expand_array_lock);
  338. c = get_cpu(vcpu_index);
  339. c->last_exec = g_string_new(NULL);
  340. c->registers = registers_init(vcpu_index);
  341. }
  342. /**
  343. * On plugin exit, print last instruction in cache
  344. */
  345. static void plugin_exit(qemu_plugin_id_t id, void *p)
  346. {
  347. guint i;
  348. g_rw_lock_reader_lock(&expand_array_lock);
  349. for (i = 0; i < cpus->len; i++) {
  350. CPU *c = get_cpu(i);
  351. if (c->last_exec && c->last_exec->str) {
  352. qemu_plugin_outs(c->last_exec->str);
  353. qemu_plugin_outs("\n");
  354. }
  355. }
  356. g_rw_lock_reader_unlock(&expand_array_lock);
  357. }
  358. /* Add a match to the array of matches */
  359. static void parse_insn_match(char *match)
  360. {
  361. if (!imatches) {
  362. imatches = g_ptr_array_new();
  363. }
  364. g_ptr_array_add(imatches, g_strdup(match));
  365. }
  366. static void parse_vaddr_match(char *match)
  367. {
  368. uint64_t v = g_ascii_strtoull(match, NULL, 16);
  369. if (!amatches) {
  370. amatches = g_array_new(false, true, sizeof(uint64_t));
  371. }
  372. g_array_append_val(amatches, v);
  373. }
  374. /*
  375. * We have to wait until vCPUs are started before we can check the
  376. * patterns find anything.
  377. */
  378. static void add_regpat(char *regpat)
  379. {
  380. if (!rmatches) {
  381. rmatches = g_ptr_array_new();
  382. }
  383. g_ptr_array_add(rmatches, g_strdup(regpat));
  384. }
  385. /**
  386. * Install the plugin
  387. */
  388. QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
  389. const qemu_info_t *info, int argc,
  390. char **argv)
  391. {
  392. /*
  393. * Initialize dynamic array to cache vCPU instruction. In user mode
  394. * we don't know the size before emulation.
  395. */
  396. cpus = g_array_sized_new(true, true, sizeof(CPU),
  397. info->system_emulation ? info->system.max_vcpus : 1);
  398. for (int i = 0; i < argc; i++) {
  399. char *opt = argv[i];
  400. g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
  401. if (g_strcmp0(tokens[0], "ifilter") == 0) {
  402. parse_insn_match(tokens[1]);
  403. } else if (g_strcmp0(tokens[0], "afilter") == 0) {
  404. parse_vaddr_match(tokens[1]);
  405. } else if (g_strcmp0(tokens[0], "reg") == 0) {
  406. add_regpat(tokens[1]);
  407. } else if (g_strcmp0(tokens[0], "rdisas") == 0) {
  408. if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &disas_assist)) {
  409. fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
  410. return -1;
  411. }
  412. all_reg_names = g_ptr_array_new();
  413. } else {
  414. fprintf(stderr, "option parsing failed: %s\n", opt);
  415. return -1;
  416. }
  417. }
  418. /* Register init, translation block and exit callbacks */
  419. qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
  420. qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
  421. qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
  422. return 0;
  423. }