From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id E1C6E3858C01 for ; Tue, 24 Oct 2023 09:48:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E1C6E3858C01 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 E1C6E3858C01 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=1698140933; cv=none; b=gGMmZAZconffTM5yb1UrRKoUSg/lbQG2lNrq+AqgfvtYd51olAebcwten/eG6d4j73zbZv7AygtkSItzTC2Kvr7Emo5iB+MWgzb/rZdkqVc88/7vEAnysNrLFaxMG9X9uB32CvMrLgj8/kykYQblLbyoaqDWhm05UqGnpmSeW+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698140933; c=relaxed/simple; bh=QcwRNICGLhTVEHcOMT1NMCdneIjoosJwLFzXYAvGdPo=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=BqKYjnBs/HTQ6Ayk5yhNobGTZzF5rFkSiSmjz4g6yXBuI4BIFMSBo/oyAZYg+MUp6vW3GXeAkZen/WMassiW/vCBYU4t68oJGjWIXrILr1aVfTEJeS+WuRl9KqYmrt0QrsU23myU9TeohyVC96gvF81W041Fst6RqdC4qnnF780= 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 96F4E1FD71 for ; Tue, 24 Oct 2023 09:48:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1698140926; 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=DCoodf1+/sC59b2GOgTijxlyq3Pv3TTetfJ537DlUMI=; b=wjTLvjQzDRYl5j+XoCC4Ygq8jR/WZRW78Wduqk043Wd6FYCVUhxoj+nRLfoHgm4jpvNvFd UjRYWgJKQSDiMDUHR7TvU31eI46rMmHW06SvJOUmvwidbH6itpy3g8Cowf/c49eAU0fv4t FDHtwlkrVKnV0fO0jwWm7Hg7RvxzJ2Y= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1698140926; 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=DCoodf1+/sC59b2GOgTijxlyq3Pv3TTetfJ537DlUMI=; b=BOm8XQRyTd20zQEFk3K1IG/RCBUPF25lFJzOgbnl9mHKFXjrX1t7jsIfmoMlc9dtC4e43a NFYfDfzvE0sMocCg== 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 798E813A92 for ; Tue, 24 Oct 2023 09:48:46 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id aNg5HP6SN2UhEQAAMHmgww (envelope-from ) for ; Tue, 24 Oct 2023 09:48:46 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v5 5/6] [gdb/cli] Ask if source highlighting should be interrupted Date: Tue, 24 Oct 2023 11:49:32 +0200 Message-Id: <20231024094933.12009-6-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231024094933.12009-1-tdevries@suse.de> References: <20231024094933.12009-1-tdevries@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -2.10 X-Spamd-Result: default: False [-2.10 / 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_LONG(-3.00)[-1.000]; NEURAL_HAM_SHORT(-1.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=-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 List-Id: When source highlighting is done using the GNU source-highlight library, and it takes a long time, it can be interrupted using ^C, but that cancels the rest of the command: ... $ 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 ^C (gdb) ... That means in this case that the user doesn't get to see the line gdb stopped at. This is especially problematic if the user actually meant to interrupt the inferior execution. Instead, ask the user whether highlighting needs to be interrupted, and if so, interrupt only the highlighting part, and continue with the command: ... $ 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 after cancelling, line 56 still can be highlighted, just by pygments instead of source-highlight. Co-Authored-By: Lancelot Six Co-Authored-By: Pedro Alves Tested on x86_64-linux. --- gdb/source-cache.c | 71 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/gdb/source-cache.c b/gdb/source-cache.c index 2d59957f42c..6f514cbba3e 100644 --- a/gdb/source-cache.c +++ b/gdb/source-cache.c @@ -43,6 +43,7 @@ #if GDB_SELF_TEST #include "gdbsupport/selftest.h" #include "event-top.h" +#include "top.h" #endif /* The number of source files we'll cache. */ @@ -200,10 +201,56 @@ get_language_name (enum language lang) class gdb_highlight_event_listener : public srchilite::HighlightEventListener { public: + explicit gdb_highlight_event_listener (const std::string &fullname) + : m_fullname (fullname) + { + } + void notify (const srchilite::HighlightEvent &event) override { - QUIT; + /* If target_terminal::is_ours, we can ask the user a question and get an + answer. We're not sure if or how !target_terminal::is_ours can happen. + Assert to catch it happening. Note that we're asserting before + checking the quit flag, such that we don't rely on the user issuing + a SIGINT or SIGTERM to detect this. */ + gdb_assert (target_terminal::is_ours ()); + + if (!check_quit_flag ()) + { + /* No SIGTERM or SIGINT pending, nothing to do. */ + return; + } + + /* If we got a SIGTERM, skip querying. This isn't strictly + necessary as yquery runs a nested event loop for the secondary + prompt, which runs pending async signal handlers. However, + this helps with the unit test, which makes sure that the quit + call at the bottom throws a gdb_exception_forced_quit exception + and that our caller doesn't swallow it. Note we may receive a + SIGTERM in between the query and the quit call. */ + if (!sync_quit_force_run) + { + /* Ask the user what to do. */ + int resp + = (yquery + (_("Cancel source styling using GNU source highlight of %s? "), + m_fullname.c_str ())); + if (!resp) + { + /* Continue highlighting. */ + return; + } + } + + /* Interrupt highlighting. Note we don't abort via the QUIT macro + as that may do nothing. E.g. if the current quit_handler is + infrun_quit_handler, and target_terminal::is_ours, pressing ^C + is ignored by QUIT. */ + quit (); } + +private: + const std::string &m_fullname; }; #endif /* HAVE_SOURCE_HIGHLIGHT */ @@ -242,9 +289,6 @@ try_source_highlight (std::string &contents ATTRIBUTE_UNUSED, const std::string &datadir = srchilite::Settings::retrieveDataDir (); langmap = new srchilite::LangMap (datadir, "lang.map"); - - static gdb_highlight_event_listener event_listener; - highlighter->setHighlightEventListener (&event_listener); } std::string detected_lang; @@ -256,6 +300,12 @@ try_source_highlight (std::string &contents ATTRIBUTE_UNUSED, lang_name = detected_lang.c_str (); } + gdb_highlight_event_listener event_listener (fullname); + highlighter->setHighlightEventListener (&event_listener); + /* Make sure that the highlighter's EventListener doesn't become a + dangling pointer. */ + SCOPE_EXIT { highlighter->setHighlightEventListener (nullptr); }; + std::istringstream input (contents); std::ostringstream output; highlighter->highlight (input, output, lang_name, fullname); @@ -269,8 +319,7 @@ try_source_highlight (std::string &contents ATTRIBUTE_UNUSED, } catch (const gdb_exception_quit &) { - /* SIGINT, rethrow. */ - throw; + /* SIGINT, ignore. */ } catch (...) { @@ -356,6 +405,9 @@ static void gnu_source_highlight_test () if (!a_signal_handler_selftest.valid ()) return; + scoped_restore save_confirm + = make_scoped_restore (&confirm, false); + /* Pretend user sent SIGINT by calling the signal handler. */ handle_sigint (SIGINT); @@ -370,7 +422,7 @@ static void gnu_source_highlight_test () { saw_quit = true; } - SELF_CHECK (saw_quit); + SELF_CHECK (!saw_quit); SELF_CHECK (!res); SELF_CHECK (prog == styled_prog); } @@ -439,13 +491,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); } } -- 2.35.3