Index: lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h =================================================================== --- lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h +++ lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h @@ -9,6 +9,7 @@ #ifndef liblldb_Lua_h_ #define liblldb_Lua_h_ +#include "lldb/lldb-types.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" @@ -35,6 +36,7 @@ luaL_openlibs(m_lua_state); } + llvm::Error EnterSession(lldb::user_id_t debugger_id); llvm::Error Run(llvm::StringRef buffer); private: Index: lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp =================================================================== --- lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp +++ lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp @@ -10,6 +10,7 @@ #include "llvm/Support/FormatVariadic.h" using namespace lldb_private; +using namespace lldb; llvm::Error Lua::Run(llvm::StringRef buffer) { std::lock_guard lock(m_mutex); @@ -26,3 +27,13 @@ lua_pop(m_lua_state, 1); return e; } + +llvm::Error Lua::EnterSession(user_id_t debugger_id) { + const char *fmt_str = + "lldb.debugger = lldb.SBDebugger.FindDebuggerWithID({0}); " + "lldb.target = lldb.debugger:GetSelectedTarget(); " + "lldb.process = lldb.target:GetProcess(); " + "lldb.thread = lldb.process:GetSelectedThread(); " + "lldb.frame = lldb.thread:GetSelectedFrame()"; + return Run(llvm::formatv(fmt_str, debugger_id).str()); +} Index: lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp =================================================================== --- lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp +++ lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp @@ -42,7 +42,9 @@ ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger) : ScriptInterpreter(debugger, eScriptLanguageLua), - m_lua(std::make_unique()) {} + m_lua(std::make_unique()) { + llvm::cantFail(GetLua().EnterSession(debugger.GetID())); +} ScriptInterpreterLua::~ScriptInterpreterLua() {} Index: lldb/test/Shell/ScriptInterpreter/Lua/Inputs/independent_state.in =================================================================== --- /dev/null +++ lldb/test/Shell/ScriptInterpreter/Lua/Inputs/independent_state.in @@ -0,0 +1,6 @@ +script foobar = 40 + 7 +script print(foobar) +script d = lldb.SBDebugger.Create() +script d:HandleCommand("script foobar = 40 + 2") +script print(foobar) +script d:HandleCommand("script print(foobar)") Index: lldb/test/Shell/ScriptInterpreter/Lua/convenience_variables.test =================================================================== --- /dev/null +++ lldb/test/Shell/ScriptInterpreter/Lua/convenience_variables.test @@ -0,0 +1,17 @@ +# REQUIRES: lua +# +# This tests that the convenience variables are not nil. Given that there is no +# target we only expect the debugger to be valid. +# +# RUN: cat %s | %lldb --script-language lua 2>&1 | FileCheck %s +script +print(string.format("lldb.debugger is valid: %s", lldb.debugger:IsValid())) +print(string.format("lldb.target is valid: %s", lldb.target:IsValid())) +print(string.format("lldb.process is valid: %s", lldb.process:IsValid())) +print(string.format("lldb.thread is valid: %s", lldb.thread:IsValid())) +print(string.format("lldb.frame is valid: %s", lldb.frame:IsValid())) +# CHECK: debugger is valid: true +# CHECK: target is valid: false +# CHECK: process is valid: false +# CHECK: thread is valid: false +# CHECK: frame is valid: false Index: lldb/test/Shell/ScriptInterpreter/Lua/independent_state.test =================================================================== --- /dev/null +++ lldb/test/Shell/ScriptInterpreter/Lua/independent_state.test @@ -0,0 +1,6 @@ +# REQUIRES: lua +# +# RUN: %lldb --script-language lua -s %S/Inputs/independent_state.in 2>&1 | FileCheck %s +# CHECK: 47 +# CHECK: 47 +# CHECK: 42