From patchwork Tue Dec 6 17:42:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 61599 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4A83C3875B5C for ; Tue, 6 Dec 2022 17:43:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4A83C3875B5C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670348588; bh=/vehImK1PtcUYaJ8hVJcjaUzEX+4A2x497X8MDSa0SM=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=tRszVL4DQc2MdjlvsiCO/yRG0VaYCOoe0JXJVZUpcmnS7Bk4OxnZwuzmfB9g2VZT/ xrv4VpeEnGYsna1yLhZje2hm68fYK5mRIuaOeAcWDoR2JzgIHyfyjgcM6QpVUAwz9U 0yCekn1oriJYhqeu+2rfn96FSLC3GNoEQ6pyCNNo= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 0D467383B6D3 for ; Tue, 6 Dec 2022 17:42:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0D467383B6D3 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-271-9z5zyBHjNjmch382wdYcAA-1; Tue, 06 Dec 2022 12:42:16 -0500 X-MC-Unique: 9z5zyBHjNjmch382wdYcAA-1 Received: by mail-wm1-f70.google.com with SMTP id j2-20020a05600c1c0200b003cf7397fc9bso8734429wms.5 for ; Tue, 06 Dec 2022 09:42:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/vehImK1PtcUYaJ8hVJcjaUzEX+4A2x497X8MDSa0SM=; b=piTR6wy+55CFhP+goRrax1WtIsLf+/mii6odKL6ccb+0nc21a5eBF01WaGXiVTNjTC OQqwgAllmjUrBiHUOSt5jGmI5OxnYLJDt6ceiV+WIlu7Xmm68/0Dley/p17mKe7JqoPC t0akTdJdrUgmhoy7qoGatoBs32EckpBqp9D71q76ZzHtuwKE+YWL1DHZU6BFHkVCNw/m eD7EBO/xFXwToskz39mb7LV5f9s5SWEkASAOL7/j94IpnPz/5ebrmj5ElAXmBAoV1kay mIkv4bLUejE6cQ9UWV1JOVF91XUGQtZklwYlV6uLoZ0uBsUnyyDZ2Giby1ErSVv95l51 0Brg== X-Gm-Message-State: ANoB5pnEzEmgrFJ6bbCH2i+BFBFYb1WRp80ZrC1rdZtp4kQaHcn2rvbc L1Rj2XUovlh8dzOENMDQuA+5YyKCUIMFf3KtOEUbgE6SJrAoFSIIEt1cyjKuTUeJ95MNoUHVjr3 wLOTtmkqb5WlXdfuG9+NvdFxUpJRDVqeT4lFHMkCKzLQFEQO+dcWxI7bH3EPqQMzD9wB2e6c2Hg == X-Received: by 2002:a05:600c:1f17:b0:3c6:c796:1071 with SMTP id bd23-20020a05600c1f1700b003c6c7961071mr20155762wmb.138.1670348535143; Tue, 06 Dec 2022 09:42:15 -0800 (PST) X-Google-Smtp-Source: AA0mqf6Z8rxrU1hTw1tQG0Q6P2x8UJba1pwRQ/kb/CgJVccpVlswzlb9sd77hZLV1vvCvVj4YJgy6A== X-Received: by 2002:a05:600c:1f17:b0:3c6:c796:1071 with SMTP id bd23-20020a05600c1f1700b003c6c7961071mr20155750wmb.138.1670348534853; Tue, 06 Dec 2022 09:42:14 -0800 (PST) Received: from localhost ([31.111.84.238]) by smtp.gmail.com with ESMTPSA id b16-20020a5d4b90000000b002422202fa7fsm14763301wrt.39.2022.12.06.09.42.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 09:42:14 -0800 (PST) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/2] gdb: add SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT Date: Tue, 6 Dec 2022 17:42:09 +0000 Message-Id: <73bdc6db114e7a511a2b91d9a9f5ac6f9f589881.1670348436.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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 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-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" After the previous commit converted symbol lookup debug to use the new debug scheme, this commit adds SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT. The previous commit updated 'set debug symbol-lookup' to use the new debug scheme, however SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT still does not exist. The reason for this is that 'set debug symbol-lookup' controls an integer variable, rather than a bool, and, depending on the value of this variable, different amounts of debug will be produced. Currently the *_SCOPED_DEBUG_ENTER_EXIT mechanism will only work for control variables of type bool, this is because the underlying scoped_debug_start_end class can only handle variables of type bool. This commit templates scoped_debug_start_end so that the class can accept either a 'bool &' or an invokable object, e.g. a lambda function, or a function pointer. The existing scoped_debug_start_end and scoped_debug_enter_exit macros in common-debug.h are updated to support scoped_debug_enter_exit being templated, however, nothing outside of common-debug.h needs to change. I've then added SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT in symtab.h, and added a couple of token uses in symtab.c. I didn't want to add too much in this first commit, this is really about updating common-debug.h to support this new ability. --- gdb/symtab.c | 4 ++ gdb/symtab.h | 7 +++ gdbsupport/common-debug.h | 92 +++++++++++++++++++++++++++++++-------- 3 files changed, 84 insertions(+), 19 deletions(-) diff --git a/gdb/symtab.c b/gdb/symtab.c index 14d81f5468d..a7a54159b6d 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -1950,6 +1950,8 @@ lookup_symbol_in_language (const char *name, const struct block *block, const domain_enum domain, enum language lang, struct field_of_this_result *is_a_field_of_this) { + SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT; + demangle_result_storage storage; const char *modified_name = demangle_for_lookup (name, lang, storage); @@ -2072,6 +2074,8 @@ lookup_symbol_aux (const char *name, symbol_name_match_type match_type, const domain_enum domain, enum language language, struct field_of_this_result *is_a_field_of_this) { + SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT; + struct block_symbol result; const struct language_defn *langdef; diff --git a/gdb/symtab.h b/gdb/symtab.h index 6eca61a759a..2d1f0cec877 100644 --- a/gdb/symtab.h +++ b/gdb/symtab.h @@ -2637,6 +2637,13 @@ extern unsigned int symbol_lookup_debug; debug_prefixed_printf_cond (symbol_lookup_debug >= 2, "symbol-lookup", fmt, \ ##__VA_ARGS__) +/* Print "symbol-lookup" enter/exit debug statements. */ + +#define SYMBOL_LOOKUP_SCOPED_DEBUG_ENTER_EXIT \ + scoped_debug_enter_exit ([&] () -> bool { \ + return symbol_lookup_debug > 0; \ + }, "symbol-lookup") + extern bool basenames_may_differ; bool compare_filenames_for_search (const char *filename, diff --git a/gdbsupport/common-debug.h b/gdbsupport/common-debug.h index 00a1e1599bc..904c1a1ea24 100644 --- a/gdbsupport/common-debug.h +++ b/gdbsupport/common-debug.h @@ -88,6 +88,7 @@ extern int debug_print_depth; it on destruction, such that nested debug statements will be printed with an indent and appear "inside" this one. */ +template struct scoped_debug_start_end { /* DEBUG_ENABLED is a reference to a variable that indicates whether debugging @@ -95,35 +96,35 @@ struct scoped_debug_start_end separately at construction and destruction, such that the start statement could be printed but not the end statement, or vice-versa. + DEBUG_ENABLED should either be of type 'bool &' or should be a type + that can be invoked. + MODULE and FUNC are forwarded to debug_prefixed_printf. START_PREFIX and END_PREFIX are the statements to print on construction and destruction, respectively. If the FMT format string is non-nullptr, then a `: ` is appended to the - messages, followed by the rendering of that format string. The format - string is rendered during construction and is re-used as is for the - message on exit. */ + messages, followed by the rendering of that format string with ARGS. + The format string is rendered during construction and is re-used as is + for the message on exit. */ - scoped_debug_start_end (bool &debug_enabled, const char *module, + scoped_debug_start_end (PT &debug_enabled, const char *module, const char *func, const char *start_prefix, - const char *end_prefix, const char *fmt, ...) - ATTRIBUTE_NULL_PRINTF (7, 8) + const char *end_prefix, const char *fmt, + va_list args) + ATTRIBUTE_NULL_PRINTF (7, 0) : m_debug_enabled (debug_enabled), m_module (module), m_func (func), m_end_prefix (end_prefix), m_with_format (fmt != nullptr) { - if (m_debug_enabled) + if (is_debug_enabled ()) { if (fmt != nullptr) { - va_list args; - va_start (args, fmt); m_msg = string_vprintf (fmt, args); - va_end (args); - debug_prefixed_printf (m_module, m_func, "%s: %s", start_prefix, m_msg->c_str ()); } @@ -137,6 +138,8 @@ struct scoped_debug_start_end DISABLE_COPY_AND_ASSIGN (scoped_debug_start_end); + scoped_debug_start_end (scoped_debug_start_end &&other) = default; + ~scoped_debug_start_end () { if (m_must_decrement_print_depth) @@ -145,7 +148,7 @@ struct scoped_debug_start_end --debug_print_depth; } - if (m_debug_enabled) + if (is_debug_enabled ()) { if (m_with_format) { @@ -167,7 +170,16 @@ struct scoped_debug_start_end } private: - bool &m_debug_enabled; + + /* This function is specialized based on the type PT. Returns true if + M_DEBUG_ENABLED indicates this debug setting is enabled, otherwise, + return false. */ + bool is_debug_enabled () const; + + /* Reference to the debug setting, or a callback that can read the debug + setting. Access the value of this by calling IS_DEBUG_ENABLED. */ + PT &m_debug_enabled; + const char *m_module; const char *m_func; const char *m_end_prefix; @@ -184,18 +196,60 @@ struct scoped_debug_start_end bool m_must_decrement_print_depth = false; }; +/* Implementation of is_debug_enabled when PT is an invokable type. */ + +template +inline bool +scoped_debug_start_end::is_debug_enabled () const +{ + return m_debug_enabled (); +} + +/* Implementation of is_debug_enabled when PT is 'bool &'. */ + +template<> +inline bool +scoped_debug_start_end::is_debug_enabled () const +{ + return m_debug_enabled; +} + +/* Wrapper around the scoped_debug_start_end constructor to allow the + caller to create an object using 'auto' type, the actual type will be + based on the type of the PRED argument. All arguments are forwarded to + the scoped_debug_start_end constructor. */ + +template +static inline scoped_debug_start_end ATTRIBUTE_NULL_PRINTF (6, 7) +make_scoped_debug_start_end (PT &&pred, const char *module, const char *func, + const char *start_prefix, + const char *end_prefix, const char *fmt, ...) +{ + va_list args; + va_start (args, fmt); + auto res = scoped_debug_start_end (pred, module, func, start_prefix, + end_prefix, fmt, args); + va_end (args); + + return res; +} + /* Helper to define a module-specific start/end debug macro. */ -#define scoped_debug_start_end(debug_enabled, module, fmt, ...) \ - scoped_debug_start_end CONCAT(scoped_debug_start_end, __LINE__) \ - (debug_enabled, module, __func__, "start", "end", fmt, ##__VA_ARGS__) +#define scoped_debug_start_end(debug_enabled, module, fmt, ...) \ + auto CONCAT(scoped_debug_start_end, __LINE__) \ + = make_scoped_debug_start_end (debug_enabled, module, \ + __func__, "start", "end", \ + fmt, ##__VA_ARGS__) /* Helper to define a module-specific enter/exit debug macro. This is a special case of `scoped_debug_start_end` where the start and end messages are "enter" and "exit", to denote entry and exit of a function. */ -#define scoped_debug_enter_exit(debug_enabled, module) \ - scoped_debug_start_end CONCAT(scoped_debug_start_end, __LINE__) \ - (debug_enabled, module, __func__, "enter", "exit", nullptr) +#define scoped_debug_enter_exit(debug_enabled, module) \ + auto CONCAT(scoped_debug_start_end, __LINE__) \ + = make_scoped_debug_start_end (debug_enabled, module, \ + __func__, "enter", "exit", \ + nullptr) #endif /* COMMON_COMMON_DEBUG_H */