From nobody Mon Sep 16 19:17:32 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1721314881; cv=none; d=zohomail.com; s=zohoarc; b=Qq0j4bZFvaOBEc8rDJw3aHO2FiAYbon07n6JhtUv1aonS1gWv/nGOZ6Rq3W/4OSH0CNN8LYO63KfErqkk4jMXAooJ3ad4KWThCo3sRwJdym/dm2RG+i0o3IWN+7HDGFZB0pbaCzOzahikU2tg/q3kdXaRewmX+xFFRoMAsb239U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721314881; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=CtgI2/bCtqOUCgIeNq1RIzqckcmuRDLDCMflU+8PpRw=; b=I9ppVQs6/tg2E7VeEr4WdeOa4So10r7uYLhdQg/MgZOatVymHMuXGnzJ/CNq85IsuQCCw5ScnrSyClRJzqnv9cm+C4hLox407j+z3KlLsTK7CaZHnbcPBm4N6Lu0beUTNr9f5gAkvtRH7E9hK9CQTZ5HHD5z6HZY0vcMOjajFrs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1721314881370111.37369079277892; Thu, 18 Jul 2024 08:01:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sUScC-00051o-AQ; Thu, 18 Jul 2024 11:00:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sUScB-000519-5H for qemu-devel@nongnu.org; Thu, 18 Jul 2024 11:00:15 -0400 Received: from mail-ed1-x52c.google.com ([2a00:1450:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sUSc8-00081l-Mm for qemu-devel@nongnu.org; Thu, 18 Jul 2024 11:00:14 -0400 Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-58ba3e38027so786128a12.1 for ; Thu, 18 Jul 2024 08:00:11 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a79bc800d6bsm565621966b.182.2024.07.18.08.00.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jul 2024 08:00:07 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id D7F9E5F863; Thu, 18 Jul 2024 16:00:02 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1721314810; x=1721919610; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=CtgI2/bCtqOUCgIeNq1RIzqckcmuRDLDCMflU+8PpRw=; b=bT48ftN661DxI2UvPKZQzoAiwwACLmGp6e37kPKSfgVyomQvE2ODWwXUQlwFYGGX7D lnqaZTJHifAr6mBQTjf/C3xv4o7zFkVcriviXOhEr47jj4PP+Fg3dhBwQiSU3hX6+mKF FcE08BSi56/IuCnp70xk3UOPgNSDkIXvKN4XNwPCpIYZhvachRUqa3DDielnIytYzPXQ 76BY+CkQ7ldDYiVFqILKo0BZsC0wfq8PgLccICngbZeOvqbLTdEOP3jV8vlc1RkMcwIW yc5cGJoCNErvmCzMcKIjuBp2xmTBL2TdumsenJrxRcgziycBQWW/NC6dE1h52BB3dewi niRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721314810; x=1721919610; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CtgI2/bCtqOUCgIeNq1RIzqckcmuRDLDCMflU+8PpRw=; b=qBRe0IT0vi73dC3FfGkyh57jvFi4H8XBOgj2tOMIFsQjPYNjsgG/WlR4dfse8r/4m2 0e4MeaQOZcts5KW4G03fRgyaDuhjXU7lHLMLAZkYXlb8txXtmW4D/Pmp6TuW87uUG3W2 xTjYrRzTHBPLd2mSUlQGknXNwymh+f4r1k/zl5mlune9QOcIbu+utdec9C6AiHHAs4lR g4vWO8pBSJ7fgjCl6RHKgzLAN9DLDGWKOrXzpIsS9H3xSwzHpZR9QT8XeySBvrHOkcs2 P/nsT6BvrjjEBoYNRyi8J6MJh6k1OTZqTtpddjGYWBRVYQhovbvdDsaEysODdF0M+DKP 5l4Q== X-Gm-Message-State: AOJu0Yz1qX9NeLPD1sM+94GwMkC0RKKItOk82bCVXGSIBoczWvmN3xlB KVZE8JNzXUG4v5aKU+tDuQYMJJKoowZPvvT3/Fq+UOA2lSMx92dAVoFp2R1tFYQ= X-Google-Smtp-Source: AGHT+IEXqS1JrmWELpOxUR9azXbHySWXGgfJeQ5S4VBTh7qTbbhHkBtNovqhsXg7IykmzGj3hEjuAA== X-Received: by 2002:a17:906:f18e:b0:a6f:ddb3:bf2b with SMTP id a640c23a62f3a-a7a013312b5mr418224266b.41.1721314809692; Thu, 18 Jul 2024 08:00:09 -0700 (PDT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Gustavo Romero , Pierrick Bouvier , Alexandre Iooss , Mahmoud Mandour Subject: [RFC PATCH v3] contrib/plugins: control flow plugin Date: Thu, 18 Jul 2024 15:59:58 +0100 Message-Id: <20240718145958.1315270-1-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::52c; envelope-from=alex.bennee@linaro.org; helo=mail-ed1-x52c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1721314882286116600 This is a simple control flow tracking plugin that uses the latest inline and conditional operations to detect and track control flow changes. It is currently an exercise at seeing how useful the changes are. Based-on: <20240312075428.244210-1-pierrick.bouvier@linaro.org> Cc: Gustavo Romero Cc: Pierrick Bouvier Signed-off-by: Alex Benn=C3=A9e Message-Id: <20240311153432.1395190-1-alex.bennee@linaro.org> --- v2 - only need a single call back - drop need for INSN_WIDTH - still don't understand the early exits v3 - move initial STORE ops to first instruction to avoid confusion with the conditional callback on the start - filter out non-branches before processing - fix off-by-one with accounting - display "sync fault" or "branch" instead of raw numbers --- contrib/plugins/cflow.c | 364 +++++++++++++++++++++++++++++++++++++++ contrib/plugins/Makefile | 1 + 2 files changed, 365 insertions(+) create mode 100644 contrib/plugins/cflow.c diff --git a/contrib/plugins/cflow.c b/contrib/plugins/cflow.c new file mode 100644 index 0000000000..e5b0d4bbbd --- /dev/null +++ b/contrib/plugins/cflow.c @@ -0,0 +1,364 @@ +/* + * Control Flow plugin + * + * This plugin will track changes to control flow and detect where + * instructions fault. + * + * Copyright (c) 2024 Linaro Ltd + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#include +#include +#include +#include +#include +#include + +#include + +QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; + +typedef enum { + SORT_HOTDEST, /* hottest branch */ + SORT_EARLY, /* most early exits */ + SORT_POPDEST, /* most destinations */ +} ReportType; + +ReportType report =3D SORT_HOTDEST; +int topn =3D 10; + +typedef struct { + uint64_t daddr; + uint64_t dcount; +} DestData; + +/* A node is an address where we can go to multiple places */ +typedef struct { + GMutex lock; + /* address of the branch point */ + uint64_t addr; + /* array of DestData */ + GArray *dests; + /* early exit/fault count */ + uint64_t early_exit; + /* jump destination count */ + uint64_t dest_count; + /* instruction data */ + char *insn_disas; + /* symbol? */ + const char *symbol; + /* times translated as last in block? */ + int last_count; + /* times translated in the middle of block? */ + int mid_count; +} NodeData; + +/* We use this to track the current execution state */ +typedef struct { + /* address of end of block */ + uint64_t end_block; + /* next pc after end of block */ + uint64_t pc_after_block; + /* address of last executed PC */ + uint64_t last_pc; +} VCPUScoreBoard; + +/* descriptors for accessing the above scoreboard */ +static qemu_plugin_u64 end_block; +static qemu_plugin_u64 pc_after_block; +static qemu_plugin_u64 last_pc; + + +static GMutex node_lock; +static GHashTable *nodes; +struct qemu_plugin_scoreboard *state; + +/* SORT_HOTDEST */ +static gint hottest(gconstpointer a, gconstpointer b) +{ + NodeData *na =3D (NodeData *) a; + NodeData *nb =3D (NodeData *) b; + + return na->dest_count > nb->dest_count ? -1 : + na->dest_count =3D=3D nb->dest_count ? 0 : 1; +} + +static gint early(gconstpointer a, gconstpointer b) +{ + NodeData *na =3D (NodeData *) a; + NodeData *nb =3D (NodeData *) b; + + return na->early_exit > nb->early_exit ? -1 : + na->early_exit =3D=3D nb->early_exit ? 0 : 1; +} + +static gint popular(gconstpointer a, gconstpointer b) +{ + NodeData *na =3D (NodeData *) a; + NodeData *nb =3D (NodeData *) b; + + return na->dests->len > nb->dests->len ? -1 : + na->dests->len =3D=3D nb->dests->len ? 0 : 1; +} + +/* Filter out non-branches - returns true to remove entry */ +static gboolean filter_non_branches(gpointer key, gpointer value, gpointer= user_data) +{ + NodeData *node =3D (NodeData *) value; + + return node->dest_count =3D=3D 0; +} + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + g_autoptr(GString) result =3D g_string_new("collected "); + GList *data; + GCompareFunc sort =3D &hottest; + int n =3D 0; + + g_mutex_lock(&node_lock); + g_string_append_printf(result, "%d control flow nodes in the hash tabl= e\n", + g_hash_table_size(nodes)); + + /* remove all nodes that didn't branch */ + g_hash_table_foreach_remove(nodes, filter_non_branches, NULL); + + data =3D g_hash_table_get_values(nodes); + + switch (report) { + case SORT_HOTDEST: + sort =3D &hottest; + break; + case SORT_EARLY: + sort =3D &early; + break; + case SORT_POPDEST: + sort =3D &popular; + break; + } + + data =3D g_list_sort(data, sort); + + for (GList *l =3D data; + l !=3D NULL && n < topn; + l =3D l->next, n++) { + NodeData *n =3D l->data; + const char *type =3D n->mid_count ? "sync fault" : "branch"; + g_string_append_printf(result, " addr: 0x%"PRIx64 " %s: %s (%s)\n= ", + n->addr, n->symbol, n->insn_disas, type); + if (n->early_exit) { + g_string_append_printf(result, " early exits %"PRId64"\n", + n->early_exit); + } + g_string_append_printf(result, " branches %"PRId64"\n", + n->dest_count); + for (int j =3D 0; j < n->dests->len; j++ ) { + DestData *dd =3D &g_array_index(n->dests, DestData, j); + g_string_append_printf(result, " to 0x%"PRIx64" (%"PRId64= ")\n", + dd->daddr, dd->dcount); + } + } + + qemu_plugin_outs(result->str); + + g_mutex_unlock(&node_lock); +} + +static void plugin_init(void) +{ + g_mutex_init(&node_lock); + nodes =3D g_hash_table_new(NULL, g_direct_equal); + state =3D qemu_plugin_scoreboard_new(sizeof(VCPUScoreBoard)); + + /* score board declarations */ + end_block =3D qemu_plugin_scoreboard_u64_in_struct(state, VCPUScoreBoa= rd, end_block); + pc_after_block =3D qemu_plugin_scoreboard_u64_in_struct(state, VCPUSco= reBoard, pc_after_block); + last_pc =3D qemu_plugin_scoreboard_u64_in_struct(state, VCPUScoreBoard= , last_pc); +} + +static NodeData *create_node(uint64_t addr) +{ + NodeData *node =3D g_new0(NodeData, 1); + g_mutex_init(&node->lock); + node->addr =3D addr; + node->dests =3D g_array_new(true, true, sizeof(DestData)); + return node; +} + +static NodeData *fetch_node(uint64_t addr, bool create_if_not_found) +{ + NodeData *node =3D NULL; + + g_mutex_lock(&node_lock); + node =3D (NodeData *) g_hash_table_lookup(nodes, (gconstpointer) addr); + if (!node && create_if_not_found) { + node =3D create_node(addr); + g_hash_table_insert(nodes, (gpointer) addr, (gpointer) node); + } + g_mutex_unlock(&node_lock); + return node; +} + +/* + * Called when we detect a non-linear execution (pc !=3D + * pc_after_block). This could be due to a fault causing some sort of + * exit exception (if last_pc !=3D block_end) or just a taken branch. + */ +static void vcpu_tb_branched_exec(unsigned int cpu_index, void *udata) +{ + uint64_t lpc =3D qemu_plugin_u64_get(last_pc, cpu_index); + uint64_t ebpc =3D qemu_plugin_u64_get(end_block, cpu_index); + uint64_t npc =3D qemu_plugin_u64_get(pc_after_block, cpu_index); + uint64_t pc =3D GPOINTER_TO_UINT(udata); + + /* return early for address 0 */ + if (!lpc) { + return; + } + + NodeData *node =3D fetch_node(lpc, true); + DestData *data =3D NULL; + bool early_exit =3D (lpc !=3D ebpc); + GArray *dests; + + /* the condition should never hit */ + g_assert(pc !=3D npc); + + g_mutex_lock(&node->lock); + + if (early_exit) { + fprintf(stderr, "%s: pc=3D%"PRIx64", epbc=3D%"PRIx64 + " npc=3D%"PRIx64", lpc=3D%"PRIx64", \n", + __func__, pc, ebpc, npc, lpc); + node->early_exit++; + if (!node->mid_count) { + /* count now as we've only just allocated */ + node->mid_count++; + } + } + + dests =3D node->dests; + for (int i =3D 0; i < dests->len; i++) { + if (g_array_index(dests, DestData, i).daddr =3D=3D pc) { + data =3D &g_array_index(dests, DestData, i); + } + } + + /* we've never seen this before, allocate a new entry */ + if (!data) { + DestData new_entry =3D { .daddr =3D pc }; + g_array_append_val(dests, new_entry); + data =3D &g_array_index(dests, DestData, dests->len - 1); + g_assert(data->daddr =3D=3D pc); + } + + data->dcount++; + node->dest_count++; + + g_mutex_unlock(&node->lock); +} + +/* + * At the start of each block we need to resolve two things: + * + * - is last_pc =3D=3D block_end, if not we had an early exit + * - is start of block last_pc + insn width, if not we jumped + * + * Once those are dealt with we can instrument the rest of the + * instructions for their execution. + * + */ +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + uint64_t pc =3D qemu_plugin_tb_vaddr(tb); + size_t insns =3D qemu_plugin_tb_n_insns(tb); + struct qemu_plugin_insn *first_insn =3D qemu_plugin_tb_get_insn(tb, 0); + struct qemu_plugin_insn *last_insn =3D qemu_plugin_tb_get_insn(tb, ins= ns - 1); + + /* + * check if we are executing linearly after the last block. We can + * handle both early block exits and normal branches in the + * callback if we hit it. + */ + gpointer udata =3D GUINT_TO_POINTER(pc); + qemu_plugin_register_vcpu_tb_exec_cond_cb( + tb, vcpu_tb_branched_exec, QEMU_PLUGIN_CB_NO_REGS, + QEMU_PLUGIN_COND_NE, pc_after_block, pc, udata); + + /* + * Now we can set start/end for this block so the next block can + * check where we are at. Do this on the first instruction and not + * the TB so we don't get mixed up with above. + */ + qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(first_insn, + QEMU_PLUGIN_INLINE_S= TORE_U64, + end_block, qemu_plug= in_insn_vaddr(last_insn)); + qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(first_insn, + QEMU_PLUGIN_INLINE_S= TORE_U64, + pc_after_block, + qemu_plugin_insn_vad= dr(last_insn) + + qemu_plugin_insn_siz= e(last_insn)); + + for (int idx =3D 0; idx < qemu_plugin_tb_n_insns(tb); ++idx) { + struct qemu_plugin_insn *insn =3D qemu_plugin_tb_get_insn(tb, idx); + uint64_t ipc =3D qemu_plugin_insn_vaddr(insn); + /* + * If this is a potential branch point check if we could grab + * the disassembly for it. If it is the last instruction + * always create an entry. + */ + NodeData *node =3D fetch_node(ipc, last_insn); + if (node) { + g_mutex_lock(&node->lock); + if (!node->insn_disas) { + node->insn_disas =3D qemu_plugin_insn_disas(insn); + } + if (!node->symbol) { + node->symbol =3D qemu_plugin_insn_symbol(insn); + } + if (last_insn =3D=3D insn) { + node->last_count++; + } else { + node->mid_count++; + } + g_mutex_unlock(&node->lock); + } + + /* Store the PC of what we are about to execute */ + qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(insn, + QEMU_PLUGIN_IN= LINE_STORE_U64, + last_pc, ipc); + } +} + +QEMU_PLUGIN_EXPORT +int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, + int argc, char **argv) +{ + for (int i =3D 0; i < argc; i++) { + char *opt =3D argv[i]; + g_auto(GStrv) tokens =3D g_strsplit(opt, "=3D", 2); + if (g_strcmp0(tokens[0], "sort") =3D=3D 0) { + if (g_strcmp0(tokens[1], "hottest") =3D=3D 0) { + report =3D SORT_HOTDEST; + } else if (g_strcmp0(tokens[1], "early") =3D=3D 0) { + report =3D SORT_EARLY; + } else if (g_strcmp0(tokens[1], "popular") =3D=3D 0) { + report =3D SORT_POPDEST; + } else { + fprintf(stderr, "failed to parse: %s\n", tokens[1]); + return -1; + } + } else { + fprintf(stderr, "option parsing failed: %s\n", opt); + return -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 98a89d5c40..ea81fde2b5 100644 --- a/contrib/plugins/Makefile +++ b/contrib/plugins/Makefile @@ -29,6 +29,7 @@ NAMES +=3D cache NAMES +=3D drcov NAMES +=3D ips NAMES +=3D stoptrigger +NAMES +=3D cflow =20 ifeq ($(CONFIG_WIN32),y) SO_SUFFIX :=3D .dll --=20 2.39.2