From 32a01efbbe446cb703ea4fcf296b59ed29e948d2 Mon Sep 17 00:00:00 2001 From: Hardy <100662313+wanghailu0717@users.noreply.github.com> Date: Thu, 1 Sep 2022 20:30:12 +0800 Subject: [PATCH] add code for backtrace (#21) * add code for backtrace * Add: infini::Exception ``` Test project /home/zly/InfiniTensor_aux/build Start 1: test_graph 1/4 Test #1: test_graph ....................... Passed 0.05 sec Start 2: test_hash 2/4 Test #2: test_hash ........................ Passed 0.02 sec Start 3: test_conv 3/4 Test #3: test_conv ........................ Passed 4.40 sec Start 4: test_pooling 4/4 Test #4: test_pooling ..................... Passed 2.47 sec 100% tests passed, 0 tests failed out of 4 Total Test time (real) = 6.94 sec ``` * Fix: USE_BACKTRACE in cmake Co-authored-by: wanghailu Co-authored-by: Liyan Zheng --- .gitmodules | 3 +++ 3rd-party/backward-cpp | 1 + CMakeLists.txt | 13 +++++++++++++ include/core/common.h | 3 ++- include/utils/exception.h | 12 ++++++++++++ src/core/common.cc | 2 +- src/utils/exception.cc | 26 ++++++++++++++++++++++++++ test/core/test_graph.cc | 2 +- 8 files changed, 59 insertions(+), 3 deletions(-) create mode 160000 3rd-party/backward-cpp create mode 100644 include/utils/exception.h create mode 100644 src/utils/exception.cc diff --git a/.gitmodules b/.gitmodules index 2f905b6a..a40171ca 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "3rd-party/googletest"] path = 3rd-party/googletest url = git@github.com:google/googletest.git +[submodule "3rd-party/backward-cpp"] + path = 3rd-party/backward-cpp + url = git@github.com:bombela/backward-cpp.git diff --git a/3rd-party/backward-cpp b/3rd-party/backward-cpp new file mode 160000 index 00000000..f30744bc --- /dev/null +++ b/3rd-party/backward-cpp @@ -0,0 +1 @@ +Subproject commit f30744bcf726ea3735df7ecf9e9de9ddac540283 diff --git a/CMakeLists.txt b/CMakeLists.txt index 251ad0b0..157c38b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,9 @@ project(InfiniTensor C CXX) # Do not change these options in this file. Use cmake.config, cmake -DOPTION=VALUE, or ccmake to specify them. option(USE_CUDA "Support CUDA GPU" ON) +option(USE_BACKTRACE "Print backtrace on exception and segmentation fault" ON) option(BUILD_TEST "Build tests" ON) + cmake_dependent_option(BUILD_TEST_CORE "Build tests for core components" ON BUILD_TEST OFF) cmake_dependent_option(BUILD_TEST_PET "Build tests for PET" OFF BUILD_TEST OFF) cmake_dependent_option(BUILD_TEST_EINNET "Build tests for EINNET" OFF BUILD_TEST OFF) @@ -56,6 +58,14 @@ file(GLOB_RECURSE SRC src/*.cc src/*.cu) add_library(InfiniTensor SHARED ${SRC}) +if(USE_BACKTRACE) + add_definitions(-D BACKWARD_TRACE) + add_subdirectory(3rd-party/backward-cpp) + include_directories(3rd-party/backward-cpp) + add_backward(InfiniTensor) + target_link_libraries(InfiniTensor dw) +endif() + if(USE_CUDA) # set(CUDA_HOST_COMPILER /home/spack/spack/opt/spack/linux-ubuntu22.04-broadwell/gcc-9.4.0/gcc-9.4.0-st36klijpsnquihiy463hmedsyhoc3g6/bin/gcc) enable_language(CUDA) @@ -84,6 +94,9 @@ endfunction() if(BUILD_TEST) enable_testing() + if(USE_TRACE) + build_test(test/trace/*.cc) + endif() if(BUILD_TEST_CORE) build_test(test/core/*.cc) build_test(test/operators/*.cc) diff --git a/include/core/common.h b/include/core/common.h index c9b8fe81..600b44ef 100644 --- a/include/core/common.h +++ b/include/core/common.h @@ -1,4 +1,5 @@ #pragma once +#include "utils/exception.h" #include #include #include @@ -41,7 +42,7 @@ using HashType = uint64_t; // compatible with std::hash #define _IT_ASSERT_2(name, info) \ (static_cast(name) \ ? void(0) \ - : throw std::runtime_error( \ + : throw infini::Exception( \ std::string("[") + __FILE__ + ":" + std::to_string(__LINE__) + \ "] Assertion failed (" + #name + "): " + #info)) #define _IT_ASSERT_1(name) _IT_ASSERT_2(name, ""); diff --git a/include/utils/exception.h b/include/utils/exception.h new file mode 100644 index 00000000..f2a7dc94 --- /dev/null +++ b/include/utils/exception.h @@ -0,0 +1,12 @@ +#pragma once +#include +#include + +namespace infini { + +class Exception : public std::runtime_error { + public: + Exception(const std::string &msg); +}; + +} // namespace infini diff --git a/src/core/common.cc b/src/core/common.cc index 71f39244..94e09a10 100644 --- a/src/core/common.cc +++ b/src/core/common.cc @@ -21,4 +21,4 @@ double timeit(const std::function &func, timingRounds; } -} // namespace infini \ No newline at end of file +} // namespace infini diff --git a/src/utils/exception.cc b/src/utils/exception.cc new file mode 100644 index 00000000..304571b0 --- /dev/null +++ b/src/utils/exception.cc @@ -0,0 +1,26 @@ +#include "utils/exception.h" + +#ifdef BACKWARD_TRACE +#include "backward.hpp" + +namespace backtrace = backward; + +// signal handler +backtrace::SignalHandling sh; + +namespace infini { +Exception::Exception(const std::string &msg) : std::runtime_error(msg) { + backtrace::StackTrace st; + st.load_here(32); + backtrace::Printer p; + p.print(st); +} +}; // namespace infini + +#else + +namespace infini { +Exception::Exception(const std::string &msg) : std::runtime_error(msg) {} +} // namespace infini + +#endif diff --git a/test/core/test_graph.cc b/test/core/test_graph.cc index a91d8096..96240fe6 100644 --- a/test/core/test_graph.cc +++ b/test/core/test_graph.cc @@ -46,4 +46,4 @@ TEST(Graph, perf_engine) { EXPECT_TRUE(matmul->getOutput()->equalData(ans)); } -} // namespace infini \ No newline at end of file +} // namespace infini