Use abseil for readable POSIX stack traces in debug builds#28405
Open
tianleiwu wants to merge 7 commits into
Open
Use abseil for readable POSIX stack traces in debug builds#28405tianleiwu wants to merge 7 commits into
tianleiwu wants to merge 7 commits into
Conversation
821dab8 to
65086c2
Compare
Replace glibc backtrace()/backtrace_symbols() with abseil's GetStackTrace()/Symbolize() in debug builds on Linux/POSIX. The previous implementation produced raw addresses requiring manual addr2line translation. The new implementation produces demangled function names (e.g. onnxruntime::InferenceSession::Run) directly in exception messages. No new dependency is introduced — absl::stacktrace and absl::symbolize are already linked via ABSEIL_LIBS. Windows stack traces (C++23 <stacktrace>) are unchanged. Closes #26257
418a610 to
f4a02a5
Compare
f4a02a5 to
45c25d2
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates ONNX Runtime’s POSIX/Linux debug stack trace generation to produce more readable, symbolized call stacks by switching from glibc backtrace()/backtrace_symbols() to Abseil’s stacktrace/symbolization APIs, with optional addr2line-based file:line resolution.
Changes:
- Replace glibc backtrace collection/symbol printing with
absl::GetStackTrace()+absl::Symbolize()in POSIX debug builds. - Add best-effort
addr2lineintegration to resolve source file and line numbers for stack frames (opt-in via env var). - Initialize Abseil’s symbolizer once during
Environment::Initialize()on non-Windows POSIX platforms.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
onnxruntime/core/session/environment.cc |
Adds one-time Abseil symbolizer initialization during environment setup on non-Windows POSIX platforms. |
onnxruntime/core/platform/posix/stacktrace.cc |
Reworks debug stack trace capture/symbolization to use Abseil and optionally resolve file:line via addr2line. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Fix env var name inconsistency: comments now correctly say ORT_ADDR2LINE to match the implementation (was ORT_ENABLE_ADDR2LINE) - Replace popen() shell command with posix_spawnp() + pipe to eliminate shell injection risk from binary paths with spaces or special chars - Guard InitializeSymbolizer() and its include with #ifndef NDEBUG to match the debug-only stacktrace implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Replace glibc
backtrace()/backtrace_symbols()with abseil'sabsl::GetStackTrace()/absl::Symbolize()for POSIX/Linux debug builds, and add automaticaddr2lineresolution for file paths and line numbers. The previous implementation produced raw addresses requiring manualaddr2linetranslation. The new implementation produces demangled function names with source locations directly in exception messages, with zero new dependencies.Summary of Changes
Stack Trace Implementation
onnxruntime/core/platform/posix/stacktrace.ccbacktrace()/backtrace_symbols()withabsl::GetStackTrace()/absl::Symbolize(). Usedladdr()+addr2lineto resolve source file and line number for each frame.onnxruntime/core/session/environment.ccabsl::InitializeSymbolizer(nullptr)call viastd::call_onceinEnvironment::Initialize(). On Linux,nullptrworks because abseil reads/proc/self/exe.Before vs After
Before (raw addresses requiring manual
addr2line):After (demangled function names + file:line):
Motivation and Context
Follow-up on #26257, which was closed because abseil's backtrace/symbolize is already available as a dependency. This PR implements that suggestion with additional file:line resolution:
absl::stacktraceandabsl::symbolizeare already inABSEIL_LIBSand linked toonnxruntime_common.dladdr()andaddr2lineare standard POSIX/Linux utilities.#ifndef NDEBUG— no performance impact in release buildsdladdr()to compute file offsets, then callsaddr2linein batch (once per binary). Falls back gracefully to function-name-only output ifaddr2lineis unavailable.<stacktrace>_OPSCHEMA_LIB_builds continue to return empty stack tracesHow it works
absl::GetStackTrace()captures raw frame addressesabsl::Symbolize()resolves each address to a demangled function namedladdr()determines which binary each address belongs to and computes the file offsetaddr2lineis called in batch (one invocation per binary) to resolve file:lineTesting
onnxruntime_test_all --gtest_filter="*BadModelInvalidDimParamUsage*"— confirmed stack trace shows demangled function names with file paths and line numbers through the full call chain