Skip to content

Commit 962469a

Browse files
authored
Merge pull request #5132 from apple/🍒/rebranch/6ff49af33d09+6ac608b3d897+de7475657156+09dea546692f+70841b97eb2e+6879391908ca+1e5d5261e2b6+87a32939611a+be265d25ca5e+9bdb7e573427
Cherrypick logging changes
2 parents bb18631 + bbee0c7 commit 962469a

File tree

34 files changed

+757
-389
lines changed

34 files changed

+757
-389
lines changed

lldb/include/lldb/Core/Debugger.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class ThreadPool;
5454

5555
namespace lldb_private {
5656
class Address;
57+
class CallbackLogHandler;
5758
class CommandInterpreter;
59+
class LogHandler;
5860
class Process;
5961
class Stream;
6062
class SymbolContext;
@@ -247,6 +249,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
247249
bool EnableLog(llvm::StringRef channel,
248250
llvm::ArrayRef<const char *> categories,
249251
llvm::StringRef log_file, uint32_t log_options,
252+
size_t buffer_size, LogHandlerKind log_handler_kind,
250253
llvm::raw_ostream &error_stream);
251254

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

563566
llvm::Optional<uint64_t> m_current_event_id;
564567

565-
llvm::StringMap<std::weak_ptr<llvm::raw_ostream>> m_log_streams;
566-
std::shared_ptr<llvm::raw_ostream> m_log_callback_stream_sp;
568+
llvm::StringMap<std::weak_ptr<LogHandler>> m_stream_handlers;
569+
std::shared_ptr<CallbackLogHandler> m_callback_handler_sp;
567570
ConstString m_instance_name;
568571
static LoadPluginCallbackType g_load_plugin_callback;
569572
typedef std::vector<llvm::sys::DynamicLibrary> LoadedPluginsList;

lldb/include/lldb/Host/Host.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "lldb/Host/HostThread.h"
1414
#include "lldb/Utility/Environment.h"
1515
#include "lldb/Utility/FileSpec.h"
16+
#include "lldb/Utility/Log.h"
1617
#include "lldb/Utility/Timeout.h"
1718
#include "lldb/lldb-private-forward.h"
1819
#include "lldb/lldb-private.h"
@@ -86,12 +87,8 @@ class Host {
8687
StartMonitoringChildProcess(const MonitorChildProcessCallback &callback,
8788
lldb::pid_t pid);
8889

89-
enum SystemLogType { eSystemLogWarning, eSystemLogError };
90-
91-
static void SystemLog(SystemLogType type, const char *format, ...)
92-
__attribute__((format(printf, 2, 3)));
93-
94-
static void SystemLog(SystemLogType type, const char *format, va_list args);
90+
/// Emit the given message to the operating system log.
91+
static void SystemLog(llvm::StringRef message);
9592

9693
/// Get the process ID for the calling process.
9794
///
@@ -259,6 +256,19 @@ class Host {
259256
ProcessInstanceInfoList &proc_infos);
260257
};
261258

259+
/// Log handler that emits log messages to the operating system log.
260+
class SystemLogHandler : public LogHandler {
261+
public:
262+
SystemLogHandler();
263+
void Emit(llvm::StringRef message) override;
264+
265+
bool isA(const void *ClassID) const override { return ClassID == &ID; }
266+
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }
267+
268+
private:
269+
static char ID;
270+
};
271+
262272
} // namespace lldb_private
263273

