From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by sourceware.org (Postfix) with ESMTPS id 6F9E93858C20 for ; Wed, 6 Sep 2023 07:47:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6F9E93858C20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1693986464; x=1725522464; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7Ri0lssapqErbtQmpciS8vhutdmLmMt91zMLdHQFzps=; b=Cws9B+CvunbHlzz3kOgXBrsssgJ8AdmUZ5au8t/deYREhA6UFaAlGk59 xokKjRRfq3Q/9xqSBAF+1c7dH12oAE10DAt+9BLbDarT2kD6++6VXwTBf h6JpSxBtqA2FjjsDJ69MjXZZW/N4a3enlhQU8PjEwX9P3C7KN5FwF8bRn BwGmO1V+lKqTDdNVsbD13QNGn5H2d0d9WU2vNFzT1PCyUeYL9kuDtrbQS /TcqrxG0j/9/Md3o/BDpVeMPYgrI/glX5bjoskPkrprTzfAtlu8DzRVea pDsXlzVbug6DcgJm0IDS4H2DagYI1jmeDUpOynqn1Zc25haIPQ46uFk1h A==; X-IronPort-AV: E=McAfee;i="6600,9927,10824"; a="380802617" X-IronPort-AV: E=Sophos;i="6.02,231,1688454000"; d="scan'208";a="380802617" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Sep 2023 00:47:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10824"; a="776505657" X-IronPort-AV: E=Sophos;i="6.02,231,1688454000"; d="scan'208";a="776505657" Received: from gkldtt-dev-004.igk.intel.com (HELO localhost) ([10.123.221.202]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Sep 2023 00:47:42 -0700 From: Markus Metzger To: gdb-patches@sourceware.org Cc: vries@gcc.gnu.org Subject: [PATCH 2/2] gdb: c++ify btrace_target_info Date: Wed, 6 Sep 2023 07:47:27 +0000 Message-Id: <20230906074727.426831-2-markus.t.metzger@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230906074727.426831-1-markus.t.metzger@intel.com> References: <20230906074727.426831-1-markus.t.metzger@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.5 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Following the example of private_thread_info and private_inferior, turn struct btrace_target_info into a small class hierarchy. Fixes PR gdb/30751. --- gdb/nat/linux-btrace.c | 57 ++++++++++++++++++++++++++++---------- gdb/nat/linux-btrace.h | 12 +++++++- gdb/remote.c | 55 ++++++++++++++++++++++++++++-------- gdbsupport/btrace-common.h | 5 +++- 4 files changed, 101 insertions(+), 28 deletions(-) diff --git a/gdb/nat/linux-btrace.c b/gdb/nat/linux-btrace.c index c5b3f1c93cf..3960d462e67 100644 --- a/gdb/nat/linux-btrace.c +++ b/gdb/nat/linux-btrace.c @@ -277,8 +277,9 @@ perf_event_sample_ok (const struct perf_event_sample *sample) part at the end and its upper part at the beginning of the buffer. */ static std::vector * -perf_event_read_bts (struct btrace_target_info* tinfo, const uint8_t *begin, - const uint8_t *end, const uint8_t *start, size_t size) +perf_event_read_bts (struct linux_btrace_target_info* tinfo, + const uint8_t *begin, const uint8_t *end, + const uint8_t *start, size_t size) { std::vector *btrace = new std::vector; struct perf_event_sample sample; @@ -447,6 +448,28 @@ diagnose_perf_event_open_fail () error (_("Failed to start recording: %s"), safe_strerror (errno)); } +/* Get the linux version of a btrace_target_info. */ + +static linux_btrace_target_info * +get_linux_btrace_target_info (btrace_target_info *gtinfo) +{ + if (gtinfo == nullptr) + return nullptr; + + return gdb::checked_static_cast (gtinfo); +} + +/* Get the linux version of a btrace_target_info - const version. */ + +static const linux_btrace_target_info * +get_linux_btrace_target_info (const btrace_target_info *gtinfo) +{ + if (gtinfo == nullptr) + return nullptr; + + return gdb::checked_static_cast (gtinfo); +} + /* Enable branch tracing in BTS format. */ static struct btrace_target_info * @@ -460,9 +483,8 @@ linux_enable_bts (ptid_t ptid, const struct btrace_config_bts *conf) if (!cpu_supports_bts ()) error (_("BTS support has been disabled for the target cpu.")); - gdb::unique_xmalloc_ptr tinfo - (XCNEW (btrace_target_info)); - tinfo->ptid = ptid; + std::unique_ptr tinfo + { new linux_btrace_target_info (ptid) }; tinfo->conf.format = BTRACE_FORMAT_BTS; bts = &tinfo->variant.bts; @@ -612,9 +634,8 @@ linux_enable_pt (ptid_t ptid, const struct btrace_config_pt *conf) if (pid == 0) pid = ptid.pid (); - gdb::unique_xmalloc_ptr tinfo - (XCNEW (btrace_target_info)); - tinfo->ptid = ptid; + std::unique_ptr tinfo + { new linux_btrace_target_info (ptid) }; tinfo->conf.format = BTRACE_FORMAT_PT; pt = &tinfo->variant.pt; @@ -755,10 +776,13 @@ linux_disable_pt (struct btrace_tinfo_pt *tinfo) /* See linux-btrace.h. */ enum btrace_error -linux_disable_btrace (struct btrace_target_info *tinfo) +linux_disable_btrace (struct btrace_target_info *gtinfo) { enum btrace_error errcode; + linux_btrace_target_info *tinfo + = get_linux_btrace_target_info (gtinfo); + errcode = BTRACE_ERR_NOT_SUPPORTED; switch (tinfo->conf.format) { @@ -775,7 +799,7 @@ linux_disable_btrace (struct btrace_target_info *tinfo) } if (errcode == BTRACE_ERR_NONE) - xfree (tinfo); + delete gtinfo; return errcode; } @@ -785,7 +809,7 @@ linux_disable_btrace (struct btrace_target_info *tinfo) static enum btrace_error linux_read_bts (struct btrace_data_bts *btrace, - struct btrace_target_info *tinfo, + struct linux_btrace_target_info *tinfo, enum btrace_read_type type) { struct perf_event_buffer *pevent; @@ -888,7 +912,7 @@ linux_fill_btrace_pt_config (struct btrace_data_pt_config *conf) static enum btrace_error linux_read_pt (struct btrace_data_pt *btrace, - struct btrace_target_info *tinfo, + struct linux_btrace_target_info *tinfo, enum btrace_read_type type) { struct perf_event_buffer *pt; @@ -921,9 +945,12 @@ linux_read_pt (struct btrace_data_pt *btrace, enum btrace_error linux_read_btrace (struct btrace_data *btrace, - struct btrace_target_info *tinfo, + struct btrace_target_info *gtinfo, enum btrace_read_type type) { + linux_btrace_target_info *tinfo + = get_linux_btrace_target_info (gtinfo); + switch (tinfo->conf.format) { case BTRACE_FORMAT_NONE: @@ -951,8 +978,10 @@ linux_read_btrace (struct btrace_data *btrace, /* See linux-btrace.h. */ const struct btrace_config * -linux_btrace_conf (const struct btrace_target_info *tinfo) +linux_btrace_conf (const struct btrace_target_info *gtinfo) { + const linux_btrace_target_info *tinfo + = get_linux_btrace_target_info (gtinfo); return &tinfo->conf; } diff --git a/gdb/nat/linux-btrace.h b/gdb/nat/linux-btrace.h index ab69647c591..1dff977cb44 100644 --- a/gdb/nat/linux-btrace.h +++ b/gdb/nat/linux-btrace.h @@ -23,6 +23,7 @@ #define NAT_LINUX_BTRACE_H #include "gdbsupport/btrace-common.h" +#include "gdbsupport/gdb-checked-static-cast.h" #if HAVE_LINUX_PERF_EVENT_H # include #endif @@ -81,7 +82,7 @@ struct btrace_tinfo_pt #endif /* HAVE_LINUX_PERF_EVENT_H */ /* Branch trace target information per thread. */ -struct btrace_target_info +struct linux_btrace_target_info : public btrace_target_info { /* The ptid of this thread. */ ptid_t ptid; @@ -100,6 +101,15 @@ struct btrace_target_info struct btrace_tinfo_pt pt; } variant; #endif /* HAVE_LINUX_PERF_EVENT_H */ + + + linux_btrace_target_info (ptid_t _ptid, const struct btrace_config &_conf) + : ptid (_ptid), conf (_conf), variant ({}) + {} + + linux_btrace_target_info (ptid_t _ptid) + : ptid (_ptid), conf ({}), variant ({}) + {} }; /* See to_enable_btrace in target.h. */ diff --git a/gdb/remote.c b/gdb/remote.c index 55f2fc3b6b5..043781cea06 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -14423,15 +14423,45 @@ parse_xml_btrace_conf (struct btrace_config *conf, const char *xml) #endif /* !defined (HAVE_LIBEXPAT) */ } -struct btrace_target_info +struct remote_btrace_target_info : public btrace_target_info { /* The ptid of the traced thread. */ ptid_t ptid; /* The obtained branch trace configuration. */ struct btrace_config conf; + + remote_btrace_target_info (ptid_t _ptid, const struct btrace_config &_conf) + : ptid (_ptid), conf (_conf) + {} + + remote_btrace_target_info (ptid_t _ptid) + : ptid (_ptid), conf ({}) + {} }; +/* Get the remote version of a btrace_target_info. */ + +static remote_btrace_target_info * +get_remote_btrace_target_info (btrace_target_info *gtinfo) +{ + if (gtinfo == nullptr) + return nullptr; + + return gdb::checked_static_cast (gtinfo); +} + +/* Get the remote version of a btrace_target_info - const version. */ + +static const remote_btrace_target_info * +get_remote_btrace_target_info (const btrace_target_info *gtinfo) +{ + if (gtinfo == nullptr) + return nullptr; + + return gdb::checked_static_cast (gtinfo); +} + /* Reset our idea of our target's btrace configuration. */ static void @@ -14502,7 +14532,7 @@ remote_target::btrace_sync_conf (const btrace_config *conf) /* Read TP's btrace configuration from the target and store it into CONF. */ static void -btrace_read_config (thread_info *tp, struct btrace_config *conf) +btrace_read_config (thread_info *tp, btrace_config *conf) { /* target_read_stralloc relies on INFERIOR_PTID. */ scoped_restore_current_thread restore_thread; @@ -14564,9 +14594,8 @@ remote_target::remote_btrace_maybe_reopen () btrace_format_string (rs->btrace_config.format)); } - tp->btrace.target = XCNEW (struct btrace_target_info); - tp->btrace.target->ptid = tp->ptid; - tp->btrace.target->conf = rs->btrace_config; + tp->btrace.target + = new remote_btrace_target_info { tp->ptid, rs->btrace_config }; } } @@ -14576,7 +14605,7 @@ struct btrace_target_info * remote_target::enable_btrace (thread_info *tp, const struct btrace_config *conf) { - struct btrace_target_info *tinfo = NULL; + struct remote_btrace_target_info *tinfo = NULL; struct packet_config *packet = NULL; struct remote_state *rs = get_remote_state (); char *buf = rs->buf.data (); @@ -14620,8 +14649,7 @@ remote_target::enable_btrace (thread_info *tp, target_pid_to_str (ptid).c_str ()); } - tinfo = XCNEW (struct btrace_target_info); - tinfo->ptid = ptid; + tinfo = new remote_btrace_target_info { ptid }; /* If we fail to read the configuration, we lose some information, but the tracing itself is not impacted. */ @@ -14641,7 +14669,7 @@ remote_target::enable_btrace (thread_info *tp, /* Disable branch tracing. */ void -remote_target::disable_btrace (struct btrace_target_info *tinfo) +remote_target::disable_btrace (struct btrace_target_info *gtinfo) { struct remote_state *rs = get_remote_state (); char *buf = rs->buf.data (); @@ -14650,6 +14678,7 @@ remote_target::disable_btrace (struct btrace_target_info *tinfo) if (m_features.packet_support (PACKET_Qbtrace_off) != PACKET_ENABLE) error (_("Target does not support branch tracing.")); + remote_btrace_target_info *tinfo = get_remote_btrace_target_info (gtinfo); set_general_thread (tinfo->ptid); buf += xsnprintf (buf, endbuf - buf, "%s", @@ -14667,7 +14696,7 @@ remote_target::disable_btrace (struct btrace_target_info *tinfo) target_pid_to_str (tinfo->ptid).c_str ()); } - xfree (tinfo); + delete gtinfo; } /* Teardown branch tracing. */ @@ -14676,7 +14705,7 @@ void remote_target::teardown_btrace (struct btrace_target_info *tinfo) { /* We must not talk to the target during teardown. */ - xfree (tinfo); + delete tinfo; } /* Read the branch trace. */ @@ -14723,8 +14752,10 @@ remote_target::read_btrace (struct btrace_data *btrace, } const struct btrace_config * -remote_target::btrace_conf (const struct btrace_target_info *tinfo) +remote_target::btrace_conf (const struct btrace_target_info *gtinfo) { + const remote_btrace_target_info *tinfo + = get_remote_btrace_target_info (gtinfo); return &tinfo->conf; } diff --git a/gdbsupport/btrace-common.h b/gdbsupport/btrace-common.h index e287c93a6c1..7ae865b3978 100644 --- a/gdbsupport/btrace-common.h +++ b/gdbsupport/btrace-common.h @@ -214,7 +214,10 @@ struct btrace_data }; /* Target specific branch trace information. */ -struct btrace_target_info; +struct btrace_target_info +{ + virtual ~btrace_target_info () {} +}; /* Enumeration of btrace read types. */ -- 2.34.1 Intel Deutschland GmbH Registered Address: Am Campeon 10, 85579 Neubiberg, Germany Tel: +49 89 99 8853-0, www.intel.de Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva Chairperson of the Supervisory Board: Nicole Lau Registered Office: Munich Commercial Register: Amtsgericht Muenchen HRB 186928