From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id WE8xL4Np8GXl2QMAWB0awg (envelope-from ) for ; Tue, 12 Mar 2024 10:41:07 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=RLWWd6Ug; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id ABF2F1E0D2; Tue, 12 Mar 2024 10:41:07 -0400 (EDT) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 464841E0AC for ; Tue, 12 Mar 2024 10:41:05 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 966F338582B4 for ; Tue, 12 Mar 2024 14:41:04 +0000 (GMT) Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) by sourceware.org (Postfix) with ESMTPS id 5A7CF385840C for ; Tue, 12 Mar 2024 14:40:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A7CF385840C Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5A7CF385840C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d30 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710254445; cv=none; b=gJtR0U0iiBR6GIka7rj+375vVO5n3FohpAFXPh9gqLQRncUuWHaSaahMAn/zxSVrphgDrrHvFrtBuqAB/Fp4bAlWkerTJjlChZUyKEwdYDUPkn+EF82HhoY3qr8SgZJ7Iuf2FQR5LSFeocuK/HV+mg6t0hNm8CTggYSyfSUIi20= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710254445; c=relaxed/simple; bh=P7MYaLgN/P3KNnOZVHbCHKmjaONFY2ULxZ2nLMM8PR4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=VcujjjqdoIs74p9ux4ytdHGB2OahgjJlhVzdiKgJhMpcMN8MWeKCAklbA5VxJw4MdOmoJMUw0Dx8m548zPezIVAmsgUcz5O5NqO1lGDnkonq0ifcDXuc7eJYWmRRTgFzeXGLRAQh29XD1MOhorm2yGRgew55xWJs/1QLPDX3kDQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd30.google.com with SMTP id ca18e2360f4ac-7c89cd8df36so56545939f.1 for ; Tue, 12 Mar 2024 07:40:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1710254442; x=1710859242; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=IBXMbG42ImJMONjoGBiyDCJnYik6W4mmzr8mefwhVlc=; b=RLWWd6Ug+PNGJs9R7jfdc7VkeIatjerBP1QvhpGXkLLMpJezlNLd8t7hFYrLN+dhGT NlddUkhfGDlV1PDalGmzPSNmyN8xgM8qzmP4e0CEhG1s9H20Xd3gBEitEEE1jGJPtZcl AS1OfzfZR5XJ2iTG2nWDH24dA520uR/QJz4vt87+jWetf0voyELLdKmdbQPKNi94EsqW NuMBLtRpCkj6DzYoXVe30x8En9fKnFIV8B8XjAFqqnQCoBm1Hl30L2tkm9eUGxPieWGV rL4fMRNWgn1UzXFVItt+YWLR2v603NPDT8n1qNfKs7rjczo22CZAhWcZu/kcDC6j6Bay lE2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710254442; x=1710859242; 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=IBXMbG42ImJMONjoGBiyDCJnYik6W4mmzr8mefwhVlc=; b=d3rSk0dBwdTEQGNVz2/KDOs9igxf7XJi/ZwQwhJA+JWONeiCp1thrCfFoSH7zLDjlu deh20jnr0L39EmU+Bs/HSVMohzcKYnbVMyimTjNIblWuR6+V8zaR9Umbkiq6wPdJAQQt nNPGzB6ADjZTIz+l+7TFSvFinVhb6VAP4DRW2qQdEInNlPiHYweFJjnhg9ijnhyoZsGI +QzXPXJXoO4LMTkP075jCUwiwDrc8jCXXD9u/SjRqsGAbzrxen3d7AIUKWdAjncnSJln TZqxQrt/Fg3RtqslyBRtm6AW8MaSryZki7cTra6uX8X1q/B6CLLHt3NA8z1stND5g/la WQrQ== X-Gm-Message-State: AOJu0YySLCAS3G/fH/hkZzzL73uy917QiUTeN/31fniB0+GRNq6m5Dw/ xavgcKnxp3YBn9jkmZKQNkM2QSZWhzJ2ibe79MktXdUJvBuMaP4C2cDHgajQStbP4iaMD37nuVQ = X-Google-Smtp-Source: AGHT+IGQkuuvNZbcTZDn/rheMstKf/039pOXA8KbQR6+/IyLQDs1jamQCEnbU6BFKD0jpjBImAotTQ== X-Received: by 2002:a05:6602:3411:b0:7c8:ce83:5462 with SMTP id n17-20020a056602341100b007c8ce835462mr668251ioz.17.1710254442432; Tue, 12 Mar 2024 07:40:42 -0700 (PDT) Received: from localhost.localdomain (97-122-82-115.hlrn.qwest.net. [97.122.82.115]) by smtp.gmail.com with ESMTPSA id do31-20020a0566384c9f00b00474f719b8c3sm2226376jab.33.2024.03.12.07.40.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 07:40:42 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2] Capture warnings when writing to the index cache Date: Tue, 12 Mar 2024 08:40:33 -0600 Message-ID: <20240312144033.77496-1-tromey@adacore.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org PR symtab/30837 points out a race that can occur when writing to the index cache: a call to ada_encode can cause a warning, which is forbidden on a worker thread. This patch fixes the problem by arranging to capture any such warnings. This is v2 of the patch. It is rebased on top of some other changes in the same area. v1 was here: https://sourceware.org/pipermail/gdb-patches/2024-February/206595.html Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30837 --- gdb/dwarf2/cooked-index.c | 18 +++++++++++++----- gdb/dwarf2/cooked-index.h | 9 ++++++--- gdb/dwarf2/read-debug-names.c | 2 +- gdb/dwarf2/read.c | 2 +- gdb/utils.h | 14 +++++++++++++- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c index 2a1ca6fd225..f78b00df446 100644 --- a/gdb/dwarf2/cooked-index.c +++ b/gdb/dwarf2/cooked-index.c @@ -581,10 +581,18 @@ cooked_index_worker::set (cooked_state desired_state) /* See cooked-index.h. */ void -cooked_index_worker::write_to_cache (const cooked_index *idx) const +cooked_index_worker::write_to_cache (const cooked_index *idx, + deferred_warnings *warn) const { if (idx != nullptr) - m_cache_store.store (); + { + /* Writing to the index cache may cause a warning to be emitted. + See PR symtab/30837. This arranges to capture all such + warnings. This is safe because we know the deferred_warnings + object isn't in use by any other thread at this point. */ + scoped_restore_warning_hook defer (*warn); + m_cache_store.store (); + } } cooked_index::cooked_index (dwarf2_per_objfile *per_objfile, @@ -623,7 +631,7 @@ cooked_index::wait (cooked_state desired_state, bool allow_quit) } void -cooked_index::set_contents (vec_type &&vec) +cooked_index::set_contents (vec_type &&vec, deferred_warnings *warn) { gdb_assert (m_vector.empty ()); m_vector = std::move (vec); @@ -635,10 +643,10 @@ cooked_index::set_contents (vec_type &&vec) finalization. However, that would take a slot in the global thread pool, and if enough such tasks were submitted at once, it would cause a livelock. */ - gdb::task_group finalizers ([this] () + gdb::task_group finalizers ([=] () { m_state->set (cooked_state::FINALIZED); - m_state->write_to_cache (index_for_writing ()); + m_state->write_to_cache (index_for_writing (), warn); m_state->set (cooked_state::CACHE_DONE); }); diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h index 91419b79edc..19720c256db 100644 --- a/gdb/dwarf2/cooked-index.h +++ b/gdb/dwarf2/cooked-index.h @@ -494,7 +494,8 @@ class cooked_index_worker void set (cooked_state desired_state); /* Write to the index cache. */ - void write_to_cache (const cooked_index *idx) const; + void write_to_cache (const cooked_index *idx, + deferred_warnings *warn) const; /* Helper function that does the work of reading. This must be able to be run in a worker thread without problems. */ @@ -615,8 +616,10 @@ class cooked_index : public dwarf_scanner_base void start_reading (); /* Called by cooked_index_worker to set the contents of this index - and transition to the MAIN_AVAILABLE state. */ - void set_contents (vec_type &&vec); + and transition to the MAIN_AVAILABLE state. WARN is used to + collect any warnings that may arise when writing to the + cache. */ + void set_contents (vec_type &&vec, deferred_warnings *warn); /* A range over a vector of subranges. */ using range = range_chain; diff --git a/gdb/dwarf2/read-debug-names.c b/gdb/dwarf2/read-debug-names.c index 0add8040894..0d60b01f408 100644 --- a/gdb/dwarf2/read-debug-names.c +++ b/gdb/dwarf2/read-debug-names.c @@ -352,7 +352,7 @@ cooked_index_debug_names::do_reading () cooked_index *table = (gdb::checked_static_cast (per_bfd->index_table.get ())); - table->set_contents (std::move (indexes)); + table->set_contents (std::move (indexes), &m_warnings); bfd_thread_cleanup (); } diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 4afb026b8ce..390224dfe1c 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -4923,7 +4923,7 @@ cooked_index_debug_info::done_reading () cooked_index *table = (gdb::checked_static_cast (per_bfd->index_table.get ())); - table->set_contents (std::move (indexes)); + table->set_contents (std::move (indexes), &m_warnings); } void diff --git a/gdb/utils.h b/gdb/utils.h index 2acd1fc4624..d7db1d84e2f 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -21,6 +21,7 @@ #include "exceptions.h" #include "gdbsupport/array-view.h" +#include "gdbsupport/function-view.h" #include "gdbsupport/scoped_restore.h" #include @@ -374,7 +375,7 @@ assign_return_if_changed (T &lval, const T &val) } /* A function that can be used to intercept warnings. */ -typedef void (*warning_hook_handler) (const char *, va_list); +typedef gdb::function_view warning_hook_handler; /* Set the thread-local warning hook, and restore the old value when finished. */ @@ -439,6 +440,17 @@ struct deferred_warnings m_warnings.emplace_back (std::move (msg)); } + /* A variant of 'warn' so that this object can be used as a warning + hook; see scoped_restore_warning_hook. Note that no locking is + done, so users have to be careful to only install this into a + single thread at a time. */ + void operator() (const char *format, va_list args) ATTRIBUTE_PRINTF (2, 0) + { + string_file msg (m_can_style); + msg.vprintf (format, args); + m_warnings.emplace_back (std::move (msg)); + } + /* Emit all warnings. */ void emit () const { -- 2.43.0