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(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() { 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 Debugger &debugger = m_debugger; 74 75 // At the moment, the only time the debugger does not have an input file 76 // handle is when this is called directly from lua, in which case it is 77 // both dangerous and unnecessary (not to mention confusing) to try to embed 78 // a running interpreter loop inside the already running lua interpreter 79 // loop, so we won't do it. 80 81 if (!debugger.GetInputFile().IsValid()) 82 return; 83 84 IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(debugger, *this)); 85 debugger.RunIOHandlerAsync(io_handler_sp); 86 } 87 88 bool ScriptInterpreterLua::LoadScriptingModule( 89 const char *filename, bool init_session, lldb_private::Status &error, 90 StructuredData::ObjectSP *module_sp) { 91 92 if (llvm::Error e = m_lua->LoadModule(filename)) { 93 error.SetErrorStringWithFormatv("lua failed to import '{0}': {1}\n", 94 filename, llvm::toString(std::move(e))); 95 return false; 96 } 97 return true; 98 } 99 100 void ScriptInterpreterLua::Initialize() { 101 static llvm::once_flag g_once_flag; 102 103 llvm::call_once(g_once_flag, []() { 104 PluginManager::RegisterPlugin(GetPluginNameStatic(), 105 GetPluginDescriptionStatic(), 106 lldb::eScriptLanguageLua, CreateInstance); 107 }); 108 } 109 110 void ScriptInterpreterLua::Terminate() {} 111 112 llvm::Error ScriptInterpreterLua::EnterSession(user_id_t debugger_id) { 113 if (m_session_is_active) 114 return llvm::Error::success(); 115 116 const char *fmt_str = 117 "lldb.debugger = lldb.SBDebugger.FindDebuggerWithID({0}); " 118 "lldb.target = lldb.debugger:GetSelectedTarget(); " 119 "lldb.process = lldb.target:GetProcess(); " 120 "lldb.thread = lldb.process:GetSelectedThread(); " 121 "lldb.frame = lldb.thread:GetSelectedFrame()"; 122 return m_lua->Run(llvm::formatv(fmt_str, debugger_id).str()); 123 } 124 125 llvm::Error ScriptInterpreterLua::LeaveSession() { 126 if (!m_session_is_active) 127 return llvm::Error::success(); 128 129 m_session_is_active = false; 130 131 llvm::StringRef str = "lldb.debugger = nil; " 132 "lldb.target = nil; " 133 "lldb.process = nil; " 134 "lldb.thread = nil; " 135 "lldb.frame = nil"; 136 return m_lua->Run(str); 137 } 138 139 lldb::ScriptInterpreterSP 140 ScriptInterpreterLua::CreateInstance(Debugger &debugger) { 141 return std::make_shared<ScriptInterpreterLua>(debugger); 142 } 143 144 lldb_private::ConstString ScriptInterpreterLua::GetPluginNameStatic() { 145 static ConstString g_name("script-lua"); 146 return g_name; 147 } 148 149 const char *ScriptInterpreterLua::GetPluginDescriptionStatic() { 150 return "Lua script interpreter"; 151 } 152 153 lldb_private::ConstString ScriptInterpreterLua::GetPluginName() { 154 return GetPluginNameStatic(); 155 } 156 157 uint32_t ScriptInterpreterLua::GetPluginVersion() { return 1; } 158 159 Lua &ScriptInterpreterLua::GetLua() { return *m_lua; } 160