Skip to content

Commit 175ff48

Browse files
driazatiylc
authored andcommitted
Add user-configurable backtrace limit (apache#10025)
A spin off of apache#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 <[email protected]>
1 parent f5b3b4b commit 175ff48

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/runtime/logging.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
#include <tvm/runtime/logging.h>
2020

21+
#include <stdexcept>
2122
#include <string>
2223

2324
#if TVM_LOG_STACK_TRACE
@@ -120,7 +121,22 @@ int BacktraceFullCallback(void* data, uintptr_t pc, const char* filename, int li
120121

121122
std::string Backtrace() {
122123
BacktraceInfo bt;
123-
bt.max_size = 500;
124+
125+
// Limit backtrace length based on TVM_BACKTRACE_LIMIT env variable
126+
auto user_limit_s = getenv("TVM_BACKTRACE_LIMIT");
127+
const auto default_limit = 500;
128+
129+
if (user_limit_s == nullptr) {
130+
bt.max_size = default_limit;
131+
} else {
132+
// Parse out the user-set backtrace limit
133+
try {
134+
bt.max_size = std::stoi(user_limit_s);
135+
} catch (const std::invalid_argument& e) {
136+
bt.max_size = default_limit;
137+
}
138+
}
139+
124140
if (_bt_state == nullptr) {
125141
return "";
126142
}

0 commit comments

Comments
 (0)