From patchwork Tue Feb 1 18:20:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 538972 Delivered-To: patch@linaro.org Received: by 2002:ac0:f7d2:0:0:0:0:0 with SMTP id i18csp1114646imr; Tue, 1 Feb 2022 14:23:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJwP3aAXVyAqYKFGrZP/PTpOu7VVFVPaXnKC/amfzbL9TNdHdEDdGKx0F6ssRF8l1Y26glGf X-Received: by 2002:a25:d6c4:: with SMTP id n187mr36377115ybg.279.1643754182547; Tue, 01 Feb 2022 14:23:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643754182; cv=none; d=google.com; s=arc-20160816; b=YPAdyCRs2AqFJuaO/YLWn3HMTcUYQCHvBNRQmUijewtPUWNEMn7F7iS00/iIVjfd1J 9TTZO0W3qllZIIgaGJR9c/gMJ/pnxPgQq7dda+gWBdTYLcUG0Cd/3vspfkMT3eOVnSq+ amdQL9+W7Vjr6Q8oMadAslkWaai9AKKJGnCoBS7MixH37pzHCSCRANSHH9too3OuxhU7 RIKPqYeTLbx+Mf9SFWQAFCvnRhsZlB4mgvM1icxOEBviAv1nSXtOVAAGinAzFRQ0VwS8 6JFiC8F1O+K1agL2HxdoOVWa1v/CGi3FvC5BfzzZk85In9Zpba32cr4NZYgb2MyXZqeb 2ohg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=WxxlAQjFne43H7oMKEwUdOkD0mNmEgn9CffHZbyJIkk=; b=NW/FuOh47sF2NSyrWrHvTyAGWKI/ya6o9Nfnej8FoixwmDZ4MGp/EN+C+3+8srwyC0 7MFBJQJ+hsB1H+Ftx6tF/HlX8I2KbfrT3km2wRta8t8ddbfrXi+miU/gsDU2U6czBBq3 lt/eWlO0wpflc0sYbs+95ucFM2axa83+CFH8I3jVPEFPfOHuFwWgPqmR0Q8Bmka1zw3X SuRD6OaSgHMC27LIs0TjNEEKbr8+ozj5OYLAitsB9HhHutQfSsIir5LxORMVdeO2lvwA DRNh7EZ5yc7zsGR1kV7DmQHr0E3etMMze2F2wJtvZ4OmwjcWaWzGRpp9FsWg08Bkin6M R3nA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=nd8QUAMn; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id b2si36983234yba.233.2022.02.01.14.23.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Feb 2022 14:23:02 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=nd8QUAMn; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:50642 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nF1YD-0005RF-UA for patch@linaro.org; Tue, 01 Feb 2022 17:23:01 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46118) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nExtw-0002hz-Pe for qemu-devel@nongnu.org; Tue, 01 Feb 2022 13:29:13 -0500 Received: from [2a00:1450:4864:20::329] (port=46663 helo=mail-wm1-x329.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nExtv-0001gm-0l for qemu-devel@nongnu.org; Tue, 01 Feb 2022 13:29:12 -0500 Received: by mail-wm1-x329.google.com with SMTP id m13-20020a05600c3b0d00b00353951c3f62so2117602wms.5 for ; Tue, 01 Feb 2022 10:29:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WxxlAQjFne43H7oMKEwUdOkD0mNmEgn9CffHZbyJIkk=; b=nd8QUAMnm+0zrxX0OSCClF36hE6wFpaUH1E69YhCdbUc3rEQcnFFgL12cgMmOv8VYB +y+hZ9zzazLr0dR6V4QRRWNEAFpdvlvAJBdnPW3codauR5K/pB7stLk56dyIaJWcH1ML uKL9rwL+c0ws9vARtlaYSzmfelA2FdNbc+AH37RoEh848cgboCx/oiXLTXTpNIPWvfM3 XdOxDBguOSrjaMjn7ucBNQg4yzbvp/nouS+giQfaOU0JZM1ppyW8c587taLaj9nsnIHg rinDToXFqvlGf2l4P+Z8ko8b2PyYGU/HazEzJHyjwEe7rElYJGxEOeKJnpEUoPGrd/Xr Rq8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WxxlAQjFne43H7oMKEwUdOkD0mNmEgn9CffHZbyJIkk=; b=tiDoqx59+8wMiwvwoqHs3zUeH5NspMOrzvAu/E6yD6wHXZQZpbb88AYB91ooUn7YET Ginbs+JkGbYbvCFdQb0QQNU2c4jC2bP3dYhO8Cxshn0J+3ffkwWKVGiFQ/AkNTNBt7SV GsMq08QAGG7Z9iulTzr8PxSGSj55JZTJzy/uart0dAQThcfGX2mZf84pFO0WIlmmOW81 i8VdXboFI3qnaTrt224bObqnFvifoAm6uDxzkPe5K57NGnOi7ikMe8Cw2c8zlbZgrOWB upgUnyW3T6OoVJffgclFwMsQfptZxiJgnLiUbMGOvA+iDujQ5MCJxfL1VBCzFuuK0viQ umxw== X-Gm-Message-State: AOAM531080XqRzIb+KNDMiM93xk+3QBhuIlgVcHYkffSvgB2nFzyrAxi H+dNfr1T83P0clSQQSE0NCYpmw== X-Received: by 2002:a05:600c:4f4e:: with SMTP id m14mr2871591wmq.155.1643740149666; Tue, 01 Feb 2022 10:29:09 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id m187sm2638932wme.25.2022.02.01.10.29.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 10:29:05 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id C77521FFCC; Tue, 1 Feb 2022 18:20:52 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v2 21/25] contrib/plugins: add a drcov plugin Date: Tue, 1 Feb 2022 18:20:46 +0000 Message-Id: <20220201182050.15087-22-alex.bennee@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220201182050.15087-1-alex.bennee@linaro.org> References: <20220201182050.15087-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::329 (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::329; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x329.google.com X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, PDS_HP_HELO_NORDNS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, berrange@redhat.com, stefanha@redhat.com, crosa@redhat.com, aaron@os.amperecomputing.com, robhenry@microsoft.com, f4bug@amsat.org, mahmoudabdalghany@outlook.com, minyihh@uci.edu, cota@braap.org, Ivanov Arkady , Luke.Craig@ll.mit.edu, pbonzini@redhat.com, kuhn.chenqun@huawei.com, ma.mandourr@gmail.com, Alexandre Iooss , =?utf-8?q?Alex_?= =?utf-8?q?Benn=C3=A9e?= , aurelien@aurel32.net Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Ivanov Arkady This patch adds the ability to generate files in drcov format. Primary goal this script is to have coverage logfiles thatwork in Lighthouse. Signed-off-by: Ivanov Arkady Message-Id: <163491884553.304355.13246023070235438959.stgit@pc-System-Product-Name> [AJB: use g_ptr_array instead of slist] Signed-off-by: Alex Bennée Message-Id: <20220124201608.604599-19-alex.bennee@linaro.org> --- contrib/plugins/drcov.c | 163 +++++++++++++++++++++++++++++++++++++++ contrib/plugins/Makefile | 1 + 2 files changed, 164 insertions(+) create mode 100644 contrib/plugins/drcov.c diff --git a/contrib/plugins/drcov.c b/contrib/plugins/drcov.c new file mode 100644 index 0000000000..b4a855adaf --- /dev/null +++ b/contrib/plugins/drcov.c @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2021, Ivanov Arkady + * + * 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 +#include +#include +#include +#include +#include +#include +#include + +#include + +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; +static const char *file_name = "file.drcov.trace"; +static GMutex lock; + +typedef struct { + uint32_t start; + uint16_t size; + uint16_t mod_id; + bool exec; +} bb_entry_t; + +/* Translated blocks */ +static GPtrArray *blocks; + +static void printf_header(unsigned long count) +{ + fprintf(fp, "%s", header); + const char *path = qemu_plugin_path_to_binary(); + uint64_t start_code = qemu_plugin_start_code(); + uint64_t end_code = qemu_plugin_end_code(); + uint64_t entry = qemu_plugin_entry_code(); + fprintf(fp, "0, 0x%lx, 0x%lx, 0x%lx, %s\n", + start_code, end_code, entry, path); + fprintf(fp, "BB Table: %ld bbs\n", count); +} + +static void printf_char_array32(uint32_t data) +{ + const uint8_t *bytes = (const uint8_t *)(&data); + fwrite(bytes, sizeof(char), sizeof(data), fp); +} + +static void printf_char_array16(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; + if (bb->exec) { + printf_char_array32(bb->start); + printf_char_array16(bb->size); + printf_char_array16(bb->mod_id); + } + g_free(bb); +} + +static void count_block(gpointer data, gpointer user_data) +{ + unsigned long *count = (unsigned long *) user_data; + bb_entry_t *bb = (bb_entry_t *)data; + if (bb->exec) { + *count = *count + 1; + } +} + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + unsigned long count = 0; + g_mutex_lock(&lock); + g_ptr_array_foreach(blocks, count_block, &count); + + /* Print function */ + printf_header(count); + g_ptr_array_foreach(blocks, printf_el, NULL); + + /* Clear */ + g_ptr_array_free(blocks, true); + + fclose(fp); + + g_mutex_unlock(&lock); +} + +static void plugin_init(void) +{ + fp = fopen(file_name, "wb"); + blocks = g_ptr_array_sized_new(128); +} + +static void vcpu_tb_exec(unsigned int cpu_index, void *udata) +{ + bb_entry_t *bb = (bb_entry_t *) udata; + + g_mutex_lock(&lock); + bb->exec = true; + g_mutex_unlock(&lock); +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + uint64_t pc = qemu_plugin_tb_vaddr(tb); + size_t n = qemu_plugin_tb_n_insns(tb); + + g_mutex_lock(&lock); + + bb_entry_t *bb = g_new0(bb_entry_t, 1); + 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; + bb->exec = false; + g_ptr_array_add(blocks, bb); + + g_mutex_unlock(&lock); + qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec, + QEMU_PLUGIN_CB_NO_REGS, + (void *)bb); + +} + +QEMU_PLUGIN_EXPORT +int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, + int argc, char **argv) +{ + for (int i = 0; i < argc; i++) { + g_autofree char **tokens = g_strsplit(argv[i], "=", 2); + if (g_strcmp0(tokens[0], "filename") == 0) { + file_name = g_strdup(tokens[1]); + } + } + + plugin_init(); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + + return 0; +} diff --git a/contrib/plugins/Makefile b/contrib/plugins/Makefile index 54ac5ccd9f..df3499f4f2 100644 --- a/contrib/plugins/Makefile +++ b/contrib/plugins/Makefile @@ -20,6 +20,7 @@ NAMES += howvec NAMES += lockstep NAMES += hwprofile NAMES += cache +NAMES += drcov SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES)))