Skip to content

Commit

Permalink
refactor: use cpptrace
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Oct 17, 2024
1 parent 8e6556e commit 9f81a5e
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 84 deletions.
4 changes: 2 additions & 2 deletions .docker/build-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ if [[ "-$BUILD_TYPE-" == *-noperfmon-* ]]; then
CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_PERFMON=0 -DWITH_MAN_OPTION=0"
fi

if [[ "-$BUILD_TYPE-" == *-nostacktrace-* ]]; then
CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_STACKTRACE=0"
if [[ "-$BUILD_TYPE-" == *-stacktrace-* ]]; then
CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_STACKTRACE=ON"
fi

if [[ "-$BUILD_TYPE-" == *-source-* ]]; then
Expand Down
38 changes: 24 additions & 14 deletions .docker/install-static-libs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ cd "$HOME"
mkdir pkgs
cd pkgs

# TODO: move to gcc-14 once libunwind compiles with it
GCC_VERSION=13
GCC_VERSION=14
CLANG_VERSION=18

FILE_VERSION=5.45
FILE_SHA512=12611a59ff766c22a55db4b4a9f80f95a0a2e916a1d8593612c6ead32c247102a8fdc23693c6bf81bda9b604d951a62c0051e91580b1b79e190a3504c0efc20a
BZIP2_VERSION=1.0.8
LIBARCHIVE_VERSION=3.7.4
LIBARCHIVE_VERSION=3.7.7
FLAC_VERSION=1.4.3
LIBUNWIND_VERSION=1.7.2
BENCHMARK_VERSION=1.8.5
OPENSSL_VERSION=3.0.14
# LIBUNWIND_VERSION=1.7.2
BENCHMARK_VERSION=1.9.0
OPENSSL_VERSION=3.0.15
CPPTRACE_VERSION=0.7.2

RETRY=0
while true; do
Expand All @@ -36,9 +36,10 @@ done
wget https://sourceware.org/pub/bzip2/bzip2-${BZIP2_VERSION}.tar.gz
wget https://github.com/libarchive/libarchive/releases/download/v${LIBARCHIVE_VERSION}/libarchive-${LIBARCHIVE_VERSION}.tar.xz
wget https://github.com/xiph/flac/releases/download/${FLAC_VERSION}/flac-${FLAC_VERSION}.tar.xz
wget https://github.com/libunwind/libunwind/releases/download/v${LIBUNWIND_VERSION}/libunwind-${LIBUNWIND_VERSION}.tar.gz
# wget https://github.com/libunwind/libunwind/releases/download/v${LIBUNWIND_VERSION}/libunwind-${LIBUNWIND_VERSION}.tar.gz
wget https://github.com/google/benchmark/archive/refs/tags/v${BENCHMARK_VERSION}.tar.gz
wget https://github.com/openssl/openssl/releases/download/openssl-${OPENSSL_VERSION}/openssl-${OPENSSL_VERSION}.tar.gz
wget https://github.com/jeremy-rifkin/cpptrace/archive/refs/tags/v${CPPTRACE_VERSION}.tar.gz

for COMPILER in clang gcc; do
if [[ "$COMPILER" == "clang" ]]; then
Expand All @@ -57,12 +58,12 @@ for COMPILER in clang gcc; do

INSTALL_DIR=/opt/static-libs/$COMPILER

cd "$HOME/pkgs/$COMPILER"
tar xf ../libunwind-${LIBUNWIND_VERSION}.tar.gz
cd libunwind-${LIBUNWIND_VERSION}
./configure --prefix="$INSTALL_DIR"
make -j$(nproc)
make install
# cd "$HOME/pkgs/$COMPILER"
# tar xf ../libunwind-${LIBUNWIND_VERSION}.tar.gz
# cd libunwind-${LIBUNWIND_VERSION}
# ./configure --prefix="$INSTALL_DIR"
# make -j$(nproc)
# make install

