From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from qproxy5-pub.mail.unifiedlayer.com (qproxy5-pub.mail.unifiedlayer.com [69.89.21.30]) by sourceware.org (Postfix) with ESMTPS id A628A3857C71 for ; Mon, 4 Apr 2022 19:54:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A628A3857C71 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from outbound-ss-761.bluehost.com (outbound-ss-761.bluehost.com [74.220.211.250]) by qproxy5.mail.unifiedlayer.com (Postfix) with ESMTP id DB9EF80347E5 for ; Mon, 4 Apr 2022 19:54:45 +0000 (UTC) Received: from cmgw12.mail.unifiedlayer.com (unknown [10.0.90.127]) by progateway8.mail.pro1.eigbox.com (Postfix) with ESMTP id 9C074100AF0D2 for ; Mon, 4 Apr 2022 19:53:45 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id bSllnZcMkb2WGbSllntAsu; Mon, 04 Apr 2022 19:53:45 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=cZ8XElPM c=1 sm=1 tr=0 ts=624b4cc9 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=z0gMJWrwH1QA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=yTBxE7gOT9IeEaDc_ZIA:9 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=XzrbH5Xqxf8T4qcVGhn3J3/Ax/VEmchVqtwU23aNsgs=; b=ePi8OLGKASC24wIG98wJIpCBEr BTtaRN0Gq9NBM4GhU5rgXA8IesiicGVIqnDPqMcoymR+1P5hKLVY5AS/fYP6VO8m+GT14RXcneVye q49SMPzd0+sMRIWAcNj52Oq44; Received: from 71-211-176-135.hlrn.qwest.net ([71.211.176.135]:34802 helo=prentzel.Home) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nbSlk-003uHX-Uh; Mon, 04 Apr 2022 13:53:45 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v4 22/34] Introduce thread-safe handling for complaints Date: Mon, 4 Apr 2022 13:53:23 -0600 Message-Id: <20220404195335.2111906-23-tom@tromey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404195335.2111906-1-tom@tromey.com> References: <20220404195335.2111906-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: 71.211.176.135 X-Source-L: No X-Exim-ID: 1nbSlk-003uHX-Uh X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 71-211-176-135.hlrn.qwest.net (prentzel.Home) [71.211.176.135]:34802 X-Source-Auth: tom+tromey.com X-Email-Count: 30 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3030.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: Mon, 04 Apr 2022 19:54:48 -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 122f2c2810d..7e39f241938 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 + gdb_printf (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 e3d5d4474e7..09d8cd39b27 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.34.1