From: Arkadiy <arkaisp2021@gmail.com>
To: qemu-devel@nongnu.org
Cc: NDNF <arkaisp2021@gmail.com>,
Ivanov Arkady <arkadiy.ivanov@ispras.ru>,
alex.bennee@linaro.org, pavel.dovgaluk@ispras.ru
Subject: [PATCH] contrib/plugins: add a drcov plugin
Date: Mon, 11 Oct 2021 14:11:30 +0300 [thread overview]
Message-ID: <20211011111130.170178-1-arkaisp2021@gmail.com> (raw)
From: NDNF <arkaisp2021@gmail.com>
This patch adds the ability to generate files in drcov format.
Primary goal this script is to have coverage
logfiles thatwork in Lighthouse.
Problems:
- The path to the executable file is not specified.
- base, end, entry take incorrect values.
(Lighthouse + IDA Pro anyway work).
Signed-off-by: Ivanov Arkady <arkadiy.ivanov@ispras.ru>
---
contrib/plugins/Makefile | 1 +
contrib/plugins/drcov.c | 112 +++++++++++++++++++++++++++++++++++++++
2 files changed, 113 insertions(+)
create mode 100644 contrib/plugins/drcov.c
diff --git a/contrib/plugins/Makefile b/contrib/plugins/Makefile
index 7801b08b0d..0a681efeec 100644
--- a/contrib/plugins/Makefile
+++ b/contrib/plugins/Makefile
@@ -17,6 +17,7 @@ NAMES += hotblocks
NAMES += hotpages
NAMES += howvec
NAMES += lockstep
+NAMES += drcov
SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES)))
diff --git a/contrib/plugins/drcov.c b/contrib/plugins/drcov.c
new file mode 100644
index 0000000000..d6a7d131c0
--- /dev/null
+++ b/contrib/plugins/drcov.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2021, Ivanov Arkady <arkadiy.ivanov@ispras.ru>
+ *
+ * Drcov - a DynamoRIO-based tool that collects coverage information
+ * from a binary. Primary goal this script is to have coverage log
+ * files that work in Lighthouse.
+ *
+ * License: GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include <inttypes.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <glib.h>
+
+#include <qemu-plugin.h>
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
+
+static char header[] = "DRCOV VERSION: 2\n"
+ "DRCOV FLAVOR: drcov-64\n"
+ "Module Table: version 2, count 1\n"
+ "Columns: id, base, end, entry, path\n";
+
+static FILE *fp;
+
+typedef struct {
+ uint32_t start;
+ uint16_t size;
+ uint16_t mod_id;
+} bb_entry_t;
+
+static GSList *bbs;
+
+static void printfHeader()
+{
+ g_autoptr(GString) head = g_string_new("");
+ g_string_append(head, header);
+ g_string_append_printf(head, "0, 0x%x, 0x%x, 0x%x, %s\n",
+ 0, 0xffffffff, 0, "path");
+ g_string_append_printf(head, "BB Table: %d bbs\n", g_slist_length(bbs));
+ fwrite(head->str, sizeof(char), head->len, fp);
+}
+
+static void printfCharArray32(uint32_t data)
+{
+ const uint8_t *bytes = (const uint8_t *)(&data);
+ fwrite(bytes, sizeof(char), sizeof(data), fp);
+}
+
+static void printfCharArray16(uint16_t data)
+{
+ const uint8_t *bytes = (const uint8_t *)(&data);
+ fwrite(bytes, sizeof(char), sizeof(data), fp);
+}
+
+
+static void printf_el(gpointer data, gpointer user_data)
+{
+ bb_entry_t *bb = (bb_entry_t *)data;
+ printfCharArray32(bb->start);
+ printfCharArray16(bb->size);
+ printfCharArray16(bb->mod_id);
+}
+
+static void plugin_exit(qemu_plugin_id_t id, void *p)
+{
+ /* Print function */
+ printfHeader();
+ g_slist_foreach(bbs, printf_el, NULL);
+
+ /* Clear */
+ g_slist_free_full(bbs, &g_free);
+ fclose(fp);
+}
+
+static void plugin_init(void)
+{
+ fp = fopen("file.drcov.trace", "wb");
+}
+
+static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
+{
+ bb_entry_t *bb = g_new0(bb_entry_t, 1);
+ uint64_t pc = qemu_plugin_tb_vaddr(tb);
+
+ size_t n = qemu_plugin_tb_n_insns(tb);
+ for (int i = 0; i < n; i++) {
+ bb->size += qemu_plugin_insn_size(qemu_plugin_tb_get_insn(tb, i));
+ }
+
+ bb->start = pc;
+ bb->mod_id = 0;
+ bbs = g_slist_append(bbs, bb);
+
+}
+
+QEMU_PLUGIN_EXPORT
+int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
+ int argc, char **argv)
+{
+ plugin_init();
+
+ qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
+ qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
+ return 0;
+}
--
2.25.1
next reply other threads:[~2021-10-11 11:13 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-11 11:11 Arkadiy [this message]
2021-10-12 10:36 ` [PATCH] contrib/plugins: add a drcov plugin Alex Bennée
2021-10-13 8:55 ` arkadiy.ivanov
2021-10-13 9:13 ` Alex Bennée
2021-10-15 10:36 ` arkadiy.ivanov
2021-10-15 13:29 ` Alex Bennée
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211011111130.170178-1-arkaisp2021@gmail.com \
--to=arkaisp2021@gmail.com \
--cc=alex.bennee@linaro.org \
--cc=arkadiy.ivanov@ispras.ru \
--cc=pavel.dovgaluk@ispras.ru \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).