From 9a6423cd42fb03d8e09ec389b9ecb95935d1fa80 Mon Sep 17 00:00:00 2001 From: David Riazati <9407960+driazati@users.noreply.github.com> Date: Sat, 22 Jan 2022 12:58:19 -0800 Subject: [PATCH] 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 --- 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 9c193921f93b..ec71e4453957 100644 --- a/src/runtime/logging.cc +++ b/src/runtime/logging.cc @@ -18,6 +18,7 @@ */ #include +#include #include #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 ""; }