From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/2] [gdb] Fix sig_write for null gdb_stderr
Date: Fri, 25 Apr 2025 19:18:44 +0200 [thread overview]
Message-ID: <20250425171845.9934-2-tdevries@suse.de> (raw)
In-Reply-To: <20250425171845.9934-1-tdevries@suse.de>
When running test-case gdb.tui/tui-layout-asm.exp with target board
dwarf5-fission-debug-types, the test-case fails and I get a core dump:
...
# of unexpected core files 1
...
Looking at the backtrace of the core file, what seems to be happening is that:
- gdbpy_flush attempts to flush gdb_stdout, which is nullptr
- that causes a segfault
- gdb intercepts this and starts to handle it using handle_fatal_signal
- handle_fatal_signal calls sig_write, which attempts to write to gdb_stderr,
which is nullptr,
- that causes another segfault
- gdb exits
I managed to reproduce the problem by the following trigger patch in
stdin_event_handler:
...
- if (error)
+ if (1 || error)
{
current_ui = main_ui;
ui->unregister_file_handler ();
- if (main_ui == ui)
+ if (1 || main_ui == ui)
{
gdb_printf (gdb_stderr, _("error detected on stdin\n"));
+ gdb_stderr = nullptr;
+ gdb_stdout = nullptr;
+ gdb_stdlog = nullptr;
quit_command ((char *) 0, 0);
}
...
which gives us:
...
$ gdb
(gdb) <q> error detected on stdin
Segmentation fault (core dumped)
$ q
...
Fix sig_write to handle the case that gdb_stderr == nullptr, such that we get
instead:
...
$ gdb
(gdb) <q> error detected on stdin
Fatal signal: Segmentation fault
----- Backtrace -----
...
---------------------
A fatal error internal to GDB has been detected, further
debugging is not possible. GDB will now terminate.
This is a bug, please report it. For instructions, see:
<https://www.gnu.org/software/gdb/bugs/>.
Segmentation fault (core dumped)
$ q
...
Tested on x86_64-linux.
---
gdb/bt-utils.c | 30 +++++++++++++++++++++---------
gdb/event-top.c | 13 +++++++++++--
2 files changed, 32 insertions(+), 11 deletions(-)
diff --git a/gdb/bt-utils.c b/gdb/bt-utils.c
index 8e782450ae9..9e9f680f9ea 100644
--- a/gdb/bt-utils.c
+++ b/gdb/bt-utils.c
@@ -55,7 +55,10 @@ libbacktrace_error (void *data, const char *errmsg, int errnum)
const auto sig_write = [] (const char *msg) -> void
{
- gdb_stderr->write_async_safe (msg, strlen (msg));
+ if (gdb_stderr == nullptr || gdb_stderr->fd () == -1)
+ std::ignore = ::write (2, msg, strlen (msg));
+ else
+ gdb_stderr->write_async_safe (msg, strlen (msg));
};
sig_write ("error creating backtrace: ");
@@ -79,7 +82,10 @@ libbacktrace_print (void *data, uintptr_t pc, const char *filename,
{
const auto sig_write = [] (const char *msg) -> void
{
- gdb_stderr->write_async_safe (msg, strlen (msg));
+ if (gdb_stderr == nullptr || gdb_stderr->fd () == -1)
+ std::ignore = ::write (2, msg, strlen (msg));
+ else
+ gdb_stderr->write_async_safe (msg, strlen (msg));
};
/* Buffer to print addresses and line numbers into. An 8-byte address
@@ -130,14 +136,20 @@ gdb_internal_backtrace_1 ()
{
const auto sig_write = [] (const char *msg) -> void
{
- gdb_stderr->write_async_safe (msg, strlen (msg));
+ if (gdb_stderr == nullptr || gdb_stderr->fd () == -1)
+ std::ignore = ::write (2, msg, strlen (msg));
+ else
+ gdb_stderr->write_async_safe (msg, strlen (msg));
};
/* Allow up to 25 frames of backtrace. */
void *buffer[25];
int frames = backtrace (buffer, ARRAY_SIZE (buffer));
- backtrace_symbols_fd (buffer, frames, gdb_stderr->fd ());
+ int fd = ((gdb_stderr == nullptr || gdb_stderr->fd () == -1)
+ ? 2
+ : gdb_stderr->fd ());
+ backtrace_symbols_fd (buffer, frames, fd);
if (frames == ARRAY_SIZE (buffer))
sig_write (_("Backtrace might be incomplete.\n"));
}
@@ -173,15 +185,15 @@ gdb_internal_backtrace ()
#ifdef GDB_PRINT_INTERNAL_BACKTRACE
const auto sig_write = [] (const char *msg) -> void
{
- gdb_stderr->write_async_safe (msg, strlen (msg));
+ if (gdb_stderr == nullptr || gdb_stderr->fd () == -1)
+ std::ignore = ::write (2, msg, strlen (msg));
+ else
+ gdb_stderr->write_async_safe (msg, strlen (msg));
};
sig_write (str_backtrace);
- if (gdb_stderr->fd () > -1)
- gdb_internal_backtrace_1 ();
- else
- sig_write (str_backtrace_unavailable);
+ gdb_internal_backtrace_1 ();
sig_write ("---------------------\n");
#endif
diff --git a/gdb/event-top.c b/gdb/event-top.c
index c533e74811d..0c4e06b7749 100644
--- a/gdb/event-top.c
+++ b/gdb/event-top.c
@@ -982,7 +982,10 @@ handle_fatal_signal (int sig)
#ifdef GDB_PRINT_INTERNAL_BACKTRACE
const auto sig_write = [] (const char *msg) -> void
{
- gdb_stderr->write_async_safe (msg, strlen (msg));
+ if (gdb_stderr == nullptr || gdb_stderr->fd () == -1)
+ std::ignore = ::write (2, msg, strlen (msg));
+ else
+ gdb_stderr->write_async_safe (msg, strlen (msg));
};
if (bt_on_fatal_signal)
@@ -1027,7 +1030,13 @@ handle_fatal_signal (int sig)
}
sig_write ("\n\n");
- gdb_stderr->flush ();
+ if (gdb_stderr == nullptr || gdb_stderr->fd () == -1)
+ {
+ /* Writing to file descriptor instead of stream, no flush
+ required. */
+ }
+ else
+ gdb_stderr->flush ();
}
#endif
--
2.43.0
next prev parent reply other threads:[~2025-04-25 17:19 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-25 17:18 [PATCH 0/2] [gdb] Fix error handling with nullptr gdb_stdout/gdb_stderr Tom de Vries
2025-04-25 17:18 ` Tom de Vries [this message]
2025-04-28 15:17 ` [PATCH 1/2] [gdb] Fix sig_write for null gdb_stderr Simon Marchi
2025-04-29 11:50 ` Tom de Vries
2025-04-29 15:34 ` Tom Tromey
2025-04-30 18:49 ` Tom de Vries
2025-04-30 19:11 ` Tom de Vries
2025-05-02 18:45 ` Tom Tromey
2025-04-25 17:18 ` [PATCH 2/2] [gdb] Handle nullptr stream in gdb_flush Tom de Vries
2025-04-28 15:21 ` Simon Marchi
2025-04-28 16:32 ` Tom de Vries
2025-04-28 16:38 ` Simon Marchi
2025-04-29 11:52 ` 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=20250425171845.9934-2-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