Skip to content

Cherrypick logging changes #5132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
7 changes: 5 additions & 2 deletions lldb/include/lldb/Core/Debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class ThreadPool;

namespace lldb_private {
class Address;
class CallbackLogHandler;
class CommandInterpreter;
class LogHandler;
class Process;
class Stream;
class SymbolContext;
Expand Down Expand Up @@ -247,6 +249,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,
size_t buffer_size, LogHandlerKind log_handler_kind,
llvm::raw_ostream &error_stream);

void SetLoggingCallback(lldb::LogOutputCallback log_callback, void *baton);
Expand Down Expand Up @@ -562,8 +565,8 @@ class Debugger : public std::enable_shared_from_this<Debugger>,

llvm::Optional<uint64_t> m_current_event_id;

llvm::StringMap<std::weak_ptr<llvm::raw_ostream>> m_log_streams;
std::shared_ptr<llvm::raw_ostream> m_log_callback_stream_sp;
llvm::StringMap<std::weak_ptr<LogHandler>> m_stream_handlers;
std::shared_ptr<CallbackLogHandler> m_callback_handler_sp;
ConstString m_instance_name;
static LoadPluginCallbackType g_load_plugin_callback;
typedef std::vector<llvm::sys::DynamicLibrary> LoadedPluginsList;
Expand Down
22 changes: 16 additions & 6 deletions lldb/include/lldb/Host/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "lldb/Host/HostThread.h"
#include "lldb/Utility/Environment.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Timeout.h"
#include "lldb/lldb-private-forward.h"
#include "lldb/lldb-private.h"
Expand Down Expand Up @@ -86,12 +87,8 @@ class Host {
StartMonitoringChildProcess(const MonitorChildProcessCallback &callback,
lldb::pid_t pid);

enum SystemLogType { eSystemLogWarning, eSystemLogError };

static void SystemLog(SystemLogType type, const char *format, ...)
__attribute__((format(printf, 2, 3)));

static void SystemLog(SystemLogType type, const char *format, va_list args);
/// Emit the given message to the operating system log.
static void SystemLog(llvm::StringRef message);

/// Get the process ID for the calling process.
///
Expand Down Expand Up @@ -259,6 +256,19 @@ class Host {
ProcessInstanceInfoList &proc_infos);
};

/// Log handler that emits log messages to the operating system log.
class SystemLogHandler : public LogHandler {
public:
SystemLogHandler();
void Emit(llvm::StringRef message) override;

bool isA(const void *ClassID) const override { return ClassID == &ID; }
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }

private:
static char ID;
};

} // namespace lldb_private

namespace llvm {
Expand Down
87 changes: 80 additions & 7 deletions lldb/include/lldb/Utility/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
#include <cstdarg>
#include <cstdint>
#include <memory>
#include <mutex>
#include <string>
#include <type_traits>

namespace llvm {
class raw_ostream;
}
// Logging Options
#define LLDB_LOG_OPTION_THREADSAFE (1u << 0)
#define LLDB_LOG_OPTION_VERBOSE (1u << 1)
#define LLDB_LOG_OPTION_PREPEND_SEQUENCE (1u << 3)
#define LLDB_LOG_OPTION_PREPEND_TIMESTAMP (1u << 4)
Expand All @@ -45,6 +45,73 @@ class raw_ostream;
// Logging Functions
namespace lldb_private {

class LogHandler {
public:
virtual ~LogHandler() = default;
virtual void Emit(llvm::StringRef message) = 0;

virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }

private:
static char ID;
};

class StreamLogHandler : public LogHandler {
public:
StreamLogHandler(int fd, bool should_close, size_t buffer_size = 0);
~StreamLogHandler() override;

void Emit(llvm::StringRef message) override;
void Flush();

bool isA(const void *ClassID) const override { return ClassID == &ID; }
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }

private:
std::mutex m_mutex;
llvm::raw_fd_ostream m_stream;
static char ID;
};

class CallbackLogHandler : public LogHandler {
public:
CallbackLogHandler(lldb::LogOutputCallback callback, void *baton);

void Emit(llvm::StringRef message) override;

bool isA(const void *ClassID) const override { return ClassID == &ID; }
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }

