1227b45dcSIvanov Arkady /*
2227b45dcSIvanov Arkady * Copyright (C) 2021, Ivanov Arkady <arkadiy.ivanov@ispras.ru>
3227b45dcSIvanov Arkady *
4227b45dcSIvanov Arkady * Drcov - a DynamoRIO-based tool that collects coverage information
5227b45dcSIvanov Arkady * from a binary. Primary goal this script is to have coverage log
6227b45dcSIvanov Arkady * files that work in Lighthouse.
7227b45dcSIvanov Arkady *
8227b45dcSIvanov Arkady * License: GNU GPL, version 2 or later.
9227b45dcSIvanov Arkady * See the COPYING file in the top-level directory.
10227b45dcSIvanov Arkady */
11227b45dcSIvanov Arkady
12227b45dcSIvanov Arkady #include <inttypes.h>
13227b45dcSIvanov Arkady #include <assert.h>
14227b45dcSIvanov Arkady #include <stdlib.h>
15227b45dcSIvanov Arkady #include <inttypes.h>
16227b45dcSIvanov Arkady #include <string.h>
17227b45dcSIvanov Arkady #include <unistd.h>
18227b45dcSIvanov Arkady #include <stdio.h>
19227b45dcSIvanov Arkady #include <glib.h>
20227b45dcSIvanov Arkady
21227b45dcSIvanov Arkady #include <qemu-plugin.h>
22227b45dcSIvanov Arkady
23227b45dcSIvanov Arkady QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
24227b45dcSIvanov Arkady
25227b45dcSIvanov Arkady static char header[] = "DRCOV VERSION: 2\n"
26227b45dcSIvanov Arkady "DRCOV FLAVOR: drcov-64\n"
27227b45dcSIvanov Arkady "Module Table: version 2, count 1\n"
28227b45dcSIvanov Arkady "Columns: id, base, end, entry, path\n";
29227b45dcSIvanov Arkady
30227b45dcSIvanov Arkady static FILE *fp;
31227b45dcSIvanov Arkady static const char *file_name = "file.drcov.trace";
32227b45dcSIvanov Arkady static GMutex lock;
33227b45dcSIvanov Arkady
34227b45dcSIvanov Arkady typedef struct {
35227b45dcSIvanov Arkady uint32_t start;
36227b45dcSIvanov Arkady uint16_t size;
37227b45dcSIvanov Arkady uint16_t mod_id;
38227b45dcSIvanov Arkady bool exec;
39227b45dcSIvanov Arkady } bb_entry_t;
40227b45dcSIvanov Arkady
41227b45dcSIvanov Arkady /* Translated blocks */
42227b45dcSIvanov Arkady static GPtrArray *blocks;
43227b45dcSIvanov Arkady
printf_header(unsigned long count)44227b45dcSIvanov Arkady static void printf_header(unsigned long count)
45227b45dcSIvanov Arkady {
46227b45dcSIvanov Arkady fprintf(fp, "%s", header);
47227b45dcSIvanov Arkady const char *path = qemu_plugin_path_to_binary();
48227b45dcSIvanov Arkady uint64_t start_code = qemu_plugin_start_code();
49227b45dcSIvanov Arkady uint64_t end_code = qemu_plugin_end_code();
50227b45dcSIvanov Arkady uint64_t entry = qemu_plugin_entry_code();
51*3df17650SPhilippe Mathieu-Daudé fprintf(fp, "0, 0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRIx64 ", %s\n",
52227b45dcSIvanov Arkady start_code, end_code, entry, path);
53227b45dcSIvanov Arkady fprintf(fp, "BB Table: %ld bbs\n", count);
54227b45dcSIvanov Arkady }
55227b45dcSIvanov Arkady
printf_char_array32(uint32_t data)56227b45dcSIvanov Arkady static void printf_char_array32(uint32_t data)
57227b45dcSIvanov Arkady {
58227b45dcSIvanov Arkady const uint8_t *bytes = (const uint8_t *)(&data);
59227b45dcSIvanov Arkady fwrite(bytes, sizeof(char), sizeof(data), fp);
60227b45dcSIvanov Arkady }
61227b45dcSIvanov Arkady
printf_char_array16(uint16_t data)62227b45dcSIvanov Arkady static void printf_char_array16(uint16_t data)
63227b45dcSIvanov Arkady {
64227b45dcSIvanov Arkady const uint8_t *bytes = (const uint8_t *)(&data);
65227b45dcSIvanov Arkady fwrite(bytes, sizeof(char), sizeof(data), fp);
66227b45dcSIvanov Arkady }
67227b45dcSIvanov Arkady
68227b45dcSIvanov Arkady
printf_el(gpointer data,gpointer user_data)69227b45dcSIvanov Arkady static void printf_el(gpointer data, gpointer user_data)
70227b45dcSIvanov Arkady {
71227b45dcSIvanov Arkady bb_entry_t *bb = (bb_entry_t *)data;
72227b45dcSIvanov Arkady if (bb->exec) {
73227b45dcSIvanov Arkady printf_char_array32(bb->start);
74227b45dcSIvanov Arkady printf_char_array16(bb->size);
75227b45dcSIvanov Arkady printf_char_array16(bb->mod_id);
76227b45dcSIvanov Arkady }
77227b45dcSIvanov Arkady g_free(bb);
78227b45dcSIvanov Arkady }
79227b45dcSIvanov Arkady
count_block(gpointer data,gpointer user_data)80227b45dcSIvanov Arkady static void count_block(gpointer data, gpointer user_data)
81227b45dcSIvanov Arkady {
82227b45dcSIvanov Arkady unsigned long *count = (unsigned long *) user_data;
83227b45dcSIvanov Arkady bb_entry_t *bb = (bb_entry_t *)data;
84227b45dcSIvanov Arkady if (bb->exec) {
85227b45dcSIvanov Arkady *count = *count + 1;
86227b45dcSIvanov Arkady }
87227b45dcSIvanov Arkady }
88227b45dcSIvanov Arkady
plugin_exit(qemu_plugin_id_t id,void * p)89227b45dcSIvanov Arkady static void plugin_exit(qemu_plugin_id_t id, void *p)
90227b45dcSIvanov Arkady {
91227b45dcSIvanov Arkady unsigned long count = 0;
92227b45dcSIvanov Arkady g_mutex_lock(&lock);
93227b45dcSIvanov Arkady g_ptr_array_foreach(blocks, count_block, &count);
94227b45dcSIvanov Arkady
95227b45dcSIvanov Arkady /* Print function */
96227b45dcSIvanov Arkady printf_header(count);
97227b45dcSIvanov Arkady g_ptr_array_foreach(blocks, printf_el, NULL);
98227b45dcSIvanov Arkady
99227b45dcSIvanov Arkady /* Clear */
100227b45dcSIvanov Arkady g_ptr_array_free(blocks, true);
101227b45dcSIvanov Arkady
102227b45dcSIvanov Arkady fclose(fp);
103227b45dcSIvanov Arkady
104227b45dcSIvanov Arkady g_mutex_unlock(&lock);
105227b45dcSIvanov Arkady }
106227b45dcSIvanov Arkady
plugin_init(void)107227b45dcSIvanov Arkady static void plugin_init(void)
108227b45dcSIvanov Arkady {
109227b45dcSIvanov Arkady fp = fopen(file_name, "wb");
110227b45dcSIvanov Arkady blocks = g_ptr_array_sized_new(128);
111227b45dcSIvanov Arkady }
112227b45dcSIvanov Arkady
vcpu_tb_exec(unsigned int cpu_index,void * udata)113227b45dcSIvanov Arkady static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
114227b45dcSIvanov Arkady {
115227b45dcSIvanov Arkady bb_entry_t *bb = (bb_entry_t *) udata;
116227b45dcSIvanov Arkady
117227b45dcSIvanov Arkady g_mutex_lock(&lock);
118227b45dcSIvanov Arkady bb->exec = true;
119227b45dcSIvanov Arkady g_mutex_unlock(&lock);
120227b45dcSIvanov Arkady }
121227b45dcSIvanov Arkady
vcpu_tb_trans(qemu_plugin_id_t id,struct qemu_plugin_tb * tb)122227b45dcSIvanov Arkady static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
123227b45dcSIvanov Arkady {
124227b45dcSIvanov Arkady uint64_t pc = qemu_plugin_tb_vaddr(tb);
125227b45dcSIvanov Arkady size_t n = qemu_plugin_tb_n_insns(tb);
126227b45dcSIvanov Arkady
127227b45dcSIvanov Arkady g_mutex_lock(&lock);
128227b45dcSIvanov Arkady
129227b45dcSIvanov Arkady bb_entry_t *bb = g_new0(bb_entry_t, 1);
130227b45dcSIvanov Arkady for (int i = 0; i < n; i++) {
131227b45dcSIvanov Arkady bb->size += qemu_plugin_insn_size(qemu_plugin_tb_get_insn(tb, i));
132227b45dcSIvanov Arkady }
133227b45dcSIvanov Arkady
134227b45dcSIvanov Arkady bb->start = pc;
135227b45dcSIvanov Arkady bb->mod_id = 0;
136227b45dcSIvanov Arkady bb->exec = false;
137227b45dcSIvanov Arkady g_ptr_array_add(blocks, bb);
138227b45dcSIvanov Arkady
139227b45dcSIvanov Arkady g_mutex_unlock(&lock);
140227b45dcSIvanov Arkady qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec,
141227b45dcSIvanov Arkady QEMU_PLUGIN_CB_NO_REGS,
142227b45dcSIvanov Arkady (void *)bb);
143227b45dcSIvanov Arkady
144227b45dcSIvanov Arkady }
145227b45dcSIvanov Arkady
146227b45dcSIvanov Arkady QEMU_PLUGIN_EXPORT
qemu_plugin_install(qemu_plugin_id_t id,const qemu_info_t * info,int argc,char ** argv)147227b45dcSIvanov Arkady int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
148227b45dcSIvanov Arkady int argc, char **argv)
149227b45dcSIvanov Arkady {
150227b45dcSIvanov Arkady for (int i = 0; i < argc; i++) {
15140258741SAlex Bennée g_auto(GStrv) tokens = g_strsplit(argv[i], "=", 2);
152227b45dcSIvanov Arkady if (g_strcmp0(tokens[0], "filename") == 0) {
153227b45dcSIvanov Arkady file_name = g_strdup(tokens[1]);
154227b45dcSIvanov Arkady }
155227b45dcSIvanov Arkady }
156227b45dcSIvanov Arkady
157227b45dcSIvanov Arkady plugin_init();
158227b45dcSIvanov Arkady
159227b45dcSIvanov Arkady qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
160227b45dcSIvanov Arkady qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
161227b45dcSIvanov Arkady
162227b45dcSIvanov Arkady return 0;
163227b45dcSIvanov Arkady }
164