https://github.com/GlobalStar117 created https://github.com/llvm/llvm-project/pull/176801
## Summary This PR fixes an issue where evaluating expressions that reference registers (e.g., `po $x0`) from Python scripts via `HandleCommand` fails with: ``` error: Couldn't apply expression side effects: couldn't dematerialize register x0 without a stack frame ``` ## Related Issue Fixes https://github.com/llvm/llvm-project/issues/176717 ## Root Cause The `Dematerializer` stores a weak pointer (`m_thread_wp`) to the thread and a stack ID (`m_stack_id`) during materialization. During dematerialization: 1. It tries to lock the weak pointer to get the thread 2. Then gets the frame by stack ID from the thread 3. If either step fails, `frame_sp` becomes null 4. `EntityRegister::Dematerialize` then fails because it requires a valid stack frame In Python script context, the weak pointer may expire or the stack ID may become invalid between materialization and dematerialization, even though the execution context is still valid. ## The Fix Add a fallback in `Dematerializer::Dematerialize()`: if the stored weak pointer/stack ID doesn't yield a valid frame, try to get the frame from `GetBestExecutionContextScope()->CalculateStackFrame()`. ```cpp // If we couldn't get the frame from the stored weak pointer/stack ID, // try to get it from the best execution context scope as a fallback. if (!frame_sp && exe_scope) { frame_sp = exe_scope->CalculateStackFrame(); } ``` This handles cases where: - The execution context is still valid - But the stored thread reference has expired - Common in Python scripting scenarios ## Testing This fix was identified by analyzing the issue report where: - `register read x0` works from Python scripts - `po $x0` fails from Python scripts - `po $x0` works interactively after the script completes The fix ensures the execution context is properly retrieved when the stored references are stale. --- <!-- Gittensor Contribution Tag: @GlobalStar117 --> >From 6c1d83f5ab858357d1c07889c32dad942d88e626 Mon Sep 17 00:00:00 2001 From: Globalstar117 <[email protected]> Date: Tue, 20 Jan 2026 05:39:38 +1100 Subject: [PATCH] [LLDB] Fix register dematerialization failure in Python script context When evaluating expressions that reference registers (e.g., 'po $x0') from Python scripts via HandleCommand, the dematerialization step could fail with 'couldn't dematerialize register x0 without a stack frame'. Root cause: The Dematerializer stores a weak pointer to the thread and a stack ID during materialization. During dematerialization, if the weak pointer has expired or the stack ID is no longer valid, the frame_sp becomes null. This causes EntityRegister::Dematerialize to fail since it requires a valid stack frame to write back register values. The fix adds a fallback: if the stored weak pointer/stack ID doesn't yield a valid frame, try to get the frame from GetBestExecutionContextScope(). This handles cases where the execution context is still valid but the stored references have become stale, which is common in Python scripting scenarios. Fixes #176717 This is a Gittensor contribution --- lldb/source/Expression/Materializer.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lldb/source/Expression/Materializer.cpp b/lldb/source/Expression/Materializer.cpp index 771a9ab84a20c..594362f07dfff 100644 --- a/lldb/source/Expression/Materializer.cpp +++ b/lldb/source/Expression/Materializer.cpp @@ -1575,6 +1575,15 @@ void Materializer::Dematerializer::Dematerialize(Status &error, if (!exe_scope) exe_scope = m_map->GetBestExecutionContextScope(); + // If we couldn't get the frame from the stored weak pointer/stack ID, + // try to get it from the best execution context scope as a fallback. + // This handles cases where the expression evaluation is triggered from + // Python scripts where the stored thread reference may have expired + // but the execution context is still valid. + if (!frame_sp && exe_scope) { + frame_sp = exe_scope->CalculateStackFrame(); + } + if (!IsValid()) { error = Status::FromErrorString( "Couldn't dematerialize: invalid dematerializer"); _______________________________________________ lldb-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