cd "$HOME/pkgs/$COMPILER"
tar xf ../bzip2-${BZIP2_VERSION}.tar.gz
Expand All @@ -75,7 +76,7 @@ for COMPILER in clang gcc; do
cd openssl-${OPENSSL_VERSION}
./Configure --prefix="$INSTALL_DIR" --libdir=lib threads no-fips no-shared no-pic no-dso
make -j$(nproc)
make install
make install_sw

cd "$HOME/pkgs/$COMPILER"
tar xf ../libarchive-${LIBARCHIVE_VERSION}.tar.xz
Expand Down Expand Up @@ -106,6 +107,15 @@ for COMPILER in clang gcc; do
cmake .. -DBENCHMARK_ENABLE_TESTING=OFF -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR"
make -j$(nproc)
make install

cd "$HOME/pkgs/$COMPILER"
tar xf ../v${CPPTRACE_VERSION}.tar.gz
cd cpptrace-${CPPTRACE_VERSION}
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR"
make -j$(nproc)
make install
done

cd "$HOME"
Expand Down
17 changes: 7 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Configure Build
shell: cmd
run: |
cmake -B${{ runner.temp }}\build -S${{ runner.workspace }}\dwarfs -GNinja -DCMAKE_MAKE_PROGRAM=C:\bin\ninja.exe -DCMAKE_BUILD_TYPE=${{ matrix.build_mode }} -DWITH_UNIVERSAL_BINARY=ON -DWITH_TESTS=ON ${{ matrix.config.cmake_args }} -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static -DVCPKG_INSTALLED_DIR=${{ runner.workspace }}\vcpkg-install-dwarfs -DDWARFS_ARTIFACTS_DIR=Z:\artifacts\dwarfs -DWITH_PXATTR=ON
cmake -B${{ runner.temp }}\build -S${{ runner.workspace }}\dwarfs -GNinja -DCMAKE_MAKE_PROGRAM=C:\bin\ninja.exe -DCMAKE_BUILD_TYPE=${{ matrix.build_mode }} -DWITH_UNIVERSAL_BINARY=ON -DWITH_TESTS=ON ${{ matrix.config.cmake_args }} -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static -DVCPKG_INSTALLED_DIR=${{ runner.workspace }}\vcpkg-install-dwarfs -DDWARFS_ARTIFACTS_DIR=Z:\artifacts\dwarfs -DWITH_PXATTR=ON -DENABLE_STACKTRACE=${{ matrix.build_mode == 'Debug' && 'ON' || 'OFF' }}
- name: Run Build
shell: cmd
Expand Down Expand Up @@ -182,9 +182,6 @@ jobs:
- arch: arm64v8
dist: ubuntu
build_type: gcc-debug-shared-noperfmon-ninja-split
- arch: amd64
dist: ubuntu
build_type: gcc-debug-shared-nostacktrace-ninja-split
- arch: arm64v8
dist: ubuntu
build_type: oldclang-debug-shared-make-split
Expand Down Expand Up @@ -291,10 +288,10 @@ jobs:
build_type: gcc-release-ninja-O2-static
- arch: amd64
dist: ubuntu
build_type: clang-release-nostacktrace-ninja-static
build_type: clang-release-stacktrace-ninja-static
- arch: arm64v8
dist: ubuntu
build_type: clang-release-nostacktrace-ninja-static
build_type: clang-release-stacktrace-ninja-static
- arch: amd64
dist: ubuntu
build_type: clang-release-ninja-static
Expand All @@ -309,16 +306,16 @@ jobs:
build_type: clang-release-ninja-O2-static
- arch: amd64
dist: ubuntu
build_type: clang-reldbg-ninja-static
build_type: clang-reldbg-stacktrace-ninja-static
- arch: arm64v8
dist: ubuntu
build_type: clang-reldbg-ninja-static
build_type: clang-reldbg-stacktrace-ninja-static
- arch: amd64
dist: ubuntu
build_type: clang-debug-ninja-static
build_type: clang-debug-stacktrace-ninja-static
- arch: arm64v8
dist: ubuntu
build_type: clang-debug-ninja-static
build_type: clang-debug-stacktrace-ninja-static

