Skip to content

Commit

Permalink
Add user-configurable backtrace limit
Browse files Browse the repository at this point in the history
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
  • Loading branch information
driazati committed Jan 22, 2022
1 parent 1ac01b4 commit 90552dc
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/runtime/logging.cc
Expand Up @@ -18,6 +18,7 @@
*/
#include <tvm/runtime/logging.h>

#include <stdexcept>
#include <string>

#if TVM_LOG_STACK_TRACE
Expand Down Expand Up @@ -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 "";
}
Expand Down

0 comments on commit 90552dc

Please sign in to comment.