Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Tom de Vries <tdevries@suse.de>, gdb-patches@sourceware.org
Subject: Re: [PATCH 1/2] [gdb] Fix sig_write for null gdb_stderr
Date: Mon, 28 Apr 2025 11:17:02 -0400	[thread overview]
Message-ID: <4d29d800-175e-453f-9743-4413431d3c56@simark.ca> (raw)
In-Reply-To: <20250425171845.9934-2-tdevries@suse.de>

On 4/25/25 1:18 PM, Tom de Vries wrote:
> 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));

TIL about std::ignore.

Is it possible to factor out the lambdas:

  const auto sig_write = [] (const char *msg) -> void
  {
    if (gdb_stderr == nullptr || gdb_stderr->fd () == -1)
      std::ignore = ::write (2, msg, strlen (msg));
    else
      gdb_stderr->write_async_safe (msg, strlen (msg));
  };

to a function?  At first glance they all seem to be the same.

Otherwise, LGTM.

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Simon

  reply	other threads:[~2025-04-28 15:17 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 ` [PATCH 1/2] [gdb] Fix sig_write for null gdb_stderr Tom de Vries
2025-04-28 15:17   ` Simon Marchi [this message]
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=4d29d800-175e-453f-9743-4413431d3c56@simark.ca \
    --to=simark@simark.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=tdevries@suse.de \
    /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