runs-on:
- self-hosted
Expand Down
15 changes: 10 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ option(ENABLE_RICEPP "build with RICEPP compression support" ON)
option(WITH_UNIVERSAL_BINARY "build with universal binary" OFF)
option(WITH_PXATTR "build with pxattr binary" OFF)
option(WITH_EXAMPLE "build with example binary" OFF)
if(NOT (APPLE OR WIN32))
option(ENABLE_STACKTRACE "build with symbolizer support" ON)
endif()
option(ENABLE_STACKTRACE "build with stack trace support" OFF)
if(APPLE)
option(USE_HOMEBREW_LIBARCHIVE "use libarchive from homebrew" ON)
endif()
Expand Down Expand Up @@ -202,7 +200,7 @@ if(WITH_LIBDWARFS)
endif()
endif()

if(WIN32)
if(ENABLE_STACKTRACE)
find_package(cpptrace REQUIRED CONFIG)
endif()

Expand Down Expand Up @@ -235,6 +233,7 @@ if(WITH_LIBDWARFS)

include(${CMAKE_SOURCE_DIR}/cmake/libdwarfs.cmake)

set(DWARFS_HAVE_CPPTRACE ${cpptrace_FOUND})
set(DWARFS_HAVE_LIBZSTD ON)
set(DWARFS_USE_JEMALLOC ${USE_JEMALLOC})
set(DWARFS_HAVE_RICEPP ${ENABLE_RICEPP})
Expand Down Expand Up @@ -274,6 +273,9 @@ if(WITH_TOOLS)
list(APPEND BINARY_TARGETS ${tgt})

install(TARGETS ${tgt} RUNTIME DESTINATION bin)
if(WIN32)
install(FILES $<TARGET_PDB_FILE:${tgt}> DESTINATION bin OPTIONAL)
endif()
endforeach()

target_link_libraries(mkdwarfs_main PRIVATE dwarfs_reader dwarfs_writer dwarfs_rewrite)
Expand All @@ -296,6 +298,9 @@ if(WITH_PXATTR)
add_executable(pxattr tools/src/pxattr.cpp)
list(APPEND BINARY_TARGETS pxattr)
install(TARGETS pxattr RUNTIME DESTINATION bin)
if(WIN32)
install(FILES $<TARGET_PDB_FILE:pxattr> DESTINATION bin OPTIONAL)
endif()
endif()

