Author: Jonas Devlieghere Date: 2020-06-23T09:05:51-07:00 New Revision: fa1b4a96a0110c87cdd83eb4ea3e9a0d002c3c3d
URL: https://github.com/llvm/llvm-project/commit/fa1b4a96a0110c87cdd83eb4ea3e9a0d002c3c3d DIFF: https://github.com/llvm/llvm-project/commit/fa1b4a96a0110c87cdd83eb4ea3e9a0d002c3c3d.diff LOG: [lldb/Lua] Use the debugger's output and error file for Lua's I/O library. Add support for changing the stdout and stderr file in Lua's I/O library and hook it up with the debugger's output and error file respectively for the interactive Lua interpreter. https://reviews.llvm.org/D82273 Added: lldb/test/Shell/ScriptInterpreter/Lua/io.test Modified: lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp Removed: ################################################################################ diff --git a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp index ecee8cc674f8..4c3cde9a11ec 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, "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 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 f2984a925dfe..300115aac8a7 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h @@ -38,6 +38,7 @@ class Lua { 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 f8fd8ff94aa3..30a50e1c9888 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp @@ -30,6 +30,9 @@ class IOHandlerLuaInterpreter : public IOHandlerDelegate, ">>> ", "..> ", 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())); } diff --git a/lldb/test/Shell/ScriptInterpreter/Lua/io.test b/lldb/test/Shell/ScriptInterpreter/Lua/io.test new file mode 100644 index 000000000000..fecdd344b885 --- /dev/null +++ b/lldb/test/Shell/ScriptInterpreter/Lua/io.test @@ -0,0 +1,16 @@ +# REQUIRES: lua +# UNSUPPORTED: lldb-repro +# +# RUN: cat %s | %lldb --script-language lua 2> %t.stderr > %t.stdout +# RUN: cat %t.stdout | FileCheck %s --check-prefix STDOUT +# RUN: cat %t.stderr | FileCheck %s --check-prefix STDERR +script +file = lldb.SBFile(2, "w", false) +lldb.debugger:SetOutputFile(file) +io.write(95000 + 126, "\n") +quit +script +io.write(95000 + 14, "\n") +# STDOUT: 95126 +# STDOUT-NOT: 95014 +# STDERR: 95014 _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits