Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:boostorg/stacktrace into win-check
Browse files Browse the repository at this point in the history
  • Loading branch information
apolukhin committed Feb 4, 2024
2 parents af22118 + 3de5aea commit 4632199
Show file tree
Hide file tree
Showing 50 changed files with 408 additions and 234 deletions.
27 changes: 12 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,20 @@ jobs:
fail-fast: false
matrix:
include:
- toolset: gcc-7
cxxstd: "03,11,14,17"
os: ubuntu-18.04
- toolset: gcc-9
cxxstd: "03,11,14,17,2a"
os: ubuntu-18.04
- toolset: gcc-10
- toolset: gcc-12
cxxstd: "03,11,14,17,2a"
os: ubuntu-18.04
os: ubuntu-22.04
cxxflags: "cxxflags=--coverage -fsanitize=address,leak,undefined -fno-sanitize-recover=undefined"
linkflags: "linkflags=--coverage -lasan -lubsan"
launcher: "testing.launcher=LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libasan.so.6"
gcov_tool: "gcov-10"
gcov_tool: "gcov-12"
launcher: "testing.launcher=LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libasan.so.8"
- toolset: gcc-9
cxxstd: "03,11,14,17,2a"
os: ubuntu-22.04
- toolset: clang
compiler: clang++-10
compiler: clang++-14
cxxstd: "03,11,14,17,2a"
os: ubuntu-20.04
os: ubuntu-22.04
# TODO: fix and uncomment
#- toolset: clang
# cxxstd: "03,11,14,17,2a"
Expand Down Expand Up @@ -88,9 +85,9 @@ jobs:
echo -e "#!/bin/bash\nexec ${{matrix.gcov_tool}} \"\$@\"" > $GITHUB_WORKSPACE/coveralls/gcov_wrapper.sh
chmod +x $GITHUB_WORKSPACE/coveralls/gcov_wrapper.sh
wget https://github.com/linux-test-project/lcov/archive/v1.15.zip
unzip v1.15.zip
LCOV="`pwd`/lcov-1.15/bin/lcov --gcov-tool $GITHUB_WORKSPACE/coveralls/gcov_wrapper.sh"
wget https://github.com/linux-test-project/lcov/archive/v1.16.zip
unzip v1.16.zip
LCOV="`pwd`/lcov-1.16/bin/lcov --gcov-tool $GITHUB_WORKSPACE/coveralls/gcov_wrapper.sh"
echo "$LCOV --directory ../boost-root/bin.v2/libs/$LIBRARY/ --base-directory `pwd`/libs/$LIBRARY/test --capture --output-file $GITHUB_WORKSPACE/coveralls/coverage.info"
$LCOV --directory ../boost-root/bin.v2/libs/$LIBRARY/ --base-directory ../boost-root/ --capture --output-file $GITHUB_WORKSPACE/coveralls/coverage.info
Expand Down
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ function(stacktrace_add_library suffix opt libs defs)

