Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add libunwind validation backtrace #388

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ option(UR_USE_UBSAN "enable UndefinedBehaviorSanitizer" OFF)
option(UR_USE_MSAN "enable MemorySanitizer" OFF)
option(UMA_BUILD_SHARED_LIBRARY "Build UMA as shared library" OFF)
option(UR_ENABLE_TRACING "enable api tracing through xpti" OFF)
option(VAL_USE_LIBUNWIND_BACKTRACE "enable libunwind validation backtrace" OFF)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
Expand Down
25 changes: 25 additions & 0 deletions cmake/FindLibunwind.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: MIT

#
# FindLibunwind.cmake -- module searching for libunwind library.
# LIBUNWIND_FOUND is set to true if libunwind is found.
#

find_library(LIBUNWIND_LIBRARIES NAMES unwind)
find_path(LIBUNWIND_INCLUDE_DIR NAMES libunwind.h)

if (LIBUNWIND_LIBRARIES AND LIBUNWIND_INCLUDE_DIR)
set(LIBUNWIND_FOUND TRUE)
endif()

if (LIBUNWIND_FOUND)
add_library(Libunwind INTERFACE IMPORTED)
set_target_properties(Libunwind PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${LIBUNWIND_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${LIBUNWIND_LIBRARIES}"
)
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Libunwind DEFAULT_MSG LIBUNWIND_LIBRARIES LIBUNWIND_INCLUDE_DIR)
27 changes: 24 additions & 3 deletions source/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ if (UNIX)
endif()

if(WIN32)
target_link_libraries(loader PRIVATE cfgmgr32.lib dbghelp)
target_link_libraries(loader PRIVATE cfgmgr32.lib)
endif()

if(UNIX)
Expand Down Expand Up @@ -90,18 +90,39 @@ if(UR_ENABLE_TRACING)
)
endif()


# link validation backtrace dependencies
find_package(Libunwind)
if (VAL_USE_LIBUNWIND_BACKTRACE AND LIBUNWIND_FOUND)
message(STATUS "Using libunwind backtrace for validation")

target_sources(loader PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/layers/validation/backtrace_unwind.cpp)
target_link_libraries(loader PRIVATE Libunwind)
else()
message(STATUS "Using default backtrace for validation")

if(WIN32)
target_sources(loader PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/layers/validation/backtrace_win.cpp)
target_link_libraries(loader PRIVATE dbghelp)
else()
target_sources(loader PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/layers/validation/backtrace_lin.cpp)
endif()
endif()

if(WIN32)
target_sources(loader
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/windows/lib_init.cpp
${CMAKE_CURRENT_SOURCE_DIR}/windows/loader_init.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/validation/backtrace_win.cpp

)
else()
target_sources(loader
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/linux/lib_init.cpp
${CMAKE_CURRENT_SOURCE_DIR}/linux/loader_init.cpp
${CMAKE_CURRENT_SOURCE_DIR}/layers/validation/backtrace_lin.cpp
)
endif()
59 changes: 59 additions & 0 deletions source/loader/layers/validation/backtrace_unwind.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
*
* Copyright (C) 2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "backtrace.hpp"

#include <cxxabi.h>
#include <execinfo.h>
#include <vector>

#define UNW_LOCAL_ONLY
#include <libunwind.h>

namespace validation_layer {

std::vector<BacktraceLine> getCurrentBacktrace() {
unw_cursor_t cursor;
unw_context_t context;
unw_getcontext(&context);
unw_init_local(&cursor, &context);

if (unw_step(&cursor) <= 0) {
return std::vector<BacktraceLine>(1, "Failed to acquire a backtrace");
}

std::vector<BacktraceLine> backtrace;
try {
char proc_name[256];
unw_word_t ip, offset;
std::stringstream backtraceLine;

do {
if (unw_get_proc_name(&cursor, proc_name, sizeof(proc_name),
&offset) != 0) {
backtrace.push_back("????????");
} else if (unw_get_reg(&cursor, UNW_REG_IP, &ip) != 0) {
backtraceLine.str("");
backtraceLine << "(" << proc_name << "+"
<< std::to_string(offset) << ") [????????]";
backtrace.push_back(backtraceLine.str());
} else {
backtraceLine.str("");
backtraceLine << "(" << proc_name << "+"
<< std::to_string(offset) << ") [0x" << std::hex
<< ip << "]";
backtrace.push_back(backtraceLine.str());
}
} while (unw_step(&cursor) > 0);
} catch (std::bad_alloc &) {
return std::vector<BacktraceLine>(1, "Failed to acquire a backtrace");
}

return backtrace;
}

} // namespace validation_layer