Skip to content

Commit

Permalink
[RUNTIME] Add libbacktrace for backtraces with line numbers (apache#7153
Browse files Browse the repository at this point in the history
)

* [RUNTIME] Add libbacktrace for backtraces with line numbers

Co-authored-by: Robert Kimball <[email protected]>
  • Loading branch information
2 people authored and Trevor Morris committed May 6, 2021
1 parent 3d0cca8 commit 14e8449
Show file tree
Hide file tree
Showing 135 changed files with 1,055 additions and 419 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "3rdparty/vta-hw"]
path = 3rdparty/vta-hw
url = https://github.com/apache/incubator-tvm-vta
[submodule "3rdparty/libbacktrace"]
path = 3rdparty/libbacktrace
url = https://github.com/tlc-pack/libbacktrace.git
1 change: 1 addition & 0 deletions 3rdparty/libbacktrace
Submodule libbacktrace added at 08f7c7
62 changes: 60 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ tvm_option(USE_TF_TVMDSOOP "Build with TensorFlow TVMDSOOp" OFF)
tvm_option(USE_FALLBACK_STL_MAP "Use TVM's POD compatible Map" OFF)
tvm_option(USE_ETHOSN "Build with Arm Ethos-N" OFF)
tvm_option(INDEX_DEFAULT_I64 "Defaults the index datatype to int64" ON)
set(_LIBBACKTRACE_DEFAULT OFF)
if(CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "Linux")
set(_LIBBACKTRACE_DEFAULT ON)
endif()
tvm_option(USE_LIBBACKTRACE "Build libbacktrace to supply linenumbers on stack traces" ${_LIBBACKTRACE_DEFAULT})

# 3rdparty libraries
tvm_option(DLPACK_PATH "Path to DLPACK" "3rdparty/dlpack/include")
Expand Down Expand Up @@ -140,6 +145,8 @@ if(MSVC)
add_compile_options(/wd4146)
# 'inline': used more than once
add_compile_options(/wd4141)
# unknown pragma
add_compile_options(/wd4068)
else(MSVC)

if(USE_TF_COMPILE_FLAGS)
Expand Down Expand Up @@ -407,6 +414,26 @@ set_property(TARGET tvm APPEND PROPERTY LINK_OPTIONS "${TVM_VISIBILITY_FLAG}")
add_library(tvm_runtime SHARED $<TARGET_OBJECTS:tvm_runtime_objs>)
set_property(TARGET tvm_runtime APPEND PROPERTY LINK_OPTIONS "${TVM_VISIBILITY_FLAG}")

