https://github.com/igorkudrin created 
https://github.com/llvm/llvm-project/pull/149642

This patch fixes updating persistent variables when memory cannot be allocated 
in an inferior process:
```
> lldb -c test.core
(lldb) expr int $i = 5
(lldb) expr $i = 55
(int) $0 = 55
(lldb) expr $i
(int) $i = 5
```

With this patch, the last command prints:
```
(int) $i = 55
```

The issue was introduced in #145599.

>From f524fdc952592d715aee1e3d5ec75c4180f75509 Mon Sep 17 00:00:00 2001
From: Igor Kudrin <ikud...@accesssoftek.com>
Date: Fri, 18 Jul 2025 23:45:39 -0700
Subject: [PATCH] [lldb] Fix updating persistent variables without JIT

This patch fixes updating persistent variables when memory cannot be
allocated in an inferior process:
```
> lldb -c test.core
(lldb) expr int $i = 5
(lldb) expr $i = 55
(int) $0 = 55
(lldb) expr $i
(int) $i = 5
```

With this patch, the last command prints:
```
(int) $i = 55
```

The issue was introduced in #145599.
---
 lldb/source/Expression/Materializer.cpp       | 21 ++++++++++---------
 .../postmortem/elf-core/expr/TestExpr.py      |  4 ++++
 2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/lldb/source/Expression/Materializer.cpp 
b/lldb/source/Expression/Materializer.cpp
index 17ea1596806d0..329768dd7915a 100644
--- a/lldb/source/Expression/Materializer.cpp
+++ b/lldb/source/Expression/Materializer.cpp
@@ -102,22 +102,23 @@ class EntityPersistentVariable : public 
Materializer::Entity {
         m_persistent_variable_sp->GetName(), mem, eAddressTypeLoad,
         map.GetAddressByteSize());
 
-    if (m_persistent_variable_sp->m_flags &
-        ExpressionVariable::EVKeepInTarget) {
-      if (used_policy == IRMemoryMap::eAllocationPolicyMirror) {
+    if (used_policy == IRMemoryMap::eAllocationPolicyMirror) {
+      if (m_persistent_variable_sp->m_flags &
+          ExpressionVariable::EVKeepInTarget) {
         // Clear the flag if the variable will never be deallocated.
         Status leak_error;
         map.Leak(mem, leak_error);
         m_persistent_variable_sp->m_flags &=
             ~ExpressionVariable::EVNeedsAllocation;
-      } else {
-        // If the variable cannot be kept in target, clear this flag...
-        m_persistent_variable_sp->m_flags &=
-            ~ExpressionVariable::EVKeepInTarget;
-        // ...and set the flag to copy the value during dematerialization.
-        m_persistent_variable_sp->m_flags |=
-            ExpressionVariable::EVNeedsFreezeDry;
       }
+    } else {
+      // If we cannot allocate memory in the process,
+      // - clear the 'EVKeepInTarget' flag to ensure that 'm_live_sp' is reset
+      //   during dematerialization,
+      m_persistent_variable_sp->m_flags &= ~ExpressionVariable::EVKeepInTarget;
+      // - set the 'EVNeedsFreezeDry' flag so that the value is copied to
+      //   'm_frozen_sp' during dematerialization.
+      m_persistent_variable_sp->m_flags |= 
ExpressionVariable::EVNeedsFreezeDry;
     }
 
     // Write the contents of the variable to the area.
diff --git a/lldb/test/API/functionalities/postmortem/elf-core/expr/TestExpr.py 
b/lldb/test/API/functionalities/postmortem/elf-core/expr/TestExpr.py
index dd03a0cc836a7..9dfc6859af8bf 100644
--- a/lldb/test/API/functionalities/postmortem/elf-core/expr/TestExpr.py
+++ b/lldb/test/API/functionalities/postmortem/elf-core/expr/TestExpr.py
@@ -37,6 +37,10 @@ def test_persist_var(self):
         self.target.EvaluateExpression("int $my_int = 5")
         self.expect_expr("$my_int * 2", result_type="int", result_value="10")
 
+        # Try assigning the persistent variable a new value.
+        self.target.EvaluateExpression("$my_int = 55")
+        self.expect_expr("$my_int", result_type="int", result_value="55")
+
     def test_context_object(self):
         """Test expression evaluation in context of an object."""
 

_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to