libbacktrace integration for _GLIBCXX_DEBUG mode

François Dumont frs.dumont@gmail.com
Thu May 23 05:39:00 GMT 2019


Hi

     So here what I come up with.

     _GLIBCXX_DEBUG_BACKTRACE controls the feature. If the user define 
it and there is a detectable issue with libbacktrace then I generate a 
compilation error. I want to avoid users defining it but having no 
backtrace in the end in the debug assertion.

     With this new setup I manage to run testsuite with it like that:

export LD_LIBRARY_PATH=/home/fdt/dev/gcc/install/lib/
make CXXFLAGS='-D_GLIBCXX_DEBUG_BACKTRACE 
-I/home/fdt/dev/gcc/install/include -lbacktrace' check-debug

     An example of result:

/home/fdt/dev/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/debug/vector:606:
In function:
     std::__debug::vector<_Tp, _Allocator>::iterator
     std::__debug::vector<_Tp, 
_Allocator>::insert(std::__debug::vector<_Tp,
     _Allocator>::const_iterator, _InputIterator, _InputIterator) [with
     _InputIterator = int*; <template-parameter-2-2> = void; _Tp = int;
     _Allocator = std::allocator<int>; std::__debug::vector<_Tp,
     _Allocator>::iterator =
__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int*, std::
     vector<int> >, std::__debug::vector<int>,
     std::random_access_iterator_tag>; typename 
std::iterator_traits<typename
     std::vector<_Tp, _Alloc>::iterator>::iterator_category =
     std::random_access_iterator_tag; typename std::vector<_Tp,
     _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, 
std::vector<int>
     >; std::__debug::vector<_Tp, _Allocator>::const_iterator =
__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<const int*,
     std::vector<int> >, std::__debug::vector<int>,
     std::random_access_iterator_tag>; typename 
std::iterator_traits<typename
     std::vector<_Tp, _Alloc>::const_iterator>::iterator_category =
     std::random_access_iterator_tag; typename std::vector<_Tp,
     _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const int*, 
std::
     vector<int> >]

Backtrace:
     0x402718 
__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int*, 
std::vector<int> >> std::__debug::vector<int>::insert<int*, 
void>(__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int 
const*, std::vector<int> >>, int*, int*)
/home/fdt/dev/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/debug/vector:606
     0x402718 test01()
/home/fdt/dev/gcc/git/libstdc++-v3/testsuite/23_containers/vector/debug/57779_neg.cc:29
     0x401428 main
/home/fdt/dev/gcc/git/libstdc++-v3/testsuite/23_containers/vector/debug/57779_neg.cc:34

Error: attempt to insert with an iterator range [__first, __last) from this
container.

Objects involved in the operation:
     iterator "__first" @ 0x0x7fff730b96b0 {
       type = int* (mutable iterator);
     }
     iterator "__last" @ 0x0x7fff730b96b8 {
       type = int* (mutable iterator);
     }
     sequence "this" @ 0x0x7fff730b9720 {
       type = std::__debug::vector<int>;
     }
XFAIL: 23_containers/vector/debug/57779_neg.cc execution test


     * include/debug/formatter.h [_GLIBCXX_DEBUG_BACKTRACE]: Include
     <backtrace-supported.h> and <backtrace.h>.
     [!_GLIBCXX_DEBUG_BACKTRACE]: Include <stdint.h>.
     [!_GLIBCXX_DEBUG_BACKTRACE](backtrace_error_callback): New.
     [!_GLIBCXX_DEBUG_BACKTRACE](backtrace_full_callback): New.
     [!_GLIBCXX_DEBUG_BACKTRACE](struct backtrace_state): New declaration.
     (_Error_formatter::_Bt_full_t): New function pointer type.
     (_Error_formatter::_M_print_backtrace): New.
     (_Error_formatter::_M_backtrace_state): New.
     (_Error_formatter::_M_backtrace_full_func): New.
     * src/c++11/debug.cc: Include <cstring> and <string>.
     (PrintContext::_M_demangle_name): New.
     (_Print_func_t): New.
     (print_word(PrintContext&, const char*)): New.
     (print_raw(PrintContext&, const char*)): New.
     (print_function(PrintContext&, const char*, _Print_func_t)): New.
     (print_type): Use latter.
     (print_string(PrintContext&, const char*)): New.
     (print_backtrace(void*, uintptr_t, const char*, int, const char*)):
     New.
     (_Error_formatter::_M_error()): Adapt.
     * doc/xml/manual/debug_mode.xml: Document _GLIBCXX_DEBUG_BACKTRACE.

Tested under Linux x86_64.

Ok to commit ?

François


On 12/21/18 10:03 PM, Jonathan Wakely wrote:
> On 21/12/18 22:47 +0200, Ville Voutilainen wrote:
>> On Fri, 21 Dec 2018 at 22:35, Jonathan Wakely <jwakely@redhat.com> 
>> wrote:
>>> >    I also explcitely define BACKTRACE_SUPPORTED to 0 to make sure
>>> >libstdc++ has no libbacktrace dependency after usual build.
>>
>>> I'm concerned about the requirement to link to libbacktrace
>>> explicitly (which will break existing makefiles and build systems that
>>> currently use debug mode in testing).
>>
>> But see what Francois wrote, "I also explcitely define
>> BACKTRACE_SUPPORTED to 0 to make sure
>> libstdc++ has no libbacktrace dependency after usual build."
>
> Yes, but if you happen to install libbacktrace headers, the behaviour
> for users building their own code changes. I agree that if you install
> those headers, it's probably for a reason, but it might be a different
> reason to "so that libstdc++ prints better backtraces".
>
>>> Also, some of the glibc team pointed out to me that running *any*
>>> extra code after undefined behaviour has been detected is a potential
>>> risk. The less that you do between detecting UB and calling abort(),
>>> the better. Giving the users more information is helpful, but comes
>>> with some additional risk.
>>
>> Ditto. Having said those things, I think we need to figure out a good
>> way to provide this sensibly
>> as an opt-in. The backtrace support is bloody useful, and dovetails
>> into a possible Contracts-aware
>> implementation of our library, but I think we need to do some more
>> thought-work on this, thus I agree
>> that it's not stage3 material. I do think it's something that we need
>> to keep in mind, thanks
>> for working on it, Francois!
>
> Yes, I agree that making it available via a more explicit opt-in would
> be good. Maybe require users to define _GLIBCXX_DEBUG_BACKTRACE as well
> as _GLIBCXX_DEBUG, or something like that.
>
>
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: debug_backtrace.patch
Type: text/x-patch
Size: 12212 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20190523/d000feb4/attachment.bin>


More information about the Gcc-patches mailing list