if(WITH_EXAMPLE)
Expand Down Expand Up @@ -336,6 +341,7 @@ if(WITH_FUSE_DRIVER)
endif()
if(WINFSP)
install(TARGETS dwarfs-bin RUNTIME DESTINATION bin)
install(FILES $<TARGET_PDB_FILE:dwarfs-bin> DESTINATION bin OPTIONAL)
else()
add_custom_command(OUTPUT mount.dwarfs
COMMAND ${CMAKE_COMMAND} -E create_symlink dwarfs mount.dwarfs
Expand Down Expand Up @@ -665,7 +671,6 @@ foreach(tgt ${LIBDWARFS_TARGETS} ${LIBDWARFS_OBJECT_TARGETS} dwarfs_test_helpers

if(WIN32)
target_link_libraries(${tgt} PRIVATE ntdll.lib dbghelp.lib)
target_link_libraries(${tgt} PRIVATE cpptrace::cpptrace)
endif()
endforeach()

Expand Down
4 changes: 4 additions & 0 deletions cmake/dwarfs-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pkg_check_modules(LIBARCHIVE REQUIRED IMPORTED_TARGET libarchive>=@LIBARCHIVE_RE
pkg_check_modules(XXHASH REQUIRED IMPORTED_TARGET libxxhash>=@XXHASH_REQUIRED_VERSION@)
pkg_check_modules(ZSTD REQUIRED IMPORTED_TARGET libzstd>=@ZSTD_REQUIRED_VERSION@)

if(@cpptrace_FOUND@) # cpptrace_FOUND
find_dependency(cpptrace CONFIG REQUIRED)
endif()

if(@LIBLZ4_FOUND@) # LIBLZ4_FOUND
pkg_check_modules(LIBLZ4 REQUIRED IMPORTED_TARGET liblz4>=@LIBLZ4_REQUIRED_VERSION@)
endif()
Expand Down
30 changes: 3 additions & 27 deletions cmake/folly.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,6 @@ if(WIN32)
)
endif()

if(ENABLE_STACKTRACE)
target_sources(dwarfs_folly_lite PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/Dwarf.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/DwarfImpl.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/DwarfLineNumberVM.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/DwarfSection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/DwarfUtil.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/Elf.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/ElfCache.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/LineReader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/SignalHandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/StackTrace.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/SymbolizePrinter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/SymbolizedFrame.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/debugging/symbolizer/Symbolizer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/tracing/AsyncStack.cpp
)
endif()

set_property(TARGET dwarfs_folly_lite PROPERTY CXX_STANDARD 20)
target_include_directories(
dwarfs_folly_lite SYSTEM PUBLIC
Expand All @@ -147,7 +128,7 @@ apply_folly_compile_options_to_target(dwarfs_folly_lite)
target_link_libraries(dwarfs_folly_lite PUBLIC folly_deps)

if(WITH_BENCHMARKS)
add_library(dwarfs_follybenchmark_lite OBJECT
add_library(dwarfs_follybenchmark_lite OBJECT
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/Benchmark.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/Format.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/Unicode.cpp
Expand Down Expand Up @@ -192,13 +173,8 @@ if(WITH_BENCHMARKS)
target_link_libraries(dwarfs_follybenchmark_lite PUBLIC dwarfs_folly_lite)
endif()

if(ENABLE_STACKTRACE OR WITH_BENCHMARKS)
if(ENABLE_STACKTRACE)
set(_target dwarfs_folly_lite)
else()
set(_target dwarfs_follybenchmark_lite)
endif()
target_sources(${_target} PRIVATE
if(WITH_BENCHMARKS)
target_sources(dwarfs_follybenchmark_lite PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/SharedMutex.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/concurrency/CacheLocality.cpp
${CMAKE_CURRENT_SOURCE_DIR}/folly/folly/detail/Futex.cpp
Expand Down
4 changes: 4 additions & 0 deletions cmake/libdwarfs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ if(LIBBROTLIDEC_FOUND AND LIBBROTLIENC_FOUND)
target_link_libraries(dwarfs_common PRIVATE PkgConfig::LIBBROTLIDEC PkgConfig::LIBBROTLIENC)
endif()

if(ENABLE_STACKTRACE)
target_link_libraries(dwarfs_common PRIVATE cpptrace::cpptrace)
endif()

target_link_libraries(dwarfs_extractor PRIVATE PkgConfig::LIBARCHIVE)

target_include_directories(dwarfs_common SYSTEM PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/fsst>)
Expand Down
16 changes: 2 additions & 14 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@
#include <dwarfs/config.h>

#ifdef DWARFS_STACKTRACE_ENABLED
#include <folly/debugging/symbolizer/Symbolizer.h>

#if !(FOLLY_USE_SYMBOLIZER && FOLLY_HAVE_DWARF && FOLLY_HAVE_ELF)
#error "folly symbolizer is unavailable"
#endif
#include <cpptrace/cpptrace.hpp>
#endif

#include <fmt/chrono.h>
Expand Down Expand Up @@ -180,15 +176,7 @@ void stream_logger::write(level_type level, const std::string& output,
std::vector<std::string_view> st_lines;

if (enable_stack_trace_ || level == FATAL) {
using namespace folly::symbolizer;
Symbolizer symbolizer(LocationInfoMode::FULL);
FrameArray<8> addresses;
getStackTraceSafe(addresses);
symbolizer.symbolize(addresses);
folly::symbolizer::StringSymbolizePrinter printer(
color_ ? folly::symbolizer::SymbolizePrinter::COLOR : 0);
printer.println(addresses, 3);
stacktrace = printer.str();
stacktrace = cpptrace::generate_trace().to_string(true);
split_to(stacktrace, '\n', st_lines);
if (st_lines.back().empty()) {
st_lines.pop_back();
Expand Down
Loading

0 comments on commit 9f81a5e

Please sign in to comment.