[Lldb-commits] [lldb] 09dea54 - [lldb] Support a buffered logging mode

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Thu Jun 23 09:12:11 PDT 2022


Author: Jonas Devlieghere
Date: 2022-06-23T09:12:01-07:00
New Revision: 09dea546692f4e9f32bf16f1a9d5d0de52a64691

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

LOG: [lldb] Support a buffered logging mode

This patch adds a buffered logging mode to lldb. A buffer size can be
passed to `log enable` with the -b flag. If no buffer size is specified,
logging is unbuffered.

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

Added: 
    

Modified: 
    lldb/include/lldb/Core/Debugger.h
    lldb/include/lldb/Utility/Log.h
    lldb/source/API/SBDebugger.cpp
    lldb/source/Commands/CommandObjectLog.cpp
    lldb/source/Commands/Options.td
    lldb/source/Core/Debugger.cpp
    lldb/source/Utility/Log.cpp
    lldb/tools/lldb-test/lldb-test.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h
index 4cdc6b6237c7e..b5e6c30f5f77a 100644
--- a/lldb/include/lldb/Core/Debugger.h
+++ b/lldb/include/lldb/Core/Debugger.h
@@ -245,7 +245,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
   bool EnableLog(llvm::StringRef channel,
                  llvm::ArrayRef<const char *> categories,
                  llvm::StringRef log_file, uint32_t log_options,
-                 llvm::raw_ostream &error_stream);
+                 size_t buffer_size, llvm::raw_ostream &error_stream);
 
   void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);
 

diff  --git a/lldb/include/lldb/Utility/Log.h b/lldb/include/lldb/Utility/Log.h
index 806eb390773b9..3fd474d834489 100644
--- a/lldb/include/lldb/Utility/Log.h
+++ b/lldb/include/lldb/Utility/Log.h
@@ -58,9 +58,11 @@ class LogHandler {
 
 class StreamLogHandler : public LogHandler {
 public:
-  StreamLogHandler(int fd, bool should_close, bool unbuffered = true);
+  StreamLogHandler(int fd, bool should_close, size_t buffer_size = 0);
+  ~StreamLogHandler() override;
 
   void Emit(llvm::StringRef message) override;
+  void Flush();
 
 private:
   llvm::raw_fd_ostream m_stream;

diff  --git a/lldb/source/API/SBDebugger.cpp b/lldb/source/API/SBDebugger.cpp
index 37469989dc5b5..8aa2c74d9e378 100644
--- a/lldb/source/API/SBDebugger.cpp
+++ b/lldb/source/API/SBDebugger.cpp
@@ -1621,7 +1621,7 @@ bool SBDebugger::EnableLog(const char *channel, const char **categories) {
     std::string error;
     llvm::raw_string_ostream error_stream(error);
     return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
-                                  log_options, error_stream);
+                                  log_options, /*buffer_size=*/0, error_stream);
   } else
     return false;
 }

diff  --git a/lldb/source/Commands/CommandObjectLog.cpp b/lldb/source/Commands/CommandObjectLog.cpp
index c8df30849628a..190b262ed8edc 100644
--- a/lldb/source/Commands/CommandObjectLog.cpp
+++ b/lldb/source/Commands/CommandObjectLog.cpp
@@ -11,6 +11,7 @@
 #include "lldb/Host/OptionParser.h"
 #include "lldb/Interpreter/CommandReturnObject.h"
 #include "lldb/Interpreter/OptionArgParser.h"
+#include "lldb/Interpreter/OptionValueUInt64.h"
 #include "lldb/Interpreter/Options.h"
 #include "lldb/Utility/Args.h"
 #include "lldb/Utility/FileSpec.h"
@@ -21,7 +22,7 @@
 using namespace lldb;
 using namespace lldb_private;
 
-#define LLDB_OPTIONS_log
+#define LLDB_OPTIONS_log_enable
 #include "CommandOptions.inc"
 
 /// Common completion logic for log enable/disable.
@@ -89,6 +90,10 @@ class CommandObjectLogEnable : public CommandObjectParsed {
         log_file.SetFile(option_arg, FileSpec::Style::native);
         FileSystem::Instance().Resolve(log_file);
         break;
+      case 'b':
+        error =
+            buffer_size.SetValueFromString(option_arg, eVarSetOperationAssign);
+        break;
       case 't':
         log_options |= LLDB_LOG_OPTION_THREADSAFE;
         break;
@@ -125,16 +130,16 @@ class CommandObjectLogEnable : public CommandObjectParsed {
 
     void OptionParsingStarting(ExecutionContext *execution_context) override {
       log_file.Clear();
+      buffer_size.Clear();
       log_options = 0;
     }
 
     llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
-      return llvm::makeArrayRef(g_log_options);
+      return llvm::makeArrayRef(g_log_enable_options);
     }
 
-    // Instance variables to hold the values for command options.
-
     FileSpec log_file;
+    OptionValueUInt64 buffer_size;
     uint32_t log_options = 0;
   };
 
