Skip to content

Commit 699cf51

Browse files
committed
fixup! fix user-defined persistent variables
1 parent fac89bb commit 699cf51

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

lldb/include/lldb/Expression/IRMemoryMap.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ class IRMemoryMap {
5050
///only in the process.
5151
};
5252

53+
// If 'policy' is 'eAllocationPolicyMirror' but it is impossible to allocate
54+
// memory in the process, 'eAllocationPolicyHostOnly' will be used instead.
55+
// The actual policy is returned via 'used_policy'.
5356
lldb::addr_t Malloc(size_t size, uint8_t alignment, uint32_t permissions,
54-
AllocationPolicy policy, bool zero_memory, Status &error);
57+
AllocationPolicy policy, bool zero_memory, Status &error,
58+
AllocationPolicy *used_policy = nullptr);
5559
void Leak(lldb::addr_t process_address, Status &error);
5660
void Free(lldb::addr_t process_address, Status &error);
5761

lldb/source/Expression/IRMemoryMap.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ IRMemoryMap::Allocation::Allocation(lldb::addr_t process_alloc,
321321

322322
lldb::addr_t IRMemoryMap::Malloc(size_t size, uint8_t alignment,
323323
uint32_t permissions, AllocationPolicy policy,
324-
bool zero_memory, Status &error) {
324+
bool zero_memory, Status &error,
325+
AllocationPolicy *used_policy) {
325326
lldb_private::Log *log(GetLog(LLDBLog::Expressions));
326327
error.Clear();
327328

@@ -455,6 +456,9 @@ lldb::addr_t IRMemoryMap::Malloc(size_t size, uint8_t alignment,
455456
(uint64_t)permissions, policy_string, aligned_address);
456457
}
457458

459+
if (used_policy)
460+
*used_policy = policy;
461+
458462
return aligned_address;
459463
}
460464

lldb/source/Expression/Materializer.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ class EntityPersistentVariable : public Materializer::Entity {
7676

7777
Status allocate_error;
7878
const bool zero_memory = false;
79+
IRMemoryMap::AllocationPolicy used_policy;
7980

8081
lldb::addr_t mem = map.Malloc(
8182
llvm::expectedToOptional(m_persistent_variable_sp->GetByteSize())
8283
.value_or(0),
8384
8, lldb::ePermissionsReadable | lldb::ePermissionsWritable,
84-
IRMemoryMap::eAllocationPolicyMirror, zero_memory, allocate_error);
85+
IRMemoryMap::eAllocationPolicyMirror, zero_memory, allocate_error,
86+
&used_policy);
8587

8688
if (!allocate_error.Success()) {
8789
err = Status::FromErrorStringWithFormat(
@@ -103,14 +105,22 @@ class EntityPersistentVariable : public Materializer::Entity {
103105
m_persistent_variable_sp->GetName(), mem, eAddressTypeLoad,
104106
map.GetAddressByteSize());
105107

106-
// Clear the flag if the variable will never be deallocated.
107-
108108
if (m_persistent_variable_sp->m_flags &
109109
ExpressionVariable::EVKeepInTarget) {
110-
Status leak_error;
111-
map.Leak(mem, leak_error);
112-
m_persistent_variable_sp->m_flags &=
113-
~ExpressionVariable::EVNeedsAllocation;
110+
if (used_policy == IRMemoryMap::eAllocationPolicyMirror) {
111+
// Clear the flag if the variable will never be deallocated.
112+
Status leak_error;
113+
map.Leak(mem, leak_error);
114+
m_persistent_variable_sp->m_flags &=
115+
~ExpressionVariable::EVNeedsAllocation;
116+
} else {
117+
// If the variable cannot be kept in target, clear this flag...
118+
m_persistent_variable_sp->m_flags &=
119+
~ExpressionVariable::EVKeepInTarget;
120+
// ...and set the flag to copy the value during dematerialization.
121+
m_persistent_variable_sp->m_flags |=
122+
ExpressionVariable::EVNeedsFreezeDry;
123+
}
114124
}
115125

116126
// Write the contents of the variable to the area.

lldb/test/API/functionalities/postmortem/elf-core/expr/TestExpr.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
@skipIfLLVMTargetMissing("X86")
1212
class CoreExprTestCase(TestBase):
13+
def setUp(self):
14+
TestBase.setUp(self)
15+
self.target = self.dbg.CreateTarget("linux-x86_64.out")
16+
self.process = self.target.LoadCore("linux-x86_64.core")
17+
self.assertTrue(self.process, PROCESS_IS_VALID)
18+
1319
def test_result_var(self):
1420
"""Test that the result variable can be used in subsequent expressions."""
1521

16-
target = self.dbg.CreateTarget("linux-x86_64.out")
17-
process = target.LoadCore("linux-x86_64.core")
18-
self.assertTrue(process, PROCESS_IS_VALID)
19-
2022
self.expect_expr(
2123
"outer",
2224
result_type="Outer",
@@ -29,12 +31,14 @@ def test_result_var(self):
2931
)
3032
self.expect_expr("$1.val", result_type="int", result_value="5")
3133

32-
def test_context_object(self):
33-
"""Tests expression evaluation in context of an object."""
34+
def test_persist_var(self):
35+
"""Test that user-defined variables can be used in subsequent expressions."""
3436

35-
target = self.dbg.CreateTarget("linux-x86_64.out")
36-
process = target.LoadCore("linux-x86_64.core")
37-
self.assertTrue(process, PROCESS_IS_VALID)
37+
self.target.EvaluateExpression("int $my_int = 5")
38+
self.expect_expr("$my_int * 2", result_type="int", result_value="10")
39+
40+
def test_context_object(self):
41+
"""Test expression evaluation in context of an object."""
3842

3943
val_outer = self.expect_expr("outer", result_type="Outer")
4044

0 commit comments

Comments
 (0)