public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* Debug Mode ENH 3/4: Add backtrace
@ 2018-05-07 20:20 François Dumont
  2018-05-08 15:27 ` Jonathan Wakely
  0 siblings, 1 reply; 5+ messages in thread
From: François Dumont @ 2018-05-07 20:20 UTC (permalink / raw)
  To: libstdc++, gcc-patches

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

Hi

     Here is the patch to add backtrace info to debug assertion failure 
output.

Example:

/home/fdt/dev/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/debug/vector:188:
In function:
     std::__debug::vector<_Tp, _Allocator>::vector(_InputIterator,
     _InputIterator, const _Allocator&) [with _InputIterator =
std::reverse_iterator<__gnu_debug::_Safe_tagged_iterator<__gnu_cxx::__normal_iterator<int*, 

     std::vector<int> >, std::__debug::vector<int>,
     std::random_access_iterator_tag> >; <template-parameter-2-2> = 
void; _Tp
     = int; _Allocator = std::allocator<int>]

Backtrace:
     ./debug_neg.exe() [0x4020c1]
     ./debug_neg.exe() [0x400e59]
     /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) 
[0x7f13fc56e830]
     ./debug_neg.exe() [0x400eb9]

I tried to use add2line on the output address and it worked fine.

Tested under Linux x86_64.

I'll commit tomorrow if not told otherwise.

     * src/c++11/debug.cc [_GLIBCXX_HAVE_EXECINFO_H]: Include execinfo.h.
     [_GLIBCXX_HAVE_EXECINFO_H](_Error_formatter::_M_error): Render 
backtrace.

François


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

diff --git a/libstdc++-v3/src/c++11/debug.cc b/libstdc++-v3/src/c++11/debug.cc
index 3a1558b..cd1b43d 100644
--- a/libstdc++-v3/src/c++11/debug.cc
+++ b/libstdc++-v3/src/c++11/debug.cc
@@ -40,6 +40,10 @@
 
 #include <cxxabi.h> // for __cxa_demangle
 
+#if defined _GLIBCXX_HAVE_EXECINFO_H
+# include <execinfo.h> // for backtrace
+#endif
+
 #include "mutex_pool.h"
 
 using namespace std;
@@ -1048,6 +1052,29 @@ namespace __gnu_debug
 	print_literal(ctx, "\n");
       }
 
+#if defined _GLIBCXX_HAVE_EXECINFO_H
+    {
+      void* stack[32];
+      int nb = backtrace(stack, 32);
+
+      // Note that we skip current method symbol.
+      if (nb > 1)
+	{
+	  print_literal(ctx, "Backtrace:\n");
+	  auto symbols = backtrace_symbols(stack, nb);
+	  for (int i = 1; i < nb; ++i)
+	    {
+	      print_word(ctx, symbols[i]);
+	      print_literal(ctx, "\n");
+	    }
+
+	  free(symbols);
+	  ctx._M_first_line = true;
+	  print_literal(ctx, "\n");
+	}
+    }
+#endif
+
     print_literal(ctx, "Error: ");
 
     // Print the error message


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-05-09  9:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-07 20:20 Debug Mode ENH 3/4: Add backtrace François Dumont
2018-05-08 15:27 ` Jonathan Wakely
2018-05-08 19:54   ` François Dumont
2018-05-08 23:51     ` Ian Lance Taylor via libstdc++
2018-05-09  9:05       ` Jonathan Wakely

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).