Compare commits

...

23 Commits

Author SHA1 Message Date
Peter Dimov
274aeaa351 Disable static linking in GHA because of https://github.com/boostorg/stacktrace/issues/169 2024-06-21 18:33:09 +03:00
Peter Dimov
a615135de0 Update test/CMakeLists.txt 2024-06-21 18:12:51 +03:00
Peter Dimov
32749a7041 Update ci.yml 2024-06-21 18:07:37 +03:00
Peter Dimov
08d4f46333 Update test/CMakeLists.txt 2024-06-21 18:04:40 +03:00
Peter Dimov
97bc049bcb Link to ${CMAKE_DL_LIBS} where appropriate 2024-06-21 17:29:27 +03:00
Peter Dimov
98ed11d235 Update test/CMakeLists.txt 2024-06-21 17:25:11 +03:00
Peter Dimov
f1c5cd8963 Update test/CMakeLists.txt 2024-06-21 17:19:56 +03:00
Peter Dimov
94f11c52eb Add test/CMakeLists.txt 2024-06-21 17:13:06 +03:00
Peter Dimov
f6f3623594 Update cmake_install_test/CMakeLists.txt 2024-06-21 17:09:47 +03:00
Peter Dimov
aad95c2667 Add cmake_install_test, cmake_subdir_test 2024-06-21 17:05:04 +03:00
Peter Dimov
a95e7e5860 Add CMake jobs to GHA 2024-06-21 16:59:21 +03:00
Peter Dimov
8b79199a2a Merge pull request #168 from leha-bot/hotfix-for-cmake
hotfix for boost superproject CI
2024-06-21 16:56:41 +03:00
leha-bot
6e39a41abf hotfix for boost superproject CI 2024-06-21 14:59:41 +03:00
Alex
5747f85c0b CMake: add Boost::stacktrace ALIAS target for platform defaults (#167) 2024-06-20 11:26:28 +03:00
Antony Polukhin
c093aef138 Require support for noexcept and template aliases in tests 2024-06-20 10:42:31 +03:00
Antony Polukhin
66c0f7a54f avoid memory leaks and make the code more C++ish 2024-06-12 18:13:01 +03:00
Clément Chigot
aacfc2e557 Add dladdr minimal support for AIX
AIX doesn't provide dladdr syscall. This patch implements a minimal
version in order to be able to compile stacktrace.
2024-06-12 18:13:01 +03:00
Antony Polukhin
80af3a44c3 Fix shadowing (fixes #141) 2024-06-11 12:19:03 +03:00
Antony Polukhin
69260779d1 fix a typo in assert (fixes #164) 2024-06-10 22:24:16 +03:00
Peter Dimov
2c83563695 Add a conditional rule for enabling boost_stacktrace_from_exception. Fixes #165. (#166) 2024-05-27 11:38:17 +03:00
Marat Abrarov
39afcefb64 Avoid inclusion of rarely used Windows SDK headers which can cause conflict with other code using Windows SDK. (#157)
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
2024-04-16 11:47:28 +03:00
huangqinjin
d1b7a61353 Implement stacktrace from current exception for MSVC (#159)
std::current_exception() makes a copy of current exception object
into returned std::exception_ptr. So the tracking of the original
exception object and its stacktrace are lost.
2024-04-16 11:39:07 +03:00
Antony Polukhin
351b03d522 Fix typo in documentation (fixes #158) 2024-04-01 12:17:55 +03:00
18 changed files with 828 additions and 21 deletions

View File

@@ -40,7 +40,7 @@ jobs:
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install packages
if: matrix.install
@@ -126,7 +126,7 @@ jobs:
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup Boost
shell: cmd
@@ -165,3 +165,338 @@ jobs:
with:
github-token: ${{ secrets.github_token }}
parallel-finished: true
posix-cmake-subdir:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-20.04, ubuntu-22.04, macos-12, macos-13, macos-14 ]
shared: [ ON ] # https://github.com/boostorg/stacktrace/issues/169
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
- name: Install packages
if: matrix.install
run: sudo apt-get -y install ${{matrix.install}}
- name: Setup Boost
run: |
echo GITHUB_REPOSITORY: $GITHUB_REPOSITORY
LIBRARY=${GITHUB_REPOSITORY#*/}
echo LIBRARY: $LIBRARY
echo "LIBRARY=$LIBRARY" >> $GITHUB_ENV
echo GITHUB_BASE_REF: $GITHUB_BASE_REF
echo GITHUB_REF: $GITHUB_REF
REF=${GITHUB_BASE_REF:-$GITHUB_REF}
REF=${REF#refs/heads/}
echo REF: $REF
BOOST_BRANCH=develop && [ "$REF" == "master" ] && BOOST_BRANCH=master || true
echo BOOST_BRANCH: $BOOST_BRANCH
cd ..
git clone -b $BOOST_BRANCH --depth 1 https://github.com/boostorg/boost.git boost-root
cd boost-root
cp -r $GITHUB_WORKSPACE/* libs/$LIBRARY
git submodule update --init tools/boostdep
python tools/boostdep/depinst/depinst.py --git_args "--jobs 3" $LIBRARY
- name: Use library with add_subdirectory
run: |
cd ../boost-root/libs/$LIBRARY/test/cmake_subdir_test
mkdir __build__ && cd __build__
cmake -DBUILD_SHARED_LIBS=${{matrix.shared}} ..
cmake --build .
ctest --output-on-failure --no-tests=error
posix-cmake-install:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-20.04, ubuntu-22.04, macos-12, macos-13, macos-14 ]
shared: [ ON ] # https://github.com/boostorg/stacktrace/issues/169
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
- name: Install packages
if: matrix.install
run: sudo apt-get -y install ${{matrix.install}}
- name: Setup Boost
run: |
echo GITHUB_REPOSITORY: $GITHUB_REPOSITORY
LIBRARY=${GITHUB_REPOSITORY#*/}
echo LIBRARY: $LIBRARY
echo "LIBRARY=$LIBRARY" >> $GITHUB_ENV
echo GITHUB_BASE_REF: $GITHUB_BASE_REF
echo GITHUB_REF: $GITHUB_REF
REF=${GITHUB_BASE_REF:-$GITHUB_REF}
REF=${REF#refs/heads/}
echo REF: $REF
BOOST_BRANCH=develop && [ "$REF" == "master" ] && BOOST_BRANCH=master || true
echo BOOST_BRANCH: $BOOST_BRANCH
cd ..
git clone -b $BOOST_BRANCH --depth 1 https://github.com/boostorg/boost.git boost-root
cd boost-root
cp -r $GITHUB_WORKSPACE/* libs/$LIBRARY
git submodule update --init tools/boostdep
python tools/boostdep/depinst/depinst.py --git_args "--jobs 3" $LIBRARY
- name: Configure
run: |
cd ../boost-root
mkdir __build__ && cd __build__
cmake -DBOOST_INCLUDE_LIBRARIES=$LIBRARY -DCMAKE_INSTALL_PREFIX=~/.local -DBUILD_SHARED_LIBS=${{matrix.shared}} ..
- name: Build
run: |
cd ../boost-root/__build__
cmake --build .
- name: Install
run: |
cd ../boost-root/__build__
cmake --build . --target install
- name: Use the installed library
run: |
cd ../boost-root/libs/$LIBRARY/test/cmake_install_test && mkdir __build__ && cd __build__
cmake -DCMAKE_INSTALL_PREFIX=~/.local ..
cmake --build .
export LD_LIBRARY_PATH=$HOME/.local/lib:$LD_LIBRARY_PATH
ctest --output-on-failure --no-tests=error
posix-cmake-test:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-20.04, ubuntu-22.04, macos-12, macos-13, macos-14 ]
shared: [ ON ] # https://github.com/boostorg/stacktrace/issues/169
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
- name: Install packages
if: matrix.install
run: sudo apt-get -y install ${{matrix.install}}
- name: Setup Boost
run: |
echo GITHUB_REPOSITORY: $GITHUB_REPOSITORY
LIBRARY=${GITHUB_REPOSITORY#*/}
echo LIBRARY: $LIBRARY
echo "LIBRARY=$LIBRARY" >> $GITHUB_ENV
echo GITHUB_BASE_REF: $GITHUB_BASE_REF
echo GITHUB_REF: $GITHUB_REF
REF=${GITHUB_BASE_REF:-$GITHUB_REF}
REF=${REF#refs/heads/}
echo REF: $REF
BOOST_BRANCH=develop && [ "$REF" == "master" ] && BOOST_BRANCH=master || true
echo BOOST_BRANCH: $BOOST_BRANCH
cd ..
git clone -b $BOOST_BRANCH --depth 1 https://github.com/boostorg/boost.git boost-root
cd boost-root
cp -r $GITHUB_WORKSPACE/* libs/$LIBRARY
git submodule update --init tools/boostdep
python tools/boostdep/depinst/depinst.py --git_args "--jobs 3" $LIBRARY
- name: Configure
run: |
cd ../boost-root
mkdir __build__ && cd __build__
cmake -DBOOST_INCLUDE_LIBRARIES=$LIBRARY -DBUILD_TESTING=ON -DBUILD_SHARED_LIBS=${{matrix.shared}} ..
- name: Build tests
run: |
cd ../boost-root/__build__
cmake --build . --target tests
- name: Run tests
run: |
cd ../boost-root/__build__
ctest --output-on-failure --no-tests=error
windows-cmake-subdir:
strategy:
fail-fast: false
matrix:
os: [ windows-2019, windows-2022 ]
shared: [ OFF, ON ]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
- name: Setup Boost
shell: cmd
run: |
echo GITHUB_REPOSITORY: %GITHUB_REPOSITORY%
for /f %%i in ("%GITHUB_REPOSITORY%") do set LIBRARY=%%~nxi
echo LIBRARY: %LIBRARY%
echo LIBRARY=%LIBRARY%>>%GITHUB_ENV%
echo GITHUB_BASE_REF: %GITHUB_BASE_REF%
echo GITHUB_REF: %GITHUB_REF%
if "%GITHUB_BASE_REF%" == "" set GITHUB_BASE_REF=%GITHUB_REF%
set BOOST_BRANCH=develop
for /f %%i in ("%GITHUB_BASE_REF%") do if "%%~nxi" == "master" set BOOST_BRANCH=master
echo BOOST_BRANCH: %BOOST_BRANCH%
cd ..
git clone -b %BOOST_BRANCH% --depth 1 https://github.com/boostorg/boost.git boost-root
cd boost-root
xcopy /s /e /q %GITHUB_WORKSPACE% libs\%LIBRARY%\
git submodule update --init tools/boostdep
python tools/boostdep/depinst/depinst.py --git_args "--jobs 3" %LIBRARY%
- name: Use library with add_subdirectory (Debug)
shell: cmd
run: |
cd ../boost-root/libs/%LIBRARY%/test/cmake_subdir_test
mkdir __build__ && cd __build__
cmake -DBUILD_SHARED_LIBS=${{matrix.shared}} ..
cmake --build . --config Debug
ctest --output-on-failure --no-tests=error -C Debug
- name: Use library with add_subdirectory (Release)
shell: cmd
run: |
cd ../boost-root/libs/%LIBRARY%/test/cmake_subdir_test/__build__
cmake --build . --config Release
ctest --output-on-failure --no-tests=error -C Release
windows-cmake-install:
strategy:
fail-fast: false
matrix:
os: [ windows-2019, windows-2022 ]
shared: [ OFF, ON ]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
- name: Setup Boost
shell: cmd
run: |
echo GITHUB_REPOSITORY: %GITHUB_REPOSITORY%
for /f %%i in ("%GITHUB_REPOSITORY%") do set LIBRARY=%%~nxi
echo LIBRARY: %LIBRARY%
echo LIBRARY=%LIBRARY%>>%GITHUB_ENV%
echo GITHUB_BASE_REF: %GITHUB_BASE_REF%
echo GITHUB_REF: %GITHUB_REF%
if "%GITHUB_BASE_REF%" == "" set GITHUB_BASE_REF=%GITHUB_REF%
set BOOST_BRANCH=develop
for /f %%i in ("%GITHUB_BASE_REF%") do if "%%~nxi" == "master" set BOOST_BRANCH=master
echo BOOST_BRANCH: %BOOST_BRANCH%
cd ..
git clone -b %BOOST_BRANCH% --depth 1 https://github.com/boostorg/boost.git boost-root
cd boost-root
xcopy /s /e /q %GITHUB_WORKSPACE% libs\%LIBRARY%\
git submodule update --init tools/boostdep
python tools/boostdep/depinst/depinst.py --git_args "--jobs 3" %LIBRARY%
- name: Configure
shell: cmd
run: |
cd ../boost-root
mkdir __build__ && cd __build__
cmake -DBOOST_INCLUDE_LIBRARIES=%LIBRARY% -DCMAKE_INSTALL_PREFIX=C:/cmake-prefix -DBUILD_SHARED_LIBS=${{matrix.shared}} ..
- name: Install (Debug)
shell: cmd
run: |
cd ../boost-root/__build__
cmake --build . --target install --config Debug
- name: Install (Release)
shell: cmd
run: |
cd ../boost-root/__build__
cmake --build . --target install --config Release
- name: Use the installed library (Debug)
shell: cmd
run: |
cd ../boost-root/libs/%LIBRARY%/test/cmake_install_test && mkdir __build__ && cd __build__
cmake -DCMAKE_INSTALL_PREFIX=C:/cmake-prefix ..
cmake --build . --config Debug
PATH C:\cmake-prefix\bin;%PATH%
ctest --output-on-failure --no-tests=error -C Debug
- name: Use the installed library (Release)
shell: cmd
run: |
cd ../boost-root/libs/%LIBRARY%/test/cmake_install_test/__build__
cmake --build . --config Release
PATH C:\cmake-prefix\bin;%PATH%
ctest --output-on-failure --no-tests=error -C Release
windows-cmake-test:
strategy:
fail-fast: false
matrix:
os: [ windows-2019, windows-2022 ]
shared: [ ON ] # https://github.com/boostorg/stacktrace/issues/169
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v4
- name: Setup Boost
shell: cmd
run: |
echo GITHUB_REPOSITORY: %GITHUB_REPOSITORY%
for /f %%i in ("%GITHUB_REPOSITORY%") do set LIBRARY=%%~nxi
echo LIBRARY: %LIBRARY%
echo LIBRARY=%LIBRARY%>>%GITHUB_ENV%
echo GITHUB_BASE_REF: %GITHUB_BASE_REF%
echo GITHUB_REF: %GITHUB_REF%
if "%GITHUB_BASE_REF%" == "" set GITHUB_BASE_REF=%GITHUB_REF%
set BOOST_BRANCH=develop
for /f %%i in ("%GITHUB_BASE_REF%") do if "%%~nxi" == "master" set BOOST_BRANCH=master
echo BOOST_BRANCH: %BOOST_BRANCH%
cd ..
git clone -b %BOOST_BRANCH% --depth 1 https://github.com/boostorg/boost.git boost-root
cd boost-root
xcopy /s /e /q %GITHUB_WORKSPACE% libs\%LIBRARY%\
git submodule update --init tools/boostdep
python tools/boostdep/depinst/depinst.py --git_args "--jobs 3" %LIBRARY%
- name: Configure
shell: cmd
run: |
cd ../boost-root
mkdir __build__ && cd __build__
cmake -DBOOST_INCLUDE_LIBRARIES=%LIBRARY% -DBUILD_TESTING=ON -DBUILD_SHARED_LIBS=${{matrix.shared}} ..
- name: Build tests (Debug)
shell: cmd
run: |
cd ../boost-root/__build__
cmake --build . --target tests --config Debug
- name: Run tests (Debug)
shell: cmd
run: |
cd ../boost-root/__build__
ctest --output-on-failure --no-tests=error -C Debug
- name: Build tests (Release)
shell: cmd
run: |
cd ../boost-root/__build__
cmake --build . --target tests --config Release
- name: Run tests (Release)
shell: cmd
run: |
cd ../boost-root/__build__
ctest --output-on-failure --no-tests=error -C Release

View File

@@ -90,12 +90,26 @@ message(STATUS "Boost.Stacktrace: "
)
stacktrace_add_library(noop ${BOOST_STACKTRACE_ENABLE_NOOP} "" "")
stacktrace_add_library(backtrace ${BOOST_STACKTRACE_ENABLE_BACKTRACE} "backtrace" "")
stacktrace_add_library(addr2line ${BOOST_STACKTRACE_ENABLE_ADDR2LINE} "" "")
stacktrace_add_library(basic ${BOOST_STACKTRACE_ENABLE_BASIC} "" "")
stacktrace_add_library(backtrace ${BOOST_STACKTRACE_ENABLE_BACKTRACE} "backtrace;${CMAKE_DL_LIBS}" "")
stacktrace_add_library(addr2line ${BOOST_STACKTRACE_ENABLE_ADDR2LINE} "${CMAKE_DL_LIBS}" "")
stacktrace_add_library(basic ${BOOST_STACKTRACE_ENABLE_BASIC} "${CMAKE_DL_LIBS}" "")
stacktrace_add_library(windbg ${BOOST_STACKTRACE_ENABLE_WINDBG} "dbgeng;ole32" "_GNU_SOURCE=1")
stacktrace_add_library(windbg_cached ${BOOST_STACKTRACE_ENABLE_WINDBG_CACHED} "dbgeng;ole32" "_GNU_SOURCE=1")
# Alias target for platform defaults (only if they enabled).
if(WIN32)
if(BOOST_STACKTRACE_ENABLE_WINDBG)
add_library(Boost::stacktrace ALIAS boost_stacktrace_windbg)
endif()
else()
if(BOOST_STACKTRACE_ENABLE_BACKTRACE)
add_library(Boost::stacktrace ALIAS boost_stacktrace_backtrace)
elseif(BOOST_STACKTRACE_ENABLE_BASIC)
add_library(Boost::stacktrace ALIAS boost_stacktrace_basic)
endif()
endif()
if(BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt")
add_subdirectory(test)

View File

@@ -6,6 +6,7 @@
#
import feature ;
import property ;
import ../../config/checks/config : requires ;
project
@@ -134,19 +135,34 @@ lib boost_stacktrace_windbg_cached
#<link>shared:<define>BOOST_STACKTRACE_DYN_LINK=1
;
rule build-stacktrace-from-exception ( props * )
{
local enabled = [ property.select <boost.stacktrace.from_exception> : $(props) ] ;
switch $(enabled:G=)
{
case "on" : return ;
case "off" : return <build>no ;
}
local arch = [ property.select <architecture> : $(props) ] ;
if $(arch) && ( $(arch:G=) != x86 )
{
return <build>no ;
}
}
lib boost_stacktrace_from_exception
: # sources
../src/from_exception.cpp
: # requirements
<warnings>all
<target-os>linux:<library>dl
<target-os>windows:<build>no # not supported at the moment
# Command line option to disable build
<boost.stacktrace.from_exception>off:<build>no
# Enable build when explicitly requested, or by default, when on x86
<conditional>@build-stacktrace-from-exception
# Require usable libbacktrace on other platforms
[ check-target-builds ../build//libbacktrace : : <build>no ]
#[ check-target-builds ../build//libbacktrace : : <build>no ]
: # default build
: # usage-requirements
#<link>shared:<define>BOOST_STACKTRACE_DYN_LINK=1

View File

@@ -137,7 +137,7 @@ See [link stacktrace.theoretical_async_signal_safety "Theoretical async signal s
[section Stacktrace from arbitrary exception]
[warning At the moment the functionality is only available for some of the
popular C++ runtimes for POSIX systems and requires *libbacktrace*.
popular C++ runtimes for POSIX systems with *libbacktrace* and for Windows.
Make sure that your platform is supported by running some tests.
]
@@ -163,7 +163,7 @@ int main() {
foo("test1");
bar("test2");
} catch (const std::exception& exc) {
boost::stacktrace::stacktrace trace = std::stacktrace::from_current_exception(); // <---
boost::stacktrace::stacktrace trace = boost::stacktrace::from_current_exception(); // <---
std::cerr << "Caught exception: " << exc.what() << ", trace:\n" << trace;
}
}

View File

@@ -18,7 +18,17 @@
#include <boost/core/noncopyable.hpp>
#include <boost/stacktrace/detail/to_dec_array.hpp>
#include <boost/stacktrace/detail/to_hex_array.hpp>
#ifdef WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
// Prevent inclusion of extra Windows SDK headers which can cause conflict
// with other code using Windows SDK
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#endif
#include "dbgeng.h"
#include <mutex>

View File

@@ -90,8 +90,8 @@ std::string frame::name() const {
}
#if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
::Dl_info dli;
const bool dl_ok = !!::dladdr(const_cast<void*>(addr_), &dli); // `dladdr` on Solaris accepts nonconst addresses
boost::stacktrace::detail::Dl_info dli;
const bool dl_ok = !!boost::stacktrace::detail::dladdr(addr_, dli);
if (dl_ok && dli.dli_sname) {
return boost::core::demangle(dli.dli_sname);
}

View File

@@ -18,17 +18,110 @@
# include <boost/winapi/dll.hpp>
#endif
#ifdef _AIX
/* AIX doesn't provide dladdr syscall.
This provides a minimal implementation of dladdr which retrieves
only files information.
TODO: Implement the symbol name. */
#include <sys/ldr.h>
#include <sys/debug.h>
#include <cstring>
#include <string>
#include <vector>
namespace boost { namespace stacktrace { namespace detail {
struct Dl_info {
std::string fname_storage{};
const char *dli_fname = nullptr;
const char *dli_sname = nullptr;
};
int dladdr(const void* address_raw, Dl_info* info) noexcept {
static constexpr std::size_t dl_buff_size = 0x1000;
try {
std::vector<struct ld_info> pld_info_storage;
pld_info_storage.resize(
(dl_buff_size + sizeof(struct ld_info) - 1) / sizeof(struct ld_info)
);
if (loadquery(L_GETINFO, pld_info_storage.data(), dl_buff_size) == -1) {
return 0;
}
const auto* pld_info = pld_info_storage.data();
const char* const address = static_cast<const char*>(address_raw);
while (true) {
const auto* const dataorg = static_cast<char*>(pld_info->ldinfo_dataorg);
const auto* const textorg = static_cast<char*>(pld_info->ldinfo_textorg);
if ((address >= dataorg && address < dataorg + pld_info->ldinfo_datasize )
|| (address >= textorg && address < textorg + pld_info->ldinfo_textsize )) {
/* ldinfo_filename is the null-terminated path name followed
by null-terminated member name.
If the file is not an archive, then member name is null. */
const auto size_filename = std::strlen(pld_info->ldinfo_filename);
const auto size_member = std::strlen(pld_info->ldinfo_filename + size_filename + 1);
/* If member is not null, '(' and ')' must be added to create a
fname looking like "filename(membername)". */
info->fname_storage.reserve(size_filename + (size_member ? size_member + 3 : 1));
info->fname_storage = pld_info->ldinfo_filename;
if (size_member) {
info->fname_storage += "(";
info->fname_storage += pld_info->ldinfo_filename + size_filename + 1;
info->fname_storage += ")";
}
info->dli_fname = info->fname_storage.c_str();
return 1;
}
if (!pld_info->ldinfo_next) {
break;
}
pld_info = reinterpret_cast<const struct ld_info *>(
reinterpret_cast<const char*>(pld_info) + pld_info->ldinfo_next
);
};
} catch (...) {
// ignore
}
return 0;
}
}}} // namespace boost::stacktrace::detail
#elif !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
namespace boost { namespace stacktrace { namespace detail {
using Dl_info = ::Dl_info;
inline int dladdr(const void* addr, Dl_info& dli) noexcept {
// `dladdr` on Solaris accepts nonconst addresses
return ::dladdr(const_cast<void*>(addr), &dli);
}
}}} // namespace boost::stacktrace::detail
#endif
namespace boost { namespace stacktrace { namespace detail {
#if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
class location_from_symbol {
::Dl_info dli_;
boost::stacktrace::detail::Dl_info dli_;
public:
explicit location_from_symbol(const void* addr) noexcept
: dli_()
{
if (!::dladdr(const_cast<void*>(addr), &dli_)) { // `dladdr` on Solaris accepts nonconst addresses
if (!boost::stacktrace::detail::dladdr(addr, dli_)) {
dli_.dli_fname = 0;
}
}

View File

@@ -35,7 +35,7 @@ namespace boost { namespace stacktrace {
/// @cond
namespace detail {
typedef const void* native_frame_ptr_t; // TODO: change to `typedef void(*native_frame_ptr_t)();`
using native_frame_ptr_t = const void*;
enum helper{ max_frames_dump = 128 };
BOOST_STACKTRACE_FUNCTION std::size_t from_dump(const char* filename, native_frame_ptr_t* out_frames);
@@ -52,7 +52,7 @@ struct this_thread_frames { // struct is required to avoid warning about usage o
BOOST_NOINLINE BOOST_STACKTRACE_FUNCTION static std::size_t collect(native_frame_ptr_t* out_frames, std::size_t max_frames_count, std::size_t skip) noexcept;
BOOST_NOINLINE static std::size_t safe_dump_to_impl(void* memory, std::size_t size, std::size_t skip) noexcept {
typedef boost::stacktrace::detail::native_frame_ptr_t native_frame_ptr_t;
using boost::stacktrace::detail::native_frame_ptr_t;
if (size < sizeof(native_frame_ptr_t)) {
return 0;
@@ -66,7 +66,7 @@ struct this_thread_frames { // struct is required to avoid warning about usage o
template <class T>
BOOST_NOINLINE static std::size_t safe_dump_to_impl(T file, std::size_t skip, std::size_t max_depth) noexcept {
typedef boost::stacktrace::detail::native_frame_ptr_t native_frame_ptr_t;
using boost::stacktrace::detail::native_frame_ptr_t;
native_frame_ptr_t buffer[boost::stacktrace::detail::max_frames_dump + 1];
if (max_depth > boost::stacktrace::detail::max_frames_dump) {

View File

@@ -33,6 +33,26 @@
# pragma warning(disable:2196) // warning #2196: routine is both "inline" and "noinline"
#endif
#if defined(BOOST_MSVC)
extern "C" {
BOOST_SYMBOL_EXPORT inline void* boost_stacktrace_impl_return_nullptr() { return nullptr; }
const char* boost_stacktrace_impl_current_exception_stacktrace();
bool* boost_stacktrace_impl_ref_capture_stacktraces_at_throw();
}
#ifdef _M_IX86
# pragma comment(linker, "/ALTERNATENAME:_boost_stacktrace_impl_current_exception_stacktrace=_boost_stacktrace_impl_return_nullptr")
# pragma comment(linker, "/ALTERNATENAME:_boost_stacktrace_impl_ref_capture_stacktraces_at_throw=_boost_stacktrace_impl_return_nullptr")
#else
# pragma comment(linker, "/ALTERNATENAME:boost_stacktrace_impl_current_exception_stacktrace=boost_stacktrace_impl_return_nullptr")
# pragma comment(linker, "/ALTERNATENAME:boost_stacktrace_impl_ref_capture_stacktraces_at_throw=boost_stacktrace_impl_return_nullptr")
#endif
#endif
namespace boost { namespace stacktrace {
namespace impl {
@@ -381,6 +401,8 @@ public:
if (impl::current_exception_stacktrace) {
trace = impl::current_exception_stacktrace();
}
#elif defined(BOOST_MSVC)
trace = boost_stacktrace_impl_current_exception_stacktrace();
#endif
if (trace) {

View File

@@ -27,6 +27,10 @@ inline void set_capture_stacktraces_at_throw(bool enable = true) noexcept {
if (impl::ref_capture_stacktraces_at_throw) {
impl::ref_capture_stacktraces_at_throw() = enable;
}
#elif defined(BOOST_MSVC)
if (bool* p = boost_stacktrace_impl_ref_capture_stacktraces_at_throw()) {
*p = enable;
}
#endif
(void)enable;
}
@@ -45,6 +49,10 @@ inline bool get_capture_stacktraces_at_throw() noexcept {
if (impl::ref_capture_stacktraces_at_throw) {
return impl::ref_capture_stacktraces_at_throw();
}
#elif defined(BOOST_MSVC)
if (bool* p = boost_stacktrace_impl_ref_capture_stacktraces_at_throw()) {
return *p;
}
#endif
return false;
}

View File

@@ -4,6 +4,147 @@
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if defined(_MSC_VER)
#include <boost/stacktrace/safe_dump_to.hpp>
#include <windows.h>
extern "C" void** __cdecl __current_exception(); // exported from vcruntime.dll
#define _pCurrentException static_cast<PEXCEPTION_RECORD>(*__current_exception())
namespace {
constexpr std::size_t kStacktraceDumpSize = 4096;
struct thrown_info {
ULONG_PTR object;
char* dump;
};
struct exception_data {
bool capture_stacktraces_at_throw = true;
unsigned count = 0;
thrown_info* info = nullptr;
~exception_data() noexcept {
HANDLE hHeap = GetProcessHeap();
for (unsigned i = 0; i < count; ++i) {
HeapFree(hHeap, 0, info[i].dump);
}
HeapFree(hHeap, 0, info);
}
};
thread_local exception_data data;
inline bool PER_IS_MSVC_EH(PEXCEPTION_RECORD p) noexcept {
const DWORD EH_EXCEPTION_NUMBER = 0xE06D7363;
const ULONG_PTR EH_MAGIC_NUMBER1 = 0x19930520;
return p->ExceptionCode == EH_EXCEPTION_NUMBER &&
(p->NumberParameters == 3 || p->NumberParameters == 4) &&
p->ExceptionInformation[0] == EH_MAGIC_NUMBER1;
}
inline ULONG_PTR PER_PEXCEPTOBJ(PEXCEPTION_RECORD p) noexcept {
return p->ExceptionInformation[1];
}
unsigned current_cxx_exception_index() noexcept {
if (PEXCEPTION_RECORD current_cxx_exception = _pCurrentException) {
for (unsigned i = data.count; i > 0;) {
--i;
if (data.info[i].object == PER_PEXCEPTOBJ(current_cxx_exception)) {
return i;
}
}
}
return data.count;
}
bool is_processing_rethrow(PEXCEPTION_RECORD p) noexcept {
// Processing flow:
// 0. throw;
// 1. _CxxThrowException(pExceptionObject = nullptr)
// 2. VEH & SEH (may throw new c++ exceptions!)
// 3. __RethrowException(_pCurrentException)
// 4. VEH
if (PER_PEXCEPTOBJ(p) == 0) return true;
PEXCEPTION_RECORD current_cxx_exception = _pCurrentException;
if (current_cxx_exception == nullptr) return false;
return PER_PEXCEPTOBJ(p) == PER_PEXCEPTOBJ(current_cxx_exception);
}
LONG NTAPI veh(PEXCEPTION_POINTERS p) {
if (data.capture_stacktraces_at_throw &&
PER_IS_MSVC_EH(p->ExceptionRecord) &&
!is_processing_rethrow(p->ExceptionRecord)) {
HANDLE hHeap = GetProcessHeap();
unsigned index = current_cxx_exception_index();
unsigned new_count = 1 + (index < data.count ? index + 1 : 0);
for (unsigned i = new_count; i < data.count; ++i) {
HeapFree(hHeap, 0, data.info[i].dump);
data.info[i].dump = nullptr;
}
void* new_info;
if (data.info) {
new_info = HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, data.info, sizeof(thrown_info) * new_count);
} else {
new_info = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(thrown_info) * new_count);
}
if (new_info) {
data.count = new_count;
data.info = static_cast<thrown_info*>(new_info);
data.info[data.count - 1].object = PER_PEXCEPTOBJ(p->ExceptionRecord);
char*& dump_ptr = data.info[data.count - 1].dump;
if (dump_ptr == nullptr) {
dump_ptr = static_cast<char*>(HeapAlloc(hHeap, 0, kStacktraceDumpSize));
}
if (dump_ptr != nullptr) {
const std::size_t kSkip = 4;
boost::stacktrace::safe_dump_to(kSkip, dump_ptr, kStacktraceDumpSize);
}
} else if (new_count <= data.count) {
data.count = new_count - 1;
HeapFree(hHeap, 0, data.info[data.count - 1].dump);
data.info[data.count - 1].dump = nullptr;
}
}
return EXCEPTION_CONTINUE_SEARCH;
}
struct veh_installer {
PVOID h;
veh_installer() noexcept : h(AddVectoredExceptionHandler(1, veh)) {}
~veh_installer() noexcept { RemoveVectoredExceptionHandler(h); }
} installer;
}
extern "C" {
BOOST_SYMBOL_EXPORT const char* boost_stacktrace_impl_current_exception_stacktrace() {
unsigned index = current_cxx_exception_index();
return index < data.count ? data.info[index].dump : nullptr;
}
BOOST_SYMBOL_EXPORT bool* boost_stacktrace_impl_ref_capture_stacktraces_at_throw() {
return &data.capture_stacktraces_at_throw;
}
}
namespace boost { namespace stacktrace { namespace impl {
BOOST_SYMBOL_EXPORT void assert_no_pending_traces() noexcept {
}
}}} // namespace boost::stacktrace::impl
#else
#include "exception_headers.h"
// At the moment the file is used only on POSIX. _Unwind_Backtrace may be
@@ -92,7 +233,7 @@ static const char*& reference_to_empty_padding(void* ptr) noexcept {
if (is_libcpp_runtime()) {
// libc++-runtime
BOOST_ASSERT_MSG(
sizeof(void*) == 4,
sizeof(void*) != 4,
"32bit platforms are unsupported with libc++ runtime padding reusage. "
"Please report this issue to the library maintainters."
);
@@ -222,3 +363,4 @@ BOOST_SYMBOL_EXPORT void assert_no_pending_traces() noexcept {
}}} // namespace boost::stacktrace::impl
#endif

15
test/CMakeLists.txt Normal file
View File

@@ -0,0 +1,15 @@
# Copyright 2018-2020 Peter Dimov
# Distributed under 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
include(BoostTest OPTIONAL RESULT_VARIABLE HAVE_BOOST_TEST)
if(NOT HAVE_BOOST_TEST)
return()
endif()
# This test doesn't work with stacktrace_basic
# boost_test(TYPE run SOURCES test.cpp test_impl.cpp Boost::stacktrace_basic Boost::core)
boost_test(TYPE run SOURCES test_noop.cpp test_impl.cpp LINK_LIBRARIES Boost::stacktrace_noop Boost::core)
boost_test(TYPE run SOURCES test_trivial.cpp LINK_LIBRARIES Boost::stacktrace_basic Boost::core)

View File

@@ -24,7 +24,7 @@ lib backtrace
project
: requirements
[ requires cxx11_rvalue_references ]
[ requires cxx11_rvalue_references cxx11_template_aliases cxx11_noexcept ]
<toolset>msvc:<asynch-exceptions>on
<toolset>intel:<cxxflags>-wd2196
<target-os>linux:<linkflags>-lpthread
@@ -183,17 +183,29 @@ test-suite stacktrace_tests
[ run test_from_exception_none.cpp : : : $(FORCE_SYMBOL_EXPORT) $(BASIC_DEPS) <debug-symbols>on : from_exception_none_basic_ho ]
[ run test_from_exception_none.cpp : : : $(LINKSHARED_BT) <debug-symbols>on : from_exception_none_bt ]
[ run test_from_exception_none.cpp : : : <define>BOOST_STACKTRACE_USE_BACKTRACE $(BT_DEPS) <debug-symbols>on : from_exception_none_bt_ho ]
[ run test_from_exception_none.cpp : : : $(LINKSHARED_WIND) <debug-symbols>on : from_exception_none_windbg ]
[ run test_from_exception_none.cpp : : : <define>BOOST_STACKTRACE_USE_WINDBG $(WIND_DEPS) <debug-symbols>on : from_exception_none_windbg_ho ]
[ run test_from_exception_none.cpp : : : $(LINKSHARED_WIND_CACHED) <debug-symbols>on : from_exception_none_windbg_cached ]
[ run test_from_exception_none.cpp : : : <define>BOOST_STACKTRACE_USE_WINDBG_CACHED $(WICA_DEPS) <debug-symbols>on : from_exception_none_windbg_cached_ho ]
[ run test_from_exception_none.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception $(LINKSHARED_BASIC) <debug-symbols>on : from_exception_disabled_basic ]
[ run test_from_exception_none.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception $(FORCE_SYMBOL_EXPORT) $(BASIC_DEPS) <debug-symbols>on : from_exception_disabled_basic_ho ]
[ run test_from_exception_none.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception $(LINKSHARED_BT) <debug-symbols>on : from_exception_disabled_bt ]
[ run test_from_exception_none.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception <define>BOOST_STACKTRACE_USE_BACKTRACE $(BT_DEPS) : from_exception_disabled_bt_ho ]
[ run test_from_exception_none.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception $(LINKSHARED_WIND) <debug-symbols>on : from_exception_disabled_windbg ]
[ run test_from_exception_none.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception <define>BOOST_STACKTRACE_USE_WINDBG $(WIND_DEPS) <debug-symbols>on : from_exception_disabled_windbg_ho ]
[ run test_from_exception_none.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception $(LINKSHARED_WIND_CACHED) <debug-symbols>on : from_exception_disabled_windbg_cached ]
[ run test_from_exception_none.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception <define>BOOST_STACKTRACE_USE_WINDBG_CACHED $(WICA_DEPS) <debug-symbols>on : from_exception_disabled_windbg_cached_ho ]
[ link test_from_exception.cpp : <library>/boost/stacktrace//boost_stacktrace_from_exception $(LINKSHARED_BASIC) <debug-symbols>on : from_exception_basic ]
[ run test_from_exception.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception $(LINKSHARED_BT) <debug-symbols>on : from_exception_bt ]
[ run test_from_exception.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception $(LINKSHARED_WIND) <debug-symbols>on : from_exception_windbg ]
[ run test_from_exception.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception $(LINKSHARED_WIND_CACHED) <debug-symbols>on : from_exception_windbg_cached ]
[ link test_from_exception.cpp : <library>/boost/stacktrace//boost_stacktrace_from_exception $(FORCE_SYMBOL_EXPORT) $(BASIC_DEPS) <debug-symbols>on : from_exception_basic_ho ]
[ run test_from_exception.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception <define>BOOST_STACKTRACE_USE_BACKTRACE $(BT_DEPS) <debug-symbols>on : from_exception_bt_ho ]
[ run test_from_exception.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception <define>BOOST_STACKTRACE_USE_WINDBG $(WIND_DEPS) <debug-symbols>on : from_exception_windbg_ho ]
[ run test_from_exception.cpp : : : <library>/boost/stacktrace//boost_stacktrace_from_exception <define>BOOST_STACKTRACE_USE_WINDBG_CACHED $(WICA_DEPS) <debug-symbols>on : from_exception_windbg_cached_ho ]
;
# Assuring that examples compile and run. Adding sources from `examples` directory to the `type_index` test suite.

View File

@@ -0,0 +1,15 @@
# Copyright 2018-2021 Peter Dimov
# Distributed under the Boost Software License, Version 1.0.
# https://www.boost.org/LICENSE_1_0.txt
cmake_minimum_required(VERSION 3.5...3.20)
project(cmake_install_test LANGUAGES CXX)
find_package(boost_stacktrace_basic REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main Boost::stacktrace_basic)
enable_testing()
add_test(main main)

View File

@@ -0,0 +1,12 @@
// Copyright 2019 Peter Dimov
// Copyright 2022-2024 Antony Polukhin
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/stacktrace/stacktrace.hpp>
#include <iostream>
int main()
{
std::cout << boost::stacktrace::stacktrace() << std::endl;
}

View File

@@ -0,0 +1,46 @@
# Copyright 2018-2021 Peter Dimov
# Distributed under 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
cmake_minimum_required(VERSION 3.5...3.20)
project(cmake_subdir_test LANGUAGES CXX)
# Put boost_stacktrace_*.dll in the same directory as main.exe
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
add_subdirectory(../.. boostorg/stacktrace)
# boostdep --brief stacktrace
set(deps
# Primary dependencies
assert
config
container_hash
core
predef
winapi
# Secondary dependencies
describe
mp11
static_assert
throw_exception
)
foreach(dep IN LISTS deps)
add_subdirectory(../../../${dep} boostorg/${dep})
endforeach()
add_executable(main main.cpp)
target_link_libraries(main Boost::stacktrace_basic)
enable_testing()
add_test(main main)

View File

@@ -0,0 +1,12 @@
// Copyright 2019 Peter Dimov
// Copyright 2022-2024 Antony Polukhin
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/stacktrace/stacktrace.hpp>
#include <iostream>
int main()
{
std::cout << boost::stacktrace::stacktrace() << std::endl;
}

View File

@@ -31,7 +31,27 @@ BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void in_test_throw_1(const char* msg) {
BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void in_test_throw_2(const char* msg) {
std::string new_msg{msg};
throw std::runtime_error(new_msg);
throw std::logic_error(new_msg);
}
BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void in_test_rethrow_1(const char* msg) {
try {
in_test_throw_1(msg);
} catch (const std::exception&) {
throw;
}
}
BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void in_test_rethrow_2(const char* msg) {
try {
in_test_throw_2(msg);
} catch (const std::exception&) {
try {
in_test_throw_1(msg);
} catch (const std::exception&) {}
throw;
}
}
BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void test_no_exception() {
@@ -67,6 +87,31 @@ BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void test_after_other_exception() {
}
}
BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void test_rethrow() {
try {
in_test_rethrow_1("test rethrow");
} catch (const std::exception&) {
auto trace = stacktrace::from_current_exception();
BOOST_TEST(trace);
std::cout << "Tarce in test_rethrow(): " << trace << '\n';
BOOST_TEST(to_string(trace).find("in_test_throw_1") != std::string::npos);
BOOST_TEST(to_string(trace).find("in_test_rethrow_1") != std::string::npos);
}
}
BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void test_rethrow_after_other_exception() {
try {
in_test_rethrow_2("test_rethrow_after_other_exception");
} catch (const std::exception&) {
auto trace = stacktrace::from_current_exception();
BOOST_TEST(trace);
std::cout << "Tarce in test_rethrow_after_other_exception(): " << trace << '\n';
BOOST_TEST(to_string(trace).find("in_test_throw_1") == std::string::npos);
BOOST_TEST(to_string(trace).find("in_test_throw_2") != std::string::npos);
BOOST_TEST(to_string(trace).find("in_test_rethrow_2") != std::string::npos);
}
}
BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void test_nested() {
try {
in_test_throw_1("test_other_exception_active");
@@ -103,7 +148,11 @@ BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void test_rethrow_nested() {
BOOST_TEST(trace);
std::cout << "Tarce in test_rethrow_nested(): " << trace << '\n';
BOOST_TEST(to_string(trace).find("in_test_throw_1") == std::string::npos);
#if defined(BOOST_MSVC)
BOOST_TEST(to_string(trace).find("in_test_throw_2") == std::string::npos);
#else
BOOST_TEST(to_string(trace).find("in_test_throw_2") != std::string::npos);
#endif
}
}
@@ -133,7 +182,11 @@ BOOST_NOINLINE BOOST_SYMBOL_VISIBLE void test_from_other_thread() {
BOOST_TEST(trace);
std::cout << "Tarce in test_rethrow_nested(): " << trace << '\n';
BOOST_TEST(to_string(trace).find("in_test_throw_1") == std::string::npos);
#if defined(BOOST_MSVC)
BOOST_TEST(to_string(trace).find("in_test_throw_2") == std::string::npos);
#else
BOOST_TEST(to_string(trace).find("in_test_throw_2") != std::string::npos);
#endif
}
#endif
}
@@ -146,6 +199,8 @@ int main() {
test_no_exception();
test_trace_from_exception();
test_after_other_exception();
test_rethrow();
test_rethrow_after_other_exception();
test_nested();
test_rethrow_nested();
test_from_other_thread();