From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 6002E3856954 for ; Fri, 6 Oct 2023 16:03:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6002E3856954 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696608227; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=doCDqfFKu4PxGDykhee79e+TUeyxpe8JNTyX8MBIULk=; b=TRFBeWIVzSF1L95WgaLSx6yYJbJy8V3GcQEvq1hLXONLxjwUm9FFNHRqZTWU1d0EiIozVH 21aJtBiJ2ZSaJBNtL4fLxFVjIweFy0gQB08G4vXQoJ9vU24VXHkyAEnMKK++lc02uxKJtv 36Azt0Pe/nU5nf1o7tVtQqJzsaMu7l4= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-100--EC1DhxcNyOLDIR-6y-ItQ-1; Fri, 06 Oct 2023 12:03:46 -0400 X-MC-Unique: -EC1DhxcNyOLDIR-6y-ItQ-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-65b08994e15so36436526d6.0 for ; Fri, 06 Oct 2023 09:03:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696608226; x=1697213026; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=doCDqfFKu4PxGDykhee79e+TUeyxpe8JNTyX8MBIULk=; b=c1uXo+UtN8Bwc5Gdk3yxwchF7nUOaTE1L/DaGFs1HaHqow7Mn8YK7Pontiw8c21N0c FiXGPzkEKcyNfHtkpmxXqr8RUVVw7toK6y/btKKSju1sWXV6hVnC/I+01MjhwVoHbO8L 8uZNlUk5k0l3+PlI9fLZx+ewWb0AYSGq04P2Q8N6hzf5InRicuT6rwzepyXE0fXph5yO ++TEiyLjrsyJiHowQkdXyO0NW1MKWHJOp6BoOdH41GFUZ8nxKIJ20WI5v3JftqrhD/gE qJcdVxvgRyPYUCGeooK3ZDxdji3S1BK73r++6+DqcK9654+x29UYByAuVp7TuGuD8Aa6 4z/w== X-Gm-Message-State: AOJu0Yx2wJJxyD9E2kCX5ij3OTulgC5BP+YbEaA15JbpevGt2RnJ1emd 5kQBJWfLsnvL68NFpj7GJiXMZ2B5Ml3dyz/tzYKANqLPk7ZEPGmQt6t7v+CVR3Vds8IKL8RkD7r fLyBkHCP0yVFvILZZVFRTt7u7ETQ8CpxGsU2pGJXCv9yKlYeHPZBvBEYMCSSjHfF6ZtqpAzPdFg zKkg== X-Received: by 2002:a0c:e001:0:b0:656:3045:5638 with SMTP id j1-20020a0ce001000000b0065630455638mr8373643qvk.16.1696608225810; Fri, 06 Oct 2023 09:03:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7CDb+SSrPmvrMMlrT3TOsStEfgl3ZpPkiPPukGBB69/eEhE082Hx4ofwH+dJPtdNbvzeMBw== X-Received: by 2002:a0c:e001:0:b0:656:3045:5638 with SMTP id j1-20020a0ce001000000b0065630455638mr8373130qvk.16.1696608219482; Fri, 06 Oct 2023 09:03:39 -0700 (PDT) Received: from localhost ([31.111.84.209]) by smtp.gmail.com with ESMTPSA id m4-20020a0cac44000000b00647386a3234sm1508421qvb.85.2023.10.06.09.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 09:03:39 -0700 (PDT) From: Andrew Burgess To: binutils@sourceware.org Cc: Andrew Burgess Subject: [PATCH] bfd: add new bfd_cache_size() function Date: Fri, 6 Oct 2023 17:03:35 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP 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: In GDB we have a problem with the BFD cache. As GDB runs for a potentially extended period of time, if the BFD cache holds a file descriptor for an open on-disk file, this can, on some targets (e.g. Win32) prevent the OS writing to the file. This might, for example, prevent a user from recompiling their executable as GDB is (via the BFD cache) holding an open reference to that file. Another problem, relates to bfd_stat, for BFDs that are using the BFD cache (i.e. they call cache_bstat to implement bfd_stat). The cache_bstat function finds the BFD in the cache, opening the file if needed, and then uses fstat on the open file descriptor. What this means is that, if the on-disk file changes, but the cache was holding an open reference to the file, the bfd_stat will return the 'struct stat' for the old file, not the new file. Now, for this second problem, we might be tempted to make use of an actual stat call, instead of calling bfd_stat, however, this isn't ideal as we have some BFDs that use a custom iovec, and implement the various functions over GDB's remote protocol. By using bfd_stat we can have a single call that should work for both local files, and for remote files. To solve both of these problems GDB has calls to bfd_cache_close_all sprinkled around its code base. And in theory this should work fine. However, I recently ran into a case where we had missed a bfd_cache_close_all call, and as a result some BFDs were held open. This caused a bfd_stat call to return an unexpected result (old file vs new file). What I'd like is some way within GDB that I can do: gdb_assert ( /* Nothing is held open in the cache. */ ); As this would allow GDB to quickly identify when we've missed some bfd_cache_close_all calls. And so, to support this, I would like to add a new bfd_cache_size function. This function returns an integer, which is the number of open files in the cache. I can then start adding: gdb_assert (bfd_cache_size() == 0); to GDB in some strategic spots, and start fixing all of the missing bfd_cache_close_all calls that crop up as a result. --- bfd/bfd-in2.h | 2 ++ bfd/cache.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index eddb9902f5e..8ac7a2f2f8e 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -2790,6 +2790,8 @@ bool bfd_cache_close (bfd *abfd); bool bfd_cache_close_all (void); +int bfd_cache_size (void); + /* Extracted from compress.c. */ /* Types of compressed DWARF debug sections. */ enum compressed_debug_section_type diff --git a/bfd/cache.c b/bfd/cache.c index 357a38da599..8db42a26e66 100644 --- a/bfd/cache.c +++ b/bfd/cache.c @@ -574,6 +574,23 @@ bfd_cache_close_all (void) return ret; } +/* +FUNCTION + bfd_cache_size + +SYNOPSIS + int bfd_cache_size (void); + +DESCRIPTION + Return the number of open files in the cache. +*/ + +int +bfd_cache_size (void) +{ + return open_files; +} + /* INTERNAL_FUNCTION bfd_open_file base-commit: 9a896be33224654760c46d3698218241d0a1f354 -- 2.25.4