libbacktrace integration for _GLIBCXX_DEBUG mode

François Dumont frs.dumont@gmail.com
Thu Oct 8 19:32:03 GMT 2020


I eventually consider your last remark about using weak symbols to 
inject libbacktrace calls when _GLIBCXX_DEBUG_BACKTRACE is defined.

     libstdc++: [_GLIBCXX_DEBUG] Integrate libbacktrace

       Add _GLIBCXX_DEBUG_BACKTRACE macro to ask for a backtrace on 
_GLIBCXX_DEBUG
     assertions using libbacktrace.

             * config/abi/pre/gnu.ver: Add new symbols.
             * include/debug/formatter.h [_GLIBCXX_DEBUG_BACKTRACE]:
             Include <backtrace-supported.h>.
             [_GLIBCXX_DEBUG_BACKTRACE && BACKTRACE_SUPPORTED]:
             Include <backtrace.h>.
             [(!_GLIBCXX_DEBUG_BACKTRACE || !BACKTRACE_SUPPORTED) &&
             _GLIBCXX_USE_C99_STDINT_TR1]: Include <stdint.h>.
             [BACKTRACE_SUPPORTED || _GLIBCXX_USE_C99_STDINT_TR1]
             [_GLIBCXX_DEBUG_USE_LIBBACKTRACE]
             (__gnu_debug::__create_backtrace_state): New.
             [_GLIBCXX_DEBUG_USE_LIBBACKTRACE]
             (__gnu_debug::__render_backtrace): New.
[_GLIBCXX_DEBUG_USE_LIBBACKTRACE](_Error_formatter::_M_print_backtrace):
             New.
[_GLIBCXX_DEBUG_USE_LIBBACKTRACE](_Error_formatter::_M_backtrace_state):
             New.
             * src/c++11/debug.cc: Include <cstring>.
             (_Print_func_t): New.
             (print_word): Use '%.*s' format in fprintf to render only 
expected
             number of chars.
             (print_raw(PrintContext&, const char*, ptrdiff_t)): New.
             (print_function(PrintContext&, const char*, 
_Print_func_t)): New.
             (print_type): Use latter.
             (print_string(PrintContext&, const char*, const 
_Parameter*, size_t)):
             Change signature to...
             (print_string(PrintContext&, const char*, ptrdiff_t, const 
_Parameter*,
             size_t)): ...this and adapt. Remove intermediate buffer to 
render input
             string.
             (print_string(PrintContext&, const char*, ptrdiff_t)): New.
             [_GLIBCXX_DEBUG_USE_LIBBACKTRACE]
             (print_backtrace(void*, uintptr_t, const char*, int, const 
char*)): New.
             (_Error_formatter::_M_error()): Adapt.
             [_GLIBCXX_DEBUG_USE_LIBBACKTRACE]
             (__gnu_debug::__create_backtrace_state): New, weak symbol.
             [_GLIBCXX_DEBUG_USE_LIBBACKTRACE]
             (__gnu_debug::__render_backtrace): New, weak symbol.
             * doc/xml/manual/debug_mode.xml: Document 
_GLIBCXX_DEBUG_BACKTRACE.
             * doc/xml/manual/using.xml: Likewise.

Tested under Linux x86_64.

Ok to commit ?

François

