insn.c (2502B)
1/* 2 * Copyright (C) 2018, Emilio G. Cota <cota@braap.org> 3 * 4 * License: GNU GPL, version 2 or later. 5 * See the COPYING file in the top-level directory. 6 */ 7#include <inttypes.h> 8#include <assert.h> 9#include <stdlib.h> 10#include <string.h> 11#include <unistd.h> 12#include <stdio.h> 13#include <glib.h> 14 15#include <qemu-plugin.h> 16 17QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION; 18 19static uint64_t insn_count; 20static bool do_inline; 21 22static void vcpu_insn_exec_before(unsigned int cpu_index, void *udata) 23{ 24 static uint64_t last_pc; 25 uint64_t this_pc = GPOINTER_TO_UINT(udata); 26 if (this_pc == last_pc) { 27 g_autofree gchar *out = g_strdup_printf("detected repeat execution @ 0x%" 28 PRIx64 "\n", this_pc); 29 qemu_plugin_outs(out); 30 } 31 last_pc = this_pc; 32 insn_count++; 33} 34 35static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) 36{ 37 size_t n = qemu_plugin_tb_n_insns(tb); 38 size_t i; 39 40 for (i = 0; i < n; i++) { 41 struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i); 42 43 if (do_inline) { 44 qemu_plugin_register_vcpu_insn_exec_inline( 45 insn, QEMU_PLUGIN_INLINE_ADD_U64, &insn_count, 1); 46 } else { 47 uint64_t vaddr = qemu_plugin_insn_vaddr(insn); 48 qemu_plugin_register_vcpu_insn_exec_cb( 49 insn, vcpu_insn_exec_before, QEMU_PLUGIN_CB_NO_REGS, 50 GUINT_TO_POINTER(vaddr)); 51 } 52 } 53} 54 55static void plugin_exit(qemu_plugin_id_t id, void *p) 56{ 57 g_autofree gchar *out = g_strdup_printf("insns: %" PRIu64 "\n", insn_count); 58 qemu_plugin_outs(out); 59} 60 61QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, 62 const qemu_info_t *info, 63 int argc, char **argv) 64{ 65 for (int i = 0; i < argc; i++) { 66 char *opt = argv[i]; 67 g_autofree char **tokens = g_strsplit(opt, "=", 2); 68 if (g_strcmp0(tokens[0], "inline") == 0) { 69 if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) { 70 fprintf(stderr, "boolean argument parsing failed: %s\n", opt); 71 return -1; 72 } 73 } else { 74 fprintf(stderr, "option parsing failed: %s\n", opt); 75 return -1; 76 } 77 } 78 79 qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); 80 qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); 81 return 0; 82}