[Lldb-commits] [PATCH] D82412: [lldb/Lua] Redirect Lua stdout/stderr to the CommandReturnObject

Jonas Devlieghere via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Jun 23 14:32:13 PDT 2020


JDevlieghere created this revision.
JDevlieghere added reviewers: labath, LLDB.
JDevlieghere added a parent revision: D82396: [lldb/ScriptInterpreter] Extract IO redirection logic and move it out of ScriptInterpreterPython (NFC).

Redirect the output of stdout and stderr to the CommandReturnObject for one line commands.


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D82412

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


Index: lldb/test/Shell/ScriptInterpreter/Lua/io.test
===================================================================
--- lldb/test/Shell/ScriptInterpreter/Lua/io.test
+++ lldb/test/Shell/ScriptInterpreter/Lua/io.test
@@ -1,6 +1,7 @@
 # REQUIRES: lua
 # UNSUPPORTED: lldb-repro
 #
+# RUN: rm -rf %t.stderr %t.stdout
 # 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
@@ -11,6 +12,11 @@
 quit
 script
 io.write(95000 + 14, "\n")
+
 # STDOUT: 95126
 # STDOUT-NOT: 95014
 # STDERR: 95014
+
+# RUN: rm -rf %t.stderr %t.stdout
+# RUN: %lldb --script-language lua -o 'script io.stderr:write(95000 + 126, "\n")' 2> %t.stderr > %t.stdout
+# RUN: cat %t.stdout | FileCheck %s --check-prefix STDOUT
Index: lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
@@ -65,12 +65,45 @@
 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;
+  }
+
+  std::unique_ptr<ScriptInterpreterIORedirect> io_redirect;
+  if (options.GetEnableIO()) {
+    io_redirect =
+        std::make_unique<ScriptInterpreterIORedirect>(m_debugger, result);
+  } else {
+    llvm::Error error = llvm::Error::success();
+    io_redirect = std::make_unique<ScriptInterpreterIORedirect>(error);
+    if (error) {
+      if (result)
+        result->AppendErrorWithFormatv("failed to open /dev/null: {0}\n",
+                                        llvm::toString(std::move(error)));
+      else
+        llvm::consumeError(std::move(error));
+      return false;
+    }
+  }
+
+  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;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82412.272823.patch
Type: text/x-patch
Size: 2684 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20200623/1cec1ef2/attachment-0001.bin>


More information about the lldb-commits mailing list