From 43792b8a13289f9cee247cf6ff1577a55c31888b Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Fri, 29 Dec 2023 23:18:08 +0100 Subject: [PATCH] rework (lib)unwind integration Allow to switch between the two backends. --- CMakeLists.txt | 41 ++++++++++++++++++++++++++++++++++++----- bazel/glog.bzl | 3 +-- src/config.h.cmake.in | 11 ++++------- src/utilities.h | 6 +++--- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e76fb1486..f7f08fe0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,7 +40,9 @@ 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 libunwind CACHE STRING "unwind driver") +set_property (CACHE WITH_UNWIND PROPERTY STRINGS none unwind libunwind) 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) @@ -48,9 +50,9 @@ cmake_dependent_option (WITH_TLS "Enable Thread Local Storage (TLS) support" ON 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) @@ -91,13 +93,42 @@ 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 (_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 (_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) diff --git a/bazel/glog.bzl b/bazel/glog.bzl index 8234d0f50..b92473b36 100644 --- a/bazel/glog.bzl +++ b/bazel/glog.bzl @@ -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. diff --git a/src/config.h.cmake.in b/src/config.h.cmake.in index 70440971d..752d76648 100644 --- a/src/config.h.cmake.in +++ b/src/config.h.cmake.in @@ -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 header file. */ #cmakedefine HAVE_MEMORY_H @@ -103,11 +100,11 @@ /* Define to 1 if you have the 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__ diff --git a/src/utilities.h b/src/utilities.h index ae34d8d4c..e06a308e8 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -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 @@ -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