264274
namespace llvm {

lldb/include/lldb/Utility/Log.h

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
#include <cstdarg>
2626
#include <cstdint>
2727
#include <memory>
28+
#include <mutex>
2829
#include <string>
2930
#include <type_traits>
3031

3132
namespace llvm {
3233
class raw_ostream;
3334
}
3435
// Logging Options
35-
#define LLDB_LOG_OPTION_THREADSAFE (1u << 0)
3636
#define LLDB_LOG_OPTION_VERBOSE (1u << 1)
3737
#define LLDB_LOG_OPTION_PREPEND_SEQUENCE (1u << 3)
3838
#define LLDB_LOG_OPTION_PREPEND_TIMESTAMP (1u << 4)
@@ -45,6 +45,73 @@ class raw_ostream;
4545
// Logging Functions
4646
namespace lldb_private {
4747

48+
class LogHandler {
49+
public:
50+
virtual ~LogHandler() = default;
51+
virtual void Emit(llvm::StringRef message) = 0;
52+
53+
virtual bool isA(const void *ClassID) const { return ClassID == &ID; }
54+
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }
55+
56+
private:
57+
static char ID;
58+
};
59+
60+
class StreamLogHandler : public LogHandler {
61+
public:
62+
StreamLogHandler(int fd, bool should_close, size_t buffer_size = 0);
63+
~StreamLogHandler() override;
64+
65+
void Emit(llvm::StringRef message) override;
66+
void Flush();
67+
68+
bool isA(const void *ClassID) const override { return ClassID == &ID; }
69+
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }
70+
71+
private:
72+
std::mutex m_mutex;
73+
llvm::raw_fd_ostream m_stream;
74+
static char ID;
75+
};
76+
77+
class CallbackLogHandler : public LogHandler {
78+
public:
79+
CallbackLogHandler(lldb::LogOutputCallback callback, void *baton);
80+
81+
void Emit(llvm::StringRef message) override;
82+
83+
bool isA(const void *ClassID) const override { return ClassID == &ID; }
84+
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }
85+
86+
private:
87+
lldb::LogOutputCallback m_callback;
88+
void *m_baton;
89+
static char ID;
90+
};
91+
92+
class RotatingLogHandler : public LogHandler {
93+
public:
94+
RotatingLogHandler(size_t size);
95+
96+
void Emit(llvm::StringRef message) override;
97+
void Dump(llvm::raw_ostream &stream) const;
98+
99+
bool isA(const void *ClassID) const override { return ClassID == &ID; }
100+
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }
101+
102+
private:
103+
size_t NormalizeIndex(size_t i) const;
104+
size_t GetNumMessages() const;
105+
size_t GetFirstMessageIndex() const;
106+
107+
mutable std::mutex m_mutex;
108+
std::unique_ptr<std::string[]> m_messages;
109+
const size_t m_size = 0;
110+
size_t m_next_index = 0;
111+
size_t m_total_count = 0;
112+
static char ID;
113+
};
114+
48115
class Log final {
49116
public:
50117
/// The underlying type of all log channel enums. Declare them as:
@@ -111,7 +178,7 @@ class Log final {
111178
static void Unregister(llvm::StringRef name);
112179

113180
static bool
114-
EnableLogChannel(const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
181+
EnableLogChannel(const std::shared_ptr<LogHandler> &log_handler_sp,
115182
uint32_t log_options, llvm::StringRef channel,
116183
llvm::ArrayRef<const char *> categories,
117184
llvm::raw_ostream &error_stream);
@@ -120,6 +187,10 @@ class Log final {
120187
llvm::ArrayRef<const char *> categories,
121188
llvm::raw_ostream &error_stream);
122189

190+
static bool DumpLogChannel(llvm::StringRef channel,
191+
llvm::raw_ostream &output_stream,
192+
llvm::raw_ostream &error_stream);
193+
123194
static bool ListChannelCategories(llvm::StringRef channel,
124195
llvm::raw_ostream &stream);
125196

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

191-
std::shared_ptr<llvm::raw_ostream> m_stream_sp;
262+
std::shared_ptr<LogHandler> m_handler;
192263
std::atomic<uint32_t> m_options{0};
193264
std::atomic<MaskType> m_mask{0};
194265

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

202-
std::shared_ptr<llvm::raw_ostream> GetStream() {
273+
std::shared_ptr<LogHandler> GetHandler() {
203274
llvm::sys::ScopedReader lock(m_mutex);
204-
return m_stream_sp;
275+
return m_handler;
205276
}
206277

207-
void Enable(const std::shared_ptr<llvm::raw_ostream> &stream_sp,
208-
uint32_t options, uint32_t flags);
278+
void Enable(const std::shared_ptr<LogHandler> &handler_sp, uint32_t options,
279+
uint32_t flags);
209280

210281
void Disable(uint32_t flags);
211282

283+
bool Dump(llvm::raw_ostream &stream);
284+
212285
typedef llvm::StringMap<Log> ChannelMap;
213286
static llvm::ManagedStatic<ChannelMap> g_channel_map;
214287

lldb/include/lldb/Utility/StreamCallback.h

Lines changed: 0 additions & 35 deletions
This file was deleted.

lldb/include/lldb/lldb-enumerations.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ enum CommandArgumentType {
602602
eArgTypeColumnNum,
603603
eArgTypeModuleUUID,
604604
eArgTypeSaveCoreStyle,
605+
eArgTypeLogHandler,
605606
eArgTypeLastArg // Always keep this entry as the last entry in this
606607
// enumeration!!
607608
};

lldb/include/lldb/lldb-private-enumerations.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ enum StatisticKind {
233233
StatisticMax = 4
234234
};
235235

236+
// Enumeration that can be used to specify a log handler.
237+
enum LogHandlerKind {
238+
eLogHandlerStream,
239+
eLogHandlerCallback,
240+
eLogHandlerCircular,
241+
eLogHandlerSystem,
242+
eLogHandlerDefault = eLogHandlerStream,
243+
};
236244

237245
inline std::string GetStatDescription(lldb_private::StatisticKind K) {
238246
switch (K) {

lldb/source/API/SBDebugger.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,8 @@ bool SBDebugger::EnableLog(const char *channel, const char **categories) {
16261626
std::string error;
16271627
llvm::raw_string_ostream error_stream(error);
16281628
return m_opaque_sp->EnableLog(channel, GetCategoryArray(categories), "",
1629-
log_options, error_stream);
1629+
log_options, /*buffer_size=*/0,
1630+
eLogHandlerStream, error_stream);
16301631
} else
16311632
return false;
16321633
}

0 commit comments

Comments
 (0)