Skip to content

Commit

Permalink
Merge pull request #4771 from clemahieu/boost_stacktrace
Browse files Browse the repository at this point in the history
Link boost stacktrace appropriately for line information.
  • Loading branch information
pwojcikdev authored Feb 11, 2025
2 parents f82f161 + e056dd7 commit b4ba2d5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 39 deletions.
49 changes: 35 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,6 @@ set(NANO_ROCKSDB_TOOLS
OFF
CACHE BOOL "")

option(NANO_STACKTRACE_BACKTRACE
"Use BOOST_STACKTRACE_USE_BACKTRACE in stacktraces, for POSIX" OFF)

if(NANO_STACKTRACE_BACKTRACE)
add_definitions(-DNANO_STACKTRACE_BACKTRACE)
add_definitions(-DBOOST_STACKTRACE_USE_BACKTRACE)
if(NANO_BACKTRACE_INCLUDE)
add_definitions(
-DBOOST_STACKTRACE_BACKTRACE_INCLUDE_FILE=${NANO_BACKTRACE_INCLUDE})
endif()
endif()

# Enable NANO_TRACING by default in Debug builds
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
option(NANO_TRACING "Enable trace logging" ON)
Expand Down Expand Up @@ -400,6 +388,22 @@ endif()
include_directories(${CMAKE_SOURCE_DIR})
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules")

# Find libbacktrace first (Unix/Linux only)
if(NOT WIN32)
find_path(
BACKTRACE_INCLUDE_DIR
NAMES backtrace.h
PATHS /usr/local/include /usr/include)

if(BACKTRACE_INCLUDE_DIR)
set(BACKTRACE_FOUND TRUE)
# Most systems just need -lbacktrace
set(BACKTRACE_LIBRARIES backtrace)
message(STATUS "Found backtrace.h at ${BACKTRACE_INCLUDE_DIR}")
endif()
endif()

# Then set up boost modules
set(Boost_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/submodules/boost/libs/config/include)
set(BOOST_MODULE_LIBS
algorithm
Expand Down Expand Up @@ -495,8 +499,25 @@ foreach(lib IN LISTS BOOST_MODULE_LIBS)
add_subdirectory(submodules/boost/libs/${lib} EXCLUDE_FROM_ALL)
endforeach()
include_directories(${BOOST_LIBRARY_INCLUDES})
add_library(Boost::stacktrace ALIAS boost_stacktrace_basic)
add_definitions(-DBOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED)

# Configure stacktrace with appropriate backend
if(WIN32)
message(STATUS "Windows platform - Using WinDbg stacktrace")
add_definitions(-DBOOST_STACKTRACE_USE_WINDBG)
add_library(Boost::stacktrace ALIAS boost_stacktrace_windbg)
target_link_libraries(boost_stacktrace_windbg PRIVATE dbghelp)
elseif(BOOST_STACKTRACE_HAS_BACKTRACE)
message(
STATUS "Found libbacktrace - enabling Boost stacktrace backtrace support")
add_definitions(-DBOOST_STACKTRACE_USE_BACKTRACE)
add_library(Boost::stacktrace ALIAS boost_stacktrace_backtrace)
target_link_libraries(boost_stacktrace_backtrace
PRIVATE ${BACKTRACE_LIBRARIES})
else()
message(STATUS "Using basic stacktrace backend")
add_definitions(-DBOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED)
add_library(Boost::stacktrace ALIAS boost_stacktrace_basic)
endif()

# Workaround for missing reference errata in the boost property_tree module
target_link_libraries(boost_property_tree INTERFACE Boost::any)
Expand Down
11 changes: 0 additions & 11 deletions ci/build-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ fi

ulimit -S -n 8192

if [[ "$OS" == 'Linux' ]]; then
if clang --version && [ ${LCOV:-0} == 0 ]; then
BACKTRACE="-DNANO_STACKTRACE_BACKTRACE=ON \
-DNANO_BACKTRACE_INCLUDE=</tmp/backtrace.h>"
else
BACKTRACE="-DNANO_STACKTRACE_BACKTRACE=ON"
fi
else
BACKTRACE=""
fi

cmake \
-G'Unix Makefiles' \
-DACTIVE_NETWORK=nano_dev_network \
Expand Down
9 changes: 0 additions & 9 deletions ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ fi
SRC=${SRC:-${PWD}}
OS=$(uname)

CMAKE_BACKTRACE=""
if [[ ${OS} == 'Linux' ]]; then
CMAKE_BACKTRACE="-DNANO_STACKTRACE_BACKTRACE=ON"

if [[ ${COMPILER:-} == 'clang' ]]; then
CMAKE_BACKTRACE="${CMAKE_BACKTRACE} -DNANO_BACKTRACE_INCLUDE=</tmp/backtrace.h>"
fi
fi

CMAKE_QT_DIR=""
if [[ ${QT_DIR:-} ]]; then
CMAKE_QT_DIR="-DQt5_DIR=${QT_DIR}"
Expand Down
2 changes: 1 addition & 1 deletion ci/prepare/linux/prepare-clang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ update-alternatives --install /usr/bin/lldb lldb /usr/bin/lldb-$CLANG_VERSION 10

# Workaround to get a path that can be easily passed into cmake for BOOST_STACKTRACE_BACKTRACE_INCLUDE_FILE
# See https://www.boost.org/doc/libs/1_70_0/doc/html/stacktrace/configuration_and_build.html#stacktrace.configuration_and_build.f3
backtrace_file=$(find /usr/lib/gcc/ -name 'backtrace.h' | head -n 1) && test -f $backtrace_file && ln -s $backtrace_file /tmp/backtrace.h
backtrace_file=$(find /usr/lib/gcc/ -name 'backtrace.h' | head -n 1) && test -f $backtrace_file && ln -s $backtrace_file /usr/local/include/backtrace.h
1 change: 1 addition & 0 deletions nano/core_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ add_executable(
request_aggregator.cpp
signal_manager.cpp
socket.cpp
stacktrace.cpp
system.cpp
tcp_listener.cpp
telemetry.cpp
Expand Down
14 changes: 14 additions & 0 deletions nano/core_test/stacktrace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <nano/lib/stacktrace.hpp>

#include <gtest/gtest.h>

// Check that the stacktrace contains the current function name
// This depends on the way testcase names are compiled by gtest
// Current name: "stacktrace_human_readable_Test::TestBody()"
TEST (stacktrace, human_readable)
{
auto stacktrace = nano::generate_stacktrace ();
std::cout << stacktrace << std::endl;
ASSERT_FALSE (stacktrace.empty ());
ASSERT_TRUE (stacktrace.find ("stacktrace_human_readable_Test") != std::string::npos);
}
4 changes: 0 additions & 4 deletions nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ target_link_libraries(
Boost::property_tree
Boost::stacktrace)

if(NANO_STACKTRACE_BACKTRACE)
target_link_libraries(nano_lib backtrace)
endif()

target_compile_definitions(
nano_lib
PRIVATE -DMAJOR_VERSION_STRING=${CPACK_PACKAGE_VERSION_MAJOR}
Expand Down

0 comments on commit b4ba2d5

Please sign in to comment.