Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lldb/include/lldb/Core/Debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ class Debugger : public std::enable_shared_from_this<Debugger>,

void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);

/// Copy this debugger's file-backed log files into the given directory, for
/// inclusion in a diagnostics bundle. Best-effort; failures are skipped.
void CopyLogFilesToDirectory(const FileSpec &dir);

Status SetPropertyValue(const ExecutionContext *exe_ctx,
VarSetOperationType op, llvm::StringRef property_path,
llvm::StringRef value) override;
Expand Down Expand Up @@ -805,7 +809,6 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
lldb::ListenerSP m_forward_listener_sp;
llvm::once_flag m_clear_once;
lldb::TargetSP m_dummy_target_sp;
Diagnostics::CallbackID m_diagnostics_callback_id;

/// Bookkeeping for command line progress events.
/// @{
Expand Down
39 changes: 6 additions & 33 deletions lldb/include/lldb/Utility/Diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,32 @@

#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/Log.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Error.h"

#include <functional>
#include <mutex>
#include <optional>
#include <vector>

namespace lldb_private {

/// Diagnostics are a collection of files to help investigate bugs and
/// troubleshoot issues. Any part of the debugger can register itself with the
/// help of a callback to emit one or more files into the diagnostic directory.
/// Diagnostics maintain an always-on, in-memory log of recent diagnostic
/// messages that can be written out to help investigate bugs and troubleshoot
/// issues.
class Diagnostics {
public:
Diagnostics();
~Diagnostics();

/// Gather diagnostics in the given directory.
/// Write the in-memory diagnostic log into the given directory.
llvm::Error Create(const FileSpec &dir);

/// Gather diagnostics and print a message to the given output stream.
/// Write the diagnostic log into a directory and print a message to the given
/// output stream.
/// @{
bool Dump(llvm::raw_ostream &stream);
bool Dump(llvm::raw_ostream &stream, const FileSpec &dir);
/// @}

void Report(llvm::StringRef message);

using Callback = std::function<llvm::Error(const FileSpec &)>;
using CallbackID = uint64_t;

CallbackID AddCallback(Callback callback);
void RemoveCallback(CallbackID id);

static Diagnostics &Instance();

static bool Enabled();
Expand All @@ -62,23 +52,6 @@ class Diagnostics {
llvm::Error DumpDiangosticsLog(const FileSpec &dir) const;

RotatingLogHandler m_log_handler;

struct CallbackEntry {
CallbackEntry(CallbackID id, Callback callback)
: id(id), callback(std::move(callback)) {}
CallbackID id;
Callback callback;
};

/// Monotonically increasing callback identifier. Unique per Diagnostic
/// instance.
CallbackID m_callback_id;

/// List of callback entries.
llvm::SmallVector<CallbackEntry, 4> m_callbacks;

/// Mutex to protect callback list and callback identifier.
std::mutex m_callbacks_mutex;
};

} // namespace lldb_private
Expand Down
5 changes: 5 additions & 0 deletions lldb/source/Commands/CommandObjectDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "CommandObjectDiagnostics.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Host/OptionParser.h"
#include "lldb/Interpreter/CommandOptionArgumentTable.h"
#include "lldb/Interpreter/CommandReturnObject.h"
Expand Down Expand Up @@ -93,6 +94,10 @@ class CommandObjectDiagnosticsDump : public CommandObjectParsed {
return;
}

// Copy this debugger's file-backed logs into the directory. This used to be
// done by a Diagnostics callback registered by the Debugger.
GetDebugger().CopyLogFilesToDirectory(*directory);

result.GetOutputStream() << "diagnostics written to " << *directory << '\n';

result.SetStatus(eReturnStatusSuccessFinishResult);
Expand Down
29 changes: 10 additions & 19 deletions lldb/source/Core/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,22 +1103,6 @@ Debugger::Debugger(lldb::LogOutputCallback log_callback, void *baton)
if (!GetOutputFileSP()->GetIsTerminalWithColors())
disable_color();

if (Diagnostics::Enabled()) {
m_diagnostics_callback_id = Diagnostics::Instance().AddCallback(
[this](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(log_path, 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.
Expand Down Expand Up @@ -1164,9 +1148,6 @@ void Debugger::Clear() {
GetInputFile().Close();

m_command_interpreter_up->Clear();

if (Diagnostics::Enabled())
Diagnostics::Instance().RemoveCallback(m_diagnostics_callback_id);
});
}

Expand Down Expand Up @@ -1692,6 +1673,16 @@ void Debugger::SetLoggingCallback(lldb::LogOutputCallback log_callback,
std::make_shared<CallbackLogHandler>(log_callback, baton);
}

void Debugger::CopyLogFilesToDirectory(const FileSpec &dir) {
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);
// Best-effort: skip logs that can't be copied rather than aborting.
llvm::sys::fs::copy_file(log_path, destination.GetPath());
}
}

void Debugger::SetDestroyCallback(
lldb_private::DebuggerDestroyCallback destroy_callback, void *baton) {
std::lock_guard<std::mutex> guard(m_destroy_callback_mutex);
Expand Down
18 changes: 0 additions & 18 deletions lldb/source/Utility/Diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,6 @@ Diagnostics::Diagnostics() : m_log_handler(g_num_log_messages) {}

Diagnostics::~Diagnostics() {}

Diagnostics::CallbackID Diagnostics::AddCallback(Callback callback) {
std::lock_guard<std::mutex> guard(m_callbacks_mutex);
CallbackID id = m_callback_id++;
m_callbacks.emplace_back(id, callback);
return id;
}

void Diagnostics::RemoveCallback(CallbackID id) {
std::lock_guard<std::mutex> guard(m_callbacks_mutex);
llvm::erase_if(m_callbacks,
[id](const CallbackEntry &e) { return e.id == id; });
}

bool Diagnostics::Dump(raw_ostream &stream) {
Expected<FileSpec> diagnostics_dir = CreateUniqueDirectory();
if (!diagnostics_dir) {
Expand Down Expand Up @@ -92,11 +79,6 @@ Error Diagnostics::Create(const FileSpec &dir) {
if (Error err = DumpDiangosticsLog(dir))
return err;

for (CallbackEntry e : m_callbacks) {
if (Error err = e.callback(dir))
return err;
}

return Error::success();
}

Expand Down