On Wed, 30 Nov 2022 at 18:00, François Dumont <frs.dum...@gmail.com> wrote:
>
> On 30/11/22 14:07, Jonathan Wakely wrote:
> > On Wed, 30 Nov 2022 at 11:57, Jonathan Wakely <jwak...@redhat.com> wrote:
> >>
> >>
> >> On Wed, 30 Nov 2022 at 11:54, Jonathan Wakely <jwak...@redhat.com> wrote:
> >>>
> >>>
> >>> On Wed, 30 Nov 2022 at 06:04, François Dumont via Libstdc++ 
> >>> <libstd...@gcc.gnu.org> wrote:
> >>>> Good catch, then we also need this patch.
> >>>
> >>> Is it worth printing an error? If we can't show the backtrace because of 
> >>> an error, we can just print nothing there.
>
> No strong opinion on that but if we do not print anything the output
> will be:
>
> Backtrace:
>
> Error: ...
>
> I just considered that it did not cost much to report the issue to the
> user that defined _GLIBCXX_DEBUG_BACKTRACE and so is expecting a backtrace.
>
> Maybe printing "Backtrace:\n" could be done in the normal callback
> leaving the user with the feeling that _GLIBCXX_DEBUG_BACKTRACE does not
> work.

OK, how's this?

Tested x86_64-linux.
commit aadf83283f0d3e1d473ac17595ba73d0210bb9ba
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Wed Nov 30 12:32:53 2022

    libstdc++: Pass error handler to libbacktrace functions
    
    Also pass threaded=1 to __glibcxx_backtrace_create_state and remove some
    of the namespace scope declarations in the header.
    
    Co-authored-by: François Dumont <frs.dum...@gmail.com>
    
    libstdc++-v3/ChangeLog:
    
            * include/debug/formatter.h [_GLIBCXX_DEBUG_BACKTRACE]
            (_Error_formatter::_Error_formatter): Pass error handler to
            __glibcxx_backtrace_create_state. Pass 1 for threaded argument.
            (_Error_formatter::_S_err): Define empty function.
            * src/c++11/debug.cc (_Error_formatter::_M_error): Pass error
            handler to __glibcxx_backtrace_full.

diff --git a/libstdc++-v3/include/debug/formatter.h 
b/libstdc++-v3/include/debug/formatter.h
index f120163c6d4..e8a83a21bde 100644
--- a/libstdc++-v3/include/debug/formatter.h
+++ b/libstdc++-v3/include/debug/formatter.h
@@ -32,32 +32,17 @@
 #include <bits/c++config.h>
 
 #if _GLIBCXX_HAVE_STACKTRACE
-struct __glibcxx_backtrace_state;
-
 extern "C"
 {
-  __glibcxx_backtrace_state*
+  struct __glibcxx_backtrace_state*
   __glibcxx_backtrace_create_state(const char*, int,
                                   void(*)(void*, const char*, int),
                                   void*);
-
-  typedef int (*__glibcxx_backtrace_full_callback) (
-    void*, __UINTPTR_TYPE__, const char *, int, const char*);
-
-  typedef void (*__glibcxx_backtrace_error_callback) (
-    void*, const char*, int);
-
-  typedef int (*__glibcxx_backtrace_full_func) (
-    __glibcxx_backtrace_state*, int,
-    __glibcxx_backtrace_full_callback,
-    __glibcxx_backtrace_error_callback,
-    void*);
-
   int
   __glibcxx_backtrace_full(
-    __glibcxx_backtrace_state*, int,
-    __glibcxx_backtrace_full_callback,
-    __glibcxx_backtrace_error_callback,
+    struct __glibcxx_backtrace_state*, int,
+    int (*)(void*, __UINTPTR_TYPE__, const char *, int, const char*),
+    void (*)(void*, const char*, int),
     void*);
 }
 #endif
@@ -609,10 +594,10 @@ namespace __gnu_debug
     , _M_function(__function)
 #if _GLIBCXX_HAVE_STACKTRACE
 # ifdef _GLIBCXX_DEBUG_BACKTRACE
-    , _M_backtrace_state(__glibcxx_backtrace_create_state(0, 0, 0, 0))
+    , _M_backtrace_state(__glibcxx_backtrace_create_state(0, 1, _S_err, 0))
     , _M_backtrace_full(&__glibcxx_backtrace_full)
 # else
-    , _M_backtrace_state()
+    , _M_backtrace_state(0)
 # endif
 #endif
     { }
@@ -631,8 +616,12 @@ namespace __gnu_debug
     const char*                _M_text;
     const char*                _M_function;
 #if _GLIBCXX_HAVE_STACKTRACE
-    __glibcxx_backtrace_state*         _M_backtrace_state;
-    __glibcxx_backtrace_full_func      _M_backtrace_full;
+    struct __glibcxx_backtrace_state*          _M_backtrace_state;
+    // TODO: Remove _M_backtrace_full after __glibcxx_backtrace_full is moved
+    // from libstdc++_libbacktrace.a to libstdc++.so:
+    __decltype(&__glibcxx_backtrace_full)      _M_backtrace_full;
+
+    static void _S_err(void*, const char*, int) { }
 #endif
 
   public:
diff --git a/libstdc++-v3/src/c++11/debug.cc b/libstdc++-v3/src/c++11/debug.cc
index 9eda38023f7..83996ffe622 100644
--- a/libstdc++-v3/src/c++11/debug.cc
+++ b/libstdc++-v3/src/c++11/debug.cc
@@ -1141,6 +1141,23 @@ namespace
 
     return ret;
   }
+
+  void
+  print_backtrace_error(void* data, const char* msg, int errnum)
+  {
+    PrintContext& ctx = *static_cast<PrintContext*>(data);
+
+    print_literal(ctx, "Backtrace unavailable: ");
+    print_word(ctx, msg ? msg : "<unknown error>");
+    if (errnum > 0)
+      {
+       char buf[64];
+       int written = __builtin_sprintf(buf, " (errno=%d)\n", errnum);
+       print_word(ctx, buf, written);
+      }
+    else
+      print_literal(ctx, "\n");
+  }
 #endif
 }
 
@@ -1193,7 +1210,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 via email to