22// Licensed under the MIT License.
33
44#include " core/common/common.h"
5-
65#include < vector>
76
87#if !defined(NDEBUG) && !defined(__ANDROID__) && !defined(__wasm__) && !defined(_OPSCHEMA_LIB_) && !defined(_AIX)
8+ #include < cstdlib>
99#include < cstdio>
1010#include < cstring>
1111#include < dlfcn.h>
1616
1717namespace {
1818
19+ inline int GetAddr2LineEnv () {
20+ const char * val = std::getenv (" ORT_ADDR2LINE" );
21+ if (val == nullptr ) {
22+ return 0 ;
23+ }
24+ return atoi (val);
25+ }
26+
27+ // Check once whether ORT_ENABLE_ADDR2LINE is set.
28+ bool GetAddr2LineCount () {
29+ static const int count = GetAddr2LineEnv ();
30+ return count;
31+ }
32+
1933// Resolve file offsets to file:line using addr2line, grouped by binary.
2034// Returns a map from original address to "file:line" string.
2135std::unordered_map<void *, std::string> ResolveWithAddr2Line (
@@ -90,8 +104,14 @@ std::vector<std::string> GetStackTrace() {
90104 int depth = absl::GetStackTrace (addresses, kCallstackLimit , /* skip_count=*/ 2 );
91105 stack.reserve (depth);
92106
93- // Attempt to resolve file:line via addr2line (best-effort, debug builds only).
94- auto resolved = ResolveWithAddr2Line (addresses, depth);
107+ // Resolve file:line via addr2line only when explicitly opted in via
108+ // ORT_ENABLE_ADDR2LINE=1, since it spawns external processes and can be
109+ // very slow on large debug binaries.
110+ std::unordered_map<void *, std::string> resolved;
111+ int count = GetAddr2LineCount ();
112+ if (count > 0 ) {
113+ resolved = ResolveWithAddr2Line (addresses, std::min (depth, count));
114+ }
95115
96116 for (int i = 0 ; i < depth; ++i) {
97117 std::ostringstream oss;
@@ -102,7 +122,6 @@ std::vector<std::string> GetStackTrace() {
102122 oss << " [unknown]" ;
103123 }
104124
105- // Append file:line if resolved, otherwise the raw address as fallback.
106125 auto it = resolved.find (addresses[i]);
107126 if (it != resolved.end ()) {
108127 oss << " at " << it->second ;
0 commit comments