From c6bc939ea869cb392c466fd468b0284da641864c Mon Sep 17 00:00:00 2001 From: Christian von Elm Date: Thu, 7 Nov 2024 14:43:45 +0100 Subject: [PATCH] Make debuginfod configurable --- CMakeLists.txt | 30 +++++++------- cmake/FindDebuginfod.cmake | 56 +++++++++++++++++++++++++++ include/lo2s/function_resolver.hpp | 4 +- include/lo2s/instruction_resolver.hpp | 3 +- include/lo2s/process_info.hpp | 3 +- 5 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 cmake/FindDebuginfod.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 75254c1c..ee5439a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,9 @@ IfUpdatedUnsetAll(lo2s_USE_STATIC_LIBS Radare_USE_STATIC_LIBS Sensors_USE_STATIC_LIBS Veosinfo_USE_STATIC_LIBS - Audit_USE_STATIC_LIBS) + Audit_USE_STATIC_LIBS + Debuginfod_USE_STATIC_LIBS +) if(lo2s_USE_STATIC_LIBS STREQUAL "OFF") set(Dl_USE_STATIC_LIBS OFF CACHE BOOL "") @@ -56,7 +58,6 @@ if(lo2s_USE_STATIC_LIBS STREQUAL "OFF") else() if(lo2s_USE_STATIC_LIBS STREQUAL "MOSTLY") set(Dl_USE_STATIC_LIBS OFF CACHE BOOL "") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc") elseif(lo2s_USE_STATIC_LIBS STREQUAL "ALL") set(Dl_USE_STATIC_LIBS ON CACHE BOOL "") @@ -67,8 +68,6 @@ else() set(CMAKE_LINK_SEARCH_START_STATIC 1) set(CMAKE_LINK_SEARCH_END_STATIC 1) endif() - - if(lo2s_USE_STATIC_LIBS STREQUAL "MOSTLY") set(Dl_USE_STATIC_LIBS OFF CACHE BOOL "") set(LibElf_USE_STATIC_LIBS ON CACHE BOOL "") set(OTF2_USE_STATIC_LIBS ON CACHE BOOL "") @@ -81,6 +80,8 @@ else() set(Veosinfo_USE_STATIC_LIBS ON CACHE BOOL "") set(Radare_USE_STATIC_LIBS ON CACHE BOOL "") set(Audit_USE_STATIC_LIBS ON CACHE BOOL "") + set(Debuginfod_USE_STATIC_LIBS ON CACHE BOOL "") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc") endif() # Check if we are running Linux @@ -116,6 +117,7 @@ find_package(CUDAToolkit) find_package(Radare) find_package(Audit) find_package(LibElf REQUIRED) +find_package(Debuginfod) # configurable options @@ -135,6 +137,8 @@ CMAKE_DEPENDENT_OPTION(USE_VEOSINFO "Use libveosinfo to sample NEC SX-Aurora Tsu add_feature_info("USE_VEOSINFO" USE_VEOSINFO "Use libveosinfo to sample NEC SX-Aurora Tsubasa cards.") CMAKE_DEPENDENT_OPTION(USE_CUPTI "Use CUPTI to record CUDA activity." ON "CUDAToolkit_FOUND" OFF) add_feature_info("USE_CUPTI" USE_CUPTI "Use CUPTI to record CUDA activity.") +CMAKE_DEPENDENT_OPTION(USE_DEBUGINFOD "Use Debuginfod to download debug information on-demand." ON "Debuginfod_FOUND" OFF) +add_feature_info("USE_DEBUGINFOD" USE_DEBUGINFOD "Use Debuginfod to download debug information on-demand.") # system configuration checks CHECK_INCLUDE_FILES(linux/hw_breakpoint.h HAVE_HW_BREAKPOINT_H) CHECK_STRUCT_HAS_MEMBER("struct perf_event_attr" clockid linux/perf_event.h HAVE_PERF_EVENT_ATTR_CLOCKID) @@ -231,17 +235,8 @@ target_link_libraries(lo2s std::filesystem LibElf::LibElf LibElf::LibDw - debuginfod ) -find_path(DEBUGINFOD_INCLUDE_DIRS elfutils/debuginfod.h - PATHS ENV C_INCLUDE_PATH ENV CPATH - PATH_SUFFIXES include) - -if(DEBUGINFOD_INCLUDE_DIRS) - target_compile_definitions(lo2s PUBLIC HAVE_DEBUGINFOD) -endif() - # old glibc versions require -lrt for clock_gettime() if(NOT CLOCK_GETTIME_FOUND) if(CLOCK_GETTIME_FOUND_WITH_RT) @@ -321,6 +316,15 @@ if (USE_VEOSINFO) endif() endif() +if (USE_DEBUGINFOD) + if (Debuginfod_FOUND) + target_compile_definitions(lo2s PUBLIC HAVE_DEBUGINFOD) + target_link_libraries(lo2s PRIVATE Debuginfod::Debuginfod) + else() + message(SEND_ERROR "Debuginfod not found but requested") + endif() +endif() + if (USE_LIBPFM) if (Libpfm_FOUND) target_compile_definitions(lo2s PUBLIC HAVE_LIBPFM) diff --git a/cmake/FindDebuginfod.cmake b/cmake/FindDebuginfod.cmake new file mode 100644 index 00000000..cb39e068 --- /dev/null +++ b/cmake/FindDebuginfod.cmake @@ -0,0 +1,56 @@ +# Copyright (c) 2022, Technische Universität Dresden, Germany +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of conditions +# and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions +# and the following disclaimer in the documentation and/or other materials provided with the +# distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse +# or promote products derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +include(${CMAKE_CURRENT_LIST_DIR}/UnsetIfUpdated.cmake) + +# Linking libelf isn't a great default because it produces warnings +option(Debuginfod_USE_STATIC_LIBS "Link debuginfod statically." OFF) + +UnsetIfUpdated(Debuginfod_LIBRARY Debuginfod_USE_STATIC_LIBS) + +find_path(Debuginfod_INCLUDE_DIRS libelf.h + PATHS ENV C_INCLUDE_PATH ENV CPATH + PATH_SUFFIXES include) + +if(Debuginfod_USE_STATIC_LIBS) + find_library(Debuginfod_LIBRARY NAMES libdebuginfod.a + HINTS ENV LIBRARY_PATH) +else() + find_library(Debuginfod_LIBRARY NAMES libdebuginfod.so + HINTS ENV LIBRARY_PATH LD_LIBRARY_PATH) +endif() + +include (FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(Debuginfod DEFAULT_MSG + Debuginfod_LIBRARY + Debuginfod_INCLUDE_DIRS) + +if(Debuginfod_FOUND) + add_library(Debuginfod::Debuginfod UNKNOWN IMPORTED) + set_property(TARGET Debuginfod::Debuginfod PROPERTY IMPORTED_LOCATION ${Debuginfod_LIBRARY}) + target_include_directories(Debuginfod::Debuginfod INTERFACE ${Debuginfod_INCLUDE_DIRS}) +endif() + +mark_as_advanced(Debuginfod_LIBRARY Debuginfod_INCLUDE_DIRS) diff --git a/include/lo2s/function_resolver.hpp b/include/lo2s/function_resolver.hpp index bd05256d..a5ee6e51 100644 --- a/include/lo2s/function_resolver.hpp +++ b/include/lo2s/function_resolver.hpp @@ -24,8 +24,8 @@ #include #include -#include #include +#include namespace lo2s { @@ -127,4 +127,4 @@ class Kallsyms : public FunctionResolver std::map kallsyms_; uint64_t start_; }; -} +} // namespace lo2s diff --git a/include/lo2s/instruction_resolver.hpp b/include/lo2s/instruction_resolver.hpp index c1d32971..6633c6b4 100644 --- a/include/lo2s/instruction_resolver.hpp +++ b/include/lo2s/instruction_resolver.hpp @@ -19,7 +19,7 @@ * along with lo2s. If not, see . */ -#pragma once +#pragma once #include #ifdef HAVE_RADARE @@ -28,6 +28,7 @@ #include #include + namespace lo2s { class InstructionResolver diff --git a/include/lo2s/process_info.hpp b/include/lo2s/process_info.hpp index 53d586f0..cccae59a 100644 --- a/include/lo2s/process_info.hpp +++ b/include/lo2s/process_info.hpp @@ -22,9 +22,9 @@ #pragma once #include +#include #include #include -#include #include #include @@ -72,6 +72,7 @@ class ProcessInfo FunctionResolver& fr; InstructionResolver& ir; }; + const Process process_; mutable std::shared_mutex mutex_; std::map map_;