From patchwork Wed Feb 9 14:15:23 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: 541113 Delivered-To: patch@linaro.org Received: by 2002:ad5:420f:0:0:0:0:0 with SMTP id e15csp1442320imo; Wed, 9 Feb 2022 07:36:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJyM/52GoWk3DUdZP9xStarjqAbzr4nfRB6Sk2HJtk7vtisNZyBLJgzWEzyYhF0IPYfv6S3k X-Received: by 2002:a81:df0a:: with SMTP id c10mr2721228ywn.0.1644420992914; Wed, 09 Feb 2022 07:36:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644420992; cv=none; d=google.com; s=arc-20160816; b=wm6hysJDqfYoMX83gJGCSaJAXrqfaJWno3gfWEPxCmNk3LQZbYnPIl7uSr3d/S5e5z C+IkJEQVlDMdR/3KY2LWlGrX0fkQw55jZs9wDBNY8gdCdwZYE6Wl/k4vpEL++5yppLb6 NickBh9KBcP3J23U3GzxhysE04RZ2vw9W2YJB5GV5pyDcEt20Dy9t9FlXuo/jIHyYtnC zMwa/sMwbEcfH62Gln/iQYqUTGjMMr3lgrAtlosyT/XfoZbWNe42nyH1y2lF2UrLMD8M g5Th2P4QpTJZsol/trbrhPjoQ7waKiwMabKKJuWy7a3tciA/WT8VVp/J1jrtJqT9GOde 9V6A== 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=jZCQ7LtYjkNpZcIUcntr9yNzTTMtZKopN6QvGhfoSiw=; b=ycvbu7VVY9XiqGSIvkQIimpuUKZkdn4q3IB6+Sl3gj+3ERT1SQyo4QMPaeZ3I8EzpB xR2cSK5ymCpdvhdT+vBeyItfOrjUONgGKg8p/6D3dC12q3SNrVLaK5XHoiGN9DwqvtjN O2qJ1/OYRS3Ep5NwRIpiDxkdDnxNHP7HbfZQ1JpDmP1nX59arYe70drGeA37jq6E/R9N zAPc6lf908aDgbXmqij0PrCdm0rTx+h+mWGJdZ9YFKaw9iD4S3C9bs/Ht20Gr02+kDVW EzdWJxrr2jubuDwsDYVKrVamewH4/ow/yiktmQ32YfJcM7mNHUEulVvOAvmMAs6Iu30M iC9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="vYpQh95/"; 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 c62si12533889ybc.235.2022.02.09.07.36.32 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 09 Feb 2022 07:36:32 -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="vYpQh95/"; 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]:43688 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nHp1E-0003Sc-BQ for patch@linaro.org; Wed, 09 Feb 2022 10:36:32 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44470) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nHnlN-0002Zy-VQ for qemu-devel@nongnu.org; Wed, 09 Feb 2022 09:16:06 -0500 Received: from [2a00:1450:4864:20::630] (port=42533 helo=mail-ej1-x630.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nHnlJ-0007sf-3n for qemu-devel@nongnu.org; Wed, 09 Feb 2022 09:16:05 -0500 Received: by mail-ej1-x630.google.com with SMTP id m4so7693841ejb.9 for ; Wed, 09 Feb 2022 06:15:54 -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=jZCQ7LtYjkNpZcIUcntr9yNzTTMtZKopN6QvGhfoSiw=; b=vYpQh95/sMuzoS4QOqzwF4vDmS99XBMPp5ebTmumsXs03kgTsRXhAMvgumAgoxwZXV iNv4r6rb1b5YLFJ39l90SkVzfVZMESgEU6SxK+l8gzA8ZPRHxnO1eM/DIVXTUAI6Qxxg iw3cYBuDGlLD1baG83TRG3xl0/FgMZfiLSgD7+iFtLBt3On/7SdNZn9+mqpzMwzoLVIZ LTQH93imL1eEdAq68IjwXJ5vDzlDRsYAlJydSa+qVmjqHS0xgBTFTx+uo4PK5/iDPMRM EDDtbsR7JNIuQAS+tfNnLxzi21yWhyLq8tUYNJIS732yP4+rqTIP/xe8eV9Tkil14cge HvxQ== 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=jZCQ7LtYjkNpZcIUcntr9yNzTTMtZKopN6QvGhfoSiw=; b=SWWMyb/4Q16t6ZiAoTIyuaM0AoNAxDk0FkN6R8a4c09GVjSkF8yEcGpSPGmPqCBqFM yFTrCi7NrHpZyT6wrx/QZMgHZfWwIAl0HMPhdrNv0gKMNBBh6j/oiMSuqDwXSvvYNRr4 2tUPBqn22bRdN1zU7pMb4M2lTWFhmWQRobFmtOWIp2kyrgHpNIhwePHmRsYmSky++u4n /yaw6C/5rRYtdyt13mbAAwcXGZ//eCe2Jp+Q/y6M4kh+WmihnWoRdeVloisST0y/GQ4f +dxZdWEHmmEG+crbtp8rQ6kwjZqEVmvvxi+iYsRPMnPxKjL9gIWeoh+HKjCHHEWCL2rd cfXg== X-Gm-Message-State: AOAM530SCNN6OPnhFKpAsQLoRoWfP281VHZDu4fryJDYV1NLtoOiNUBV /w0Wgvc3Ii3iaQpBB+FZSwP/+g== X-Received: by 2002:a17:907:9620:: with SMTP id gb32mr2195826ejc.546.1644416153048; Wed, 09 Feb 2022 06:15:53 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id i24sm2043339edt.86.2022.02.09.06.15.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 06:15:48 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 531361FFCD; Wed, 9 Feb 2022 14:15:31 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Subject: [PULL 22/28] contrib/plugins: add a drcov plugin Date: Wed, 9 Feb 2022 14:15:23 +0000 Message-Id: <20220209141529.3418384-23-alex.bennee@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220209141529.3418384-1-alex.bennee@linaro.org> References: <20220209141529.3418384-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::630 (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::630; envelope-from=alex.bennee@linaro.org; helo=mail-ej1-x630.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: Ivanov Arkady , Mahmoud Mandour , =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-devel@nongnu.org, Alexandre Iooss 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: <20220204204335.1689602-23-alex.bennee@linaro.org> 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)))