@@ -164,9 +169,9 @@ class CommandObjectLogEnable : public CommandObjectParsed {
 
     std::string error;
     llvm::raw_string_ostream error_stream(error);
-    bool success =
-        GetDebugger().EnableLog(channel, args.GetArgumentArrayRef(), log_file,
-                                m_options.log_options, error_stream);
+    bool success = GetDebugger().EnableLog(
+        channel, args.GetArgumentArrayRef(), log_file, m_options.log_options,
+        m_options.buffer_size.GetCurrentValue(), error_stream);
     result.GetErrorStream() << error_stream.str();
 
     if (success)

diff  --git a/lldb/source/Commands/Options.td b/lldb/source/Commands/Options.td
index 9da3ca36b5af6..f3c041eab0a32 100644
--- a/lldb/source/Commands/Options.td
+++ b/lldb/source/Commands/Options.td
@@ -428,9 +428,11 @@ let Command = "history" in {
     Desc<"Clears the current command history.">;
 }
 
-let Command = "log" in {
+let Command = "log enable" in {
   def log_file : Option<"file", "f">, Group<1>, Arg<"Filename">,
     Desc<"Set the destination file to log to.">;
+  def log_buffer_size : Option<"buffer", "b">, Group<1>, Arg<"UnsignedInteger">,
+    Desc<"Set the log to be buffered, using the specified buffer size.">;
   def log_threadsafe : Option<"threadsafe", "t">, Group<1>,
     Desc<"Enable thread safe logging to avoid interweaved log lines.">;
   def log_verbose : Option<"verbose", "v">, Group<1>,

diff  --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index aad3cc412baf0..0be7748bb326b 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -1409,7 +1409,7 @@ void Debugger::ReportError(std::string message,
 bool Debugger::EnableLog(llvm::StringRef channel,
                          llvm::ArrayRef<const char *> categories,
                          llvm::StringRef log_file, uint32_t log_options,
-                         llvm::raw_ostream &error_stream) {
+                         size_t buffer_size, llvm::raw_ostream &error_stream) {
   const bool should_close = true;
 
   std::shared_ptr<LogHandler> log_handler_sp;
@@ -1420,7 +1420,7 @@ bool Debugger::EnableLog(llvm::StringRef channel,
         LLDB_LOG_OPTION_PREPEND_TIMESTAMP | LLDB_LOG_OPTION_PREPEND_THREAD_NAME;
   } else if (log_file.empty()) {
     log_handler_sp = std::make_shared<StreamLogHandler>(
-        GetOutputFile().GetDescriptor(), !should_close);
+        GetOutputFile().GetDescriptor(), !should_close, buffer_size);
   } else {
     auto pos = m_stream_handlers.find(log_file);
     if (pos != m_stream_handlers.end())
@@ -1441,7 +1441,7 @@ bool Debugger::EnableLog(llvm::StringRef channel,
       }
 
       log_handler_sp = std::make_shared<StreamLogHandler>(
-          (*file)->GetDescriptor(), should_close);
+          (*file)->GetDescriptor(), should_close, buffer_size);
       m_stream_handlers[log_file] = log_handler_sp;
     }
   }

diff  --git a/lldb/source/Utility/Log.cpp b/lldb/source/Utility/Log.cpp
index 6649dc0b9f730..37704b7663bbf 100644
--- a/lldb/source/Utility/Log.cpp
+++ b/lldb/source/Utility/Log.cpp
@@ -339,14 +339,19 @@ void LogHandler::EmitThreadSafe(llvm::StringRef message) {
   Emit(message);
 }
 
-StreamLogHandler::StreamLogHandler(int fd, bool should_close, bool unbuffered)
-    : m_stream(fd, should_close, unbuffered) {}
-
-void StreamLogHandler::Emit(llvm::StringRef message) {
-  m_stream << message;
-  m_stream.flush();
+StreamLogHandler::StreamLogHandler(int fd, bool should_close,
+                                   size_t buffer_size)
+    : m_stream(fd, should_close, buffer_size == 0) {
+  if (buffer_size > 0)
+    m_stream.SetBufferSize(buffer_size);
 }
 
+StreamLogHandler::~StreamLogHandler() { Flush(); }
+
+void StreamLogHandler::Flush() { m_stream.flush(); }
+
+void StreamLogHandler::Emit(llvm::StringRef message) { m_stream << message; }
+
 CallbackLogHandler::CallbackLogHandler(lldb::LogOutputCallback callback,
                                        void *baton)
     : m_callback(callback), m_baton(baton) {}

diff  --git a/lldb/tools/lldb-test/lldb-test.cpp b/lldb/tools/lldb-test/lldb-test.cpp
index 1109a6bb65580..ce2b9542930c1 100644
--- a/lldb/tools/lldb-test/lldb-test.cpp
+++ b/lldb/tools/lldb-test/lldb-test.cpp
@@ -1120,7 +1120,7 @@ int main(int argc, const char *argv[]) {
       /*add_to_history*/ eLazyBoolNo, Result);
 
   if (!opts::Log.empty())
-    Dbg->EnableLog("lldb", {"all"}, opts::Log, 0, errs());
+    Dbg->EnableLog("lldb", {"all"}, opts::Log, 0, 0, errs());
 
   if (opts::BreakpointSubcommand)
     return opts::breakpoint::evaluateBreakpoints(*Dbg);


        


More information about the lldb-commits mailing list