From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from omta38.uswest2.a.cloudfilter.net (omta38.uswest2.a.cloudfilter.net [35.89.44.37]) by sourceware.org (Postfix) with ESMTPS id 293B2384DEEA for ; Thu, 23 Nov 2023 05:32:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 293B2384DEEA Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 293B2384DEEA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=35.89.44.37 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700717527; cv=none; b=c+dWXO5m3ycb1mhGIcFBNSGfhRpQJGRxhNnwb7A+0u9M75onNicc8De6uYEKTdc65LI8jze+nQWuyK1DRDuiijx9+YF2kL8v/IgFrx2HOlw2LtF9R761OMCtgod0u3AcfOmmD6gwEy5lZGir91Hz8nQ/5uuDDrEc5r9SJFge+/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700717527; c=relaxed/simple; bh=3hHLB01PKCCT/t5EGNd6u4Ih+PeY0biA6rl5z9/lSkE=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=fYTznaTwf/3GXT/m7o9zkvxmJbXsDYi4dCR2tx6HTg41XqVQ5tOLmPlHd9ZRVhTnkET401Or+iNF2TE5Ezju9YRTCW0kT2Fkyuun9CjjtUNgthSfD+8EYKkLoT+FoEpeCXr+RaAFEb4bCrh53/4/pY61ALBflXgMGG3N/7Wem1w= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from eig-obgw-5004a.ext.cloudfilter.net ([10.0.29.221]) by cmsmtp with ESMTPS id 5tOFrJ9Z5KOkL62Jnrp5t6; Thu, 23 Nov 2023 05:32:03 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id 62JmriFVD11XZ62JnrT4oc; Thu, 23 Nov 2023 05:32:03 +0000 X-Authority-Analysis: v=2.4 cv=Z+v/oVdA c=1 sm=1 tr=0 ts=655ee3d3 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=OWjo9vPv0XrRhIrVQ50Ab3nP57M=:19 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10 a=BNY50KLci1gA:10 a=Qbun_eYptAEA:10 a=29SN2e_Ic2r5L5NX2uIA:9 a=QEXdDO2ut3YA:10 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=DJM2+Sd2BY69GU8a4V6PtB6IQ8Zj4PH0erZPWoqe7Pk=; b=AIfdIIPlkXpQD1YuK+Wjo8I44c ZgQT82473PFLX8GO4PiQqGpUNYaqHOIT8ojMKckbqL7GsXgP7uHtu+JMwQ01HzTdht2e46GGfi+j8 7+65DE3eGkx/D37U7hsFE1zhk; Received: from 97-122-77-73.hlrn.qwest.net ([97.122.77.73]:46354 helo=[192.168.0.21]) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1r62Jm-003TGR-25 for gdb-patches@sourceware.org; Wed, 22 Nov 2023 22:32:02 -0700 From: Tom Tromey Date: Wed, 22 Nov 2023 22:32:13 -0700 Subject: [PATCH v3 04/18] Add thread-safety to gdb's BFD wrappers MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231122-t-bg-dwarf-reading-v3-4-fc3180de63c4@tromey.com> References: <20231122-t-bg-dwarf-reading-v3-0-fc3180de63c4@tromey.com> In-Reply-To: <20231122-t-bg-dwarf-reading-v3-0-fc3180de63c4@tromey.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.77.73 X-Source-L: No X-Exim-ID: 1r62Jm-003TGR-25 X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-77-73.hlrn.qwest.net ([192.168.0.21]) [97.122.77.73]:46354 X-Source-Auth: tom+tromey.com X-Email-Count: 6 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfHjFGKT0cLrREJNMqy1MtlEwMGoFK7qTWHoC/R9nKwEBRJ1x1mBQ1/OVMJVrj1eqpAb3qDfSI91zedO22ZnoOf7uNM2ZpfRkv22wBoc0FiEatr6POyRp EDmR9mmUtJb0zLtqoVh5vSCO6ScGd9pblhnOWSr+uNMCBgBwlMixWXGLg9jaEyAyUstsobhzp1KrXx57Ye19O96BazfHo8M5qmU= X-Spam-Status: No, score=-3024.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,JMQ_SPF_NEUTRAL,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,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 List-Id: This changes gdb to ensure that gdb's BFD cache is guarded by a lock. This avoids any races when multiple threads might open a BFD (and thus use the BFD cache) at the same time. Currently, this change is not needed because the the main thread waits for some DWARF scanning to be completed before returning. The only locking that's required is when opening DWO files, and there's a local lock to this end in dwarf2/read.c. However, in the coming patches, the DWARF reader will begin its work earlier, in the background. This means there is the potential for the DWARF reader and other code on the main thread to both attempt to open BFDs at the same time. --- gdb/dwarf2/read.c | 3 +-- gdb/gdb_bfd.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ gdb/gdb_bfd.h | 5 +++++ gdb/main.c | 3 +-- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 6e6609ef86c..e08cf1a91b3 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -3931,8 +3931,7 @@ static struct dwo_unit * lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name) { #if CXX_STD_THREAD - /* We need a lock here both to handle the DWO hash table, and BFD, - which is not thread-safe. */ + /* We need a lock here both to handle the DWO hash table. */ static std::mutex dwo_lock; std::lock_guard guard (dwo_lock); diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c index b72821f4c35..0df264cd94f 100644 --- a/gdb/gdb_bfd.c +++ b/gdb/gdb_bfd.c @@ -35,6 +35,36 @@ #include "cli/cli-style.h" #include +#if CXX_STD_THREAD + +#include + +/* Lock held when doing BFD operations. A recursive mutex is used + because we use this mutex internally and also for BFD, just to make + life a bit simpler, and we may sometimes hold it while calling into + BFD. */ +static std::recursive_mutex gdb_bfd_mutex; + +/* BFD locking function. */ + +static bool +gdb_bfd_lock (void *ignore) +{ + gdb_bfd_mutex.lock (); + return true; +} + +/* BFD unlocking function. */ + +static bool +gdb_bfd_unlock (void *ignore) +{ + gdb_bfd_mutex.unlock (); + return true; +} + +#endif /* CXX_STD_THREAD */ + /* An object of this type is stored in the section's user data when mapping a section. */ @@ -498,6 +528,10 @@ gdb_bfd_open (const char *name, const char *target, int fd, name += strlen (TARGET_SYSROOT_PREFIX); } +#if CXX_STD_THREAD + std::lock_guard guard (gdb_bfd_mutex); +#endif + if (gdb_bfd_cache == NULL) gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL, xcalloc, xfree); @@ -625,6 +659,10 @@ gdb_bfd_ref (struct bfd *abfd) if (abfd == NULL) return; +#if CXX_STD_THREAD + std::lock_guard guard (gdb_bfd_mutex); +#endif + gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd); bfd_cache_debug_printf ("Increase reference count on bfd %s (%s)", @@ -654,6 +692,10 @@ gdb_bfd_unref (struct bfd *abfd) if (abfd == NULL) return; +#if CXX_STD_THREAD + std::lock_guard guard (gdb_bfd_mutex); +#endif + gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd); gdb_assert (gdata->refc >= 1); @@ -1171,6 +1213,22 @@ gdb_bfd_error_handler (const char *fmt, va_list ap) (*default_bfd_error_handler) (fmt, ap); } +/* See gdb_bfd.h. */ + +void +gdb_bfd_init () +{ + if (bfd_init () == BFD_INIT_MAGIC) + { +#if CXX_STD_THREAD + if (bfd_thread_init (gdb_bfd_lock, gdb_bfd_unlock, nullptr)) +#endif + return; + } + + error (_("fatal error: libbfd ABI mismatch")); +} + void _initialize_gdb_bfd (); void _initialize_gdb_bfd () diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h index 604365b61b1..669c3a06c98 100644 --- a/gdb/gdb_bfd.h +++ b/gdb/gdb_bfd.h @@ -250,4 +250,9 @@ gdb_bfd_sections (const gdb_bfd_ref_ptr &abfd) extern std::string gdb_bfd_errmsg (bfd_error_type error_tag, char **matching); +/* A wrapper for bfd_init that also handles setting up for + multi-threading. */ + +extern void gdb_bfd_init (); + #endif /* GDB_BFD_H */ diff --git a/gdb/main.c b/gdb/main.c index 486c1ffe071..07635326c6c 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -688,8 +688,7 @@ captured_main_1 (struct captured_main_args *context) gdb_stdtargerr = gdb_stderr; gdb_stdtargin = gdb_stdin; - if (bfd_init () != BFD_INIT_MAGIC) - error (_("fatal error: libbfd ABI mismatch")); + gdb_bfd_init (); #ifdef __MINGW32__ /* On Windows, argv[0] is not necessarily set to absolute form when -- 2.41.0