private:
lldb::LogOutputCallback m_callback;
void *m_baton;
static char ID;
};

class RotatingLogHandler : public LogHandler {
public:
RotatingLogHandler(size_t size);

void Emit(llvm::StringRef message) override;
void Dump(llvm::raw_ostream &stream) const;

bool isA(const void *ClassID) const override { return ClassID == &ID; }
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }

private:
size_t NormalizeIndex(size_t i) const;
size_t GetNumMessages() const;
size_t GetFirstMessageIndex() const;

mutable std::mutex m_mutex;
std::unique_ptr<std::string[]> m_messages;
const size_t m_size = 0;
size_t m_next_index = 0;
size_t m_total_count = 0;
static char ID;
};

class Log final {
public:
/// The underlying type of all log channel enums. Declare them as:
Expand Down Expand Up @@ -111,7 +178,7 @@ class Log final {
static void Unregister(llvm::StringRef name);

static bool
EnableLogChannel(const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
EnableLogChannel(const std::shared_ptr<LogHandler> &log_handler_sp,
uint32_t log_options, llvm::StringRef channel,
llvm::ArrayRef<const char *> categories,
llvm::raw_ostream &error_stream);
Expand All @@ -120,6 +187,10 @@ class Log final {
llvm::ArrayRef<const char *> categories,
llvm::raw_ostream &error_stream);

static bool DumpLogChannel(llvm::StringRef channel,
llvm::raw_ostream &output_stream,
llvm::raw_ostream &error_stream);

static bool ListChannelCategories(llvm::StringRef channel,
llvm::raw_ostream &stream);

Expand Down Expand Up @@ -188,7 +259,7 @@ class Log final {
// Their modification however, is still protected by this mutex.
llvm::sys::RWMutex m_mutex;

std::shared_ptr<llvm::raw_ostream> m_stream_sp;
std::shared_ptr<LogHandler> m_handler;
std::atomic<uint32_t> m_options{0};
std::atomic<MaskType> m_mask{0};

Expand All @@ -199,16 +270,18 @@ class Log final {
void Format(llvm::StringRef file, llvm::StringRef function,
const llvm::formatv_object_base &payload);

std::shared_ptr<llvm::raw_ostream> GetStream() {
std::shared_ptr<LogHandler> GetHandler() {
llvm::sys::ScopedReader lock(m_mutex);
return m_stream_sp;
return m_handler;
}

void Enable(const std::shared_ptr<llvm::raw_ostream> &stream_sp,
uint32_t options, uint32_t flags);
void Enable(const std::shared_ptr<LogHandler> &handler_sp, uint32_t options,
uint32_t flags);

void Disable(uint32_t flags);

bool Dump(llvm::raw_ostream &stream);

typedef llvm::StringMap<Log> ChannelMap;
static llvm::ManagedStatic<ChannelMap> g_channel_map;

Expand Down
35 changes: 0 additions & 35 deletions lldb/include/lldb/Utility/StreamCallback.h

This file was deleted.

1 change: 1 addition & 0 deletions lldb/include/lldb/lldb-enumerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ enum CommandArgumentType {
eArgTypeColumnNum,
eArgTypeModuleUUID,
eArgTypeSaveCoreStyle,
eArgTypeLogHandler,
eArgTypeLastArg // Always keep this entry as the last entry in this
// enumeration!!
};
Expand Down
8 changes: 8 additions & 0 deletions lldb/include/lldb/lldb-private-enumerations.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ enum StatisticKind {
StatisticMax = 4
};

// Enumeration that can be used to specify a log handler.
enum LogHandlerKind {
eLogHandlerStream,
eLogHandlerCallback,
eLogHandlerCircular,
eLogHandlerSystem,
eLogHandlerDefault = eLogHandlerStream,
};

inline std::string GetStatDescription(lldb_private::StatisticKind K) {
switch (K) {
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/API/SBDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,8 @@ 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,
eLogHandlerStream, error_stream);
} else
return false;
}
Expand Down
Loading