public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: Andrew Burgess <andrew.burgess@embecosm.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 5/6] gdb: use libbacktrace to create a better backtrace for fatal signals
Date: Thu, 19 Aug 2021 14:58:36 -0400	[thread overview]
Message-ID: <cf253771-7925-4822-9824-e8ae77e5b1b1@polymtl.ca> (raw)
In-Reply-To: <78bd37d17d8c123f9c45c8f9c1524304a68db9f3.1629366146.git.andrew.burgess@embecosm.com>

On 2021-08-19 5:49 a.m., Andrew Burgess wrote:
> GDB recently gained the ability to print a backtrace when a fatal
> signal is encountered.  This backtrace is produced using the backtrace
> and backtrace_symbols_fd API available in glibc.
> 
> However, in order for this API to actually map addresses to symbol
> names it is required that the application (GDB) be compiled with
> -rdynamic, which GDB is not by default.
> 
> As a result, the backtrace produced often looks like this:
> 
>   Fatal signal: Bus error
>   ----- Backtrace -----
>   ./gdb/gdb[0x80ec00]
>   ./gdb/gdb[0x80ed56]
>   /lib64/libc.so.6(+0x3c6b0)[0x7fc2ce1936b0]
>   /lib64/libc.so.6(__poll+0x4f)[0x7fc2ce24da5f]
>   ./gdb/gdb[0x15495ba]
>   ./gdb/gdb[0x15489b8]
>   ./gdb/gdb[0x9b794d]
>   ./gdb/gdb[0x9b7a6d]
>   ./gdb/gdb[0x9b943b]
>   ./gdb/gdb[0x9b94a1]
>   ./gdb/gdb[0x4175dd]
>   /lib64/libc.so.6(__libc_start_main+0xf3)[0x7fc2ce17e1a3]
>   ./gdb/gdb[0x4174de]
>   ---------------------

I haven't seen these backtraces on my builds, I wonder why.  It probably
means GDB never crashes!

> This is OK if you have access to the exact same build of GDB, you can
> manually map the addresses back to symbols, however, it is next to
> useless if all you have is a backtrace copied into a bug report.
> 
> GCC uses libbacktrace for printing a backtrace when it encounters an
> error.  In recent commits I added this library into the binutils-gdb
> repository, and in this commit I allow this library to be used by
> GDB.  Now (when GDB is compiled with debug information) the backtrace
> looks like this:
> 
>   ----- Backtrace -----
>   0x80ee08 gdb_internal_backtrace
>   	../../src/gdb/event-top.c:989
>   0x80ef0b handle_fatal_signal
>   	../../src/gdb/event-top.c:1036
>   0x7f24539dd6af ???
>   0x7f2453a97a5f ???
>   0x154976f gdb_wait_for_event
>   	../../src/gdbsupport/event-loop.cc:613
>   0x1548b6d _Z16gdb_do_one_eventv
>   	../../src/gdbsupport/event-loop.cc:237
>   0x9b7b02 start_event_loop
>   	../../src/gdb/main.c:421
>   0x9b7c22 captured_command_loop
>   	../../src/gdb/main.c:481
>   0x9b95f0 captured_main
>   	../../src/gdb/main.c:1353
>   0x9b9656 _Z8gdb_mainP18captured_main_args
>   	../../src/gdb/main.c:1368
>   0x4175ec main
>   	../../src/gdb/gdb.c:32
>   ---------------------
> 
> Which seems much more useful.
> 
> Use of libbacktrace is optional.  If GDB is configured with
> --disable-libbacktrace then the libbacktrace directory will not be
> built, and GDB will not try to use this library.  In this case GDB
> would try to use the old backtrace and backtrace_symbols_fd API.
> 
> All of the functions related to writing the backtrace of GDB itself
> have been moved into the new files gdb/by-utils.{c,h}.

bt-utils

Will we want to have this in GDBserver as well?  If so, could you put
the bits that can be shared in gdbsupport?  That would mean the code to
produce the backtrace, but not the code that deals with
cmd_list_element, for example.

> @@ -636,12 +640,12 @@ INTERNAL_LDFLAGS = \
>  # LIBIBERTY appears twice on purpose.
>  CLIBS = $(SIM) $(READLINE) $(OPCODES) $(LIBCTF) $(BFD) $(ZLIB) \
>          $(LIBSUPPORT) $(INTL) $(LIBIBERTY) $(LIBDECNUMBER) \
> -	$(XM_CLIBS) $(GDBTKLIBS) \
> +	$(XM_CLIBS) $(GDBTKLIBS)  $(LIBBACKTRACE_LIB) \
>  	@LIBS@ @GUILE_LIBS@ @PYTHON_LIBS@ \
>  	$(LIBEXPAT) $(LIBLZMA) $(LIBBABELTRACE) $(LIBIPT) \
>  	$(WIN32LIBS) $(LIBGNU) $(LIBGNU_EXTRA_LIBS) $(LIBICONV) \
>  	$(LIBMPFR) $(LIBGMP) $(SRCHIGH_LIBS) $(LIBXXHASH) $(PTHREAD_LIBS) \
> -	$(DEBUGINFOD_LIBS)
> +	$(DEBUGINFOD_LIBS) $(LIBBABELTRACE_LIB)

