From patchwork Tue Jul 4 12:35:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Felix Willgerodt X-Patchwork-Id: 72071 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 936A43853D02 for ; Tue, 4 Jul 2023 12:37:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 936A43853D02 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1688474274; bh=5EQHmZlq1LQX/YIpH0Zmt9SNcs/IKUu2qkXkbZ+e0VU=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=XALc4m31S8xf20My1oYawJ9GRWbOPLQDBZoABgNLr0jdkhu/cBYGXgujvmgeTa6bQ mn6GFCIcg3I1U3dGgbQMr6DF7Aj29fK7hECIr7zLyCpKh9OrV2fPn00TJjmA1MszfX fWFWrcE7Sslh38WY/V/psuCNYKbtSga0pxy6+8Ic= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by sourceware.org (Postfix) with ESMTPS id C81033857342 for ; Tue, 4 Jul 2023 12:36:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C81033857342 X-IronPort-AV: E=McAfee;i="6600,9927,10760"; a="361974967" X-IronPort-AV: E=Sophos;i="6.01,180,1684825200"; d="scan'208";a="361974967" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jul 2023 05:36:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10760"; a="808923678" X-IronPort-AV: E=Sophos;i="6.01,180,1684825200"; d="scan'208";a="808923678" Received: from unknown (HELO localhost) ([10.216.210.17]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jul 2023 05:36:41 -0700 To: gdb-patches@sourceware.org, markus.t.metzger@intel.com, simark@simark.ca Cc: Felix Willgerodt Subject: [PATCH v9 08/10] btrace, linux: Enable ptwrite packets. Date: Tue, 4 Jul 2023 14:35:58 +0200 Message-Id: <20230704123600.5944-9-felix.willgerodt@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230704123600.5944-1-felix.willgerodt@intel.com> References: <20230704123600.5944-1-felix.willgerodt@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Felix Willgerodt via Gdb-patches From: Felix Willgerodt Reply-To: Felix Willgerodt Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Enable ptwrite in the PT config, if it is supported by the kernel. --- gdb/nat/linux-btrace.c | 58 ++++++++++++++++++++++++++++++++++++++++++ gdb/record-btrace.c | 5 ++++ 2 files changed, 63 insertions(+) diff --git a/gdb/nat/linux-btrace.c b/gdb/nat/linux-btrace.c index c5b3f1c93cf..689f4c46dd4 100644 --- a/gdb/nat/linux-btrace.c +++ b/gdb/nat/linux-btrace.c @@ -417,6 +417,57 @@ cpu_supports_bts (void) } } +/* Read config bits. */ + +static bool +linux_read_pt_config_bit (const std::string &feature, uint64_t *config_bit) +{ + std::string filename + = "/sys/bus/event_source/devices/intel_pt/format/" + feature; + gdb_file_up file = gdb_fopen_cloexec (filename.c_str (), "r"); + + if (file.get () == nullptr || config_bit == nullptr) + return false; + + int found = fscanf (file.get (), "config:%lu", config_bit); + + if (found != 1) + { + warning (_("Failed to determine config bit from %s."), + filename.c_str ()); + return false; + } + + return true; +} + + +/* Check whether the linux target supports Intel Processor Trace PTWRITE. */ + +static bool +linux_supports_ptwrite (uint64_t *config_bit) +{ + static const char filename[] + = "/sys/bus/event_source/devices/intel_pt/caps/ptwrite"; + gdb_file_up file = gdb_fopen_cloexec (filename, "r"); + + if (file.get () == nullptr) + return false; + + int status, found = fscanf (file.get (), "%d", &status); + + if (found != 1) + { + warning (_("Failed to determine ptwrite support from %s."), filename); + return false; + } + + if (!linux_read_pt_config_bit ("ptw", config_bit)) + return false; + + return status == 1; +} + /* The perf_event_open syscall failed. Try to print a helpful error message. */ @@ -626,6 +677,13 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf) pt->attr.exclude_hv = 1; pt->attr.exclude_idle = 1; + uint64_t config_bit; + if (conf->ptwrite && linux_supports_ptwrite (&config_bit)) + { + pt->attr.config |= 1 << config_bit; + tinfo->conf.pt.ptwrite = conf->ptwrite; + } + errno = 0; scoped_fd fd (syscall (SYS_perf_event_open, &pt->attr, pid, -1, -1, 0)); if (fd.get () < 0) diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c index c93b3d7c8de..ad3160d42c5 100644 --- a/gdb/record-btrace.c +++ b/gdb/record-btrace.c @@ -3295,4 +3295,9 @@ to see the actual buffer size."), NULL, show_record_pt_buffer_size_value, record_btrace_conf.bts.size = 64 * 1024; record_btrace_conf.pt.size = 16 * 1024; +#if (LIBIPT_VERSION >= 0x200) + record_btrace_conf.pt.ptwrite = true; +#else + record_btrace_conf.pt.ptwrite = false; +#endif }