public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: "François Dumont" <frs.dumont@gmail.com>
To: "Björn Schäpers" <gcc@hazardy.de>,
	gcc-patches@gc.gnu.org, libstdc++@gcc.gnu.org
Subject: Re: [PATCH] libstdc++: Add error handler for <stacktrace>
Date: Wed, 30 Nov 2022 07:04:29 +0100	[thread overview]
Message-ID: <ee866216-8828-6b88-764d-9de8264108d5@gmail.com> (raw)
In-Reply-To: <20221129214107.25572-1-gcc@hazardy.de>

[-- Attachment #1: Type: text/plain, Size: 3474 bytes --]

Good catch, then we also need this patch.

I still need to test it thought, just to make sure it compiles. Unless 
you have a nice way to force call to the error callback ?

François

On 29/11/22 22:41, Björn Schäpers wrote:
> From: Björn Schäpers <bjoern@hazardy.de>
>
> Not providing an error handler results in a nullpointer dereference when
> an error occurs.
>
> libstdc++-v3/ChangeLog
>
> 	* include/std/stacktrace: Add __backtrace_error_handler and use
> 	it in all calls to libbacktrace.
> ---
>   libstdc++-v3/include/std/stacktrace | 21 ++++++++++++++-------
>   1 file changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/libstdc++-v3/include/std/stacktrace b/libstdc++-v3/include/std/stacktrace
> index e7cbbee5638..b786441cbad 100644
> --- a/libstdc++-v3/include/std/stacktrace
> +++ b/libstdc++-v3/include/std/stacktrace
> @@ -85,6 +85,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>   
>   #define __cpp_lib_stacktrace 202011L
>   
> +  inline void
> +  __backtrace_error_handler(void*, const char*, int) {}
> +
>     // [stacktrace.entry], class stacktrace_entry
>     class stacktrace_entry
>     {
> @@ -159,7 +162,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>       _S_init()
>       {
>         static __glibcxx_backtrace_state* __state
> -	= __glibcxx_backtrace_create_state(nullptr, 1, nullptr, nullptr);
> +	= __glibcxx_backtrace_create_state(nullptr, 1,
> +					   __backtrace_error_handler, nullptr);
>         return __state;
>       }
>   
> @@ -192,7 +196,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>   	  return __function != nullptr;
>         };
>         const auto __state = _S_init();
> -      if (::__glibcxx_backtrace_pcinfo(__state, _M_pc, +__cb, nullptr, &__data))
> +      if (::__glibcxx_backtrace_pcinfo(__state, _M_pc, +__cb,
> +				       __backtrace_error_handler, &__data))
>   	return true;
>         if (__desc && __desc->empty())
>   	{
> @@ -201,8 +206,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>   	      if (__symname)
>   		*static_cast<_Data*>(__data)->_M_desc = _S_demangle(__symname);
>   	  };
> -	  if (::__glibcxx_backtrace_syminfo(__state, _M_pc, +__cb2, nullptr,
> -					    &__data))
> +	  if (::__glibcxx_backtrace_syminfo(__state, _M_pc, +__cb2,
> +					    __backtrace_error_handler, &__data))
>   	    return true;
>   	}
>         return false;
> @@ -252,7 +257,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>   	if (auto __cb = __ret._M_prepare()) [[likely]]
>   	  {
>   	    auto __state = stacktrace_entry::_S_init();
> -	    if (__glibcxx_backtrace_simple(__state, 1, __cb, nullptr,
> +	    if (__glibcxx_backtrace_simple(__state, 1, __cb,
> +					   __backtrace_error_handler,
>   					   std::__addressof(__ret)))
>   	      __ret._M_clear();
>   	  }
> @@ -270,7 +276,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>   	if (auto __cb = __ret._M_prepare()) [[likely]]
>   	  {
>   	    auto __state = stacktrace_entry::_S_init();
> -	    if (__glibcxx_backtrace_simple(__state, __skip + 1, __cb, nullptr,
> +	    if (__glibcxx_backtrace_simple(__state, __skip + 1, __cb,
> +					   __backtrace_error_handler,
>   					   std::__addressof(__ret)))
>   	      __ret._M_clear();
>   	  }
> @@ -294,7 +301,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>   	  {
>   	    auto __state = stacktrace_entry::_S_init();
>   	    int __err = __glibcxx_backtrace_simple(__state, __skip + 1, __cb,
> -						   nullptr,
> +						   __backtrace_error_handler,
>   						   std::__addressof(__ret));
>   	    if (__err < 0)
>   	      __ret._M_clear();


[-- Attachment #2: debug.patch --]
[-- Type: text/x-patch, Size: 960 bytes --]

diff --git a/libstdc++-v3/src/c++11/debug.cc b/libstdc++-v3/src/c++11/debug.cc
index 9eda38023f7..30cf24ca891 100644
--- a/libstdc++-v3/src/c++11/debug.cc
+++ b/libstdc++-v3/src/c++11/debug.cc
@@ -1141,6 +1141,21 @@ namespace
 
     return ret;
   }
+
+  void
+  print_backtrace_error(void* data, const char* msg, int errnum)
+  {
+    const int bufsize = 64;
+    char buf[bufsize];
+
+    PrintContext& ctx = *static_cast<PrintContext*>(data);
+
+    int written = __builtin_sprintf(buf, "%d", errnum);
+    print_word(ctx, buf, written);
+    print_literal(ctx, " - ");
+    print_word(ctx, msg);
+    print_literal(ctx, "\n");
+  }
 #endif
 }
 
@@ -1193,7 +1208,7 @@ namespace __gnu_debug
       {
 	print_literal(ctx, "Backtrace:\n");
 	_M_backtrace_full(
-	  _M_backtrace_state, 1, print_backtrace, nullptr, &ctx);
+	  _M_backtrace_state, 1, print_backtrace, print_backtrace_error, &ctx);
 	ctx._M_first_line = true;
 	print_literal(ctx, "\n");
       }

  reply	other threads:[~2022-11-30  6:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-29 21:41 Björn Schäpers
2022-11-30  6:04 ` François Dumont [this message]
2022-11-30 11:54   ` Jonathan Wakely
2022-11-30 11:57     ` Jonathan Wakely
2022-11-30 13:07       ` Jonathan Wakely
2022-11-30 18:00         ` François Dumont
2022-12-06 21:44           ` Jonathan Wakely
2022-12-07 17:58             ` François Dumont
2022-12-07 20:06               ` Jonathan Wakely
2022-11-30 18:17         ` François Dumont
2022-11-30 19:20   ` Björn Schäpers
2022-11-30 12:31 ` Jonathan Wakely
2022-11-30 13:05   ` Jonathan Wakely

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=ee866216-8828-6b88-764d-9de8264108d5@gmail.com \
    --to=frs.dumont@gmail.com \
    --cc=gcc-patches@gc.gnu.org \
    --cc=gcc@hazardy.de \
    --cc=libstdc++@gcc.gnu.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).