From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway20.websitewelcome.com (gateway20.websitewelcome.com [192.185.47.18]) by sourceware.org (Postfix) with ESMTPS id 9982A3857816 for ; Thu, 4 Nov 2021 18:10:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9982A3857816 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=tromey.com Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway20.websitewelcome.com (Postfix) with ESMTP id 7252B4012C1D6 for ; Thu, 4 Nov 2021 13:09:14 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id ihAomB9KaG0jLihAomElSo; Thu, 04 Nov 2021 13:09:14 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: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=JHWXOCmU8aw/9TPJ56q6GNvaN2KOBPOdTy6YtnMj0fs=; b=EfPCwFvw5KubWO5yqwQuVcJaOQ uwHq9OPCsjr9USMz71WE0Cr4bAeeGmhdOGklculRysGYVi9NBsfC/6RMfSjFBf5hVWu68mQYdOu0A dfvoM0+NKUr09BPXa7c62kU16; Received: from 75-166-134-234.hlrn.qwest.net ([75.166.134.234]:51960 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1mihAo-003H6y-6N; Thu, 04 Nov 2021 12:09:14 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 20/32] Introduce thread-safe handling for complaints Date: Thu, 4 Nov 2021 12:08:55 -0600 Message-Id: <20211104180907.2360627-21-tom@tromey.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211104180907.2360627-1-tom@tromey.com> References: <20211104180907.2360627-1-tom@tromey.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: 75.166.134.234 X-Source-L: No X-Exim-ID: 1mihAo-003H6y-6N X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-134-234.hlrn.qwest.net (localhost.localdomain) [75.166.134.234]:51960 X-Source-Auth: tom+tromey.com X-Email-Count: 25 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3032.1 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_PASS, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Nov 2021 18:10:17 -0000 This introduces a new class that can be used to make the "complaint" code thread-safe. Instantiating the class installs a new handler that collects complaints, and then prints them all when the object is destroyed. This approach requires some locks. I couldn't think of a better way to handle this, though, because the I/O system is not thread-safe. It seemed to me that only GDB developers are likely to enable complaints, and because the complaint macro handle this case already (before any locks are required), I reasoned that any performance degradation that would result here would be fine. As an aside about complaints -- are they useful at all? I just ignore them, myself, since mostly they seem to indicate compiler problems that can't be solved in the GDB world anyway. I'd personally prefer them to be in a separate tool, like a hypothetical 'dwarflint'. --- gdb/complaints.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++-- gdb/complaints.h | 33 +++++++++++++++++++++++ 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/gdb/complaints.c b/gdb/complaints.c index a823fcb0020..65fa2afe499 100644 --- a/gdb/complaints.c +++ b/gdb/complaints.c @@ -23,6 +23,7 @@ #include "gdbcmd.h" #include "gdbsupport/selftest.h" #include +#include /* Map format strings to counters. */ @@ -34,6 +35,10 @@ static std::unordered_map counters; int stop_whining = 0; +#if CXX_STD_THREAD +static std::mutex complaint_mutex; +#endif /* CXX_STD_THREAD */ + /* See complaints.h. */ void @@ -41,8 +46,13 @@ complaint_internal (const char *fmt, ...) { va_list args; - if (++counters[fmt] > stop_whining) - return; + { +#if CXX_STD_THREAD + std::lock_guard guard (complaint_mutex); +#endif + if (++counters[fmt] > stop_whining) + return; + } va_start (args, fmt); @@ -66,6 +76,60 @@ clear_complaints () counters.clear (); } +/* See complaints.h. */ + +complaint_interceptor *complaint_interceptor::g_complaint_interceptor; + +/* See complaints.h. */ + +complaint_interceptor::complaint_interceptor () + : m_saved_warning_hook (deprecated_warning_hook) +{ + /* These cannot be stacked. */ + gdb_assert (g_complaint_interceptor == nullptr); + g_complaint_interceptor = this; + deprecated_warning_hook = issue_complaint; +} + +/* A helper that wraps a warning hook. */ + +static void +wrap_warning_hook (void (*hook) (const char *, va_list), ...) +{ + va_list args; + va_start (args, hook); + hook ("%s", args); + va_end (args); +} + +/* See complaints.h. */ + +complaint_interceptor::~complaint_interceptor () +{ + for (const std::string &str : m_complaints) + { + if (m_saved_warning_hook) + wrap_warning_hook (m_saved_warning_hook, str.c_str ()); + else + fprintf_filtered (gdb_stderr, _("During symbol reading: %s\n"), + str.c_str ()); + } + + g_complaint_interceptor = nullptr; + deprecated_warning_hook = m_saved_warning_hook; +} + +/* See complaints.h. */ + +void +complaint_interceptor::issue_complaint (const char *fmt, va_list args) +{ +#if CXX_STD_THREAD + std::lock_guard guard (complaint_mutex); +#endif + g_complaint_interceptor->m_complaints.insert (string_vprintf (fmt, args)); +} + static void complaints_show_value (struct ui_file *file, int from_tty, struct cmd_list_element *cmd, const char *value) diff --git a/gdb/complaints.h b/gdb/complaints.h index 485dbb15860..144ce09fa5d 100644 --- a/gdb/complaints.h +++ b/gdb/complaints.h @@ -21,6 +21,8 @@ #if !defined (COMPLAINTS_H) #define COMPLAINTS_H +#include + /* Helper for complaint. */ extern void complaint_internal (const char *fmt, ...) ATTRIBUTE_PRINTF (1, 2); @@ -46,5 +48,36 @@ extern int stop_whining; extern void clear_complaints (); +/* A class that can handle calls to complaint from multiple threads. + When this is instantiated, it hooks into the complaint mechanism, + so the 'complaint' macro can continue to be used. When it is + destroyed, it issues all the complaints that have been stored. It + should only be instantiated in the main thread. */ + +class complaint_interceptor +{ +public: + + complaint_interceptor (); + ~complaint_interceptor (); + + DISABLE_COPY_AND_ASSIGN (complaint_interceptor); + +private: + + /* The issued complaints. */ + std::unordered_set m_complaints; + + /* The saved value of deprecated_warning_hook. */ + void (*m_saved_warning_hook) (const char *, va_list) + ATTRIBUTE_FPTR_PRINTF(1,0); + + /* A helper function that is used by the 'complaint' implementation + to issue a complaint. */ + static void issue_complaint (const char *, va_list); + + /* This object. Used by the static callback function. */ + static complaint_interceptor *g_complaint_interceptor; +}; #endif /* !defined (COMPLAINTS_H) */ -- 2.31.1