public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH v5 4/6] [gdb/cli] Allow source highlighting to be interrupted
Date: Tue, 24 Oct 2023 11:49:31 +0200	[thread overview]
Message-ID: <20231024094933.12009-5-tdevries@suse.de> (raw)
In-Reply-To: <20231024094933.12009-1-tdevries@suse.de>

Currently highlighting by the GNU source-highlight library cannot be
interrupted by either SIGINT or SIGTERM.

Fix this by installing a srchilite::HighlightEventListener that:
- checks whether SIGINT or SIGTERM was issued, and if so
- interrupts the command.

Interrupting the highlighting with SIGINT (using ^C) looks like this to the
user:
...
$ gdb -q a.out
Reading symbols from a.out...
(gdb) list
^CQuit
(gdb)
...
and with SIGTERM (using kill -SIGTERM $(pidof gdb)):
...
$ gdb -q a.out
Reading symbols from a.out...
(gdb) list
$
...

Tested on x86_64-linux.
---
 gdb/source-cache.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)

diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index c955929b543..2d59957f42c 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -37,10 +37,12 @@
 #include <srchilite/sourcehighlight.h>
 #include <srchilite/langmap.h>
 #include <srchilite/settings.h>
+#include <srchilite/highlighteventlistener.h>
 #endif
 
 #if GDB_SELF_TEST
 #include "gdbsupport/selftest.h"
+#include "event-top.h"
 #endif
 
 /* The number of source files we'll cache.  */
@@ -193,6 +195,17 @@ get_language_name (enum language lang)
   return nullptr;
 }
 
+/* Class with notify function called during highlighting.  */
+
+class gdb_highlight_event_listener : public srchilite::HighlightEventListener
+{
+public:
+  void notify (const srchilite::HighlightEvent &event) override
+  {
+    QUIT;
+  }
+};
+
 #endif /* HAVE_SOURCE_HIGHLIGHT */
 
 /* Try to highlight CONTENTS from file FULLNAME in language LANG using
@@ -229,6 +242,9 @@ 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;
@@ -246,6 +262,16 @@ try_source_highlight (std::string &contents ATTRIBUTE_UNUSED,
       contents = std::move (output).str ();
       styled = true;
     }
+  catch (const gdb_exception_forced_quit &)
+    {
+      /* SIGTERM, rethrow.  */
+      throw;
+    }
+  catch (const gdb_exception_quit &)
+    {
+      /* SIGINT, rethrow.  */
+      throw;
+    }
   catch (...)
     {
       /* Source Highlight will throw an exception if
@@ -293,6 +319,61 @@ static void gnu_source_highlight_test ()
     SELF_CHECK (prog.size () < styled_prog.size ());
   else
     SELF_CHECK (prog == styled_prog);
+
+  if (!res)
+    {
+      /* No styling happened, bail out for the rest of the test.  */
+      return;
+    }
+
+  {
+    selftests::signal_handler_selftest a_signal_handler_selftest;
+    if (!a_signal_handler_selftest.valid ())
+      return;
+
+    /* Pretend user sent SIGTERM by calling the signal handler.  */
+    handle_sigterm (SIGTERM);
+
+    bool saw_forced_quit = false;
+    res = false;
+    styled_prog = prog;
+    try
+      {
+	res = try_source_highlight (styled_prog, language_c, fullname);
+      }
+    catch (const gdb_exception_forced_quit &ex)
+      {
+	saw_forced_quit = true;
+      }
+
+    SELF_CHECK (saw_forced_quit);
+    SELF_CHECK (!res);
+    SELF_CHECK (prog == styled_prog);
+  }
+
+  {
+    selftests::signal_handler_selftest a_signal_handler_selftest;
+    if (!a_signal_handler_selftest.valid ())
+      return;
+
+    /* Pretend user sent SIGINT by calling the signal handler.  */
+    handle_sigint (SIGINT);
+
+    bool saw_quit = false;
+    res = false;
+    styled_prog = prog;
+    try
+      {
+	res = try_source_highlight (styled_prog, language_c, fullname);
+      }
+    catch (const gdb_exception_quit &ex)
+      {
+	saw_quit = true;
+      }
+    SELF_CHECK (saw_quit);
+    SELF_CHECK (!res);
+    SELF_CHECK (prog == styled_prog);
+  }
 }
 }
 #endif /* GDB_SELF_TEST */
-- 
2.35.3


  parent reply	other threads:[~2023-10-24  9:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-24  9:49 [PATCH v5 0/6] " Tom de Vries
2023-10-24  9:49 ` [PATCH v5 1/6] [gdb/cli] Add signal_handler_selftest Tom de Vries
2023-11-22 16:26   ` Tom de Vries
2023-10-24  9:49 ` [PATCH v5 2/6] [gdb/python] Make python selftest more robust Tom de Vries
2023-10-24  9:49 ` [PATCH v5 3/6] [gdb/cli] Add gnu-source-highlight selftest Tom de Vries
2023-10-24 12:33   ` Tom de Vries
2023-10-24  9:49 ` Tom de Vries [this message]
2023-10-24  9:49 ` [PATCH v5 5/6] [gdb/cli] Ask if source highlighting should be interrupted Tom de Vries
2023-10-24  9:49 ` [PATCH v5 6/6] [gdb/cli] Simplify gdb_highlight_event_listener::notify Tom de Vries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231024094933.12009-5-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).