[Lldb-commits] [lldb] r372192 - [ScriptInterpreter] Limit LLDB's globals to interactive mode.

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Tue Sep 17 17:30:01 PDT 2019


Author: jdevlieghere
Date: Tue Sep 17 17:30:01 2019
New Revision: 372192

URL: http://llvm.org/viewvc/llvm-project?rev=372192&view=rev
Log:
[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

Modified:
    lldb/trunk/lit/Commands/Inputs/frame.py
    lldb/trunk/lit/Commands/command-script-import.test
    lldb/trunk/packages/Python/lldbsuite/test/commands/frame/recognizer/recognizer.py
    lldb/trunk/packages/Python/lldbsuite/test/python_api/sbvalue_const_addrof/main.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Modified: lldb/trunk/lit/Commands/Inputs/frame.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Commands/Inputs/frame.py?rev=372192&r1=372191&r2=372192&view=diff
==============================================================================
--- lldb/trunk/lit/Commands/Inputs/frame.py (original)
+++ lldb/trunk/lit/Commands/Inputs/frame.py Tue Sep 17 17:30:01 2019
@@ -1,2 +1,2 @@
 import lldb
-print(lldb.frame)
+print("frame:py: {}".format(lldb.frame))

Modified: lldb/trunk/lit/Commands/command-script-import.test
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lit/Commands/command-script-import.test?rev=372192&r1=372191&r2=372192&view=diff
==============================================================================
--- lldb/trunk/lit/Commands/command-script-import.test (original)
+++ lldb/trunk/lit/Commands/command-script-import.test Tue Sep 17 17:30:01 2019
@@ -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

Modified: lldb/trunk/packages/Python/lldbsuite/test/commands/frame/recognizer/recognizer.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/commands/frame/recognizer/recognizer.py?rev=372192&r1=372191&r2=372192&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/commands/frame/recognizer/recognizer.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/commands/frame/recognizer/recognizer.py Tue Sep 17 17:30:01 2019
@@ -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 []
 

Modified: lldb/trunk/packages/Python/lldbsuite/test/python_api/sbvalue_const_addrof/main.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/sbvalue_const_addrof/main.cpp?rev=372192&r1=372191&r2=372192&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/python_api/sbvalue_const_addrof/main.cpp (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/python_api/sbvalue_const_addrof/main.cpp Tue Sep 17 17:30:01 2019
@@ -28,7 +28,7 @@ ThreadInfo *g_thread_list_ptr = &g_threa
 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))

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp?rev=372192&r1=372191&r2=372192&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp Tue Sep 17 17:30:01 2019
@@ -616,6 +616,10 @@ void ScriptInterpreterPythonImpl::LeaveS
   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::LoadSc
     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);




More information about the lldb-commits mailing list