diff options
Diffstat (limited to 'lldb/source/Plugins/ScriptInterpreter/Lua')
4 files changed, 82 insertions, 9 deletions
diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp index ecee8cc674f81..acd6128d84c5a 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp @@ -57,3 +57,35 @@ llvm::Error Lua::LoadModule(llvm::StringRef filename) { lua_setglobal(m_lua_state, module_name.GetCString()); return llvm::Error::success(); } + +llvm::Error Lua::ChangeIO(FILE *out, FILE *err) { + assert(out != nullptr); + assert(err != nullptr); + + lua_getglobal(m_lua_state, "io"); + + lua_getfield(m_lua_state, -1, "stdout"); + if (luaL_Stream *s = static_cast<luaL_Stream *>( + luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) { + s->f = out; + lua_pop(m_lua_state, 1); + } else { + lua_pop(m_lua_state, 2); + return llvm::make_error<llvm::StringError>("could not get stdout", + llvm::inconvertibleErrorCode()); + } + + lua_getfield(m_lua_state, -1, "stderr"); + if (luaL_Stream *s = static_cast<luaL_Stream *>( + luaL_testudata(m_lua_state, -1, LUA_FILEHANDLE))) { + s->f = out; + lua_pop(m_lua_state, 1); + } else { + lua_pop(m_lua_state, 2); + return llvm::make_error<llvm::StringError>("could not get stderr", + llvm::inconvertibleErrorCode()); + } + + lua_pop(m_lua_state, 1); + return llvm::Error::success(); +} diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h index f2984a925dfe1..300115aac8a7d 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h @@ -38,6 +38,7 @@ public: llvm::Error Run(llvm::StringRef buffer); llvm::Error LoadModule(llvm::StringRef filename); + llvm::Error ChangeIO(FILE *out, FILE *err); private: lua_State *m_lua_state; diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp index 701d68d1ec084..8cbeac4563c30 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp @@ -1,4 +1,4 @@ -//===-- ScriptInterpreterLua.cpp --------------------------------*- C++ -*-===// +//===-- ScriptInterpreterLua.cpp ------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -15,10 +15,13 @@ #include "lldb/Utility/Stream.h" #include "lldb/Utility/StringList.h" #include "lldb/Utility/Timer.h" +#include "llvm/Support/FormatAdapters.h" using namespace lldb; using namespace lldb_private; +LLDB_PLUGIN_DEFINE(ScriptInterpreterLua) + class IOHandlerLuaInterpreter : public IOHandlerDelegate, public IOHandlerEditline { public: @@ -28,15 +31,23 @@ public: ">>> ", "..> ", true, debugger.GetUseColor(), 0, *this, nullptr), m_script_interpreter(script_interpreter) { + llvm::cantFail(m_script_interpreter.GetLua().ChangeIO( + debugger.GetOutputFile().GetStream(), + debugger.GetErrorFile().GetStream())); llvm::cantFail(m_script_interpreter.EnterSession(debugger.GetID())); } - ~IOHandlerLuaInterpreter() { + ~IOHandlerLuaInterpreter() override { llvm::cantFail(m_script_interpreter.LeaveSession()); } void IOHandlerInputComplete(IOHandler &io_handler, std::string &data) override { + if (llvm::StringRef(data).rtrim() == "quit") { + io_handler.SetIsDone(true); + return; + } + if (llvm::Error error = m_script_interpreter.GetLua().Run(data)) { *GetOutputStreamFileSP() << llvm::toString(std::move(error)); } @@ -55,12 +66,43 @@ ScriptInterpreterLua::~ScriptInterpreterLua() {} bool ScriptInterpreterLua::ExecuteOneLine(llvm::StringRef command, CommandReturnObject *result, const ExecuteScriptOptions &options) { + if (command.empty()) { + if (result) + result->AppendError("empty command passed to lua\n"); + return false; + } + + llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>> + io_redirect_or_error = ScriptInterpreterIORedirect::Create( + options.GetEnableIO(), m_debugger, result); + if (!io_redirect_or_error) { + if (result) + result->AppendErrorWithFormatv( + "failed to redirect I/O: {0}\n", + llvm::fmt_consume(io_redirect_or_error.takeError())); + else + llvm::consumeError(io_redirect_or_error.takeError()); + return false; + } + + ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error; + + if (llvm::Error e = + m_lua->ChangeIO(io_redirect.GetOutputFile()->GetStream(), + io_redirect.GetErrorFile()->GetStream())) { + result->AppendErrorWithFormatv("lua failed to redirect I/O: {0}\n", + llvm::toString(std::move(e))); + return false; + } + 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; } + + io_redirect.Flush(); return true; } @@ -68,25 +110,23 @@ 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()) + if (!m_debugger.GetInputFile().IsValid()) return; - IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(debugger, *this)); - debugger.PushIOHandler(io_handler_sp); + IOHandlerSP io_handler_sp(new IOHandlerLuaInterpreter(m_debugger, *this)); + m_debugger.RunIOHandlerAsync(io_handler_sp); } bool ScriptInterpreterLua::LoadScriptingModule( const char *filename, bool init_session, lldb_private::Status &error, StructuredData::ObjectSP *module_sp) { + FileSystem::Instance().Collect(filename); if (llvm::Error e = m_lua->LoadModule(filename)) { error.SetErrorStringWithFormatv("lua failed to import '{0}': {1}\n", filename, llvm::toString(std::move(e))); diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h index 4e922151385b6..bcc6ab24f6d0b 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h @@ -25,7 +25,7 @@ public: void ExecuteInterpreterLoop() override; - virtual bool + bool LoadScriptingModule(const char *filename, bool init_session, lldb_private::Status &error, StructuredData::ObjectSP *module_sp = nullptr) override; |