[Lldb-commits] [PATCH] D67685: [ScriptInterpreter] Make sure LLDB's global variables are only available in interactive mode.

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


JDevlieghere updated this revision to Diff 220588.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D67685/new/

https://reviews.llvm.org/D67685

Files:
  lldb/lit/Commands/Inputs/frame.py
  lldb/lit/Commands/command-script-import.test
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp


Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -616,6 +616,10 @@
   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 @@
     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);
Index: lldb/lit/Commands/command-script-import.test
===================================================================
--- lldb/lit/Commands/command-script-import.test
+++ 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
Index: lldb/lit/Commands/Inputs/frame.py
===================================================================
--- lldb/lit/Commands/Inputs/frame.py
+++ lldb/lit/Commands/Inputs/frame.py
@@ -1,2 +1,2 @@
 import lldb
-print(lldb.frame)
+print("frame:py: {}".format(lldb.frame))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67685.220588.patch
Type: text/x-patch
Size: 2637 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20190918/bee2fb0e/attachment.bin>


More information about the lldb-commits mailing list