blob: 89216f41a5bb8591e88e9efde22c0f26d79dd268 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Log.cpp -------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Pavel Labath774103c2016-11-02 12:18:42 +000010// Project includes
11#include "lldb/Core/Log.h"
12#include "lldb/Core/PluginManager.h"
13#include "lldb/Core/StreamFile.h"
Pavel Labath774103c2016-11-02 12:18:42 +000014#include "lldb/Host/Host.h"
15#include "lldb/Host/ThisThread.h"
16#include "lldb/Interpreter/Args.h"
17#include "lldb/Utility/NameMatches.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000018#include "lldb/Utility/StreamString.h"
Pavel Labath774103c2016-11-02 12:18:42 +000019
20// Other libraries and framework includes
Pavel Labathfb0d22d2017-02-17 13:27:42 +000021#include "llvm/ADT/STLExtras.h"
Pavel Labath774103c2016-11-02 12:18:42 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/Support/Chrono.h"
Pavel Labathfb0d22d2017-02-17 13:27:42 +000024#include "llvm/Support/ManagedStatic.h"
Pavel Labath107d9bb2017-01-18 11:00:26 +000025#include "llvm/Support/Path.h"
Pavel Labath774103c2016-11-02 12:18:42 +000026#include "llvm/Support/Signals.h"
27#include "llvm/Support/raw_ostream.h"
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029// C Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030// C++ Includes
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000031#include <cstdarg>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000032#include <cstdio>
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000033#include <cstdlib>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include <map>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000035#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include <string>
37
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038using namespace lldb;
39using namespace lldb_private;
40
Pavel Labathfb0d22d2017-02-17 13:27:42 +000041namespace {
42 struct ChannelAndLog {
43 Log log;
44 Log::Channel &channel;
45
46 ChannelAndLog(Log::Channel &channel) : channel(channel) {}
47 };
48 typedef llvm::StringMap<ChannelAndLog> ChannelMap;
49}
50
51static llvm::ManagedStatic<ChannelMap> g_channel_map;
52
53static void ListCategories(Stream &stream, const ChannelMap::value_type &entry) {
54 stream.Format("Logging categories for '{0}':\n", entry.first());
55 stream.Format(" all - all available logging categories\n");
56 stream.Format(" default - default set of logging categories\n");
57 for (const auto &category : entry.second.channel.categories)
58 stream.Format(" {0} - {1}\n", category.name, category.description);
59}
60
61static uint32_t GetFlags(Stream &stream, const ChannelMap::value_type &entry,
62 const char **categories) {
63 bool list_categories = false;
64 uint32_t flags = 0;
65 for (size_t i = 0; categories[i]; ++i) {
66 if (llvm::StringRef("all").equals_lower(categories[i])) {
67 flags |= UINT32_MAX;
68 continue;
69 }
70 if (llvm::StringRef("default").equals_lower(categories[i])) {
71 flags |= entry.second.channel.default_flags;
72 continue;
73 }
74 auto cat = llvm::find_if(entry.second.channel.categories,
75 [&](const Log::Category &c) {
76 return c.name.equals_lower(categories[i]);
77 });
78 if (cat != entry.second.channel.categories.end()) {
79 flags |= cat->flag;
80 continue;
81 }
82 stream.Format("error: unrecognized log category '{0}'\n", categories[i]);
83 list_categories = true;
84 }
85 if (list_categories)
86 ListCategories(stream, entry);
87 return flags;
88}
89
90void Log::Channel::Enable(Log &log,
91 const std::shared_ptr<llvm::raw_ostream> &stream_sp,
Pavel Labath88d081b2017-02-27 11:05:39 +000092 uint32_t options, uint32_t flags) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000093 log.GetMask().Set(flags);
94 if (log.GetMask().Get()) {
Pavel Labath88d081b2017-02-27 11:05:39 +000095 log.GetOptions().Set(options);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000096 log.SetStream(stream_sp);
97 log_ptr.store(&log, std::memory_order_release);
98 }
99}
100
101void Log::Channel::Disable(uint32_t flags) {
102 Log *log = log_ptr.load(std::memory_order_acquire);
103 if (!log)
104 return;
105 log->GetMask().Clear(flags);
106 if (!log->GetMask().Get()) {
107 log->SetStream(nullptr);
108 log_ptr.store(nullptr, std::memory_order_release);
109 }
110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112Log::Log() : m_stream_sp(), m_options(0), m_mask_bits(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113
Pavel Labath5fae71c2017-02-10 11:49:21 +0000114Log::Log(const std::shared_ptr<llvm::raw_ostream> &stream_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 : m_stream_sp(stream_sp), m_options(0), m_mask_bits(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +0000117Log::~Log() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119Flags &Log::GetOptions() { return m_options; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121const Flags &Log::GetOptions() const { return m_options; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123Flags &Log::GetMask() { return m_mask_bits; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125const Flags &Log::GetMask() const { return m_mask_bits; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127void Log::PutCString(const char *cstr) { Printf("%s", cstr); }
Zachary Turnerc1564272016-11-16 21:15:24 +0000128void Log::PutString(llvm::StringRef str) { PutCString(str.str().c_str()); }
Zachary Turnerc1592652015-04-29 22:55:28 +0000129
130//----------------------------------------------------------------------
131// Simple variable argument logging with flags.
132//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133void Log::Printf(const char *format, ...) {
134 va_list args;
135 va_start(args, format);
136 VAPrintf(format, args);
137 va_end(args);
Zachary Turnerc1592652015-04-29 22:55:28 +0000138}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139
140//----------------------------------------------------------------------
141// All logging eventually boils down to this function call. If we have
142// a callback registered, then we call the logging callback. If we have
143// a valid file handle, we also log to the file.
144//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145void Log::VAPrintf(const char *format, va_list args) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000146 std::string message_string;
147 llvm::raw_string_ostream message(message_string);
148 WriteHeader(message, "", "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149
Pavel Labath107d9bb2017-01-18 11:00:26 +0000150 char *text;
151 vasprintf(&text, format, args);
152 message << text;
153 free(text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154
Pavel Labath107d9bb2017-01-18 11:00:26 +0000155 message << "\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156
Pavel Labath107d9bb2017-01-18 11:00:26 +0000157 WriteMessage(message.str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161// Log only if all of the bits are set
162//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163void Log::LogIf(uint32_t bits, const char *format, ...) {
164 if (!m_options.AllSet(bits))
165 return;
Zachary Turnerc1592652015-04-29 22:55:28 +0000166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 va_list args;
168 va_start(args, format);
169 VAPrintf(format, args);
170 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171}
172
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173//----------------------------------------------------------------------
174// Printing of errors that are not fatal.
175//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176void Log::Error(const char *format, ...) {
177 va_list args;
178 va_start(args, format);
179 VAError(format, args);
180 va_end(args);
Zachary Turner610e5292015-05-07 21:39:33 +0000181}
182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183void Log::VAError(const char *format, va_list args) {
184 char *arg_msg = nullptr;
185 ::vasprintf(&arg_msg, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 if (arg_msg == nullptr)
188 return;
Zachary Turnerc1592652015-04-29 22:55:28 +0000189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 Printf("error: %s", arg_msg);
191 free(arg_msg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192}
193
194//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195// Printing of warnings that are not fatal only if verbose mode is
196// enabled.
197//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198void Log::Verbose(const char *format, ...) {
199 if (!m_options.Test(LLDB_LOG_OPTION_VERBOSE))
200 return;
Zachary Turnerc1592652015-04-29 22:55:28 +0000201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 va_list args;
203 va_start(args, format);
204 VAPrintf(format, args);
205 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
208//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209// Printing of warnings that are not fatal.
210//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211void Log::Warning(const char *format, ...) {
212 char *arg_msg = nullptr;
213 va_list args;
214 va_start(args, format);
215 ::vasprintf(&arg_msg, format, args);
216 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 if (arg_msg == nullptr)
219 return;
Zachary Turnerc1592652015-04-29 22:55:28 +0000220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 Printf("warning: %s", arg_msg);
222 free(arg_msg);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223}
224
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225typedef std::map<ConstString, Log::Callbacks> CallbackMap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226typedef CallbackMap::iterator CallbackMapIter;
227
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228// Surround our callback map with a singleton function so we don't have any
229// global initializers.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230static CallbackMap &GetCallbackMap() {
231 static CallbackMap g_callback_map;
232 return g_callback_map;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233}
234
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000235void Log::Register(llvm::StringRef name, Channel &channel) {
236 auto iter = g_channel_map->try_emplace(name, channel);
237 assert(iter.second == true);
238 (void)iter;
239}
240
241void Log::Unregister(llvm::StringRef name) {
242 auto iter = g_channel_map->find(name);
243 assert(iter != g_channel_map->end());
244 iter->second.channel.Disable(UINT32_MAX);
245 g_channel_map->erase(iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248void Log::RegisterLogChannel(const ConstString &channel,
249 const Log::Callbacks &log_callbacks) {
250 GetCallbackMap().insert(std::make_pair(channel, log_callbacks));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251}
252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253bool Log::UnregisterLogChannel(const ConstString &channel) {
254 return GetCallbackMap().erase(channel) != 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255}
256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257bool Log::GetLogChannelCallbacks(const ConstString &channel,
258 Log::Callbacks &log_callbacks) {
259 CallbackMap &callback_map = GetCallbackMap();
260 CallbackMapIter pos = callback_map.find(channel);
261 if (pos != callback_map.end()) {
262 log_callbacks = pos->second;
263 return true;
264 }
265 ::memset(&log_callbacks, 0, sizeof(log_callbacks));
266 return false;
267}
268
Pavel Labath5fae71c2017-02-10 11:49:21 +0000269bool Log::EnableLogChannel(
270 const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000271 uint32_t log_options, llvm::StringRef channel, const char **categories,
Pavel Labath5fae71c2017-02-10 11:49:21 +0000272 Stream &error_stream) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 Log::Callbacks log_callbacks;
274 if (Log::GetLogChannelCallbacks(ConstString(channel), log_callbacks)) {
275 log_callbacks.enable(log_stream_sp, log_options, categories, &error_stream);
276 return true;
277 }
278
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000279 auto iter = g_channel_map->find(channel);
280 if (iter == g_channel_map->end()) {
281 error_stream.Format("Invalid log channel '{0}'.\n", channel);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 }
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000284 uint32_t flags = categories && categories[0]
285 ? GetFlags(error_stream, *iter, categories)
286 : iter->second.channel.default_flags;
Pavel Labath88d081b2017-02-27 11:05:39 +0000287 iter->second.channel.Enable(iter->second.log, log_stream_sp, log_options,
288 flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000289 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290}
291
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000292bool Log::DisableLogChannel(llvm::StringRef channel, const char **categories,
293 Stream &error_stream) {
294 Log::Callbacks log_callbacks;
295 if (Log::GetLogChannelCallbacks(ConstString(channel), log_callbacks)) {
296 log_callbacks.disable(categories, &error_stream);
297 return true;
Pavel Labath5fb8af42017-02-15 16:11:59 +0000298 }
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000299
300 auto iter = g_channel_map->find(channel);
301 if (iter == g_channel_map->end()) {
302 error_stream.Format("Invalid log channel '{0}'.\n", channel);
303 return false;
304 }
305 uint32_t flags = categories && categories[0]
306 ? GetFlags(error_stream, *iter, categories)
307 : UINT32_MAX;
308 iter->second.channel.Disable(flags);
309 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000310}
311
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000312bool Log::ListChannelCategories(llvm::StringRef channel, Stream &stream) {
313 Log::Callbacks log_callbacks;
314 if (Log::GetLogChannelCallbacks(ConstString(channel), log_callbacks)) {
315 log_callbacks.list_categories(&stream);
316 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 }
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000318
319 auto ch = g_channel_map->find(channel);
320 if (ch == g_channel_map->end()) {
321 stream.Format("Invalid log channel '{0}'.\n", channel);
322 return false;
323 }
324 ListCategories(stream, *ch);
325 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326}
327
328void Log::DisableAllLogChannels(Stream *feedback_strm) {
329 CallbackMap &callback_map = GetCallbackMap();
330 CallbackMapIter pos, end = callback_map.end();
331 const char *categories[] = {"all", nullptr};
332
333 for (pos = callback_map.begin(); pos != end; ++pos)
334 pos->second.disable(categories, feedback_strm);
335
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000336 for (auto &entry : *g_channel_map)
337 entry.second.channel.Disable(UINT32_MAX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338}
339
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340void Log::ListAllLogChannels(Stream *strm) {
341 CallbackMap &callback_map = GetCallbackMap();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000343 if (callback_map.empty() && g_channel_map->empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 strm->PutCString("No logging channels are currently registered.\n");
345 return;
346 }
347
348 CallbackMapIter pos, end = callback_map.end();
349 for (pos = callback_map.begin(); pos != end; ++pos)
350 pos->second.list_categories(strm);
351
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000352 for (const auto &channel : *g_channel_map)
353 ListCategories(*strm, channel);
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000354}
Pavel Labath0cfd7dc92017-01-13 10:41:59 +0000355bool Log::GetVerbose() const { return m_options.Test(LLDB_LOG_OPTION_VERBOSE); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356
Pavel Labath107d9bb2017-01-18 11:00:26 +0000357void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
358 llvm::StringRef function) {
359 static uint32_t g_sequence_id = 0;
360 // Add a sequence ID if requested
361 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
362 OS << ++g_sequence_id << " ";
363
364 // Timestamp if requested
365 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
366 auto now = std::chrono::duration<double>(
367 std::chrono::system_clock::now().time_since_epoch());
368 OS << llvm::formatv("{0:f9} ", now.count());
369 }
370
371 // Add the process and thread if requested
372 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
373 OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
374 Host::GetCurrentThreadID());
375
376 // Add the thread name if requested
377 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
378 llvm::SmallString<32> thread_name;
379 ThisThread::GetName(thread_name);
380 if (!thread_name.empty())
381 OS << thread_name;
382 }
383
384 if (m_options.Test(LLDB_LOG_OPTION_BACKTRACE))
385 llvm::sys::PrintStackTrace(OS);
386
387 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
388 (!file.empty() || !function.empty())) {
389 file = llvm::sys::path::filename(file).take_front(40);
390 function = function.take_front(40);
391 OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
392 }
393}
394
395void Log::WriteMessage(const std::string &message) {
396 // Make a copy of our stream shared pointer in case someone disables our
397 // log while we are logging and releases the stream
Pavel Labathba95a282017-02-21 09:58:23 +0000398 auto stream_sp = GetStream();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000399 if (!stream_sp)
400 return;
401
402 if (m_options.Test(LLDB_LOG_OPTION_THREADSAFE)) {
403 static std::recursive_mutex g_LogThreadedMutex;
404 std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex);
Pavel Labath5fae71c2017-02-10 11:49:21 +0000405 *stream_sp << message;
406 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000407 } else {
Pavel Labath5fae71c2017-02-10 11:49:21 +0000408 *stream_sp << message;
409 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000410 }
411}
412
413void Log::Format(llvm::StringRef file, llvm::StringRef function,
414 const llvm::formatv_object_base &payload) {
415 std::string message_string;
416 llvm::raw_string_ostream message(message_string);
417 WriteHeader(message, file, function);
418 message << payload << "\n";
419 WriteMessage(message.str());
420}