You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/03/16 16:50:01 UTC

[GitHub] [tvm] junrushao1994 commented on a change in pull request #7153: [RUNTIME] Add libbacktrace for backtraces with line numbers

junrushao1994 commented on a change in pull request #7153:
URL: https://github.com/apache/tvm/pull/7153#discussion_r595356485



##########
File path: src/runtime/logging.cc
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifdef TVM_BACKTRACE_DISABLED
+#include <string>
+
+// TODO(bkimball,tkonolige) This inline function is to work around a linking error I am having when
+// using MSVC If the function definition is in logging.cc then the linker can't find it no matter
+// what kind of attributes (dllexport) I decorate it with. This is temporary and will be addressed
+// when we get backtrace working on Windows.
+namespace tvm {
+namespace runtime {
+__declspec(dllexport) std::string Backtrace() { return ""; }

Review comment:
       Do we want to use `TVM_DLL` instead? I am not sure

##########
File path: src/runtime/logging.cc
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifdef TVM_BACKTRACE_DISABLED
+#include <string>
+
+// TODO(bkimball,tkonolige) This inline function is to work around a linking error I am having when
+// using MSVC If the function definition is in logging.cc then the linker can't find it no matter
+// what kind of attributes (dllexport) I decorate it with. This is temporary and will be addressed
+// when we get backtrace working on Windows.
+namespace tvm {
+namespace runtime {
+__declspec(dllexport) std::string Backtrace() { return ""; }
+}  // namespace runtime
+}  // namespace tvm
+#else
+
+#include <backtrace.h>
+#include <cxxabi.h>
+#include <tvm/runtime/logging.h>
+
+#include <iomanip>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <vector>
+
+namespace tvm {
+namespace runtime {
+namespace {
+
+struct BacktraceInfo {
+  std::vector<std::string> lines;
+  size_t max_size;
+  std::string error_message;
+};
+
+void BacktraceCreateErrorCallback(void* data, const char* msg, int errnum) {
+  std::cerr << "Could not initialize backtrace state: " << msg << std::endl;
+}
+
+backtrace_state* BacktraceCreate() {
+  return backtrace_create_state(nullptr, 1, BacktraceCreateErrorCallback, nullptr);
+}
+
+static backtrace_state* _bt_state = BacktraceCreate();
+
+std::string DemangleName(std::string name) {
+  int status = 0;
+  size_t length = name.size();
+  std::unique_ptr<char, void (*)(void* __ptr)> demangled_name = {
+      abi::__cxa_demangle(name.c_str(), nullptr, &length, &status), &std::free};
+  if (demangled_name && status == 0 && length > 0) {
+    return demangled_name.get();
+  } else {
+    return name;
+  }
+}
+
+void BacktraceErrorCallback(void* data, const char* msg, int errnum) {
+  // do nothing
+}
+
+void BacktraceSyminfoCallback(void* data, uintptr_t pc, const char* symname, uintptr_t symval,
+                              uintptr_t symsize) {
+  auto str = reinterpret_cast<std::string*>(data);
+
+  if (symname != nullptr) {
+    std::string tmp(symname, symsize);
+    *str = DemangleName(tmp.c_str());
+  } else {
+    std::ostringstream s;
+    s << "0x" << std::setfill('0') << std::setw(sizeof(uintptr_t) * 2) << std::hex << pc;
+    *str = s.str();
+  }
+}
+int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int lineno,

Review comment:
       format: add an empty line

##########
File path: CMakeLists.txt
##########
@@ -539,3 +567,33 @@ if(MSVC)
   target_compile_definitions(tvm_objs PRIVATE -DTVM_EXPORTS)
   target_compile_definitions(tvm_runtime_objs 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
+		  )

Review comment:
       format: alignment




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org