From 2d93fc1092bfc7df858c211546ac5f8cd40eab6f Mon Sep 17 00:00:00 2001 From: Atsushi Watanabe Date: Sat, 10 Jun 2023 04:20:47 +0900 Subject: [PATCH] rtloader: Use execinfo only on glibc (#15256) Use execinfo only on glibc. Functions in execinfo.h are GNU extensions and not available on other C libraries like musl. We used to use libexecinfo package (A quick-n-dirty BSD licensed clone of the GNU libc backtrace facility.) of Alpine Linux to build datadog-agent on Alpine, but it has been removed since Alpine 3.17. This PR allow to build datadog-agent on Alpine Linux and other non-glibc environments. --- ...use-execinfo-only-on-glibc-5b734df32fc1555a.yaml | 13 +++++++++++++ rtloader/rtloader/CMakeLists.txt | 2 ++ rtloader/rtloader/api.cpp | 6 ++++++ 3 files changed, 21 insertions(+) create mode 100644 releasenotes/notes/rtloader-use-execinfo-only-on-glibc-5b734df32fc1555a.yaml diff --git a/releasenotes/notes/rtloader-use-execinfo-only-on-glibc-5b734df32fc1555a.yaml b/releasenotes/notes/rtloader-use-execinfo-only-on-glibc-5b734df32fc1555a.yaml new file mode 100644 index 0000000000000..a7337ddc044d9 --- /dev/null +++ b/releasenotes/notes/rtloader-use-execinfo-only-on-glibc-5b734df32fc1555a.yaml @@ -0,0 +1,13 @@ +# Each section from every release note are combined when the +# CHANGELOG.rst is rendered. So the text needs to be worded so that +# it does not depend on any information only available in another +# section. This may mean repeating some details, but each section +# must be readable independently of the other. +# +# Each section note must be formatted as reStructuredText. +--- +fixes: + - | + rtloader: Use `execinfo` only if provided to fix builds on + C libraries like `musl`. + diff --git a/rtloader/rtloader/CMakeLists.txt b/rtloader/rtloader/CMakeLists.txt index 8cb06c7e799d1..a830f18c995c3 100644 --- a/rtloader/rtloader/CMakeLists.txt +++ b/rtloader/rtloader/CMakeLists.txt @@ -32,6 +32,7 @@ add_library(datadog-agent-rtloader SHARED find_library(EXECINFO execinfo) if(EXECINFO) target_link_libraries(datadog-agent-rtloader execinfo) + add_compile_definitions(HAS_BACKTRACE_LIB) endif() endif() @@ -39,6 +40,7 @@ endif() if(${CMAKE_SYSTEM_NAME} STREQUAL FreeBSD) find_package(Backtrace REQUIRED) target_link_libraries(datadog-agent-rtloader ${Backtrace_LIBRARIES}) + add_compile_definitions(HAS_BACKTRACE_LIB) endif() set_target_properties(datadog-agent-rtloader PROPERTIES diff --git a/rtloader/rtloader/api.cpp b/rtloader/rtloader/api.cpp index af5fe74b5972b..f150be80d06fd 100644 --- a/rtloader/rtloader/api.cpp +++ b/rtloader/rtloader/api.cpp @@ -12,7 +12,9 @@ #ifndef _WIN32 // clang-format off // handler stuff +#ifdef HAS_BACKTRACE_LIB #include +#endif #include #include #include @@ -371,11 +373,14 @@ static inline void core(int sig) # define STACKTRACE_SIZE 500 void signalHandler(int sig, siginfo_t *, void *) { +# ifdef HAS_BACKTRACE_LIB void *buffer[STACKTRACE_SIZE]; char **symbols; size_t nptrs = backtrace(buffer, STACKTRACE_SIZE); +# endif std::cerr << "HANDLER CAUGHT signal Error: signal " << sig << std::endl; +# ifdef HAS_BACKTRACE_LIB symbols = backtrace_symbols(buffer, nptrs); if (symbols == NULL) { std::cerr << "Error getting backtrace symbols" << std::endl; @@ -387,6 +392,7 @@ void signalHandler(int sig, siginfo_t *, void *) _free(symbols); } +# endif // dump core if so configured __sync_synchronize();