Skip to content

Commit

Permalink
rework (lib)unwind integration
Browse files Browse the repository at this point in the history
Allow to switch between two unwindlib drivers.
  • Loading branch information
sergiud committed Dec 29, 2023
1 parent 3c052d9 commit 1059aa8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
44 changes: 39 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@ option (WITH_GTEST "Use Google Test" ON)
option (WITH_PKGCONFIG "Enable pkg-config support" ON)
option (WITH_SYMBOLIZE "Enable symbolize module" ON)
option (WITH_THREADS "Enable multithreading support" ON)
option (WITH_UNWIND "Enable libunwind support" ON)

set (WITH_UNWIND unwind CACHE STRING "unwind driver")
# Additional options will be appended later
set_property (CACHE WITH_UNWIND PROPERTY STRINGS none)

cmake_dependent_option (WITH_GMOCK "Use Google Mock" ON WITH_GTEST OFF)
cmake_dependent_option (WITH_TLS "Enable Thread Local Storage (TLS) support" ON WITH_THREADS OFF)

set (WITH_FUZZING none CACHE STRING "Fuzzing engine")
set_property (CACHE WITH_FUZZING PROPERTY STRINGS none libfuzzer ossfuzz)

if (NOT WITH_UNWIND)
if (WITH_UNWIND STREQUAL none)
set (CMAKE_DISABLE_FIND_PACKAGE_Unwind ON)
endif (NOT WITH_UNWIND)
endif (WITH_UNWIND STREQUAL none)

if (NOT WITH_GTEST)
set (CMAKE_DISABLE_FIND_PACKAGE_GTest ON)
Expand Down Expand Up @@ -91,13 +94,44 @@ find_package (Threads)
find_package (Unwind)

if (Unwind_FOUND)
set (HAVE_LIB_UNWIND 1)
else (Unwind_FOUND)
cmake_push_check_state (RESET)
set (CMAKE_REQUIRED_LIBRARIES unwind::unwind)

# Check whether linking actually succeeds. ARM toolchains of LLVM unwind
# implementation do not necessarily provide the _Unwind_Backtrace function
# which causes the previous check to succeed but the linking to fail.
check_cxx_symbol_exists (_Unwind_Backtrace unwind.h HAVE__UNWIND_BACKTRACE)
check_cxx_symbol_exists (_Unwind_GetIP unwind.h HAVE__UNWIND_GETIP)

check_cxx_symbol_exists (unw_get_reg libunwind.h HAVE_UNW_GET_REG)
check_cxx_symbol_exists (unw_getcontext libunwind.h HAVE_UNW_GETCONTEXT)
check_cxx_symbol_exists (unw_init_local libunwind.h HAVE_UNW_INIT_LOCAL)
check_cxx_symbol_exists (unw_step libunwind.h HAVE_UNW_STEP)

if (HAVE__UNWIND_BACKTRACE AND HAVE__UNWIND_GETIP)
set_property (CACHE WITH_UNWIND APPEND PROPERTY STRINGS unwind)
set (_HAVE_UNWIND 1)
endif (HAVE__UNWIND_BACKTRACE AND HAVE__UNWIND_GETIP)

if (HAVE_UNW_GET_REG AND HAVE_UNW_GETCONTEXT AND HAVE_UNW_INIT_LOCAL AND HAVE_UNW_STEP)
set_property (CACHE WITH_UNWIND APPEND PROPERTY STRINGS libunwind)
set (_HAVE_LIBUNWIND 1)
endif (HAVE_UNW_GET_REG AND HAVE_UNW_GETCONTEXT AND HAVE_UNW_INIT_LOCAL AND HAVE_UNW_STEP)

if (WITH_UNWIND STREQUAL unwind)
if (_HAVE_UNWIND)
set (HAVE_UNWIND 1)
endif (_HAVE_UNWIND)
elseif (WITH_UNWIND STREQUAL libunwind)
if (_HAVE_LIBUNWIND)
set (HAVE_LIBUNWIND 1)
endif (_HAVE_LIBUNWIND)
endif (WITH_UNWIND STREQUAL unwind)

unset (_HAVE_LIBUNWIND)
unset (_HAVE_UNWIND)

cmake_pop_check_state ()
endif (Unwind_FOUND)

check_include_file_cxx (dlfcn.h HAVE_DLFCN_H)
Expand Down
3 changes: 1 addition & 2 deletions bazel/glog.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
"-DHAVE_SYS_UTSNAME_H",
# For src/utilities.cc.
"-DHAVE_SYS_TIME_H",
"-DHAVE__UNWIND_BACKTRACE",
"-DHAVE__UNWIND_GETIP",
"-DHAVE_UNWIND",
# Enable dumping stacktrace upon sigaction.
"-DHAVE_SIGACTION",
# For logging.cc.
Expand Down
11 changes: 4 additions & 7 deletions src/config.h.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
/* define if you have dbghelp library */
#cmakedefine HAVE_DBGHELP

/* define if you have libunwind */
#cmakedefine HAVE_LIB_UNWIND

/* Define to 1 if you have the <memory.h> header file. */
#cmakedefine HAVE_MEMORY_H

Expand Down Expand Up @@ -103,11 +100,11 @@
/* Define to 1 if you have the <unistd.h> header file. */
#cmakedefine HAVE_UNISTD_H ${HAVE_UNISTD_H}

/* Define if you linking to _Unwind_Backtrace is possible. */
#cmakedefine HAVE__UNWIND_BACKTRACE
/* define if you have unwind */
#cmakedefine HAVE_UNWIND

/* Define if you linking to _Unwind_GetIP is possible. */
#cmakedefine HAVE__UNWIND_GETIP
/* define if you have libunwind */
#cmakedefine HAVE_LIBUNWIND

/* define if your compiler has __attribute__ */
#cmakedefine HAVE___ATTRIBUTE__
Expand Down
6 changes: 3 additions & 3 deletions src/utilities.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2008, Google Inc.
// Copyright (c) 2023, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -85,9 +85,9 @@
// correctly when GetStackTrace() is called with max_depth == 0.
// Some code may do that.

#if defined(HAVE_LIB_UNWIND)
#if defined(HAVE_LIBUNWIND)
# define STACKTRACE_H "stacktrace_libunwind-inl.h"
#elif defined(HAVE__UNWIND_BACKTRACE) && defined(HAVE__UNWIND_GETIP)
#elif defined(HAVE_UNWIND)
# define STACKTRACE_H "stacktrace_unwind-inl.h"
#elif !defined(NO_FRAME_POINTER)
# if defined(__i386__) && __GNUC__ >= 2
Expand Down

0 comments on commit 1059aa8

Please sign in to comment.