From: Christian Biesinger <cbiesinger@google.com>
To: gdb-patches@sourceware.org
Subject: [PATCH] Use RAII to set the per-thread SIGSEGV handler
Date: Tue, 9 Mar 2021 08:23:47 -0600 [thread overview]
Message-ID: <20210309142347.2927653-1-cbiesinger@google.com> (raw)
(If accepted, I would also like to merge this to the gdb 10 branch because this
fixes a build error on cygwin.)
PR threads/27239
This avoids using a thread-local extern variable, which causes link errors
on some platforms, notably Cygwin. But I think this is a better pattern
even outside of working around linker bugs because it encapsulates direct
access to the variable inside the class, instead of having a global extern
variable.
2021-03-09 Christian Biesinger <cbiesinger@google.com>
* cp-support.c: Use scoped_segv_handler_restore.
* event-top.c (thread_local_segv_handler): Made static.
(scoped_segv_handler_restore::scoped_segv_handler_restore):
New function.
(scoped_segv_handler_restore::~scoped_segv_handler_restore): New
function.
* event-top.h (class scoped_segv_handler_restore): New class.
(thread_local_segv_handler): Removed.
---
gdb/cp-support.c | 9 ++++-----
gdb/event-top.c | 18 ++++++++++++++++--
gdb/event-top.h | 19 ++++++++++++++-----
3 files changed, 34 insertions(+), 12 deletions(-)
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 1abc3e22a72..fb4e4289777 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1615,11 +1615,10 @@ gdb_demangle (const char *name, int options)
int crash_signal = 0;
#ifdef HAVE_WORKING_FORK
- scoped_restore restore_segv
- = make_scoped_restore (&thread_local_segv_handler,
- catch_demangler_crashes
- ? gdb_demangle_signal_handler
- : nullptr);
+ scoped_segv_handler_restore restore_segv
+ (catch_demangler_crashes
+ ? gdb_demangle_signal_handler
+ : nullptr);
bool core_dump_allowed = gdb_demangle_attempt_core_dump;
SIGJMP_BUF jmp_buf;
diff --git a/gdb/event-top.c b/gdb/event-top.c
index a33f6425140..79cc411fb10 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -850,9 +850,12 @@ gdb_readline_no_editing_callback (gdb_client_data client_data)
}
\f
-/* See event-top.h. */
+/* The SIGSEGV handler for this thread, or NULL if there is none. GDB
+ always installs a global SIGSEGV handler, and then lets threads
+ indicate their interest in handling the signal by setting this
+ thread-local variable. */
-thread_local void (*thread_local_segv_handler) (int);
+static thread_local void (*thread_local_segv_handler) (int);
static void handle_sigsegv (int sig);
@@ -1288,6 +1291,17 @@ gdb_disable_readline (void)
delete_file_handler (ui->input_fd);
}
+scoped_segv_handler_restore::scoped_segv_handler_restore (segv_handler_t new_handler)
+{
+ m_old_handler = thread_local_segv_handler;
+ thread_local_segv_handler = new_handler;
+}
+
+scoped_segv_handler_restore::~scoped_segv_handler_restore()
+{
+ thread_local_segv_handler = m_old_handler;
+}
+
static const char debug_event_loop_off[] = "off";
static const char debug_event_loop_all_except_ui[] = "all-except-ui";
static const char debug_event_loop_all[] = "all";
diff --git a/gdb/event-top.h b/gdb/event-top.h
index c52826eb063..b036f1385c5 100644
--- a/gdb/event-top.h
+++ b/gdb/event-top.h
@@ -70,10 +70,19 @@ extern void gdb_rl_callback_handler_install (const char *prompt);
currently installed. */
extern void gdb_rl_callback_handler_reinstall (void);
-/* The SIGSEGV handler for this thread, or NULL if there is none. GDB
- always installs a global SIGSEGV handler, and then lets threads
- indicate their interest in handling the signal by setting this
- thread-local variable. */
-extern thread_local void (*thread_local_segv_handler) (int);
+typedef void (*segv_handler_t) (int);
+
+/* On construction, replaces the current thread's SIGSEGV handler with
+ the provided one. On destruction, restores the handler to the
+ original one. */
+class scoped_segv_handler_restore
+{
+ public:
+ scoped_segv_handler_restore (segv_handler_t new_handler);
+ ~scoped_segv_handler_restore ();
+
+ private:
+ segv_handler_t m_old_handler;
+};
#endif
--
2.30.1.766.gb4fecdf3b7-goog
next reply other threads:[~2021-03-09 14:23 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-09 14:23 Christian Biesinger [this message]
2021-03-09 17:00 ` [PATCH v2] " Christian Biesinger
2021-03-12 14:27 ` Tom Tromey
2021-03-12 17:30 ` Christian Biesinger
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=20210309142347.2927653-1-cbiesinger@google.com \
--to=cbiesinger@google.com \
--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).