1 //===-- ScriptInterpreterLua.cpp ------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "ScriptInterpreterLua.h" 10 #include "Lua.h" 11 #include "lldb/Core/Debugger.h" 12 #include "lldb/Core/PluginManager.h" 13 #include "lldb/Core/StreamFile.h" 14 #include "lldb/Interpreter/CommandReturnObject.h" 15 #include "lldb/Utility/Stream.h" 16 #include "lldb/Utility/StringList.h" 17 #include "lldb/Utility/Timer.h" 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 LLDB_PLUGIN_DEFINE(ScriptInterpreterLua) 23 24 class IOHandlerLuaInterpreter : public IOHandlerDelegate, 25 public IOHandlerEditline { 26 public: 27 IOHandlerLuaInterpreter(Debugger &debugger, 28 ScriptInterpreterLua &script_interpreter) 29 : IOHandlerEditline(debugger, IOHandler::Type::LuaInterpreter, "lua", 30 ">>> ", "..> ", true, debugger.GetUseColor(), 0, 31 *this, nullptr), 32 m_script_interpreter(script_interpreter) { 33 llvm::cantFail(m_script_interpreter.EnterSession(debugger.GetID())); 34 } 35 36 ~IOHandlerLuaInterpreter() override { 37 llvm::cantFail(m_script_interpreter.LeaveSession()); 38 } 39 40 void IOHandlerInputComplete(IOHandler &io_handler, 41 std::string &data) override { 42 if (llvm::Error error = m_script_interpreter.GetLua().Run(data)) { 43 *GetOutputStreamFileSP() << llvm::toString(std::move(error)); 44 } 45 } 46 47 private: 48 ScriptInterpreterLua &m_script_interpreter; 49 }; 50 51 ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger) 52 : ScriptInterpreter(debugger, eScriptLanguageLua), 53 m_lua(std::make_unique<Lua>()) {} 54 55 ScriptInterpreterLua::~ScriptInterpreterLua() {} 56 57 bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command, 58 CommandReturnObject *result, 59 const ExecuteScriptOptions &options) { 60 if (llvm::Error e = m_lua->Run(command)) { 61 result->AppendErrorWithFormatv( 62 "lua failed attempting to evaluate '{0}': {1}\n", command, 63 llvm::toString(std::move(e))); 64 return false; 65 } 66 return true; 67 } 68 69 void ScriptInterpreterLua::ExecuteInterpreterLoop() { 70 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 71 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); 72 73 // At the moment, the only time the debugger does not have an input file 74 // handle is when this is called directly from lua, in which case it is 75 // both dangerous and unnecessary (not to mention confusing) to try to embed 76 // a running interpreter loop inside the already running lua interpreter 77 // loop, so we won't do it. 78 if (!m_debugger.GetInputFile().IsValid()) 79 return; 80 81 IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(m_debugger, *this)); 82 m_debugger.RunIOHandlerAsync(io_handler_sp); 83 } 84 85 bool ScriptInterpreterLua::LoadScriptingModule( 86 const char *filename, bool init_session, lldb_private::Status &error, 87 StructuredData::ObjectSP *module_sp) { 88 89 FileSystem::Instance().Collect(filename); 90 if (llvm::Error e = m_lua->LoadModule(filename)) { 91 error.SetErrorStringWithFormatv("lua failed to import '{0}': {1}\n", 92 filename, llvm::toString(std::move(e))); 93 return false; 94 } 95 return true; 96 } 97 98 void ScriptInterpreterLua::Initialize() { 99 static llvm::once_flag g_once_flag; 100 101 llvm::call_once(g_once_flag, []() { 102 PluginManager::RegisterPlugin(GetPluginNameStatic(), 103 GetPluginDescriptionStatic(), 104 lldb::eScriptLanguageLua, CreateInstance); 105 }); 106 } 107 108 void ScriptInterpreterLua::Terminate() {} 109 110 llvm::Error ScriptInterpreterLua::EnterSession(user_id_t debugger_id) { 111 if (m_session_is_active) 112 return llvm::Error::success(); 113 114 const char *fmt_str = 115 "lldb.debugger = lldb.SBDebugger.FindDebuggerWithID({0}); " 116 "lldb.target = lldb.debugger:GetSelectedTarget(); " 117 "lldb.process = lldb.target:GetProcess(); " 118 "lldb.thread = lldb.process:GetSelectedThread(); " 119 "lldb.frame = lldb.thread:GetSelectedFrame()"; 120 return m_lua->Run(llvm::formatv(fmt_str, debugger_id).str()); 121 } 122 123 llvm::Error ScriptInterpreterLua::LeaveSession() { 124 if (!m_session_is_active) 125 return llvm::Error::success(); 126 127 m_session_is_active = false; 128 129 llvm::StringRef str = "lldb.debugger = nil; " 130 "lldb.target = nil; " 131 "lldb.process = nil; " 132 "lldb.thread = nil; " 133 "lldb.frame = nil"; 134 return m_lua->Run(str); 135 } 136 137 lldb::ScriptInterpreterSP 138 ScriptInterpreterLua::CreateInstance(Debugger &debugger) { 139 return std::make_shared<ScriptInterpreterLua>(debugger); 140 } 141 142 lldb_private::ConstString ScriptInterpreterLua::GetPluginNameStatic() { 143 static ConstString g_name("script-lua"); 144 return g_name; 145 } 146 147 const char *ScriptInterpreterLua::GetPluginDescriptionStatic() { 148 return "Lua script interpreter"; 149 } 150 151 lldb_private::ConstString ScriptInterpreterLua::GetPluginName() { 152 return GetPluginNameStatic(); 153 } 154 155 uint32_t ScriptInterpreterLua::GetPluginVersion() { return 1; } 156 157 Lua &ScriptInterpreterLua::GetLua() { return *m_lua; } 158