diff options
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter/Lua')
4 files changed, 325 insertions, 0 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp new file mode 100644 index 0000000000000..ecee8cc674f81 --- /dev/null +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp @@ -0,0 +1,59 @@ +//===-- Lua.cpp -----------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "Lua.h" +#include "lldb/Host/FileSystem.h" +#include "lldb/Utility/FileSpec.h" +#include "llvm/Support/FormatVariadic.h" + +using namespace lldb_private; +using namespace lldb; + +llvm::Error Lua::Run(llvm::StringRef buffer) { + int error = + luaL_loadbuffer(m_lua_state, buffer.data(), buffer.size(), "buffer") || + lua_pcall(m_lua_state, 0, 0, 0); + if (!error) + return llvm::Error::success(); + + llvm::Error e = llvm::make_error<llvm::StringError>( + llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)), + llvm::inconvertibleErrorCode()); + // Pop error message from the stack. + lua_pop(m_lua_state, 1); + return e; +} + +llvm::Error Lua::LoadModule(llvm::StringRef filename) { + FileSpec file(filename); + if (!FileSystem::Instance().Exists(file)) { + return llvm::make_error<llvm::StringError>("invalid path", + llvm::inconvertibleErrorCode()); + } + + ConstString module_extension = file.GetFileNameExtension(); + if (module_extension != ".lua") { + return llvm::make_error<llvm::StringError>("invalid extension", + llvm::inconvertibleErrorCode()); + } + + int error = luaL_loadfile(m_lua_state, filename.data()) || + lua_pcall(m_lua_state, 0, 1, 0); + if (error) { + llvm::Error e = llvm::make_error<llvm::StringError>( + llvm::formatv("{0}\n", lua_tostring(m_lua_state, -1)), + llvm::inconvertibleErrorCode()); + // Pop error message from the stack. + lua_pop(m_lua_state, 1); + return e; + } + + ConstString module_name = file.GetFileNameStrippingExtension(); + lua_setglobal(m_lua_state, module_name.GetCString()); + return llvm::Error::success(); +} diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h new file mode 100644 index 0000000000000..f2984a925dfe1 --- /dev/null +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h @@ -0,0 +1,48 @@ +//===-- ScriptInterpreterLua.h ----------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Lua_h_ +#define liblldb_Lua_h_ + +#include "lldb/lldb-types.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Error.h" + +#include "lua.hpp" + +#include <mutex> + +namespace lldb_private { + +extern "C" { +int luaopen_lldb(lua_State *L); +} + +class Lua { +public: + Lua() : m_lua_state(luaL_newstate()) { + assert(m_lua_state); + luaL_openlibs(m_lua_state); + luaopen_lldb(m_lua_state); + } + + ~Lua() { + assert(m_lua_state); + luaL_openlibs(m_lua_state); + } + + llvm::Error Run(llvm::StringRef buffer); + llvm::Error LoadModule(llvm::StringRef filename); + +private: + lua_State *m_lua_state; +}; + +} // namespace lldb_private + +#endif // liblldb_Lua_h_ diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp new file mode 100644 index 0000000000000..701d68d1ec084 --- /dev/null +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp @@ -0,0 +1,157 @@ +//===-- ScriptInterpreterLua.cpp --------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "ScriptInterpreterLua.h" +#include "Lua.h" +#include "lldb/Core/Debugger.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/StreamFile.h" +#include "lldb/Interpreter/CommandReturnObject.h" +#include "lldb/Utility/Stream.h" +#include "lldb/Utility/StringList.h" +#include "lldb/Utility/Timer.h" + +using namespace lldb; +using namespace lldb_private; + +class IOHandlerLuaInterpreter : public IOHandlerDelegate, + public IOHandlerEditline { +public: + IOHandlerLuaInterpreter(Debugger &debugger, + ScriptInterpreterLua &script_interpreter) + : IOHandlerEditline(debugger, IOHandler::Type::LuaInterpreter, "lua", + ">>> ", "..> ", true, debugger.GetUseColor(), 0, + *this, nullptr), + m_script_interpreter(script_interpreter) { + llvm::cantFail(m_script_interpreter.EnterSession(debugger.GetID())); + } + + ~IOHandlerLuaInterpreter() { + llvm::cantFail(m_script_interpreter.LeaveSession()); + } + + void IOHandlerInputComplete(IOHandler &io_handler, + std::string &data) override { + if (llvm::Error error = m_script_interpreter.GetLua().Run(data)) { + *GetOutputStreamFileSP() << llvm::toString(std::move(error)); + } + } + +private: + ScriptInterpreterLua &m_script_interpreter; +}; + +ScriptInterpreterLua::ScriptInterpreterLua(Debugger &debugger) + : ScriptInterpreter(debugger, eScriptLanguageLua), + m_lua(std::make_unique<Lua>()) {} + +ScriptInterpreterLua::~ScriptInterpreterLua() {} + +bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command, + CommandReturnObject *result, + const ExecuteScriptOptions &options) { + if (llvm::Error e = m_lua->Run(command)) { + result->AppendErrorWithFormatv( + "lua failed attempting to evaluate '{0}': {1}\n", command, + llvm::toString(std::move(e))); + return false; + } + return true; +} + +void ScriptInterpreterLua::ExecuteInterpreterLoop() { + static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); + Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); + + Debugger &debugger = m_debugger; + + // At the moment, the only time the debugger does not have an input file + // handle is when this is called directly from lua, in which case it is + // both dangerous and unnecessary (not to mention confusing) to try to embed + // a running interpreter loop inside the already running lua interpreter + // loop, so we won't do it. + + if (!debugger.GetInputFile().IsValid()) + return; + + IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(debugger, *this)); + debugger.PushIOHandler(io_handler_sp); +} + +bool ScriptInterpreterLua::LoadScriptingModule( + const char *filename, bool init_session, lldb_private::Status &error, + StructuredData::ObjectSP *module_sp) { + + if (llvm::Error e = m_lua->LoadModule(filename)) { + error.SetErrorStringWithFormatv("lua failed to import '{0}': {1}\n", + filename, llvm::toString(std::move(e))); + return false; + } + return true; +} + +void ScriptInterpreterLua::Initialize() { + static llvm::once_flag g_once_flag; + + llvm::call_once(g_once_flag, []() { + PluginManager::RegisterPlugin(GetPluginNameStatic(), + GetPluginDescriptionStatic(), + lldb::eScriptLanguageLua, CreateInstance); + }); +} + +void ScriptInterpreterLua::Terminate() {} + +llvm::Error ScriptInterpreterLua::EnterSession(user_id_t debugger_id) { + if (m_session_is_active) + return llvm::Error::success(); + + 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 m_lua->Run(llvm::formatv(fmt_str, debugger_id).str()); +} + +llvm::Error ScriptInterpreterLua::LeaveSession() { + if (!m_session_is_active) + return llvm::Error::success(); + + m_session_is_active = false; + + llvm::StringRef str = "lldb.debugger = nil; " + "lldb.target = nil; " + "lldb.process = nil; " + "lldb.thread = nil; " + "lldb.frame = nil"; + return m_lua->Run(str); +} + +lldb::ScriptInterpreterSP +ScriptInterpreterLua::CreateInstance(Debugger &debugger) { + return std::make_shared<ScriptInterpreterLua>(debugger); +} + +lldb_private::ConstString ScriptInterpreterLua::GetPluginNameStatic() { + static ConstString g_name("script-lua"); + return g_name; +} + +const char *ScriptInterpreterLua::GetPluginDescriptionStatic() { + return "Lua script interpreter"; +} + +lldb_private::ConstString ScriptInterpreterLua::GetPluginName() { + return GetPluginNameStatic(); +} + +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 new file mode 100644 index 0000000000000..4e922151385b6 --- /dev/null +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h @@ -0,0 +1,61 @@ +//===-- ScriptInterpreterLua.h ----------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ScriptInterpreterLua_h_ +#define liblldb_ScriptInterpreterLua_h_ + +#include "lldb/Interpreter/ScriptInterpreter.h" + +namespace lldb_private { +class Lua; +class ScriptInterpreterLua : public ScriptInterpreter { +public: + ScriptInterpreterLua(Debugger &debugger); + + ~ScriptInterpreterLua() override; + + bool ExecuteOneLine( + llvm::StringRef command, CommandReturnObject *result, + const ExecuteScriptOptions &options = ExecuteScriptOptions()) override; + + void ExecuteInterpreterLoop() override; + + virtual bool + LoadScriptingModule(const char *filename, bool init_session, + lldb_private::Status &error, + StructuredData::ObjectSP *module_sp = nullptr) override; + + // Static Functions + static void Initialize(); + + static void Terminate(); + + static lldb::ScriptInterpreterSP CreateInstance(Debugger &debugger); + + static lldb_private::ConstString GetPluginNameStatic(); + + static const char *GetPluginDescriptionStatic(); + + // PluginInterface protocol + lldb_private::ConstString GetPluginName() override; + + uint32_t GetPluginVersion() override; + + Lua &GetLua(); + + llvm::Error EnterSession(lldb::user_id_t debugger_id); + llvm::Error LeaveSession(); + +private: + std::unique_ptr<Lua> m_lua; + bool m_session_is_active = false; +}; + +} // namespace lldb_private + +#endif // liblldb_ScriptInterpreterLua_h_ |