blob: a69ae9b0ffe572963289d517a26288da73d951bb [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
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000225void Log::Register(llvm::StringRef name, Channel &channel) {
226 auto iter = g_channel_map->try_emplace(name, channel);
227 assert(iter.second == true);
228 (void)iter;
229}
230
231void Log::Unregister(llvm::StringRef name) {
232 auto iter = g_channel_map->find(name);
233 assert(iter != g_channel_map->end());
234 iter->second.channel.Disable(UINT32_MAX);
235 g_channel_map->erase(iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236}
237
Pavel Labath5fae71c2017-02-10 11:49:21 +0000238bool Log::EnableLogChannel(
239 const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000240 uint32_t log_options, llvm::StringRef channel, const char **categories,
Pavel Labath5fae71c2017-02-10 11:49:21 +0000241 Stream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000242 auto iter = g_channel_map->find(channel);
243 if (iter == g_channel_map->end()) {
244 error_stream.Format("Invalid log channel '{0}'.\n", channel);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 }
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000247 uint32_t flags = categories && categories[0]
248 ? GetFlags(error_stream, *iter, categories)
249 : iter->second.channel.default_flags;
Pavel Labath88d081b2017-02-27 11:05:39 +0000250 iter->second.channel.Enable(iter->second.log, log_stream_sp, log_options,
251 flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000252 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253}
254
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000255bool Log::DisableLogChannel(llvm::StringRef channel, const char **categories,
256 Stream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000257 auto iter = g_channel_map->find(channel);
258 if (iter == g_channel_map->end()) {
259 error_stream.Format("Invalid log channel '{0}'.\n", channel);
260 return false;
261 }
262 uint32_t flags = categories && categories[0]
263 ? GetFlags(error_stream, *iter, categories)
264 : UINT32_MAX;
265 iter->second.channel.Disable(flags);
266 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267}
268
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000269bool Log::ListChannelCategories(llvm::StringRef channel, Stream &stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000270 auto ch = g_channel_map->find(channel);
271 if (ch == g_channel_map->end()) {
272 stream.Format("Invalid log channel '{0}'.\n", channel);
273 return false;
274 }
275 ListCategories(stream, *ch);
276 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277}
278
279void Log::DisableAllLogChannels(Stream *feedback_strm) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000280 for (auto &entry : *g_channel_map)
281 entry.second.channel.Disable(UINT32_MAX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000282}
283
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284void Log::ListAllLogChannels(Stream *strm) {
Pavel Labath3474ebc2017-02-27 12:21:16 +0000285 if (g_channel_map->empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 strm->PutCString("No logging channels are currently registered.\n");
287 return;
288 }
289
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000290 for (const auto &channel : *g_channel_map)
291 ListCategories(*strm, channel);
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000292}
Pavel Labath0cfd7dc92017-01-13 10:41:59 +0000293bool Log::GetVerbose() const { return m_options.Test(LLDB_LOG_OPTION_VERBOSE); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294
Pavel Labath107d9bb2017-01-18 11:00:26 +0000295void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
296 llvm::StringRef function) {
297 static uint32_t g_sequence_id = 0;
298 // Add a sequence ID if requested
299 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
300 OS << ++g_sequence_id << " ";
301
302 // Timestamp if requested
303 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
304 auto now = std::chrono::duration<double>(
305 std::chrono::system_clock::now().time_since_epoch());
306 OS << llvm::formatv("{0:f9} ", now.count());
307 }
308
309 // Add the process and thread if requested
310 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
311 OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
312 Host::GetCurrentThreadID());
313
314 // Add the thread name if requested
315 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
316 llvm::SmallString<32> thread_name;
317 ThisThread::GetName(thread_name);
318 if (!thread_name.empty())
319 OS << thread_name;
320 }
321
322 if (m_options.Test(LLDB_LOG_OPTION_BACKTRACE))
323 llvm::sys::PrintStackTrace(OS);
324
325 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
326 (!file.empty() || !function.empty())) {
327 file = llvm::sys::path::filename(file).take_front(40);
328 function = function.take_front(40);
329 OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
330 }
331}
332
333void Log::WriteMessage(const std::string &message) {
334 // Make a copy of our stream shared pointer in case someone disables our
335 // log while we are logging and releases the stream
Pavel Labathba95a282017-02-21 09:58:23 +0000336 auto stream_sp = GetStream();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000337 if (!stream_sp)
338 return;
339
340 if (m_options.Test(LLDB_LOG_OPTION_THREADSAFE)) {
341 static std::recursive_mutex g_LogThreadedMutex;
342 std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex);
Pavel Labath5fae71c2017-02-10 11:49:21 +0000343 *stream_sp << message;
344 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000345 } else {
Pavel Labath5fae71c2017-02-10 11:49:21 +0000346 *stream_sp << message;
347 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000348 }
349}
350
351void Log::Format(llvm::StringRef file, llvm::StringRef function,
352 const llvm::formatv_object_base &payload) {
353 std::string message_string;
354 llvm::raw_string_ostream message(message_string);
355 WriteHeader(message, file, function);
356 message << payload << "\n";
357 WriteMessage(message.str());
358}