llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: E.G (GlobalStar117)

<details>
<summary>Changes</summary>

## 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()-&gt;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 &amp;&amp; exe_scope) {
  frame_sp = exe_scope-&gt;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.

---

&lt;!-- Gittensor Contribution Tag: @<!-- -->GlobalStar117 --&gt;

---
Full diff: https://github.com/llvm/llvm-project/pull/176801.diff


1 Files Affected:

- (modified) lldb/source/Expression/Materializer.cpp (+9) 


``````````diff
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");

``````````

</details>


https://github.com/llvm/llvm-project/pull/176801
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to