Why are you adding LIBBABELTRACE_LIB here?

> +/* Callback used by libbacktrace if it encounters an error.  */
> +
> +static void
> +libbacktrace_error (void *data, const char *errmsg, int errnum)
> +{
> +  /* A negative errnum indicates no debug info was available, just
> +     skip printing a backtrace in this case.  */
> +  if (errnum < 0)
> +    return;
> +
> +  const auto sig_write = [] (const char *msg) -> void
> +  {
> +    gdb_stderr->write_async_safe (msg, strlen (msg));
> +  };
> +
> +  sig_write ("error creating backtrace: ");
> +  sig_write (errmsg);
> +  if (errnum > 0)
> +    {
> +      char buf[10];
> +      snprintf (buf, sizeof (buf), ": %d", errnum);
> +      buf[sizeof (buf) - 1] = '\0';

I think snprintf always writes the null terminator itself, even if the
output is truncated.

Other than that, it all LGTM, thanks.

Simon

  reply	other threads:[~2021-08-19 18:58 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-19  9:49 [PATCH 0/6] Display GDB backtrace for internal errors Andrew Burgess
2021-08-19  9:49 ` [PATCH 1/6] gdb: use bool instead of int in struct internal_problem Andrew Burgess
2021-08-19 18:33   ` Simon Marchi
2021-09-07 14:11     ` Andrew Burgess
2021-08-19  9:49 ` [PATCH 2/6] gdb: make use of std::string in utils.c Andrew Burgess
2021-08-19 18:41   ` Simon Marchi
2021-09-07 14:12     ` Andrew Burgess
2021-08-19  9:49 ` [PATCH 3/6] gdb: Add a dependency between gdb and libbacktrace Andrew Burgess
2021-08-19 18:43   ` Simon Marchi
2021-08-27 17:44     ` Tom Tromey
2021-08-30 20:33       ` Andrew Burgess
2021-08-19  9:49 ` [PATCH 4/6] Copy in libbacktrace from gcc Andrew Burgess
2021-08-27 17:46   ` Tom Tromey
2021-08-30 20:34     ` Andrew Burgess
2021-08-19  9:49 ` [PATCH 5/6] gdb: use libbacktrace to create a better backtrace for fatal signals Andrew Burgess
2021-08-19 18:58   ` Simon Marchi [this message]
2021-08-19  9:49 ` [PATCH 6/6] gdb: print backtrace for internal error/warning Andrew Burgess
2021-08-19 19:01   ` Simon Marchi
2021-08-30 14:16 ` [PATCH 0/6] Display GDB backtrace for internal errors Tom de Vries
2021-08-30 20:35   ` Andrew Burgess
2021-08-31 11:17 ` Florian Weimer
2021-09-28 11:26 ` [PUSHED " Andrew Burgess
2021-09-28 11:26   ` [PUSHED 1/6] top-level configure: setup target_configdirs based on repository Andrew Burgess
2021-09-28 11:26   ` [PUSHED 2/6] gdb: Add a dependency between gdb and libbacktrace Andrew Burgess
2021-09-28 11:26   ` [PUSHED 4/6] src-release.sh: add libbacktrace to GDB_SUPPORT_DIRS Andrew Burgess
2021-09-28 11:26   ` [PUSHED 5/6] gdb: use libbacktrace to create a better backtrace for fatal signals Andrew Burgess
2021-09-28 18:55     ` Pedro Alves
2021-09-29  8:21       ` Andrew Burgess
2021-09-29  3:09     ` Simon Marchi
2021-09-29  9:56       ` Andrew Burgess
2021-09-28 11:26   ` [PUSHED 6/6] gdb: print backtrace for internal error/warning Andrew Burgess
2021-09-28 12:26     ` Eli Zaretskii
2021-09-29  8:34       ` Andrew Burgess
2021-09-28 12:20   ` [PUSHED 3/6] Copy in libbacktrace from gcc Andrew Burgess

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=cf253771-7925-4822-9824-e8ae77e5b1b1@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=andrew.burgess@embecosm.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).