You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by ju...@apache.org on 2022/01/22 20:59:10 UTC

[tvm] branch main updated: Add user-configurable backtrace limit (#10025)

This is an automated email from the ASF dual-hosted git repository.

junrushao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 9a6423c  Add user-configurable backtrace limit (#10025)
9a6423c is described below

commit 9a6423cd42fb03d8e09ec389b9ecb95935d1fa80
Author: David Riazati <94...@users.noreply.github.com>
AuthorDate: Sat Jan 22 12:58:19 2022 -0800

    Add user-configurable backtrace limit (#10025)
    
    A spin off of #9872, this adds an env variable `TVM_BACKTRACE_LIMIT` which can be set to an integer to limit the frames printed out on errors. This can make it easier to run interactive TVM scripts with errors since the stack traces are often long (70+ frames).
    
    ```bash
    export TVM_BACKTRACE_LIMIT=5
    python some_code_with_an_error.py
    ```
    
    cc @tkonolige
    
    Co-authored-by: driazati <dr...@users.noreply.github.com>
---
 src/runtime/logging.cc | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/runtime/logging.cc b/src/runtime/logging.cc
index 9c19392..ec71e44 100644
--- a/src/runtime/logging.cc
+++ b/src/runtime/logging.cc
@@ -18,6 +18,7 @@
  */
 #include <tvm/runtime/logging.h>
 
+#include <stdexcept>
 #include <string>
 
 #if TVM_LOG_STACK_TRACE
@@ -120,7 +121,22 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li
 
 std::string Backtrace() {
   BacktraceInfo bt;
-  bt.max_size = 500;
+
+  // Limit backtrace length based on TVM_BACKTRACE_LIMIT env variable
+  auto user_limit_s = getenv("TVM_BACKTRACE_LIMIT");
+  const auto default_limit = 500;
+
+  if (user_limit_s == nullptr) {
+    bt.max_size = default_limit;
+  } else {
+    // Parse out the user-set backtrace limit
+    try {
+      bt.max_size = std::stoi(user_limit_s);
+    } catch (const std::invalid_argument& e) {
+      bt.max_size = default_limit;
+    }
+  }
+
   if (_bt_state == nullptr) {
     return "";
   }