From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from omta34.uswest2.a.cloudfilter.net (omta34.uswest2.a.cloudfilter.net [35.89.44.33]) by sourceware.org (Postfix) with ESMTPS id F1DF73858C54 for ; Sun, 12 Nov 2023 20:25:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F1DF73858C54 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 F1DF73858C54 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=35.89.44.33 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699820739; cv=none; b=WnGtExq9C/ifJq7lT7/aaFfa07g7MGLybN3HEILUmXez4H/HAQJTDDMmBZr9HwtOERqziCnYggpmQbHfvb0GrtklIQdBlQR17GdiMpDYszjVv0fwI/ED6Myxz2HooOKepsjc60/GJCZRdFzApuRMRMz89CM+qsIDOFAOpJgZ1KM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699820739; c=relaxed/simple; bh=B1I/MPLC+HuYwRxS0Bg39/jNudt4tkY6OhyVvP5/WWQ=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=J4NwGsWJ4X3n/glJhzOM4M33zXnybTJZFXrswFsHdi6HG2sx7sbbZcAaiysAC0BBCC6C4kJu1AVFgRVy7Q7urJm7dj6BQSGilm7ceQAynEBxfN03PvCo1WKBTytHZWfn5UmYXyOt4LgsRYn2yq9lx+TZrqOVeYyVnuCqYWyh2zk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from eig-obgw-5003a.ext.cloudfilter.net ([10.0.29.159]) by cmsmtp with ESMTPS id 1zhgrnEYp8Hte2H1Rr76RD; Sun, 12 Nov 2023 20:25:33 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id 2H1Qr0mlNnCfv2H1QrPSgS; Sun, 12 Nov 2023 20:25:32 +0000 X-Authority-Analysis: v=2.4 cv=KKpJsXJo c=1 sm=1 tr=0 ts=655134bc 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=lmp9pwg3TsK3OfVNsQywt4XHkjej1G7TtC2Q+acmGJU=; b=pvjdUL0o/J3hbeqr0LxKUQCLTU qX0lBzM/AMGD/Xul0OLRopFCPJLoZmB3GSE63kdVDlk8e6TC939z4d32Rc92Tkc0lFd4LiQpC/Q/E OJbIaED/RTvmNnhnoEHKkGkSs; Received: from 97-122-77-73.hlrn.qwest.net ([97.122.77.73]:34540 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 1r2H1Q-000cUU-0X for gdb-patches@sourceware.org; Sun, 12 Nov 2023 13:25:32 -0700 From: Tom Tromey Date: Sun, 12 Nov 2023 13:25:41 -0700 Subject: [PATCH v2 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: <20231112-t-bg-dwarf-reading-v2-4-70fb170012ba@tromey.com> References: <20231112-t-bg-dwarf-reading-v2-0-70fb170012ba@tromey.com> In-Reply-To: <20231112-t-bg-dwarf-reading-v2-0-70fb170012ba@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: 1r2H1Q-000cUU-0X 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]:34540 X-Source-Auth: tom+tromey.com X-Email-Count: 5 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfNkg9HpOC56YmGpWJX9ukY55Ld6p0BSsh1H1cpHjccUSiPbP30arFcj52Z5rP7jaZOxsU+3ayuKJCZKlWNd3/27aIlHrEdt1JGxjtD9Sh50KRr4ajEVL KC9ylRa5yV48oxcj8ad1La18TwZDoaoPQ1XSnoXPE8ZhFxJDNnEmLXxq0l+yinXFxviHhOIW+ArejA+Y+zUUY3LFBuU6TIYkgJ0= X-Spam-Status: No, score=-3024.4 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 a83095f8c5d..86fd42c3579 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -3936,8 +3936,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 56a4c5ecc91..7152459de64 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 2da39f89a90..39eacd2208b 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