Index: lldb/include/lldb/Utility/Diagnostics.h =================================================================== --- lldb/include/lldb/Utility/Diagnostics.h +++ lldb/include/lldb/Utility/Diagnostics.h @@ -43,6 +43,7 @@ static Diagnostics &Instance(); static void Initialize(); static void Terminate(); + static bool Enabled(); /// Create a unique diagnostic directory. static llvm::Expected CreateUniqueDirectory(); Index: lldb/source/Core/Debugger.cpp =================================================================== --- lldb/source/Core/Debugger.cpp +++ lldb/source/Core/Debugger.cpp @@ -810,6 +810,22 @@ if (!GetOutputFile().GetIsTerminalWithColors()) SetUseColor(false); + if (Diagnostics::Enabled()) { + Diagnostics::Instance().AddCallback( + [&](const FileSpec &dir) -> llvm::Error { + for (auto &entry : m_stream_handlers) { + llvm::StringRef log_path = entry.first(); + llvm::StringRef file_name = llvm::sys::path::filename(log_path); + FileSpec destination = dir.CopyByAppendingPathComponent(file_name); + std::error_code ec = + llvm::sys::fs::copy_file(entry.first(), destination.GetPath()); + if (ec) + return llvm::errorCodeToError(ec); + } + return llvm::Error::success(); + }); + } + #if defined(_WIN32) && defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING) // Enabling use of ANSI color codes because LLDB is using them to highlight // text. Index: lldb/source/Utility/Diagnostics.cpp =================================================================== --- lldb/source/Utility/Diagnostics.cpp +++ lldb/source/Utility/Diagnostics.cpp @@ -30,6 +30,8 @@ InstanceImpl().reset(); } +bool Diagnostics::Enabled() { return InstanceImpl().operator bool(); } + Optional &Diagnostics::InstanceImpl() { static Optional g_diagnostics; return g_diagnostics; Index: lldb/test/Shell/Diagnostics/TestCopyLogs.test =================================================================== --- /dev/null +++ lldb/test/Shell/Diagnostics/TestCopyLogs.test @@ -0,0 +1,7 @@ +# RUN: rm -rf %t +# RUN: mkdir -p %t +# The "ll''db" below is not a typo but a way to prevent lit from substituting 'lldb'. +# RUN: %lldb -o 'log enable ll''db commands -f %t/commands.log' -o 'diagnostics dump -d %t/diags' +# RUN: cat %t/diags/commands.log | FileCheck %s + +# CHECK: Processing command: diagnostics dump