public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: "François Dumont" <frs.dumont@gmail.com>
To: "libstdc++@gcc.gnu.org" <libstdc++@gcc.gnu.org>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Debug Mode ENH 3/4: Add backtrace
Date: Mon, 07 May 2018 20:20:00 -0000	[thread overview]
Message-ID: <0dd654b0-6299-766d-e218-d3a5d4f1a886@gmail.com> (raw)

[-- 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


             reply	other threads:[~2018-05-07 20:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-07 20:20 François Dumont [this message]
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

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=0dd654b0-6299-766d-e218-d3a5d4f1a886@gmail.com \
    --to=frs.dumont@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --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).