This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Debug Mode ENH 3/4: Add backtrace
- From: François Dumont <frs dot dumont at gmail dot com>
- To: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 7 May 2018 22:20:35 +0200
- Subject: Debug Mode ENH 3/4: Add backtrace
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
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