On 13/06/19 10:46 pm, François Dumont wrote:
> Here is a new proposal which I think take into account all your remarks.
>
> I discovered the great "%.*s" printf format so I was able to do some 
> cleanup on the function name without any allocation.
>
> I also agree that counting the '>' or '<' is not reliable so I remove 
> this and limit the cleanup to the __cxx1998 namespace and the __ 
> uglification, it is still better than nothing.
>
> I complete the doc as advised. I also added a note about making sure 
> that _GLIBCXX_DEBUG_BACKTRACE is defined consistently throughout the 
> application otherwise it would break the famous ODR rule.
>
> I introduced a _GLIBCXX_DEBUG_USE_LIBBACKTRACE to know when the system 
> is able to handle libbacktrace even if the user didn't activate it. I 
> could undef if when I am not building the library but I don't remember 
> if there is a macro to signal that library is being built ?
>
> Here is an output sample now:
>
> /home/fdt/dev/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/debug/string:177: 
>
> In function:
>     gnu_debug::basic_string<_CharT, _Traits, 
> _Allocator>::basic_string(const
>     _CharT*, gnu_debug::basic_string<_CharT, _Traits,
>     _Allocator>::size_type, const _Allocator&) [with _CharT = char; 
> _Traits
>     = std::char_traits<char>; _Allocator = std::allocator<char>;
>     gnu_debug::basic_string<_CharT, _Traits, _Allocator>::size_type = 
> long
>     unsigned int]
>
> Backtrace:
>     0x400afd char const* gnu_debug::check_string<char, unsigned 
> long>(char const*, unsigned long, char const*, unsigned int, char const*)
> /home/fdt/dev/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/debug/string:56 
>
>     0x400afd char const* gnu_debug::check_string<char, unsigned 
> long>(char const*, unsigned long, char const*, unsigned int, char const*)
> /home/fdt/dev/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/debug/string:49 
>
>     0x400afd gnu_debug::basic_string<char, std::char_traits<char>, 
> std::allocator<char> >::basic_string(char const*, unsigned long, 
> std::allocator<char> const&)
> /home/fdt/dev/gcc/build/x86_64-pc-linux-gnu/libstdc++-v3/include/debug/string:177 
>
>     0x400afd test01()
> /home/fdt/dev/gcc/git/libstdc++-v3/testsuite/21_strings/basic_string/debug/1_neg.cc:27 
>
>     0x4009c8 main
> /home/fdt/dev/gcc/git/libstdc++-v3/testsuite/21_strings/basic_string/debug/1_neg.cc:32 
>
>
> Error: __s != 0 || __n == 0.
> XFAIL: 21_strings/basic_string/debug/1_neg.cc execution test
>
>     * include/debug/formatter.h [_GLIBCXX_DEBUG_BACKTRACE]:
>     Include <backtrace-supported.h>.
>     [_GLIBCXX_DEBUG_BACKTRACE && BACKTRACE_SUPPORTED]:
>     Include <backtrace.h>.
>     [(!_GLIBCXX_DEBUG_BACKTRACE || !BACKTRACE_SUPPORTED) &&
>     _GLIBCXX_USE_C99_STDINT_TR1]: Include <stdint.h>.
>     [BACKTRACE_SUPPORTED || _GLIBCXX_USE_C99_STDINT_TR1]
>     (_GLIBCXX_DEBUG_USE_LIBBACKTRACE): New.
>     [_GLIBCXX_DEBUG_USE_LIBBACKTRACE](__backtrace_error_cb): New.
>     [_GLIBCXX_DEBUG_USE_LIBBACKTRACE](__backtrace_full_cb): New.
>     [_GLIBCXX_DEBUG_USE_LIBBACKTRACE](__backtrace_state): New.
> [_GLIBCXX_DEBUG_USE_LIBBACKTRACE](_Error_formatter::_Bt_full_t):
>     New.
> [_GLIBCXX_DEBUG_USE_LIBBACKTRACE](_Error_formatter::_M_print_backtrace):
>     New.
> [_GLIBCXX_DEBUG_USE_LIBBACKTRACE](_Error_formatter::_M_backtrace_state):
>     New.
>     [_GLIBCXX_DEBUG_USE_LIBBACKTRACE]
>     (_Error_formatter::_M_backtrace_full_func): New.
>     * src/c++11/debug.cc: Include <cstring>.
>     (_Print_func_t): New.
>     (print_word): Use '%.*s' format in fprintf to render only expected
>     chars.
>     (print_raw(PrintContext&, const char*, ptrdiff_t)): New.
>     (print_function(PrintContext&, const char*, _Print_func_t)): New.
>     (print_type): Use latter.
>     (print_string(PrintContext&, const char*, const _Parameter*, 
> size_t)):
>     Change signature to...
>     (print_string(PrintContext&, const char*, ptrdiff_t, const 
> _Parameter*,
>     size_t)): ...this and adapt. Remove intermediate buffer to render 
> input
>     string.
>     (print_string(PrintContext&, const char*, ptrdiff_t)): New.
>     [_GLIBCXX_DEBUG_USE_LIBBACKTRACE]
>     (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.
>     * doc/xml/manual/using.xml: Likewise.
>
> Tested under Linux x86_64 normal and debug modes.
>
> Ok to commit ?
>
> François
>
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: libbacktrace.patch
Type: text/x-patch
Size: 16115 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20201008/4f399ee8/attachment.bin>


More information about the Libstdc++ mailing list