From patchwork Mon Oct 16 09:17:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 77872 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 C44283858423 for ; Mon, 16 Oct 2023 09:18:12 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 6D8C73858284 for ; Mon, 16 Oct 2023 09:17:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6D8C73858284 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6D8C73858284 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697447850; cv=none; b=DGNgKYDjfzQTuCptDhc7NybRMQdVG0XrKaDxzz3TxXm5EI2iqmgHsYarfkPf6AhIfeeTqtbwz0xYrVZz0mqqwzA+duDbu8AedkqiKtQRqooZmA2acibiXDjkc4GOtj2h2xc7qBkjO6FPuoHKYsycbCvM7KgCzGUkIQwxZydGRUA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697447850; c=relaxed/simple; bh=PMrwwqDZFupoQSYDTewy/Xahn+NsTB5ZvFdyEJu9hlc=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=c0GQ0pLV3rS/o++bbJQkVHOhsqMcXEDkuZsvcAlR1K2R3XaSs1xVQJZ/iiijMu4tzsNLKy0wWKybKjunxr2jLig0sGJTALfV4HriUZt3rkwvsJn/fMKWrzeuhc/bs6Zzl1C2wmHgYVK6otTx+crpPMyVE8/kFQV8Zpz+RVIVFPk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 3EF1E1FEB4 for ; Mon, 16 Oct 2023 09:17:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697447847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Lkb6i5wCdqznPkojMZiwuLFh85CElWnO2Y1fWJ7tO0s=; b=OyLgitAS2F8GMLeOND6h5ykcig1ktwWdADcXjqL3SsMql5+Vz7B612UHSZsJ6s/P+/ciZJ V6AUZ6VNYtSKmZRIlE2UA6J93txd7rZSqXLwswg31DOPuVZRyE+1hVueJD94fWiXq3RIMW JigK1yHW3hs5pvo6s8DBklX7i26y8ls= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697447847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Lkb6i5wCdqznPkojMZiwuLFh85CElWnO2Y1fWJ7tO0s=; b=3kP0jOVgMFp+WiUQteZ5an1ljfJWz5+VKBIFIWW02sp7RCOQEZkFqvDSOC95vI7VU5symF 1zVF8rhFnXE44PCw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 2DC30139F9 for ; Mon, 16 Oct 2023 09:17:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id WLcnCqf/LGUXLQAAMHmgww (envelope-from ) for ; Mon, 16 Oct 2023 09:17:27 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v3 1/4] [gdb/cli] Skip string copy in source_cache::ensure Date: Mon, 16 Oct 2023 11:17:45 +0200 Message-Id: <20231016091748.26247-2-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231016091748.26247-1-tdevries@suse.de> References: <20231016091748.26247-1-tdevries@suse.de> MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spam-Score: 4.11 X-Spamd-Result: default: False [4.11 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_MISSING_CHARSET(2.50)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCPT_COUNT_ONE(0.00)[1]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.98)[-0.976]; NEURAL_SPAM_LONG(3.00)[1.000]; MID_CONTAINS_FROM(1.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-2.81)[99.20%] X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org In function source_cache::ensure we have: ... std::ostringstream output; ... contents = output.str(); ... The last line causes an unnecessary string copy. C++20 allows us to skip it, like this: ... contents = std::move (output).str(); ... Use the more efficient solution. Tested on x86_64-linux. --- gdb/source-cache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdb/source-cache.c b/gdb/source-cache.c index 77b357cb42b..ae02d2516d9 100644 --- a/gdb/source-cache.c +++ b/gdb/source-cache.c @@ -252,7 +252,7 @@ source_cache::ensure (struct symtab *s) std::istringstream input (contents); std::ostringstream output; highlighter->highlight (input, output, lang_name, fullname); - contents = output.str (); + contents = std::move (output).str (); already_styled = true; } catch (...) From patchwork Mon Oct 16 09:17:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 77870 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 4787538560AA for ; Mon, 16 Oct 2023 09:17:47 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 60C62385842C for ; Mon, 16 Oct 2023 09:17:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 60C62385842C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 60C62385842C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697447850; cv=none; b=FxT2KCr26GTucF1iEwKLTVDYW4WNoT6AEOZrrXSKFaPBbFBR5CeHdbePS0hz/yArHtQ/Gp251clpnGLgV/Ow5T3LT4vuTtKZRNYDC+55KiIKrWUbvfqfDIJ5C5hfhgjq6Oj1/fBUXaq8OjftNitJpRVYAYdj4EmFKQ94P5xnF+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697447850; c=relaxed/simple; bh=sbM8d21bgC8kiaDoGuyqqK1nw9fPYR4nU1fBPSb6uFI=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=e6A8Oksyyr2bVTJRMhMMgpp6ovkxCMXehOtSf/H9GgOLB8utiNhB6UKzOM/nMKFmSkFBD4S2mnrY94BJUTqO7YU6o02Gse5Lavy8msCBQFzZA2jersGksjiOLUnaFIx5lTRM2AaIVSURklurQ/k4ljTpAqvWef4NswWn/OvLmnI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 596221FEB7 for ; Mon, 16 Oct 2023 09:17:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697447847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JGb/vVp36UHzsoogsr1awMSvJS4+/6M8VuhFWeQM+q0=; b=cWR9k75u0qBe8HItUZZIO6CwZRmXHmFduJPKFl1t/qzfCpW6cMm4Ci+0gi76EUwWSjEAXD VeClaLXH1DCKeLIm7nesFl0L4ZZCSNpjxwq+h7FhX6Y3dQZc8P9BYmVn49kvnnqYpFXyto xUw2flsnrGxBxKqxWzS2lQLMFcxy3K8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697447847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JGb/vVp36UHzsoogsr1awMSvJS4+/6M8VuhFWeQM+q0=; b=UelNxbOiGZTd7h4ckM82CxlRS95XXst9zeC4xJ2+ZZxp17Bow6vRFHNLQCkhF5e7pp49qa UaeN7L1ELp8bANDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 4266E13A20 for ; Mon, 16 Oct 2023 09:17:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 2Mk6D6f/LGUXLQAAMHmgww (envelope-from ) for ; Mon, 16 Oct 2023 09:17:27 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v3 2/4] [gdb/cli] Factor out try_source_highlight Date: Mon, 16 Oct 2023 11:17:46 +0200 Message-Id: <20231016091748.26247-3-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231016091748.26247-1-tdevries@suse.de> References: <20231016091748.26247-1-tdevries@suse.de> MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spam-Score: 4.96 X-Spamd-Result: default: False [4.96 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_MISSING_CHARSET(2.50)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCPT_COUNT_ONE(0.00)[1]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.97)[-0.974]; NEURAL_SPAM_LONG(3.00)[1.000]; MID_CONTAINS_FROM(1.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-1.96)[94.84%] X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Function source_cache::ensure contains some code using the GNU source-highlight library. The code is a sizable part of the function, and contains conditional compilation in a slightly convoluted way: ... if (!already_styled) #endif /* HAVE_SOURCE_HIGHLIGHT */ { ... Fix this by factoring out the code into new function try_source_highlight, such that: - source_cache::ensure is easier to read, and - the conditional compilation is at the level of the function body. Tested on x86_64-linux. --- gdb/source-cache.c | 91 ++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/gdb/source-cache.c b/gdb/source-cache.c index ae02d2516d9..191d0043e66 100644 --- a/gdb/source-cache.c +++ b/gdb/source-cache.c @@ -191,6 +191,59 @@ get_language_name (enum language lang) #endif /* HAVE_SOURCE_HIGHLIGHT */ +/* Try to highlight CONTENTS from file FULLNAME in language LANG using + the GNU source-higlight library. Return true if highlighting + succeeded. */ + +static bool +try_source_highlight (std::string &contents ATTRIBUTE_UNUSED, + enum language lang ATTRIBUTE_UNUSED, + const std::string &fullname ATTRIBUTE_UNUSED) +{ +#ifdef HAVE_SOURCE_HIGHLIGHT + if (!use_gnu_source_highlight) + return false; + + const char *lang_name = get_language_name (lang); + if (lang_name == nullptr) + return false; + + /* The global source highlight object, or null if one was + never constructed. This is stored here rather than in + the class so that we don't need to include anything or do + conditional compilation in source-cache.h. */ + static srchilite::SourceHighlight *highlighter; + + bool styled = false; + try + { + if (highlighter == nullptr) + { + highlighter = new srchilite::SourceHighlight ("esc.outlang"); + highlighter->setStyleFile ("esc.style"); + } + + std::istringstream input (contents); + std::ostringstream output; + highlighter->highlight (input, output, lang_name, fullname); + contents = std::move (output).str(); + styled = true; + } + catch (...) + { + /* Source Highlight will throw an exception if + highlighting fails. One possible reason it can fail + is if the language is unknown -- which matters to gdb + because Rust support wasn't added until after 3.1.8. + Ignore exceptions here. */ + } + + return styled; +#else + return false; +#endif /* HAVE_SOURCE_HIGHLIGHT */ +} + /* See source-cache.h. */ bool @@ -230,44 +283,10 @@ source_cache::ensure (struct symtab *s) if (source_styling && gdb_stdout->can_emit_style_escape ()) { -#ifdef HAVE_SOURCE_HIGHLIGHT - bool already_styled = false; - const char *lang_name = get_language_name (s->language ()); - if (lang_name != nullptr && use_gnu_source_highlight) - { - /* The global source highlight object, or null if one was - never constructed. This is stored here rather than in - the class so that we don't need to include anything or do - conditional compilation in source-cache.h. */ - static srchilite::SourceHighlight *highlighter; - - try - { - if (highlighter == nullptr) - { - highlighter = new srchilite::SourceHighlight ("esc.outlang"); - highlighter->setStyleFile ("esc.style"); - } - - std::istringstream input (contents); - std::ostringstream output; - highlighter->highlight (input, output, lang_name, fullname); - contents = std::move (output).str (); - already_styled = true; - } - catch (...) - { - /* Source Highlight will throw an exception if - highlighting fails. One possible reason it can fail - is if the language is unknown -- which matters to gdb - because Rust support wasn't added until after 3.1.8. - Ignore exceptions here and fall back to - un-highlighted text. */ - } - } + bool already_styled + = try_source_highlight (contents, s->language (), fullname); if (!already_styled) -#endif /* HAVE_SOURCE_HIGHLIGHT */ { gdb::optional ext_contents; ext_contents = ext_lang_colorize (fullname, contents); From patchwork Mon Oct 16 09:17:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 77871 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 B84A63856243 for ; Mon, 16 Oct 2023 09:17:47 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 558E53858423 for ; Mon, 16 Oct 2023 09:17:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 558E53858423 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 558E53858423 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697447850; cv=none; b=AFRA554x76bE4bgtUJDTq9ZEoTnaO+Q5/1qkK7/yShGP5jnTiYjjhjT0szNk79KWZRLqmYEApDcMAk6lkCfW5s18tT/0D6hVcIuTgMW9Yx6j6lEwJ1KRHhrbUHUhSS61CXtAnJMJnSKdPnvqbLYG8a8aulZrJu3kT/Agto+wsHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697447850; c=relaxed/simple; bh=BzyjVPmL0n8oFpgbPoKQWVYckQNnBlOrBKucazH9AtY=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=sxaddttNmyhuvcSI6ZfmYA7al9UgcGeCLdg8XMQ/L/OheN3gWHMdenSgYmoYg2MkIf2OlJymmqETXnphUa19WSRY11sSVnbTqfkzDYHBMAD22W8XXTQ1MyArzitwdUZx5SwQpx//6jlg1oCQutJ/ZdzYjShwq3nNnGsv4pMncGo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 68EF51FEB8 for ; Mon, 16 Oct 2023 09:17:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697447847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wh0ve8TjVI7KyLRI1uGus2i3nyUEcGT2kX8laIY1qac=; b=nrdAq8Xaa1DWqVdrxoKwE4Twn/lJ3VBO7Yqqk91UR6Dtn1RjIgp39zrxgC4nJ3EcMCZ2EU GWl+09bm8LWZJiFptWJ/t2JKfww0jjnqGip6BIjj1LePMtFi01iT83aCE+SwE3uZCArNoJ TpmeN9Hoh9tg5VvKYUjAzh7qABGNT7E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697447847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wh0ve8TjVI7KyLRI1uGus2i3nyUEcGT2kX8laIY1qac=; b=b3aBeCt6tkJLu6o9CQt39jcUezc2W+VRBMAAT3W1M2+/1JySCJi1P1zHayCwe9nhvTpOm9 iyZf8vcbgR8GlOCw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 5763D139F9 for ; Mon, 16 Oct 2023 09:17:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 2DdeFKf/LGUXLQAAMHmgww (envelope-from ) for ; Mon, 16 Oct 2023 09:17:27 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v3 3/4] [gdb/cli] Keep track of styling failures in source_cache Date: Mon, 16 Oct 2023 11:17:47 +0200 Message-Id: <20231016091748.26247-4-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231016091748.26247-1-tdevries@suse.de> References: <20231016091748.26247-1-tdevries@suse.de> MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spam-Score: 3.93 X-Spamd-Result: default: False [3.93 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_MISSING_CHARSET(2.50)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCPT_COUNT_ONE(0.00)[1]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.97)[-0.975]; NEURAL_SPAM_LONG(3.00)[1.000]; MID_CONTAINS_FROM(1.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[99.99%] X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org In source_cache::ensure, keep track of which files failed to be styled, and don't attempt to style them again in case the file dropped out of the cache. Tested on x86_64-linux. --- gdb/source-cache.c | 24 ++++++++++++++++++++++-- gdb/source-cache.h | 4 ++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/gdb/source-cache.c b/gdb/source-cache.c index 191d0043e66..aa8e40425c2 100644 --- a/gdb/source-cache.c +++ b/gdb/source-cache.c @@ -281,7 +281,8 @@ source_cache::ensure (struct symtab *s) return false; } - if (source_styling && gdb_stdout->can_emit_style_escape ()) + if (source_styling && gdb_stdout->can_emit_style_escape () + && m_no_styling_files.count (fullname) == 0) { bool already_styled = try_source_highlight (contents, s->language (), fullname); @@ -291,7 +292,26 @@ source_cache::ensure (struct symtab *s) gdb::optional ext_contents; ext_contents = ext_lang_colorize (fullname, contents); if (ext_contents.has_value ()) - contents = std::move (*ext_contents); + { + contents = std::move (*ext_contents); + already_styled = true; + } + } + + if (!already_styled) + { + /* Styling failed. Styling can fail for instance for these + reasons: + - the language is not supported. + - the language cannot not be auto-detected from the file name. + - no stylers available. + + Since styling failed, don't try styling the file again after it + drops from the cache. + + Note that clearing the source cache also clears + m_no_styling_files. */ + m_no_styling_files.insert (fullname); } } diff --git a/gdb/source-cache.h b/gdb/source-cache.h index bec6598ff9f..f1d30b43ce0 100644 --- a/gdb/source-cache.h +++ b/gdb/source-cache.h @@ -66,6 +66,7 @@ class source_cache { m_source_map.clear (); m_offset_cache.clear (); + m_no_styling_files.clear (); } private: @@ -95,6 +96,9 @@ class source_cache /* The file offset cache. The key is the full name of the source file. */ std::unordered_map> m_offset_cache; + + /* The list of files where styling failed. */ + std::unordered_set m_no_styling_files; }; /* The global source cache. */ From patchwork Mon Oct 16 09:17:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 77873 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 98CE1385770B for ; Mon, 16 Oct 2023 09:18:35 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 114E73857724 for ; Mon, 16 Oct 2023 09:17:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 114E73857724 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 114E73857724 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2178:6::1c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697447851; cv=none; b=KpFH963n84nAA1KqynY5rK34T6vaQv0r3bIM8RU4+nQp4eFOpdPBg4TcAbi1LBKsJPAzhstrFVKhqLVVPGTKF46I0ijPGQswp1mRzEo+PSLc3gOAvkePNNbH2t86pOYwX7h78cLMqFBh4UquvMOV8E8xYCqsl1b6mPvt1RHJ2Zc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697447851; c=relaxed/simple; bh=v90bZ4NLaes8C1TbXg4XLRdSFG/SczifjhSEX8cE4Fk=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=lhB9+s+za9YfD2fc2d13kD2n9Xzr9Km18VShQZvdbTV5miFtoS5kW7XZWYHW4vh6LRWgcbsjR9P6CY8GF5id1R+TTYG1tp2kRQ2mWScql7fsuTgBU13xmQb0uPR5CZw01WZROYOQ8u1yjD2R3DbILYjZ6Y5JShMVIGIiV4I2CM4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 8117221B62 for ; Mon, 16 Oct 2023 09:17:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697447847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+TWlsD3fgTMx6XT6c4X4d/GHM5K/jAUAEWRSlxlcD/4=; b=e3prIEu8i5PvZtZDmKfv1xr1g6Ca07hVYX32XLfwEEwfO+12zAMXoBS9LXRRFft0qTa7I5 DgYrtgm20IkI6gnuzNQAATwT5XVVkavaW8fqKmjIcTfhWL9ZSW+B9NlSKzF8CBskPfsAUb 7Tpf7nJI1sWSqdf0vuIZIQYzsrtdmAc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697447847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+TWlsD3fgTMx6XT6c4X4d/GHM5K/jAUAEWRSlxlcD/4=; b=Jh3jXiVXNwf198bIhwLQLSOj5XIw9cK+MTFur5gJl51ikyCnJtGxF0viyrZKQmh7vy45TH amZFKKfL4pac6LDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 6D93413A20 for ; Mon, 16 Oct 2023 09:17:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id oL6vGaf/LGUXLQAAMHmgww (envelope-from ) for ; Mon, 16 Oct 2023 09:17:27 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v3 4/4] [gdb/cli] Allow source highlighting to be interrupted Date: Mon, 16 Oct 2023 11:17:48 +0200 Message-Id: <20231016091748.26247-5-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231016091748.26247-1-tdevries@suse.de> References: <20231016091748.26247-1-tdevries@suse.de> MIME-Version: 1.0 Authentication-Results: smtp-out1.suse.de; none X-Spam-Score: 3.92 X-Spamd-Result: default: False [3.92 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_MISSING_CHARSET(2.50)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; BROKEN_CONTENT_TYPE(1.50)[]; RCPT_COUNT_ONE(0.00)[1]; TO_DN_NONE(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.98)[-0.980]; NEURAL_SPAM_LONG(3.00)[1.000]; MID_CONTAINS_FROM(1.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org In PR cli/30934, a problem is reported where gdb becomes unresponsive for 2m13s because the GNU source-highlight library is taking a lot of time to annotate the source. This is due to a problem in the library [1], for which I've posted a patch [2], which brings down the annotation time to 3s. However, GDB should not become unresponsive due to such a problem. Fix this by installing a srchilite::HighlightEventListener that: - checks whether ^C was pressed, and if so - asks the user whether it should interrupt highlighting, and if so - abandons the highlighting by throwing an exception. Interrupting the highlighting looks like this to the user: ... $ gdb -q a.out -ex "b 56" Reading symbols from a.out... Breakpoint 1 at 0x400e2a: file test.cpp, line 56. (gdb) r Starting program: /data/vries/gdb/a.out Breakpoint 1, Solution::numOfSubarrays () at test.cpp:56 ^CCancel source styling using GNU source highlight of /data/vries/gdb/test.cpp? ([y] or n) y 56 return (int) t; (gdb) ... Note that line 56 still can be highlighted, just by pygments instead of source-highlight. Tested on x86_64-linux. Co-Authored-By: Lancelot Six Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30934 [1] https://savannah.gnu.org/bugs/?64747 [2] https://savannah.gnu.org/patch/index.php?10400 --- gdb/source-cache.c | 68 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/gdb/source-cache.c b/gdb/source-cache.c index aa8e40425c2..39d8bcf7aec 100644 --- a/gdb/source-cache.c +++ b/gdb/source-cache.c @@ -37,6 +37,7 @@ #include #include #include +#include #endif /* The number of source files we'll cache. */ @@ -189,6 +190,48 @@ get_language_name (enum language lang) return nullptr; } +/* Class with notify function called during highlighting. */ + +class gdb_highlight_event_listener : public srchilite::HighlightEventListener +{ +public: + gdb_highlight_event_listener (const std::string &fullname) + : m_fullname (fullname) + { + } + + void notify(const srchilite::HighlightEvent &event) override + { + /* If the terminal is ours, we can ask the user a question and get an + answer. */ + gdb_assert (target_terminal::is_ours ()); + + if (!check_quit_flag ()) + { + /* User didn't press ^C, nothing to do. */ + return; + } + + /* Ask the user what to do. */ + int resp + = yquery (_("Cancel source styling using GNU source highlight of %s?\n"), + m_fullname.c_str ()); + if (!resp) + { + /* Continue highlighting. */ + return; + } + + /* Interrupt highlighting. Note that check_quit_flag clears the + quit_flag, so we have to set it again. */ + set_quit_flag (); + QUIT; + } + +private: + const std::string &m_fullname; +}; + #endif /* HAVE_SOURCE_HIGHLIGHT */ /* Try to highlight CONTENTS from file FULLNAME in language LANG using @@ -223,9 +266,27 @@ try_source_highlight (std::string &contents ATTRIBUTE_UNUSED, highlighter->setStyleFile ("esc.style"); } + gdb_highlight_event_listener event_listener (fullname); + highlighter->setHighlightEventListener (&event_listener); + /* Make sure that the highlighter's EventListener doesn't become a + dangling pointer. */ + auto clear_event_listener = make_scope_exit ([]() + { + highlighter->setHighlightEventListener (nullptr); + }); + std::istringstream input (contents); std::ostringstream output; - highlighter->highlight (input, output, lang_name, fullname); + { + /* We want to be able to interrupt the call to source-highlight. If + the current quit_handler is infrun_quit_handler, pressing ^C is + ignored by QUIT (assuming target_terminal::is_ours), so install the + default quit handler. */ + scoped_restore restore_quit_handler + = make_scoped_restore (&quit_handler, default_quit_handler); + + highlighter->highlight (input, output, lang_name, fullname); + } contents = std::move (output).str(); styled = true; } @@ -304,13 +365,16 @@ source_cache::ensure (struct symtab *s) reasons: - the language is not supported. - the language cannot not be auto-detected from the file name. + - styling took too long and was interrupted by the user. - no stylers available. Since styling failed, don't try styling the file again after it drops from the cache. Note that clearing the source cache also clears - m_no_styling_files. */ + m_no_styling_files, so if styling took too long, and the user + interrupted it, and the source cache gets cleared, the user will + need to interrupt styling again. */ m_no_styling_files.insert (fullname); } }