LLDB mainline
Log.cpp
Go to the documentation of this file.
1//===-- Log.cpp -----------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Utility/Log.h"
11
12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/Twine.h"
14#include "llvm/ADT/iterator.h"
15
16#include "llvm/Support/Casting.h"
17#include "llvm/Support/Chrono.h"
18#include "llvm/Support/ManagedStatic.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/Signals.h"
21#include "llvm/Support/Threading.h"
22#include "llvm/Support/raw_ostream.h"
23
24#include <chrono>
25#include <cstdarg>
26#include <mutex>
27#include <utility>
28
29#include <cassert>
30#if defined(_WIN32)
31#include <process.h>
32#else
33#include <unistd.h>
34#endif
35
36using namespace lldb_private;
37
43
44llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;
45
47 const Log::ChannelMap::value_type &entry,
48 llvm::function_ref<void(llvm::StringRef, llvm::StringRef)> lambda) {
49 lambda("all", "all available logging categories");
50 lambda("default", "default set of logging categories");
51 for (const auto &category : entry.second.m_channel.categories)
52 lambda(category.name, category.description);
53}
54
55void Log::ListCategories(llvm::raw_ostream &stream,
56 const ChannelMap::value_type &entry) {
57 stream << llvm::formatv("Logging categories for '{0}':\n", entry.first());
58 ForEachCategory(entry,
59 [&stream](llvm::StringRef name, llvm::StringRef description) {
60 stream << llvm::formatv(" {0} - {1}\n", name, description);
61 });
62}
63
64Log::MaskType Log::GetFlags(llvm::raw_ostream &stream,
65 const ChannelMap::value_type &entry,
66 llvm::ArrayRef<const char *> categories) {
67 bool list_categories = false;
68 Log::MaskType flags = 0;
69 for (const char *category : categories) {
70 if (llvm::StringRef("all").equals_insensitive(category)) {
71 flags |= std::numeric_limits<Log::MaskType>::max();
72 continue;
73 }
74 if (llvm::StringRef("default").equals_insensitive(category)) {
75 flags |= entry.second.m_channel.default_flags;
76 continue;
77 }
78 auto cat = llvm::find_if(entry.second.m_channel.categories,
79 [&](const Log::Category &c) {
80 return c.name.equals_insensitive(category);
81 });
82 if (cat != entry.second.m_channel.categories.end()) {
83 flags |= cat->flag;
84 continue;
85 }
86 stream << llvm::formatv("error: unrecognized log category '{0}'\n",
87 category);
88 list_categories = true;
89 }
90 if (list_categories)
91 ListCategories(stream, entry);
92 return flags;
93}
94
95void Log::Enable(const std::shared_ptr<LogHandler> &handler_sp,
96 uint32_t options, Log::MaskType flags) {
97 llvm::sys::ScopedWriter lock(m_mutex);
98
99 MaskType mask = m_mask.fetch_or(flags, std::memory_order_relaxed);
100 if (mask | flags) {
101 m_options.store(options, std::memory_order_relaxed);
102 m_handler = handler_sp;
103 m_channel.log_ptr.store(this, std::memory_order_relaxed);
104 }
105}
106
108 llvm::sys::ScopedWriter lock(m_mutex);
109
110 MaskType mask = m_mask.fetch_and(~flags, std::memory_order_relaxed);
111 if (!(mask & ~flags)) {
112 m_handler.reset();
113 m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
114 }
115}
116
117bool Log::Dump(llvm::raw_ostream &output_stream) {
118 llvm::sys::ScopedReader lock(m_mutex);
119 if (RotatingLogHandler *handler =
120 llvm::dyn_cast_or_null<RotatingLogHandler>(m_handler.get())) {
121 handler->Dump(output_stream);
122 return true;
123 }
124 return false;
125}
126
127const Flags Log::GetOptions() const {
128 return m_options.load(std::memory_order_relaxed);
129}
130
132 return m_mask.load(std::memory_order_relaxed);
133}
134
135void Log::PutCString(const char *cstr) { PutString(cstr); }
136
137void Log::PutString(llvm::StringRef str) {
138 std::string FinalMessage;
139 llvm::raw_string_ostream Stream(FinalMessage);
140 WriteHeader(Stream, "", "");
141 Stream << str << "\n";
142 WriteMessage(FinalMessage);
143}
144
145// Simple variable argument logging with flags.
146void Log::Printf(const char *format, ...) {
147 va_list args;
148 va_start(args, format);
149 VAPrintf(format, args);
150 va_end(args);
151}
152
153void Log::VAPrintf(const char *format, va_list args) {
154 llvm::SmallString<64> Content;
155 lldb_private::VASprintf(Content, format, args);
156 PutString(Content);
157}
158
159void Log::Formatf(llvm::StringRef file, llvm::StringRef function,
160 const char *format, ...) {
161 va_list args;
162 va_start(args, format);
163 VAFormatf(file, function, format, args);
164 va_end(args);
165}
166
167void Log::VAFormatf(llvm::StringRef file, llvm::StringRef function,
168 const char *format, va_list args) {
169 llvm::SmallString<64> Content;
170 lldb_private::VASprintf(Content, format, args);
171 Format(file, function, llvm::formatv("{0}", Content));
172}
173
174// Printing of errors that are not fatal.
175void Log::Error(const char *format, ...) {
176 va_list args;
177 va_start(args, format);
178 VAError(format, args);
179 va_end(args);
180}
181
182void Log::VAError(const char *format, va_list args) {
183 llvm::SmallString<64> Content;
184 VASprintf(Content, format, args);
185
186 Printf("error: %s", Content.c_str());
187}
188
189// Printing of warnings that are not fatal only if verbose mode is enabled.
190void Log::Verbose(const char *format, ...) {
191 if (!GetVerbose())
192 return;
193
194 va_list args;
195 va_start(args, format);
196 VAPrintf(format, args);
197 va_end(args);
198}
199
200// Printing of warnings that are not fatal.
201void Log::Warning(const char *format, ...) {
202 llvm::SmallString<64> Content;
203 va_list args;
204 va_start(args, format);
205 VASprintf(Content, format, args);
206 va_end(args);
207
208 Printf("warning: %s", Content.c_str());
209}
210
211void Log::Register(llvm::StringRef name, Channel &channel) {
212 auto iter = g_channel_map->try_emplace(name, channel);
213 assert(iter.second == true);
215}
216
217void Log::Unregister(llvm::StringRef name) {
218 auto iter = g_channel_map->find(name);
219 assert(iter != g_channel_map->end());
220 iter->second.Disable(std::numeric_limits<MaskType>::max());
221 g_channel_map->erase(iter);
222}
223
224bool Log::EnableLogChannel(const std::shared_ptr<LogHandler> &log_handler_sp,
225 uint32_t log_options, llvm::StringRef channel,
226 llvm::ArrayRef<const char *> categories,
227 llvm::raw_ostream &error_stream) {
228 auto iter = g_channel_map->find(channel);
229 if (iter == g_channel_map->end()) {
230 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
231 return false;
232 }
233 MaskType flags = categories.empty()
234 ? iter->second.m_channel.default_flags
235 : GetFlags(error_stream, *iter, categories);
236 iter->second.Enable(log_handler_sp, log_options, flags);
237 return true;
238}
239
240bool Log::DisableLogChannel(llvm::StringRef channel,
241 llvm::ArrayRef<const char *> categories,
242 llvm::raw_ostream &error_stream) {
243 auto iter = g_channel_map->find(channel);
244 if (iter == g_channel_map->end()) {
245 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
246 return false;
247 }
248 MaskType flags = categories.empty()
249 ? std::numeric_limits<MaskType>::max()
250 : GetFlags(error_stream, *iter, categories);
251 iter->second.Disable(flags);
252 return true;
253}
254
255bool Log::DumpLogChannel(llvm::StringRef channel,
256 llvm::raw_ostream &output_stream,
257 llvm::raw_ostream &error_stream) {
258 auto iter = g_channel_map->find(channel);
259 if (iter == g_channel_map->end()) {
260 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
261 return false;
262 }
263 if (!iter->second.Dump(output_stream)) {
264 error_stream << llvm::formatv(
265 "log channel '{0}' does not support dumping.\n", channel);
266 return false;
267 }
268 return true;
269}
270
271bool Log::ListChannelCategories(llvm::StringRef channel,
272 llvm::raw_ostream &stream) {
273 auto ch = g_channel_map->find(channel);
274 if (ch == g_channel_map->end()) {
275 stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
276 return false;
277 }
278 ListCategories(stream, *ch);
279 return true;
280}
281
283 for (auto &entry : *g_channel_map)
284 entry.second.Disable(std::numeric_limits<MaskType>::max());
285}
286
288 llvm::StringRef channel,
289 llvm::function_ref<void(llvm::StringRef, llvm::StringRef)> lambda) {
290 auto ch = g_channel_map->find(channel);
291 if (ch == g_channel_map->end())
292 return;
293
294 ForEachCategory(*ch, lambda);
295}
296
297std::vector<llvm::StringRef> Log::ListChannels() {
298 std::vector<llvm::StringRef> result;
299 for (const auto &channel : *g_channel_map)
300 result.push_back(channel.first());
301 return result;
302}
303
304void Log::ListAllLogChannels(llvm::raw_ostream &stream) {
305 if (g_channel_map->empty()) {
306 stream << "No logging channels are currently registered.\n";
307 return;
308 }
309
310 for (const auto &channel : *g_channel_map)
311 ListCategories(stream, channel);
312}
313
314bool Log::GetVerbose() const {
315 return m_options.load(std::memory_order_relaxed) & LLDB_LOG_OPTION_VERBOSE;
316}
317
318void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
319 llvm::StringRef function) {
320 Flags options = GetOptions();
321 static uint32_t g_sequence_id = 0;
322 // Add a sequence ID if requested
324 OS << ++g_sequence_id << " ";
325
326 // Timestamp if requested
328 auto now = std::chrono::duration<double>(
329 std::chrono::system_clock::now().time_since_epoch());
330 OS << llvm::formatv("{0:f9} ", now.count());
331 }
332
333 // Add the process and thread if requested
335 OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
336 llvm::get_threadid());
337
338 // Add the thread name if requested
340 llvm::SmallString<32> thread_name;
341 llvm::get_thread_name(thread_name);
342
343 llvm::SmallString<12> format_str;
344 llvm::raw_svector_ostream format_os(format_str);
345 format_os << "{0,-" << llvm::alignTo<16>(thread_name.size()) << "} ";
346 OS << llvm::formatv(format_str.c_str(), thread_name);
347 }
348
349 if (options.Test(LLDB_LOG_OPTION_BACKTRACE))
350 llvm::sys::PrintStackTrace(OS);
351
353 (!file.empty() || !function.empty())) {
354 file = llvm::sys::path::filename(file).take_front(40);
355 function = function.take_front(40);
356 OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
357 }
358}
359
360// If we have a callback registered, then we call the logging callback. If we
361// have a valid file handle, we also log to the file.
362void Log::WriteMessage(llvm::StringRef message) {
363 // Make a copy of our stream shared pointer in case someone disables our log
364 // while we are logging and releases the stream
365 auto handler_sp = GetHandler();
366 if (!handler_sp)
367 return;
368 handler_sp->Emit(message);
369}
370
371void Log::Format(llvm::StringRef file, llvm::StringRef function,
372 const llvm::formatv_object_base &payload) {
373 std::string message_string;
374 llvm::raw_string_ostream message(message_string);
375 WriteHeader(message, file, function);
376 message << payload << "\n";
377 WriteMessage(message.str());
378}
379
380StreamLogHandler::StreamLogHandler(int fd, bool should_close,
381 size_t buffer_size)
382 : m_stream(fd, should_close, buffer_size == 0) {
383 if (buffer_size > 0)
384 m_stream.SetBufferSize(buffer_size);
385}
386
388
390 std::lock_guard<std::mutex> guard(m_mutex);
391 m_stream.flush();
392}
393
394void StreamLogHandler::Emit(llvm::StringRef message) {
395 if (m_stream.GetBufferSize() > 0) {
396 std::lock_guard<std::mutex> guard(m_mutex);
397 m_stream << message;
398 } else {
399 m_stream << message;
400 }
401}
402
404 void *baton)
405 : m_callback(callback), m_baton(baton) {}
406
407void CallbackLogHandler::Emit(llvm::StringRef message) {
408 m_callback(message.data(), m_baton);
409}
410
412 : m_messages(std::make_unique<std::string[]>(size)), m_size(size) {}
413
414void RotatingLogHandler::Emit(llvm::StringRef message) {
415 std::lock_guard<std::mutex> guard(m_mutex);
417 const size_t index = m_next_index;
418 m_next_index = NormalizeIndex(index + 1);
419 m_messages[index] = message.str();
420}
421
422size_t RotatingLogHandler::NormalizeIndex(size_t i) const { return i % m_size; }
423
426}
427
429 return m_total_count < m_size ? 0 : m_next_index;
430}
431
432void RotatingLogHandler::Dump(llvm::raw_ostream &stream) const {
433 std::lock_guard<std::mutex> guard(m_mutex);
434 const size_t start_idx = GetFirstMessageIndex();
435 const size_t stop_idx = start_idx + GetNumMessages();
436 for (size_t i = start_idx; i < stop_idx; ++i) {
437 const size_t idx = NormalizeIndex(i);
438 stream << m_messages[idx];
439 }
440 stream.flush();
441}
442
443TeeLogHandler::TeeLogHandler(std::shared_ptr<LogHandler> first_log_handler,
444 std::shared_ptr<LogHandler> second_log_handler)
445 : m_first_log_handler(first_log_handler),
446 m_second_log_handler(second_log_handler) {
447 assert(m_first_log_handler && "first log handler must be valid");
448 assert(m_second_log_handler && "second log handler must be valid");
449}
450
451void TeeLogHandler::Emit(llvm::StringRef message) {
452 m_first_log_handler->Emit(message);
453 m_second_log_handler->Emit(message);
454}
#define LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION
Definition: Log.h:43
#define LLDB_LOG_OPTION_BACKTRACE
Definition: Log.h:41
#define LLDB_LOG_OPTION_PREPEND_TIMESTAMP
Definition: Log.h:38
#define LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD
Definition: Log.h:39
#define LLDB_LOG_OPTION_PREPEND_SEQUENCE
Definition: Log.h:37
#define LLDB_LOG_OPTION_VERBOSE
Definition: Log.h:36
#define LLDB_LOG_OPTION_PREPEND_THREAD_NAME
Definition: Log.h:40
lldb::LogOutputCallback m_callback
Definition: Log.h:87
CallbackLogHandler(lldb::LogOutputCallback callback, void *baton)
Definition: Log.cpp:403
void Emit(llvm::StringRef message) override
Definition: Log.cpp:407
A class to manage flags.
Definition: Flags.h:22
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:96
static char ID
Definition: Log.h:57
std::atomic< Log * > log_ptr
Definition: Log.h:164
llvm::sys::RWMutex m_mutex
Definition: Log.h:282
static void ListCategories(llvm::raw_ostream &stream, const ChannelMap::value_type &entry)
Definition: Log.cpp:55
void Disable(MaskType flags)
Definition: Log.cpp:107
std::shared_ptr< LogHandler > m_handler
Definition: Log.h:284
void WriteMessage(llvm::StringRef message)
Definition: Log.cpp:362
void PutCString(const char *cstr)
Definition: Log.cpp:135
static void ForEachCategory(const Log::ChannelMap::value_type &entry, llvm::function_ref< void(llvm::StringRef, llvm::StringRef)> lambda)
Definition: Log.cpp:46
void Formatf(llvm::StringRef file, llvm::StringRef function, const char *format,...) __attribute__((format(printf
Definition: Log.cpp:159
uint64_t MaskType
The underlying type of all log channel enums.
Definition: Log.h:141
static bool DisableLogChannel(llvm::StringRef channel, llvm::ArrayRef< const char * > categories, llvm::raw_ostream &error_stream)
Definition: Log.cpp:240
static void Register(llvm::StringRef name, Channel &channel)
Definition: Log.cpp:211
static void ListAllLogChannels(llvm::raw_ostream &stream)
Definition: Log.cpp:304
void VAFormatf(llvm::StringRef file, llvm::StringRef function, const char *format, va_list args)
Definition: Log.cpp:167
void WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file, llvm::StringRef function)
Definition: Log.cpp:318
static bool DumpLogChannel(llvm::StringRef channel, llvm::raw_ostream &output_stream, llvm::raw_ostream &error_stream)
Definition: Log.cpp:255
void VAPrintf(const char *format, va_list args)
Definition: Log.cpp:153
void void void Error(const char *fmt,...) __attribute__((format(printf
Definition: Log.cpp:175
void Format(llvm::StringRef file, llvm::StringRef function, const char *format, Args &&... args)
Definition: Log.h:238
static llvm::ManagedStatic< ChannelMap > g_channel_map
Definition: Log.h:308
std::shared_ptr< LogHandler > GetHandler()
Definition: Log.h:295
void void void void void Warning(const char *fmt,...) __attribute__((format(printf
Definition: Log.cpp:201
std::atomic< MaskType > m_mask
Definition: Log.h:286
static void Unregister(llvm::StringRef name)
Definition: Log.cpp:217
static bool ListChannelCategories(llvm::StringRef channel, llvm::raw_ostream &stream)
Definition: Log.cpp:271
static void DisableAllLogChannels()
Definition: Log.cpp:282
static void ForEachChannelCategory(llvm::StringRef channel, llvm::function_ref< void(llvm::StringRef, llvm::StringRef)> lambda)
Calls the given lambda for every category in the given channel.
Definition: Log.cpp:287
void void void void void const Flags GetOptions() const
Definition: Log.cpp:127
static bool 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)
Definition: Log.cpp:224
MaskType GetMask() const
Definition: Log.cpp:131
void VAError(const char *format, va_list args)
Definition: Log.cpp:182
static std::vector< llvm::StringRef > ListChannels()
Returns the list of log channels.
Definition: Log.cpp:297
void Enable(const std::shared_ptr< LogHandler > &handler_sp, uint32_t options, MaskType flags)
Definition: Log.cpp:95
static Log::MaskType GetFlags(llvm::raw_ostream &stream, const ChannelMap::value_type &entry, llvm::ArrayRef< const char * > categories)
Definition: Log.cpp:64
bool GetVerbose() const
Definition: Log.cpp:314
void PutString(llvm::StringRef str)
Definition: Log.cpp:137
bool Dump(llvm::raw_ostream &stream)
Definition: Log.cpp:117
void void Printf(const char *format,...) __attribute__((format(printf
Prefer using LLDB_LOGF whenever possible.
Definition: Log.cpp:146
void void void void Verbose(const char *fmt,...) __attribute__((format(printf
Definition: Log.cpp:190
std::atomic< uint32_t > m_options
Definition: Log.h:285
Channel & m_channel
Definition: Log.h:276
void Dump(llvm::raw_ostream &stream) const
Definition: Log.cpp:432
std::unique_ptr< std::string[]> m_messages
Definition: Log.h:108
void Emit(llvm::StringRef message) override
Definition: Log.cpp:414
size_t NormalizeIndex(size_t i) const
Definition: Log.cpp:422
size_t GetNumMessages() const
Definition: Log.cpp:424
RotatingLogHandler(size_t size)
Definition: Log.cpp:411
size_t GetFirstMessageIndex() const
Definition: Log.cpp:428
~StreamLogHandler() override
Definition: Log.cpp:387
llvm::raw_fd_ostream m_stream
Definition: Log.h:73
StreamLogHandler(int fd, bool should_close, size_t buffer_size=0)
Definition: Log.cpp:380
void Emit(llvm::StringRef message) override
Definition: Log.cpp:394
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void Emit(llvm::StringRef message) override
Definition: Log.cpp:451
std::shared_ptr< LogHandler > m_first_log_handler
Definition: Log.h:127
TeeLogHandler(std::shared_ptr< LogHandler > first_log_handler, std::shared_ptr< LogHandler > second_log_handler)
Definition: Log.cpp:443
std::shared_ptr< LogHandler > m_second_log_handler
Definition: Log.h:128
#define UNUSED_IF_ASSERT_DISABLED(x)
Definition: lldb-defines.h:140
A class that represents a running process on the host machine.
bool VASprintf(llvm::SmallVectorImpl< char > &buf, const char *fmt, va_list args)
Definition: VASprintf.cpp:19
void(* LogOutputCallback)(const char *, void *baton)
Definition: lldb-types.h:73