[Lldb-commits] [lldb] ed8184b - [lldb/Lua] Redirect Lua stdout/stderr to the CommandReturnObject

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Thu Jun 25 09:55:56 PDT 2020


Author: Jonas Devlieghere
Date: 2020-06-25T09:55:46-07:00
New Revision: ed8184b7814df4310dbad065a9a1c3bb8f3bfa86

URL: https://github.com/llvm/llvm-project/commit/ed8184b7814df4310dbad065a9a1c3bb8f3bfa86
DIFF: https://github.com/llvm/llvm-project/commit/ed8184b7814df4310dbad065a9a1c3bb8f3bfa86.diff

LOG: [lldb/Lua] Redirect Lua stdout/stderr to the CommandReturnObject

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

Differential revision: https://reviews.llvm.org/D82412

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
index 30a50e1c9888..8cbeac4563c3 100644
--- a/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
+++ b/lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
@@ -15,6 +15,7 @@
 #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;
@@ -65,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;
 }
 

diff  --git a/lldb/test/Shell/ScriptInterpreter/Lua/io.test b/lldb/test/Shell/ScriptInterpreter/Lua/io.test
index fecdd344b885..af86253f90f2 100644
--- a/lldb/test/Shell/ScriptInterpreter/Lua/io.test
+++ b/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 @@ io.write(95000 + 126, "\n")
 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


        


More information about the lldb-commits mailing list