[Lldb-commits] [PATCH] D82273: [lldb/Lua] Use the debugger's output and error file for Lua's I/O library.

Jonas Devlieghere via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Sun Jun 21 01:01:48 PDT 2020


JDevlieghere created this revision.
JDevlieghere added reviewers: labath, LLDB.

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.


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D82273

Files:
  lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
  lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
  lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
  lldb/test/Shell/ScriptInterpreter/Lua/io.test


Index: lldb/test/Shell/ScriptInterpreter/Lua/io.test
===================================================================
--- /dev/null
+++ 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
Index: lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
@@ -30,6 +30,9 @@
                           ">>> ", "..> ", 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()));
   }
 
Index: lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
+++ lldb/source/Plugins/ScriptInterpreter/Lua/Lua.h
@@ -38,6 +38,7 @@
 
   llvm::Error Run(llvm::StringRef buffer);
   llvm::Error LoadModule(llvm::StringRef filename);
+  llvm::Error ChangeIO(FILE *out = nullptr, FILE *err = nullptr);
 
 private:
   lua_State *m_lua_state;
Index: lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Lua/Lua.cpp
@@ -57,3 +57,35 @@
   lua_setglobal(m_lua_state, module_name.GetCString());
   return llvm::Error::success();
 }
+
+llvm::Error Lua::ChangeIO(FILE *out, FILE *err) {
+  lua_getglobal(m_lua_state, "io");
+
+  if (out) {
+    lua_pushstring(m_lua_state, "stdout");
+    lua_gettable(m_lua_state, -2);
+    luaL_Stream *s =
+        static_cast<luaL_Stream *>(lua_touserdata(m_lua_state, -1));
+    if (!s)
+      return llvm::make_error<llvm::StringError>(
+          "could not get stdout", llvm::inconvertibleErrorCode());
+    s->f = out;
+
+    lua_pop(m_lua_state, 1);
+  }
+
+  if (err) {
+    lua_pushstring(m_lua_state, "stderr");
+    lua_gettable(m_lua_state, -2);
+    luaL_Stream *s =
+        static_cast<luaL_Stream *>(lua_touserdata(m_lua_state, -1));
+    if (!s)
+      return llvm::make_error<llvm::StringError>(
+          "could not get stderr", llvm::inconvertibleErrorCode());
+    s->f = out;
+    lua_pop(m_lua_state, 1);
+  }
+
+  lua_pop(m_lua_state, 1);
+  return llvm::Error::success();
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82273.272293.patch
Type: text/x-patch
Size: 3050 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20200621/8785d2b9/attachment.bin>


More information about the lldb-commits mailing list