[Lldb-commits] [lldb] 4164be7 - [Lldb/Lua] Persist Lua state across script interpreter calls.

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Sat Dec 21 15:01:06 PST 2019


Author: Jonas Devlieghere
Date: 2019-12-21T15:00:35-08:00
New Revision: 4164be7206d740b77b5a7b4b2f859ed122d08c10

URL: https://github.com/llvm/llvm-project/commit/4164be7206d740b77b5a7b4b2f859ed122d08c10
DIFF: https://github.com/llvm/llvm-project/commit/4164be7206d740b77b5a7b4b2f859ed122d08c10.diff

LOG: [Lldb/Lua] Persist Lua state across script interpreter calls.

Don't create a new lua state on every operation. Share a single state
across the lifetime of the script interpreter. Add simple locking to
prevent two threads from modifying the state concurrently.

Added: 
    lldb/test/Shell/ScriptInterpreter/Lua/persistent_state.test

Modified: 
    lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
    lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
    lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
    lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
index a908ef086735..dc64139fa4e5 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
@@ -12,6 +12,7 @@
 using namespace lldb_private;
 
 llvm::Error Lua::Run(llvm::StringRef buffer) {
+  std::lock_guard<std::mutex> lock(m_mutex);
   int error =
       luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer") ||
       lua_pcall(m_lua_state, 0, 0, 0);

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
index 073e7e22e7d6..ed1d159590ac 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
@@ -14,6 +14,8 @@
 
 #include "lua.hpp"
 
+#include <mutex>
+
 namespace lldb_private {
 
 extern "C" {
@@ -36,6 +38,7 @@ class Lua {
   llvm::Error Run(llvm::StringRef buffer);
 
 private:
+  std::mutex m_mutex;
   lua_State *m_lua_state;
 };
 

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
index b3f1689909fd..d5423b78b8c4 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
@@ -22,33 +22,34 @@ using namespace lldb_private;
 class IOHandlerLuaInterpreter : public IOHandlerDelegate,
                                 public IOHandlerEditline {
 public:
-  IOHandlerLuaInterpreter(Debugger &debugger)
+  IOHandlerLuaInterpreter(Debugger &debugger,
+                          ScriptInterpreterLua &script_interpreter)
       : IOHandlerEditline(debugger, IOHandler::Type::LuaInterpreter, "lua",
                           ">>> ", "..> ", true, debugger.GetUseColor(), 0,
                           *this, nullptr),
-        m_lua() {}
+        m_script_interpreter(script_interpreter) {}
 
   void IOHandlerInputComplete(IOHandler &io_handler,
                               std::string &data) override {
-    if (llvm::Error error = m_lua.Run(data)) {
+    if (llvm::Error error = m_script_interpreter.GetLua().Run(data)) {
       *GetOutputStreamFileSP() << llvm::toString(std::move(error));
     }
   }
 
 private:
-  Lua m_lua;
+  ScriptInterpreterLua &m_script_interpreter;
 };
 
 ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger)
-    : ScriptInterpreter(debugger, eScriptLanguageLua) {}
+    : ScriptInterpreter(debugger, eScriptLanguageLua),
+      m_lua(std::make_unique<Lua>()) {}
 
 ScriptInterpreterLua::~ScriptInterpreterLua() {}
 
 bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command,
                                           CommandReturnObject *result,
                                           const ExecuteScriptOptions &options) {
-  Lua l;
-  if (llvm::Error e = l.Run(command)) {
+  if (llvm::Error e = m_lua->Run(command)) {
     result->AppendErrorWithFormatv(
         "lua failed attempting to evaluate '{0}': {1}\n", command,
         llvm::toString(std::move(e)));
@@ -72,7 +73,7 @@ void ScriptInterpreterLua::ExecuteInterpreterLoop() {
   if (!debugger.GetInputFile().IsValid())
     return;
 
-  IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(debugger));
+  IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(debugger, *this));
   debugger.PushIOHandler(io_handler_sp);
 }
 
@@ -107,3 +108,5 @@ lldb_private::ConstString ScriptInterpreterLua::GetPluginName() {
 }
 
 uint32_t ScriptInterpreterLua::GetPluginVersion() { return 1; }
+
+Lua &ScriptInterpreterLua::GetLua() { return *m_lua; }

diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
index 7f69a73f5882..b34c7d0e8217 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
@@ -12,7 +12,7 @@
 #include "lldb/Interpreter/ScriptInterpreter.h"
 
 namespace lldb_private {
-
+class Lua;
 class ScriptInterpreterLua : public ScriptInterpreter {
 public:
   ScriptInterpreterLua(Debugger &debugger);
@@ -40,6 +40,11 @@ class ScriptInterpreterLua : public ScriptInterpreter {
   lldb_private::ConstString GetPluginName() override;
 
   uint32_t GetPluginVersion() override;
+
+  Lua &GetLua();
+
+private:
+  std::unique_ptr<Lua> m_lua;
 };
 
 } // namespace lldb_private

diff  --git a/lldb/test/Shell/ScriptInterpreter/Lua/persistent_state.test b/lldb/test/Shell/ScriptInterpreter/Lua/persistent_state.test
new file mode 100644
index 000000000000..4cdea152fdae
--- /dev/null
+++ b/lldb/test/Shell/ScriptInterpreter/Lua/persistent_state.test
@@ -0,0 +1,3 @@
+# REQUIRES: lua
+# RUN: %lldb --script-language lua -o 'script foo = 1010' -o 'script bar = 101' -o 'script print(foo+bar)' 2>&1 | FileCheck %s
+# CHECK: 1111


        


More information about the lldb-commits mailing list