target_link_libraries(boost_stacktrace_${suffix}
PUBLIC
Boost::array
Boost::config
Boost::container_hash
Boost::core
Boost::predef
Boost::static_assert
Boost::type_traits
Boost::winapi
PRIVATE
${libs}
Expand Down
6 changes: 5 additions & 1 deletion build/Jamfile.v2
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Copyright (C) 2016-2022, Antony Polukhin.
# Copyright (C) 2016-2024, Antony Polukhin.
#
# Use, modification and distribution is subject to the Boost Software License,
# Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
#

import ../../config/checks/config : requires ;

project
: source-location .
: requirements
[ requires cxx11_rvalue_references ]
: default-build
<visibility>hidden
;

Expand Down
2 changes: 1 addition & 1 deletion build/has_backtrace.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2022.
// Copyright Antony Polukhin, 2016-2024.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion doc/Jamfile.v2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright Antony Polukhin, 2016-2022.
# Copyright Antony Polukhin, 2016-2024.
# Use, modification, and distribution are
# subject to the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand Down
6 changes: 3 additions & 3 deletions doc/stacktrace.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[quickbook 1.6]
[version 1.0]
[id stacktrace]
[copyright 2016-2022 Antony Polukhin]
[copyright 2016-2024 Antony Polukhin]
[category Language Features Emulation]
[license
Distributed under the Boost Software License, Version 1.0.
Expand Down Expand Up @@ -285,11 +285,11 @@ By default Boost.Stacktrace is a header-only library, but you may change that an

In header only mode library could be tuned by macro. If one of the link macro from above is defined, you have to manually link with one of the libraries:
[table:libconfig Config
[[Macro name or default] [Library] [Effect] [Platforms] [Uses debug information [footnote This will provide more readable backtraces with *source code locations* if the binary is built with debug information.]] [Uses dynamic exports information [footnote This will provide readable function names in backtrace for functions that are exported by the binary. Compiling with `-rdynamic` flag, without `-fisibility=hidden` or marking functions as exported produce a better stacktraces.]] ]
[[Macro name or default] [Library] [Effect] [Platforms] [Uses debug information [footnote This will provide more readable backtraces with *source code locations* if the binary is built with debug information.]] [Uses dynamic exports information [footnote This will provide readable function names in backtrace for functions that are exported by the binary. Compiling with `-rdynamic` flag, without `-fvisibility=hidden` or marking functions as exported produce a better stacktraces.]] ]
[[['default for MSVC, Intel on Windows, MinGW-w64] / *BOOST_STACKTRACE_USE_WINDBG*] [*boost_stacktrace_windbg*] [ Uses `dbgeng.h` to show debug info. May require linking with *ole32* and *dbgeng*. ] [MSVC, MinGW-w64, Intel on Windows] [yes] [no]]
[[['default for other platforms]] [*boost_stacktrace_basic*] [Uses compiler intrinsics to collect stacktrace and if possible `::dladdr` to show information about the symbol. Requires linking with *libdl* library on POSIX platforms.] [Any compiler on POSIX or MinGW] [no] [yes]]
[[*BOOST_STACKTRACE_USE_WINDBG_CACHED*] [*boost_stacktrace_windbg_cached*] [ Uses `dbgeng.h` to show debug info and caches internals in TLS for better performance. Useful only for cases when traces are gathered very often. May require linking with *ole32* and *dbgeng*. ] [MSVC, Intel on Windows] [yes] [no]]
[[*BOOST_STACKTRACE_USE_BACKTRACE*] [*boost_stacktrace_backtrace*] [Requires linking with *libdl* on POSIX and *libbacktrace* libraries[footnote Some *libbacktrace* packages SEGFAULT if there's a concurrent work with the same `backtrace_state` instance. To avoid that issue the Boost.Stacktrace library uses `thread_local` states, unfortuantely this may consume a lot of memory if you often create and destroy execution threads in your application. Define *BOOST_STACKTRACE_BACKTRACE_FORCE_STATIC* to force single instance, but make sure that [@https://github.com/boostorg/stacktrace/blob/develop/test/thread_safety_checking.cpp thread_safety_checking.cpp] works well in your setup. ]. *libbacktrace* is probably already installed in your system[footnote If you are using Clang with libstdc++ you could get into troubles of including `<backtrace.h>`, because on some platforms Clang does not search for headers in the GCC's include paths and any attempt to add GCC's include path leads to linker errors. To explicitly specify a path to the `<backtrace.h>` header you could define the *BOOST_STACKTRACE_BACKTRACE_INCLUDE_FILE* to a full path to the header. For example on Ubuntu Xenial use the command line option *-DBOOST_STACKTRACE_BACKTRACE_INCLUDE_FILE=</usr/lib/gcc/x86_64-linux-gnu/5/include/backtrace.h>* while building with Clang. ], or built into your compiler.
[[*BOOST_STACKTRACE_USE_BACKTRACE*] [*boost_stacktrace_backtrace*] [Requires linking with *libdl* on POSIX and *libbacktrace* libraries[footnote Some *libbacktrace* packages SEGFAULT if there's a concurrent work with the same `backtrace_state` instance. To avoid that issue the Boost.Stacktrace library uses `thread_local` states, unfortunately this may consume a lot of memory if you often create and destroy execution threads in your application. Define *BOOST_STACKTRACE_BACKTRACE_FORCE_STATIC* to force single instance, but make sure that [@https://github.com/boostorg/stacktrace/blob/develop/test/thread_safety_checking.cpp thread_safety_checking.cpp] works well in your setup. ]. *libbacktrace* is probably already installed in your system[footnote If you are using Clang with libstdc++ you could get into troubles of including `<backtrace.h>`, because on some platforms Clang does not search for headers in the GCC's include paths and any attempt to add GCC's include path leads to linker errors. To explicitly specify a path to the `<backtrace.h>` header you could define the *BOOST_STACKTRACE_BACKTRACE_INCLUDE_FILE* to a full path to the header. For example on Ubuntu Xenial use the command line option *-DBOOST_STACKTRACE_BACKTRACE_INCLUDE_FILE=</usr/lib/gcc/x86_64-linux-gnu/5/include/backtrace.h>* while building with Clang. ], or built into your compiler.

Otherwise (if you are a *MinGW*/*MinGW-w64* user for example) it can be downloaded [@https://github.com/ianlancetaylor/libbacktrace from here] or [@https://github.com/gcc-mirror/gcc/tree/master/libbacktrace from here]. ] [Any compiler on POSIX, or MinGW, or MinGW-w64] [yes] [yes]]
[[*BOOST_STACKTRACE_USE_ADDR2LINE*] [*boost_stacktrace_addr2line*] [Use *addr2line* program to retrieve stacktrace. Requires linking with *libdl* library and `::fork` system call. Macro *BOOST_STACKTRACE_ADDR2LINE_LOCATION* must be defined to the absolute path to the addr2line executable if it is not located in /usr/bin/addr2line. ] [Any compiler on POSIX] [yes] [yes]]
Expand Down
2 changes: 1 addition & 1 deletion example/assert_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2022.
// Copyright Antony Polukhin, 2016-2024.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion example/debug_function.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2022.
// Copyright Antony Polukhin, 2016-2024.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
10 changes: 5 additions & 5 deletions example/terminate_handler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2022.
// Copyright Antony Polukhin, 2016-2024.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down Expand Up @@ -77,7 +77,7 @@ boost::interprocess::mapped_region g_region; // inited at program start
void my_signal_handler2(int signum) {
::signal(signum, SIG_DFL);
void** f = static_cast<void**>(g_region.get_address());
*f = reinterpret_cast<void*>(1); // Setting flag that shared memory now constains stacktrace.
*f = reinterpret_cast<void*>(1); // Setting flag that shared memory now contains stacktrace.
boost::stacktrace::safe_dump_to(f + 1, g_region.get_size() - sizeof(void*));

::raise(SIGABRT);
Expand All @@ -94,7 +94,7 @@ inline void copy_and_run(const char* exec_name, char param, bool not_null) {
std::cout << "Running with param " << param << std::endl;
boost::filesystem::path command = exec_name;
command = command.parent_path() / (command.stem().string() + param + command.extension().string());
boost::filesystem::copy_file(exec_name, command, boost::filesystem::copy_option::overwrite_if_exists);
boost::filesystem::copy_file(exec_name, command, boost::filesystem::copy_options::overwrite_existing);

boost::filesystem::path command_args = command;
command_args += ' ';
Expand Down Expand Up @@ -196,8 +196,8 @@ int run_4(const char* argv[]) {

//[getting_started_on_program_restart_shmem
void** f = static_cast<void**>(g_region.get_address());
if (*f) { // Checking if memory constains stacktrace.
boost::stacktrace::stacktrace st
if (*f) { // Checking if memory contains stacktrace.
boost::stacktrace::stacktrace st
= boost::stacktrace::stacktrace::from_dump(f + 1, g_region.get_size() - sizeof(bool));

std::cout << "Previous run crashed and left trace in shared memory:\n" << st << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion example/throwing_st.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2022.
// Copyright Antony Polukhin, 2016-2024.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion example/trace_addresses.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2022.
// Copyright Antony Polukhin, 2016-2024.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion example/user_config.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2022.
// Copyright Antony Polukhin, 2016-2024.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion example/user_config.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2022.
// Copyright Antony Polukhin, 2016-2024.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2022.
// Copyright Antony Polukhin, 2016-2024.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
99 changes: 83 additions & 16 deletions include/boost/stacktrace/detail/addr2line_impls.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2022.
// Copyright Antony Polukhin, 2016-2024.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -12,6 +12,7 @@
# pragma once
#endif

#include <boost/stacktrace/detail/addr_base.hpp>
#include <boost/stacktrace/detail/to_hex_array.hpp>
#include <boost/stacktrace/detail/to_dec_array.hpp>
#include <boost/stacktrace/detail/try_dec_convert.hpp>
Expand All @@ -28,7 +29,7 @@ namespace boost { namespace stacktrace { namespace detail {

#if defined(BOOST_STACKTRACE_ADDR2LINE_LOCATION) && !defined(BOOST_NO_CXX11_CONSTEXPR)

constexpr bool is_abs_path(const char* path) BOOST_NOEXCEPT {
constexpr bool is_abs_path(const char* path) noexcept {
return *path != '\0' && (
*path == ':' || *path == '/' || is_abs_path(path + 1)
);
Expand All @@ -41,7 +42,7 @@ class addr2line_pipe {
::pid_t pid;

public:
explicit addr2line_pipe(const char *flag, const char* exec_path, const char* addr) BOOST_NOEXCEPT
explicit addr2line_pipe(const char *flag, const char* exec_path, const char* addr) noexcept
: p(0)
, pid(0)
{
Expand Down Expand Up @@ -97,11 +98,11 @@ class addr2line_pipe {
::close(pdes[1]);
}

operator ::FILE*() const BOOST_NOEXCEPT {
operator ::FILE*() const noexcept {
return p;
}

~addr2line_pipe() BOOST_NOEXCEPT {
~addr2line_pipe() noexcept {
if (p) {
::fclose(p);
int pstat = 0;
Expand Down Expand Up @@ -155,6 +156,19 @@ inline std::string addr2line(const char* flag, const void* addr) {
return res;
}

inline std::string source_location(const void* addr, bool position_independent) {
uintptr_t addr_base = 0;
if (position_independent) {
addr_base = boost::stacktrace::detail::get_own_proc_addr_base(addr);
}
const void* offset = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) - addr_base);
std::string source_line = boost::stacktrace::detail::addr2line("-Cpe", reinterpret_cast<const void*>(offset));
if (source_line.empty() || source_line[0] == '?') {
return "";
}

return source_line;
}

struct to_string_using_addr2line {
std::string res;
Expand All @@ -163,9 +177,20 @@ struct to_string_using_addr2line {
}

bool prepare_source_location(const void* addr) {
//return addr2line("-Cfipe", addr); // Does not seem to work in all cases
std::string source_line = boost::stacktrace::detail::addr2line("-Cpe", addr);
if (!source_line.empty() && source_line[0] != '?') {
// general idea in all addr2line uses:
// in each case:
// - try to resolve whole address as if it was a non-pie binary
// - if that didn't work, try to resolve just an offset from binary base address
// this is needed because:
// - in pie binaries just passing an address to addr2line won't work (it needs an offset in this case)
// - in non-pie binaries whole address is needed (offset won't work)
// - there is no easy way to test if binary is position independent (that I know of)
std::string source_line = boost::stacktrace::detail::source_location(addr, false);
if(source_line.empty()) {
source_line = boost::stacktrace::detail::source_location(addr, true);
}

if (!source_line.empty()) {
res += " at ";
res += source_line;
return true;
Expand All @@ -178,8 +203,13 @@ struct to_string_using_addr2line {
template <class Base> class to_string_impl_base;
typedef to_string_impl_base<to_string_using_addr2line> to_string_impl;

inline std::string name_impl(const void* addr) {
std::string res = boost::stacktrace::detail::addr2line("-fe", addr);
inline std::string name(const void* addr, bool position_independent) {
uintptr_t addr_base = 0;
if(position_independent){
addr_base = boost::stacktrace::detail::get_own_proc_addr_base(addr);
}
const void* offset = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) - addr_base);
std::string res = boost::stacktrace::detail::addr2line("-fe", offset);
res = res.substr(0, res.find_last_of('\n'));
res = boost::core::demangle(res.c_str());

Expand All @@ -190,11 +220,23 @@ inline std::string name_impl(const void* addr) {
return res;
}

} // namespace detail
inline std::string name_impl(const void* addr) {
std::string res = boost::stacktrace::detail::name(addr, false);
if (res.empty()) {
res = boost::stacktrace::detail::name(addr, true);
}

std::string frame::source_file() const {
return res;
}

inline std::string source_file(const void* addr, bool position_independent) {
std::string res;
res = boost::stacktrace::detail::addr2line("-e", addr_);
uintptr_t addr_base = 0;
if(position_independent){
addr_base = boost::stacktrace::detail::get_own_proc_addr_base(addr);
}
const void* offset = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) - addr_base);
res = boost::stacktrace::detail::addr2line("-e", offset);
res = res.substr(0, res.find_last_of(':'));
if (res == "??") {
res.clear();
Expand All @@ -203,10 +245,14 @@ std::string frame::source_file() const {
return res;
}


std::size_t frame::source_line() const {
inline std::size_t source_line(const void* addr, bool position_independent) {
std::size_t line_num = 0;
std::string res = boost::stacktrace::detail::addr2line("-e", addr_);
uintptr_t addr_base = 0;
if(position_independent){
addr_base = boost::stacktrace::detail::get_own_proc_addr_base(addr);
}
const void* offset = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) - addr_base);
std::string res = boost::stacktrace::detail::addr2line("-e", offset);
const std::size_t last = res.find_last_of(':');
if (last == std::string::npos) {
return 0;
Expand All @@ -220,6 +266,27 @@ std::size_t frame::source_line() const {
return line_num;
}

} // namespace detail


std::string frame::source_file() const {
std::string res = boost::stacktrace::detail::source_file(addr_, false);
if (res.empty()) {
res = boost::stacktrace::detail::source_file(addr_, true);
}

return res;
}

std::size_t frame::source_line() const {
std::size_t line_num = boost::stacktrace::detail::source_line(addr_, false);
if (line_num == 0) {
line_num = boost::stacktrace::detail::source_line(addr_, true);
}

return line_num;
}


}} // namespace boost::stacktrace

Expand Down
Loading

0 comments on commit 4632199

Please sign in to comment.