Skip to content

Commit 754ab80

Browse files
weratTeemperor
authored andcommitted
[lldb] Use current execution context in SBDebugger
Use `GetSelectedExecutionContext()` instead of `GetCommandInterpreter().GetExecutionContext()` in `SBDebugger::GetInternalVariableValue/SBDebugger::SetInternalVariable`. The execution context in the command interpreter might be empty, if no commands has been executed yet (it is updated only when handling commands or completions -- e.g. https://github.com/llvm/llvm-project/blob/main/lldb/source/Interpreter/CommandInterpreter.cpp#L1855). Reviewed By: teemperor Differential Revision: https://reviews.llvm.org/D95761
1 parent eefa8a9 commit 754ab80

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lldb/source/API/SBDebugger.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,8 +1278,7 @@ SBError SBDebugger::SetInternalVariable(const char *var_name, const char *value,
12781278
ConstString(debugger_instance_name)));
12791279
Status error;
12801280
if (debugger_sp) {
1281-
ExecutionContext exe_ctx(
1282-
debugger_sp->GetCommandInterpreter().GetExecutionContext());
1281+
ExecutionContext exe_ctx(debugger_sp->GetSelectedExecutionContext());
12831282
error = debugger_sp->SetPropertyValue(&exe_ctx, eVarSetOperationAssign,
12841283
var_name, value);
12851284
} else {
@@ -1303,8 +1302,7 @@ SBDebugger::GetInternalVariableValue(const char *var_name,
13031302
ConstString(debugger_instance_name)));
13041303
Status error;
13051304
if (debugger_sp) {
1306-
ExecutionContext exe_ctx(
1307-
debugger_sp->GetCommandInterpreter().GetExecutionContext());
1305+
ExecutionContext exe_ctx(debugger_sp->GetSelectedExecutionContext());
13081306
lldb::OptionValueSP value_sp(
13091307
debugger_sp->GetPropertyValue(&exe_ctx, var_name, false, error));
13101308
if (value_sp) {

lldb/test/API/python_api/debugger/TestDebuggerAPI.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,35 @@ def test_debugger_delete_invalid_target(self):
4343
target = lldb.SBTarget()
4444
self.assertFalse(target.IsValid())
4545
self.dbg.DeleteTarget(target)
46+
47+
@add_test_categories(['pyapi'])
48+
def test_debugger_internal_variables(self):
49+
debugger_name = self.dbg.GetInstanceName()
50+
51+
# Set a variable and check it was written successfully.
52+
error = lldb.SBDebugger.SetInternalVariable(
53+
'target.prefer-dynamic-value', 'no-dynamic-values', debugger_name)
54+
self.assertTrue(error.Success())
55+
ret = lldb.SBDebugger.GetInternalVariableValue(
56+
'target.prefer-dynamic-value', debugger_name)
57+
self.assertEqual(ret.GetSize(), 1)
58+
self.assertEqual(ret.GetStringAtIndex(0), 'no-dynamic-values')
59+
60+
# Set a variable with a different value.
61+
error = lldb.SBDebugger.SetInternalVariable(
62+
'target.prefer-dynamic-value', 'no-run-target', debugger_name)
63+
self.assertTrue(error.Success())
64+
ret = lldb.SBDebugger.GetInternalVariableValue(
65+
'target.prefer-dynamic-value', debugger_name)
66+
self.assertEqual(ret.GetSize(), 1)
67+
self.assertEqual(ret.GetStringAtIndex(0), 'no-run-target')
68+
69+
# Try setting invalid value, check for error.
70+
error = lldb.SBDebugger.SetInternalVariable(
71+
'target.prefer-dynamic-value', 'dummy-value', debugger_name)
72+
self.assertTrue(error.Fail())
73+
# Check that the value didn't change.
74+
ret = lldb.SBDebugger.GetInternalVariableValue(
75+
'target.prefer-dynamic-value', debugger_name)
76+
self.assertEqual(ret.GetSize(), 1)
77+
self.assertEqual(ret.GetStringAtIndex(0), 'no-run-target')

0 commit comments

Comments
 (0)