target_compile_definitions(tvm_objs PUBLIC DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
target_compile_definitions(tvm_runtime_objs PUBLIC DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
target_compile_definitions(tvm PUBLIC DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
target_compile_definitions(tvm_runtime PUBLIC DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
if(USE_LIBBACKTRACE)
message(STATUS "Building with libbacktrace...")
include(cmake/modules/Libbacktrace.cmake)
target_link_libraries(tvm PRIVATE libbacktrace)
target_link_libraries(tvm_runtime PRIVATE libbacktrace)
add_dependencies(tvm_runtime_objs libbacktrace)
# pre 3.12 versions of cmake cannot propagate include directories from imported targets so we set them manually
target_include_directories(tvm PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include")
target_include_directories(tvm_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include")
target_include_directories(tvm_runtime PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include")
target_include_directories(tvm_runtime_objs PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/libbacktrace/include")
else()
target_compile_definitions(tvm_objs PRIVATE TVM_BACKTRACE_DISABLED)
target_compile_definitions(tvm_runtime_objs PRIVATE TVM_BACKTRACE_DISABLED)
endif()

if(USE_MICRO)
# NOTE: cmake doesn't track dependencies at the file level across subdirectories. For the
# Unix Makefiles generator, need to add these explicit target-level dependency)
Expand All @@ -421,9 +448,9 @@ endif()
if(USE_RELAY_DEBUG)
message(STATUS "Building Relay in debug mode...")
target_compile_definitions(tvm_objs PRIVATE "USE_RELAY_DEBUG")
target_compile_definitions(tvm_objs PRIVATE "DMLC_LOG_DEBUG")
target_compile_definitions(tvm_objs PRIVATE "TVM_LOG_DEBUG")
target_compile_definitions(tvm_runtime_objs PRIVATE "USE_RELAY_DEBUG")
target_compile_definitions(tvm_runtime_objs PRIVATE "DMLC_LOG_DEBUG")
target_compile_definitions(tvm_runtime_objs PRIVATE "TVM_LOG_DEBUG")
else()
target_compile_definitions(tvm_objs PRIVATE "NDEBUG")
target_compile_definitions(tvm_runtime_objs PRIVATE "NDEBUG")
Expand Down Expand Up @@ -494,6 +521,7 @@ if (HIDE_PRIVATE_SYMBOLS AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# once minimum CMake version is bumped up to 3.13 or above.
target_link_libraries(tvm PRIVATE ${HIDE_SYMBOLS_LINKER_FLAGS})
target_link_libraries(tvm_runtime PRIVATE ${HIDE_SYMBOLS_LINKER_FLAGS})
target_compile_definitions(tvm_allvisible PUBLIC DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
endif()

# Tests
Expand Down Expand Up @@ -563,3 +591,33 @@ if(MSVC)
target_compile_definitions(tvm_runtime_objs PRIVATE -DTVM_EXPORTS)
target_compile_definitions(tvm_runtime_static PRIVATE -DTVM_EXPORTS)
endif()

set(TVM_IS_DEBUG_BUILD OFF)
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR CMAKE_CXX_FLAGS MATCHES "-g")
set(TVM_IS_DEBUG_BUILD ON)
endif()

# Change relative paths in backtrace to absolute ones
if(TVM_IS_DEBUG_BUILD)
set(FILE_PREFIX_MAP_FLAG "-ffile-prefix-map=..=${CMAKE_CURRENT_SOURCE_DIR}")
target_compile_options(tvm PRIVATE "${FILE_PREFIX_MAP_FLAG}")
CHECK_CXX_COMPILER_FLAG("${FILE_PREFIX_MAP_FLAG}" FILE_PREFIX_MAP_SUPPORTED)
if(FILE_PREFIX_MAP_SUPPORTED)
target_compile_options(tvm PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${FILE_PREFIX_MAP_FLAG}>)
target_compile_options(tvm_objs PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${FILE_PREFIX_MAP_FLAG}>)
target_compile_options(tvm_runtime PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${FILE_PREFIX_MAP_FLAG}>)
target_compile_options(tvm_runtime_objs PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${FILE_PREFIX_MAP_FLAG}>)
endif()
endif()

# Run dsymutil to generate debugging symbols for backtraces
if(APPLE AND TVM_IS_DEBUG_BUILD)
find_program(DSYMUTIL dsymutil)
mark_as_advanced(DSYMUTIL)
add_custom_command(TARGET tvm
POST_BUILD
COMMAND ${DSYMUTIL} ARGS $<TARGET_FILE:tvm>
COMMENT "Running dsymutil"
VERBATIM
)
endif()
4 changes: 2 additions & 2 deletions apps/android_camera/app/src/main/jni/Application.mk
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ include $(config)
APP_ABI ?= all
APP_STL := c++_shared

APP_CPPFLAGS += -DDMLC_LOG_STACK_TRACE=0 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti
APP_CPPFLAGS += -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti
ifeq ($(USE_OPENCL), 1)
APP_CPPFLAGS += -DTVM_OPENCL_RUNTIME=1
endif
Expand All @@ -43,4 +43,4 @@ endif

ifeq ($(USE_SORT), 1)
APP_CPPFLAGS += -DUSE_SORT=1
endif
endif
38 changes: 23 additions & 15 deletions apps/android_camera/app/src/main/jni/tvm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@

#include <fstream>

/* Enable custom logging - this will cause TVM to pass every log message
* through CustomLogMessage instead of LogMessage. By enabling this, we must
* implement dmlc::CustomLogMessage::Log. We use this to pass TVM log
* messages to Android logcat.
#define DMLC_USE_LOGGING_LIBRARY <tvm/runtime/logging.h>
#define TVM_BACKTRACE_DISABLED 1
/* Enable custom logging - this will cause TVM to use a custom implementation
* of tvm::runtime::detail::LogMessage. We use this to pass TVM log messages to
* Android logcat.
*/
#define DMLC_LOG_CUSTOMIZE 1

/* Ensure that fatal errors are passed to the logger before throwing
* in LogMessageFatal
*/
#define DMLC_LOG_BEFORE_THROW 1
#define TVM_LOG_CUSTOMIZE 1

#include "../src/runtime/c_runtime_api.cc"
#include "../src/runtime/cpu_device_api.cc"
Expand Down Expand Up @@ -72,8 +68,20 @@

#include <android/log.h>

void dmlc::CustomLogMessage::Log(const std::string& msg) {
// This is called for every message logged by TVM.
// We pass the message to logcat.
__android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", msg.c_str());
}
namespace tvm {
namespace runtime {
namespace detail {
// Override logging mechanism
void LogFatalImpl(const std::string& file, int lineno, const std::string& message) {
std::string m = file + ":" + std::to_string(lineno) + ": " + message;
__android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", m.c_str());
throw InternalError(file, lineno, message);
}
void LogMessageImpl(const std::string& file, int lineno, const std::string& message) {
std::string m = file + ":" + std::to_string(lineno) + ": " + message;
__android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", m.c_str());
}

} // namespace detail
} // namespace runtime
} // namespace tvm
2 changes: 1 addition & 1 deletion apps/android_deploy/app/src/main/jni/Application.mk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ include $(config)

APP_STL := c++_static

APP_CPPFLAGS += -DDMLC_LOG_STACK_TRACE=0 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti
APP_CPPFLAGS += -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti
ifeq ($(USE_OPENCL), 1)
APP_CPPFLAGS += -DTVM_OPENCL_RUNTIME=1
endif
3 changes: 3 additions & 0 deletions apps/android_deploy/app/src/main/jni/tvm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

#include <fstream>

#define DMLC_USE_LOGGING_LIBRARY <tvm/runtime/logging.h>
#define TVM_BACKTRACE_DISABLED 1

#include "../src/runtime/c_runtime_api.cc"
#include "../src/runtime/cpu_device_api.cc"
#include "../src/runtime/dso_library.cc"
Expand Down
2 changes: 1 addition & 1 deletion apps/android_rpc/app/src/main/jni/Application.mk
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ include $(config)
APP_ABI ?= armeabi-v7a arm64-v8a x86 x86_64 mips
APP_STL := c++_shared

APP_CPPFLAGS += -DDMLC_LOG_STACK_TRACE=0 -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti
APP_CPPFLAGS += -DTVM4J_ANDROID=1 -std=c++14 -Oz -frtti
ifeq ($(USE_OPENCL), 1)
APP_CPPFLAGS += -DTVM_OPENCL_RUNTIME=1
endif
Expand Down
36 changes: 22 additions & 14 deletions apps/android_rpc/app/src/main/jni/tvm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@

#include <fstream>

/* Enable custom logging - this will cause TVM to pass every log message
* through CustomLogMessage instead of LogMessage. By enabling this, we must
* implement dmlc::CustomLogMessage::Log. We use this to pass TVM log
* messages to Android logcat.
#define DMLC_USE_LOGGING_LIBRARY <tvm/runtime/logging.h>
#define TVM_BACKTRACE_DISABLED 1
/* Enable custom logging - this will cause TVM to use a custom implementation
* of tvm::runtime::detail::LogMessage. We use this to pass TVM log messages to
* Android logcat.
*/
#define DMLC_LOG_CUSTOMIZE 1

/* Ensure that fatal errors are passed to the logger before throwing
* in LogMessageFatal
*/
#define DMLC_LOG_BEFORE_THROW 1
#define TVM_LOG_CUSTOMIZE 1

#include "../src/runtime/c_runtime_api.cc"
#include "../src/runtime/cpu_device_api.cc"
Expand Down Expand Up @@ -81,8 +77,20 @@

#include <android/log.h>

void dmlc::CustomLogMessage::Log(const std::string& msg) {
// This is called for every message logged by TVM.
// We pass the message to logcat.
__android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", msg.c_str());
namespace tvm {
namespace runtime {
namespace detail {
// Override logging mechanism
void LogFatalImpl(const std::string& file, int lineno, const std::string& message) {
std::string m = file + ":" + std::to_string(lineno) + ": " + message;
__android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", m.c_str());
throw InternalError(file, lineno, message);
}
void LogMessageImpl(const std::string& file, int lineno, const std::string& message) {
std::string m = file + ":" + std::to_string(lineno) + ": " + message;
__android_log_write(ANDROID_LOG_DEBUG, "TVM_RUNTIME", m.c_str());
}

} // namespace detail
} // namespace runtime
} // namespace tvm
6 changes: 4 additions & 2 deletions apps/bundle_deploy/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ PKG_CXXFLAGS = ${PKG_COMPILE_OPTS} -std=c++14 \
-I${TVM_ROOT}/include \
-I${DMLC_CORE}/include \
-I${TVM_ROOT}/3rdparty/dlpack/include \
-Icrt_config
-Icrt_config \
-DDMLC_USE_LOGGING_LIBRARY=\<tvm/runtime/logging.h\>
PKG_CFLAGS = ${PKG_COMPILE_OPTS} \
-I${TVM_ROOT}/include \
-I${DMLC_CORE}/include \
-I${TVM_ROOT}/3rdparty/dlpack/include \
-Icrt_config
-Icrt_config \
-DDMLC_USE_LOGGING_LIBRARY=\<tvm/runtime/logging.h\>

PKG_LDFLAGS = -pthread -lm

Expand Down
3 changes: 2 additions & 1 deletion apps/dso_plugin_module/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ TVM_ROOT=$(shell cd ../..; pwd)
PKG_CFLAGS = -std=c++14 -O2 -fPIC\
-I${TVM_ROOT}/include\
-I${TVM_ROOT}/3rdparty/dmlc-core/include\
-I${TVM_ROOT}/3rdparty/dlpack/include
-I${TVM_ROOT}/3rdparty/dlpack/include\
-DDMLC_USE_LOGGING_LIBRARY=\<tvm/runtime/logging.h\>

PKG_LDFLAGS =-L${TVM_ROOT}/build
UNAME_S := $(shell uname -s)
Expand Down
3 changes: 2 additions & 1 deletion apps/extension/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ TVM_ROOT=$(shell cd ../..; pwd)
PKG_CFLAGS = -std=c++14 -O2 -fPIC\
-I${TVM_ROOT}/include\
-I${TVM_ROOT}/3rdparty/dmlc-core/include\
-I${TVM_ROOT}/3rdparty/dlpack/include
-I${TVM_ROOT}/3rdparty/dlpack/include\
-DDMLC_USE_LOGGING_LIBRARY=\<tvm/runtime/logging.h\>


PKG_LDFLAGS =-L${TVM_ROOT}/build
Expand Down
6 changes: 6 additions & 0 deletions apps/ios_rpc/tvmrpc.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
"DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>",
"TVM_BACKTRACE_DISABLED=1",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
Expand Down Expand Up @@ -393,6 +395,10 @@
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_PREPROCESSOR_DEFINITIONS = (
"DMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>",
"TVM_BACKTRACE_DISABLED=1",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
Expand Down
2 changes: 1 addition & 1 deletion apps/ios_rpc/tvmrpc/TVMRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
#import <Foundation/Foundation.h>
// Customize logging mechanism, redirect to NSLOG
#define DMLC_LOG_CUSTOMIZE 1
#define TVM_LOG_CUSTOMIZE 1
#define TVM_METAL_RUNTIME 1

#include <tvm/runtime/packed_func.h>
Expand Down
16 changes: 13 additions & 3 deletions apps/ios_rpc/tvmrpc/TVMRuntime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@
// CoreML
#include "../../../src/runtime/contrib/coreml/coreml_runtime.mm"

namespace dmlc {
namespace tvm {
namespace runtime {
namespace detail {
// Override logging mechanism
void CustomLogMessage::Log(const std::string& msg) { NSLog(@"%s", msg.c_str()); }
void LogFatalImpl(const std::string& file, int lineno, const std::string& message) {
throw tvm::runtime::InternalError(file, lineno, message);
}

void LogMessageImpl(const std::string& file, int lineno, const std::string& message) {
NSLog(@"%s:%d: %s", file.c_str(), lineno, message.c_str());
}
}
}
} // namespace dmlc

namespace tvm {
Expand All @@ -69,7 +79,7 @@ size_t Send(const void* data, size_t size) final {
ssize_t nbytes = [stream_ write:reinterpret_cast<const uint8_t*>(data) maxLength:size];
if (nbytes < 0) {
NSLog(@"%@", [stream_ streamError].localizedDescription);
throw dmlc::Error("Stream error");
throw tvm::Error("Stream error");
}
return nbytes;
}
Expand Down
4 changes: 2 additions & 2 deletions apps/ios_rpc/tvmrpc/ViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ - (void)onReadAvailable {
if (flag == 2) {
[self onShutdownReceived];
}
} catch (const dmlc::Error& e) {
} catch (const tvm::Error& e) {
[self close];
}
}
Expand All @@ -123,7 +123,7 @@ - (void)onWriteAvailable {
if (flag == 2) {
[self onShutdownReceived];
}
} catch (const dmlc::Error& e) {
} catch (const tvm::Error& e) {
[self close];
}
}
Expand Down
5 changes: 5 additions & 0 deletions cmake/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,8 @@ set(USE_TARGET_ONNX OFF)

# Whether enable BNNS runtime
set(USE_BNNS OFF)

# Whether to use libbacktrace
# Libbacktrace provides line and column information on stack traces from errors. It is only
# supported on linux and macOS.
# set(USE_LIBBACKTRACE OFF)
Loading

0 comments on commit 14e8449

Please sign in to comment.