From 20b52c33ba3960340ecf326314517091b8ec98f4 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Wed, 18 Sep 2019 00:30:01 +0000 Subject: [PATCH] [ScriptInterpreter] Limit LLDB's globals to interactive mode. Jim pointed out that the LLDB global variables should only be available in interactive mode. When used from a command for example, their values might be stale or not at all what the user expects. Therefore we want to explicitly make these variables unavailable. Differential revision: https://reviews.llvm.org/D67685 llvm-svn: 372192 --- lldb/lit/Commands/Inputs/frame.py | 2 +- lldb/lit/Commands/command-script-import.test | 8 ++++++-- .../test/commands/frame/recognizer/recognizer.py | 6 +++--- .../test/python_api/sbvalue_const_addrof/main.cpp | 2 +- .../ScriptInterpreter/Python/ScriptInterpreterPython.cpp | 16 ++++++++++------ 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lldb/lit/Commands/Inputs/frame.py b/lldb/lit/Commands/Inputs/frame.py index cd7b61c..07ed2d4 100644 --- a/lldb/lit/Commands/Inputs/frame.py +++ b/lldb/lit/Commands/Inputs/frame.py @@ -1,2 +1,2 @@ import lldb -print(lldb.frame) +print("frame:py: {}".format(lldb.frame)) diff --git a/lldb/lit/Commands/command-script-import.test b/lldb/lit/Commands/command-script-import.test index bddb849..67b9d93 100644 --- a/lldb/lit/Commands/command-script-import.test +++ b/lldb/lit/Commands/command-script-import.test @@ -3,6 +3,10 @@ # RUN: echo 'command script import %S/Inputs/frame.py' >> %t.in # RUN: %clang -g -O0 %S/Inputs/main.c -o %t.out -# RUN: %lldb -b -s %t.in %t.out | FileCheck %s +# RUN: %lldb -b -s %t.in -o 'script print("script: {}").format(lldb.frame)' %t.out | FileCheck %s -# CHECK: frame #0 +# Make sure that we don't have access to lldb.frame from the Python script. +# CHECK: frame:py: None + +# Make sure that we do have access to lldb.frame from the script command. +# CHECK: script: frame #0 diff --git a/lldb/packages/Python/lldbsuite/test/commands/frame/recognizer/recognizer.py b/lldb/packages/Python/lldbsuite/test/commands/frame/recognizer/recognizer.py index a8a5067..548676c 100644 --- a/lldb/packages/Python/lldbsuite/test/commands/frame/recognizer/recognizer.py +++ b/lldb/packages/Python/lldbsuite/test/commands/frame/recognizer/recognizer.py @@ -7,12 +7,12 @@ class MyFrameRecognizer(object): if frame.name == "foo": arg1 = frame.EvaluateExpression("$arg1").signed arg2 = frame.EvaluateExpression("$arg2").signed - val1 = lldb.target.CreateValueFromExpression("a", "%d" % arg1) - val2 = lldb.target.CreateValueFromExpression("b", "%d" % arg2) + val1 = frame.GetThread().GetProcess().GetTarget().CreateValueFromExpression("a", "%d" % arg1) + val2 = frame.GetThread().GetProcess().GetTarget().CreateValueFromExpression("b", "%d" % arg2) return [val1, val2] elif frame.name == "bar": arg1 = frame.EvaluateExpression("$arg1").signed - val1 = lldb.target.CreateValueFromExpression("a", "(int *)%d" % arg1) + val1 = frame.GetThread().GetProcess().GetTarget().CreateValueFromExpression("a", "(int *)%d" % arg1) return [val1] return [] diff --git a/lldb/packages/Python/lldbsuite/test/python_api/sbvalue_const_addrof/main.cpp b/lldb/packages/Python/lldbsuite/test/python_api/sbvalue_const_addrof/main.cpp index eb9c8e0..318a45b 100644 --- a/lldb/packages/Python/lldbsuite/test/python_api/sbvalue_const_addrof/main.cpp +++ b/lldb/packages/Python/lldbsuite/test/python_api/sbvalue_const_addrof/main.cpp @@ -28,7 +28,7 @@ ThreadInfo *g_thread_list_ptr = &g_thread1; int main (int argc, char const *argv[], char const *envp[]) { printf ("g_thread_list is %p\n", g_thread_list_ptr); - return 0; //% v = lldb.target.FindFirstGlobalVariable('g_thread_list_ptr') + return 0; //% v = self.dbg.GetSelectedTarget().FindFirstGlobalVariable('g_thread_list_ptr') //% v_gla = v.GetChildMemberWithName('regs').GetLoadAddress() //% v_aof = v.GetChildMemberWithName('regs').AddressOf().GetValueAsUnsigned(lldb.LLDB_INVALID_ADDRESS) //% expr = '(%s)0x%x' % (v.GetType().GetName(), v.GetValueAsUnsigned(0)) diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp index 1eecbc1..25a81f6 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -616,6 +616,10 @@ void ScriptInterpreterPythonImpl::LeaveSession() { if (log) log->PutCString("ScriptInterpreterPythonImpl::LeaveSession()"); + // Unset the LLDB global variables. + PyRun_SimpleString("lldb.debugger = None; lldb.target = None; lldb.process " + "= None; lldb.thread = None; lldb.frame = None"); + // checking that we have a valid thread state - since we use our own // threading and locking in some (rare) cases during cleanup Python may end // up believing we have no thread state and PyImport_AddModule will crash if @@ -2687,12 +2691,12 @@ bool ScriptInterpreterPythonImpl::LoadScriptingModule( StreamString command_stream; // Before executing Python code, lock the GIL. - Locker py_lock( - this, - Locker::AcquireLock | (init_session ? Locker::InitSession : 0) | - (init_session ? Locker::InitGlobals : 0) | Locker::NoSTDIN, - Locker::FreeAcquiredLock | - (init_session ? Locker::TearDownSession : 0)); + Locker py_lock(this, + Locker::AcquireLock | + (init_session ? Locker::InitSession : 0) | + Locker::NoSTDIN, + Locker::FreeAcquiredLock | + (init_session ? Locker::TearDownSession : 0)); namespace fs = llvm::sys::fs; fs::file_status st; std::error_code ec = status(target_file.GetPath(), st); -- 2.7.4