Commit 6eee8a92 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding print_backtrace() util function that uses libunwind


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 90ded0e5
Pipeline #63467 passed with stage
in 10 minutes and 6 seconds
# - Try to find Libunwind
# Input variables:
# LIBUNWIND_ROOT_DIR - The libunwind install directory;
# if not set the LIBUNWIND_DIR environment variable will be used
# LIBUNWIND_INCLUDE_DIR - The libunwind include directory
# LIBUNWIND_LIBRARY - The libunwind library directory
# Output variables:
# LIBUNWIND_FOUND - System has libunwind
# LIBUNWIND_INCLUDE_DIRS - The libunwind include directories
# LIBUNWIND_LIBRARIES - The libraries needed to use libunwind
# LIBUNWIND_VERSION - The version string for libunwind
include(FindPackageHandleStandardArgs)
if(NOT DEFINED LIBUNWIND_FOUND)
# if not set already, set LIBUNWIND_ROOT_DIR from environment
if (DEFINED ENV{LIBUNWIND_DIR} AND NOT DEFINED LIBUNWIND_ROOT_DIR)
set(LIBUNWIND_ROOT_DIR $ENV{LIBUNWIND_DIR})
endif()
# Set default sarch paths for libunwind
if(LIBUNWIND_ROOT_DIR)
set(LIBUNWIND_INCLUDE_DIR ${LIBUNWIND_ROOT_DIR}/include CACHE PATH "The include directory for libunwind")
if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(LIBUNWIND_LIBRARY ${LIBUNWIND_ROOT_DIR}/lib64;${LIBUNWIND_ROOT_DIR}/lib CACHE PATH "The library directory for libunwind")
else()
set(LIBUNWIND_LIBRARY ${LIBUNWIND_ROOT_DIR}/lib CACHE PATH "The library directory for libunwind")
endif()
endif()
find_path(LIBUNWIND_INCLUDE_DIRS NAMES libunwind.h
HINTS ${LIBUNWIND_INCLUDE_DIR})
find_library(LIBUNWIND_LIBRARIES unwind
HINTS ${LIBUNWIND_LIBRARY})
find_library(LIBUNWINDX86_LIBRARIES unwind-x86_64
HINTS ${LIBUNWIND_LIBRARY})
# Get libunwind version
if(EXISTS "${LIBUNWIND_INCLUDE_DIRS}/libunwind-common.h")
file(READ "${LIBUNWIND_INCLUDE_DIRS}/libunwind-common.h" _libunwind_version_header)
string(REGEX REPLACE ".*define[ \t]+UNW_VERSION_MAJOR[ \t]+([0-9]+).*" "\\1"
LIBUNWIND_MAJOR_VERSION "${_libunwind_version_header}")
string(REGEX REPLACE ".*define[ \t]+UNW_VERSION_MINOR[ \t]+([0-9]+).*" "\\1"
LIBUNWIND_MINOR_VERSION "${_libunwind_version_header}")
string(REGEX REPLACE ".*define[ \t]+UNW_VERSION_EXTRA[ \t]+([0-9]+).*" "\\1"
LIBUNWIND_MICRO_VERSION "${_libunwind_version_header}")
set(LIBUNWIND_VERSION "${LIBUNWIND_MAJOR_VERSION}.${LIBUNWIND_MINOR_VERSION}.${LIBUNWIND_MICRO_VERSION}")
unset(_libunwind_version_header)
endif()
# handle the QUIETLY and REQUIRED arguments and set LIBUNWIND_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(Libunwind
FOUND_VAR LIBUNWIND_FOUND
VERSION_VAR LIBUNWIND_VERSION
REQUIRED_VARS LIBUNWIND_LIBRARIES LIBUNWIND_INCLUDE_DIRS)
mark_as_advanced(LIBUNWIND_INCLUDE_DIR LIBUNWIND_LIBRARY LIBUNWINDX86_LIBRARIES
LIBUNWIND_INCLUDE_DIRS LIBUNWIND_LIBRARIES)
endif()
......@@ -14,6 +14,8 @@ configure_file("${CMAKE_SOURCE_DIR}/cmake/xacc_config.hpp.in"
"${CMAKE_BINARY_DIR}/xacc_config.hpp")
install(FILES "${CMAKE_BINARY_DIR}/xacc_config.hpp" DESTINATION include/xacc)
find_package(Libunwind)
file(GLOB HEADERS
XACC.hpp
ir/*.hpp
......@@ -37,12 +39,15 @@ add_library(xacc
add_dependencies(xacc cpr)
if (LIBUNWIND_FOUND)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAS_LIBUNWIND")
target_include_directories(xacc
PRIVATE ${CMAKE_BINARY_DIR}
${CPR_INCLUDE_DIRS}
${CMAKE_SOURCE_DIR}/tpls/rapidjson/include
${CMAKE_SOURCE_DIR}/tpls/spdlog
${CMAKE_SOURCE_DIR}/tpls/cxxopts
${LIBUNWIND_INCLUDE_DIRS}
PUBLIC .
ir
compiler
......@@ -55,6 +60,31 @@ target_include_directories(xacc
optimizer
${CMAKE_SOURCE_DIR}/tpls/mpark-variant)
message(STATUS "LIBS: ${LIBUNWIND_LIBRARIES} ${LIBUNWINDX86_LIBRARIES}")
target_link_libraries(xacc PRIVATE CppMicroServices cpr ${LIBUNWIND_LIBRARIES} ${LIBUNWINDX86_LIBRARIES})
else()
target_include_directories(xacc
PRIVATE ${CMAKE_BINARY_DIR}
${CPR_INCLUDE_DIRS}
${CMAKE_SOURCE_DIR}/tpls/rapidjson/include
${CMAKE_SOURCE_DIR}/tpls/spdlog
${CMAKE_SOURCE_DIR}/tpls/cxxopts
PUBLIC .
ir
compiler
program
accelerator
accelerator/remote
utils
service
algorithm
optimizer
${CMAKE_SOURCE_DIR}/tpls/mpark-variant)
target_link_libraries(xacc PRIVATE CppMicroServices cpr)
endif()
if(APPLE)
set_target_properties(xacc PROPERTIES INSTALL_RPATH "@loader_path")
set_target_properties(xacc PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
......@@ -63,7 +93,6 @@ else()
set_target_properties(xacc PROPERTIES LINK_FLAGS "-shared")
endif()
target_link_libraries(xacc PRIVATE CppMicroServices cpr)
# Add the tests
if(XACC_BUILD_TESTS)
......
......@@ -115,6 +115,7 @@ void debug(const std::string &msg, MessagePredicate predicate) {
}
void error(const std::string &msg, MessagePredicate predicate) {
print_backtrace();
if (isPyApi) {
throw std::runtime_error(msg);
} else {
......
......@@ -62,12 +62,13 @@ public:
return mpark::get<T>(*this);
} catch (std::exception &e) {
std::stringstream s;
s << toString() << "\n";
s << "InstructionParameter.toString() = " << toString() << "\n";
s << "This InstructionParameter type id is " << this->which() << "\nAllowed Ids to Type\n";
for (auto& kv : whichType) {
s << kv.first << ": " << kv.second << "\n";
}
XACCLogger::instance()->error("Cannot cast Variant:\n" + s.str());
print_backtrace();
exit(0);
}
return T();
......
......@@ -19,6 +19,11 @@
#include <istream>
#include <dirent.h>
#ifdef HAS_LIBUNWIND
#include <libunwind.h>
#include <cxxabi.h>
#endif
namespace xacc {
static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
......@@ -56,6 +61,41 @@ bool directoryExists(const std::string path) {
return bExists;
}
void print_backtrace() {
#ifdef HAS_LIBUNWIND
unw_cursor_t cursor;
unw_context_t context;
// Initialize cursor to current frame for local unwinding.
unw_getcontext(&context);
unw_init_local(&cursor, &context);
// Unwind frames one by one, going up the frame stack.
while (unw_step(&cursor) > 0) {
unw_word_t offset, pc;
unw_get_reg(&cursor, UNW_REG_IP, &pc);
if (pc == 0) {
break;
}
std::printf("0x%lx:", pc);
char sym[256];
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
char* nameptr = sym;
int status;
char* demangled = abi::__cxa_demangle(sym, nullptr, nullptr, &status);
if (status == 0) {
nameptr = demangled;
}
std::printf(" (%s+0x%lx)\n", nameptr, offset);
std::free(demangled);
} else {
std::printf(" -- error: unable to obtain symbol name for this frame\n");
}
}
#endif
}
std::string base64_decode(std::string const &encoded_string) {
int in_len = encoded_string.size();
int i = 0;
......
......@@ -31,6 +31,7 @@ void trim(std::string &s);
bool fileExists(const std::string &name);
bool directoryExists(const std::string path);
void print_backtrace();
template <class Op> void split(const std::string &s, char delim, Op op) {
std::stringstream ss(s);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment