From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from omta040.useast.a.cloudfilter.net (omta040.useast.a.cloudfilter.net [44.202.169.39]) by sourceware.org (Postfix) with ESMTPS id C44FE3858C2B for ; Sun, 10 Dec 2023 21:41:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C44FE3858C2B 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 C44FE3858C2B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=44.202.169.39 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702244484; cv=none; b=O8NcDwug5OOgyoeNFBlPyziuiVpYQvaKgyAFPUAtTORlmmNAQmDvDdwWoYUqfO6QIRLKCcesvBK9btIHkSrOo6GiWK1me66oUtV/XmsgqBsl1fDDm1FP7h1iYaQRgNilLz5CFH+5xAWKd3HcegvlVu2UOw6qGbNKNPGvRxUG5nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702244484; c=relaxed/simple; bh=siK21cZgTwGULehcoEygpK8oEY4EIxcPEhsAUcbaKyk=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=k/l0iVJgQu8mrCVQK/TDWR+cq5gEAg87HVkHCaoxWdhDu7yvUJDSJFFvy57o8lPoxXlRp0YL5U+E9hevWnh0kOBUyL7kNSlnCoVHlBYNShn1D6ERkgkMb3FgH0x5Xvo8Ky3V++rVGKwJN44HLRYj5zVR3BVtCRmP2I5NOww4g+U= 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 CQSIrWoIl6nOZCRY9rrqq3; Sun, 10 Dec 2023 21:41:21 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id CRY8rRQWg11XZCRY8rb2hJ; Sun, 10 Dec 2023 21:41:20 +0000 X-Authority-Analysis: v=2.4 cv=Z+v/oVdA c=1 sm=1 tr=0 ts=65763080 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=OWjo9vPv0XrRhIrVQ50Ab3nP57M=:19 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10 a=e2cXIFwxEfEA: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=eA6B39CEHeeK8JISou9LyiH2uwIwliXcOXjlI0VNuAk=; b=o2ktm9zUHyyO/kv6eJNaO6wKwM LWDS6HQk9Rt74mAiYYHa09GSQ/fwEUY0Ah1v7L2Hl1XEcEHstOnSbibz3ASTHc5eCFpUvkT/9nVRm gSmGVy3gv/7pETAUa7avK4KYR; Received: from [198.59.47.65] (port=52450 helo=[192.168.131.83]) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1rCRY7-000SHj-3D for gdb-patches@sourceware.org; Sun, 10 Dec 2023 14:41:20 -0700 From: Tom Tromey Date: Sun, 10 Dec 2023 14:41:12 -0700 Subject: [PATCH v4 04/19] 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: <20231210-t-bg-dwarf-reading-v4-4-b978c32fd12f@tromey.com> References: <20231210-t-bg-dwarf-reading-v4-0-b978c32fd12f@tromey.com> In-Reply-To: <20231210-t-bg-dwarf-reading-v4-0-b978c32fd12f@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: 198.59.47.65 X-Source-L: No X-Exim-ID: 1rCRY7-000SHj-3D X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([192.168.131.83]) [198.59.47.65]:52450 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: MS4xfJB0UDU4Lvh2v1BfpRCJbowNeZTnzvbQawVA1BKy/EB+t6eFvjFfFhkDBgPCA/gXGs0AMjpX17PQLt5InAJJSTT+g6dEjbSZdvvg1UV0ElxWNveI7XdG ffGd7DDAX6OS/nYoDXcljPUb9Ez7vr/wV0XkmWb1/wrlOJfzGQ/t53s/M3cruLV0SJA7DZsV0Vkg1Hkwkk0jPO9o7zDfv6whQWo= X-Spam-Status: No, score=-3024.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,JMQ_SPF_NEUTRAL,RCVD_IN_DNSWL_NONE,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 ba58dae12bb..78acbd39ef1 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -3920,8 +3920,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 1c496c498ac..9f7d0f626c0 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 688db7655a9..67c7a52f54f 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -699,8 +699,7 @@ captured_main_1 (struct captured_main_args *context) auto temp_uiout = std::make_unique (gdb_stdout); current_uiout = temp_uiout.get (); - 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.43.0