public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix signal unsafe call inside a signal
@ 2024-05-21  5:00 Bernd Edlinger
  2024-05-21  8:09 ` Andreas Schwab
  0 siblings, 1 reply; 2+ messages in thread
From: Bernd Edlinger @ 2024-05-21  5:00 UTC (permalink / raw)
  To: gdb-patches

As mentioned in
https://sourceware.org/bugzilla/show_bug.cgi?id=31713#c9
it can easily happen that the signal handler function
handle_fatal_signal() uses various signal unsafe functions.
Fix that by pre-computing the necessary  language specific
strings.
---
 gdb/bt-utils.c  | 13 ++++++++---
 gdb/event-top.c | 58 +++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/gdb/bt-utils.c b/gdb/bt-utils.c
index f658ce0d4bc..c5d266a0cac 100644
--- a/gdb/bt-utils.c
+++ b/gdb/bt-utils.c
@@ -125,6 +125,8 @@ gdb_internal_backtrace_1 ()
 
 /* See the comment on previous version of this function.  */
 
+static str_backtrace_incomplete = _("Backtrace might be incomplete.\n");
+
 static void
 gdb_internal_backtrace_1 ()
 {
@@ -139,7 +141,7 @@ gdb_internal_backtrace_1 ()
 
   backtrace_symbols_fd (buffer, frames, gdb_stderr->fd ());
   if (frames == ARRAY_SIZE (buffer))
-    sig_write (_("Backtrace might be incomplete.\n"));
+    sig_write (str_backtrace_incomplete);
 }
 
 #else
@@ -149,6 +151,11 @@ gdb_internal_backtrace_1 ()
 
 /* See bt-utils.h.  */
 
+#ifdef GDB_PRINT_INTERNAL_BACKTRACE
+static const char *str_backtrace = _("----- Backtrace -----\n");
+static const char *str_backtrace_unavailable = _("Backtrace unavailable\n");
+#endif
+
 void
 gdb_internal_backtrace ()
 {
@@ -161,12 +168,12 @@ gdb_internal_backtrace ()
     gdb_stderr->write_async_safe (msg, strlen (msg));
   };
 
-  sig_write (_("----- Backtrace -----\n"));
+  sig_write (str_backtrace);
 
   if (gdb_stderr->fd () > -1)
     gdb_internal_backtrace_1 ();
   else
-    sig_write (_("Backtrace unavailable\n"));
+    sig_write (str_backtrace_unavailable);
 
   sig_write ("---------------------\n");
 #endif
diff --git a/gdb/event-top.c b/gdb/event-top.c
index 0425bcf9f99..ec75356efc5 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -891,6 +891,29 @@ unblock_signal (int sig)
   return false;
 }
 
+/* Signal safe language specific strings.  */
+
+#ifdef GDB_PRINT_INTERNAL_BACKTRACE
+static const char *str_fatal_signal = _("Fatal signal: ");
+static const char *str_sigsegv = strsignal(SIGSEGV);
+#ifdef SIGFPE
+static const char *str_sigfpe = strsignal(SIGFPE);
+#endif
+#ifdef SIGBUS
+static const char *str_sigbus = strsignal(SIGBUS);
+#endif
+#ifdef SIGABRT
+static const char *str_sigabrt = strsignal(SIGABRT);
+#endif
+static const char *str_unknown_signal = _("Unknown signal");
+static const char *str_fatal_error_detected_gdb_will_now_terminate =
+		_("A fatal error internal to GDB has been detected, "
+		  "further\ndebugging is not possible.  GDB will now "
+		  "terminate.\n\n");
+static const char *str_this_is_a_bug = _("This is a bug, please report it.");
+static const char *str_for_instructions_see = _("  For instructions, see:\n");
+#endif
+
 /* Called to handle fatal signals.  SIG is the signal number.  */
 
 static void ATTRIBUTE_NORETURN
@@ -909,19 +932,40 @@ handle_fatal_signal (int sig)
   if (bt_on_fatal_signal)
     {
       sig_write ("\n\n");
-      sig_write (_("Fatal signal: "));
-      sig_write (strsignal (sig));
+      sig_write (str_fatal_signal);
+      switch (sig)
+	{
+	case SIGSEGV:
+	  sig_write (str_sigsegv);
+	  break;
+#ifdef SIGFPE
+	case SIGFPE:
+	  sig_write (str_sigfpe);
+	  break;
+#endif
+#ifdef SIGBUS
+	case SIGBUS:
+	  sig_write (str_sigbus);
+	  break;
+#endif
+#ifdef SIGABRT
+	case SIGABRT:
+	  sig_write (str_sigabrt);
+	  break;
+#endif
+	default:
+	  sig_write (str_unknown_signal);
+	  break;
+	}
       sig_write ("\n");
 
       gdb_internal_backtrace ();
 
-      sig_write (_("A fatal error internal to GDB has been detected, "
-		   "further\ndebugging is not possible.  GDB will now "
-		   "terminate.\n\n"));
-      sig_write (_("This is a bug, please report it."));
+      sig_write (str_fatal_error_detected_gdb_will_now_terminate);
+      sig_write (str_this_is_a_bug);
       if (REPORT_BUGS_TO[0] != '\0')
 	{
-	  sig_write (_("  For instructions, see:\n"));
+	  sig_write (str_for_instructions_see);
 	  sig_write (REPORT_BUGS_TO);
 	  sig_write (".");
 	}
-- 
2.39.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix signal unsafe call inside a signal
  2024-05-21  5:00 [PATCH] Fix signal unsafe call inside a signal Bernd Edlinger
@ 2024-05-21  8:09 ` Andreas Schwab
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Schwab @ 2024-05-21  8:09 UTC (permalink / raw)
  To: Bernd Edlinger; +Cc: gdb-patches

On Mai 21 2024, Bernd Edlinger wrote:

> diff --git a/gdb/bt-utils.c b/gdb/bt-utils.c
> index f658ce0d4bc..c5d266a0cac 100644
> --- a/gdb/bt-utils.c
> +++ b/gdb/bt-utils.c
> @@ -125,6 +125,8 @@ gdb_internal_backtrace_1 ()
>  
>  /* See the comment on previous version of this function.  */
>  
> +static str_backtrace_incomplete = _("Backtrace might be incomplete.\n");

That calls gettext in a static constructor, which runs before the locale
is set.

-- 
Andreas Schwab, SUSE Labs, schwab@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-05-21  8:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-21  5:00 [PATCH] Fix signal unsafe call inside a signal Bernd Edlinger
2024-05-21  8:09 ` Andreas Schwab

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).