blob: c87e289aff4927605400fa54a8f3e6a374a130ca [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
Zachary Turner6f9e6902017-03-03 20:56:28 +000011#include "lldb/Utility/Log.h"
12
Pavel Labath774103c2016-11-02 12:18:42 +000013#include "lldb/Utility/NameMatches.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000014#include "lldb/Utility/StreamString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000015#include "lldb/Utility/VASPrintf.h"
Pavel Labath774103c2016-11-02 12:18:42 +000016
17// Other libraries and framework includes
Pavel Labathfb0d22d2017-02-17 13:27:42 +000018#include "llvm/ADT/STLExtras.h"
Pavel Labath774103c2016-11-02 12:18:42 +000019#include "llvm/ADT/SmallString.h"
20#include "llvm/Support/Chrono.h"
Pavel Labathfb0d22d2017-02-17 13:27:42 +000021#include "llvm/Support/ManagedStatic.h"
Pavel Labath107d9bb2017-01-18 11:00:26 +000022#include "llvm/Support/Path.h"
Pavel Labath774103c2016-11-02 12:18:42 +000023#include "llvm/Support/Signals.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000024#include "llvm/Support/Threading.h"
Pavel Labath774103c2016-11-02 12:18:42 +000025#include "llvm/Support/raw_ostream.h"
26
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027// C Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028// C++ Includes
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000029#include <cstdarg>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000030#include <cstdio>
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000031#include <cstdlib>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include <map>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000033#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include <string>
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036using namespace lldb;
37using namespace lldb_private;
38
Pavel Labathfb0d22d2017-02-17 13:27:42 +000039namespace {
40 struct ChannelAndLog {
41 Log log;
42 Log::Channel &channel;
43
44 ChannelAndLog(Log::Channel &channel) : channel(channel) {}
45 };
46 typedef llvm::StringMap<ChannelAndLog> ChannelMap;
47}
48
49static llvm::ManagedStatic<ChannelMap> g_channel_map;
50
51static void ListCategories(Stream &stream, const ChannelMap::value_type &entry) {
52 stream.Format("Logging categories for '{0}':\n", entry.first());
53 stream.Format(" all - all available logging categories\n");
54 stream.Format(" default - default set of logging categories\n");
55 for (const auto &category : entry.second.channel.categories)
56 stream.Format(" {0} - {1}\n", category.name, category.description);
57}
58
59static uint32_t GetFlags(Stream &stream, const ChannelMap::value_type &entry,
Pavel Labath5e336902017-03-01 10:08:40 +000060 llvm::ArrayRef<const char *> categories) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000061 bool list_categories = false;
62 uint32_t flags = 0;
Pavel Labath5e336902017-03-01 10:08:40 +000063 for (const char *category : categories) {
64 if (llvm::StringRef("all").equals_lower(category)) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000065 flags |= UINT32_MAX;
66 continue;
67 }
Pavel Labath5e336902017-03-01 10:08:40 +000068 if (llvm::StringRef("default").equals_lower(category)) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000069 flags |= entry.second.channel.default_flags;
70 continue;
71 }
Pavel Labath5e336902017-03-01 10:08:40 +000072 auto cat = llvm::find_if(
73 entry.second.channel.categories,
74 [&](const Log::Category &c) { return c.name.equals_lower(category); });
Pavel Labathfb0d22d2017-02-17 13:27:42 +000075 if (cat != entry.second.channel.categories.end()) {
76 flags |= cat->flag;
77 continue;
78 }
Pavel Labath5e336902017-03-01 10:08:40 +000079 stream.Format("error: unrecognized log category '{0}'\n", category);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000080 list_categories = true;
81 }
82 if (list_categories)
83 ListCategories(stream, entry);
84 return flags;
85}
86
87void Log::Channel::Enable(Log &log,
88 const std::shared_ptr<llvm::raw_ostream> &stream_sp,
Pavel Labath88d081b2017-02-27 11:05:39 +000089 uint32_t options, uint32_t flags) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000090 log.GetMask().Set(flags);
91 if (log.GetMask().Get()) {
Pavel Labatha96eac62017-03-06 19:10:19 +000092 log.GetOptions().Reset(options);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000093 log.SetStream(stream_sp);
94 log_ptr.store(&log, std::memory_order_release);
95 }
96}
97
98void Log::Channel::Disable(uint32_t flags) {
99 Log *log = log_ptr.load(std::memory_order_acquire);
100 if (!log)
101 return;
102 log->GetMask().Clear(flags);
103 if (!log->GetMask().Get()) {
104 log->SetStream(nullptr);
105 log_ptr.store(nullptr, std::memory_order_release);
106 }
107}
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109Log::Log() : m_stream_sp(), m_options(0), m_mask_bits(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110
Pavel Labath5fae71c2017-02-10 11:49:21 +0000111Log::Log(const std::shared_ptr<llvm::raw_ostream> &stream_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 : m_stream_sp(stream_sp), m_options(0), m_mask_bits(0) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +0000114Log::~Log() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116Flags &Log::GetOptions() { return m_options; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118const Flags &Log::GetOptions() const { return m_options; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120Flags &Log::GetMask() { return m_mask_bits; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122const Flags &Log::GetMask() const { return m_mask_bits; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124void Log::PutCString(const char *cstr) { Printf("%s", cstr); }
Zachary Turnerc1564272016-11-16 21:15:24 +0000125void Log::PutString(llvm::StringRef str) { PutCString(str.str().c_str()); }
Zachary Turnerc1592652015-04-29 22:55:28 +0000126
127//----------------------------------------------------------------------
128// Simple variable argument logging with flags.
129//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130void Log::Printf(const char *format, ...) {
131 va_list args;
132 va_start(args, format);
133 VAPrintf(format, args);
134 va_end(args);
Zachary Turnerc1592652015-04-29 22:55:28 +0000135}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136
137//----------------------------------------------------------------------
138// All logging eventually boils down to this function call. If we have
139// a callback registered, then we call the logging callback. If we have
140// a valid file handle, we also log to the file.
141//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142void Log::VAPrintf(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000143 llvm::SmallString<64> FinalMessage;
144 llvm::raw_svector_ostream Stream(FinalMessage);
145 WriteHeader(Stream, "", "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146
Zachary Turner6f9e6902017-03-03 20:56:28 +0000147 llvm::SmallString<64> Content;
148 lldb_private::VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149
Zachary Turner6f9e6902017-03-03 20:56:28 +0000150 Stream << Content << "\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151
Zachary Turner6f9e6902017-03-03 20:56:28 +0000152 WriteMessage(FinalMessage.str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153}
154
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156// Log only if all of the bits are set
157//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158void Log::LogIf(uint32_t bits, const char *format, ...) {
159 if (!m_options.AllSet(bits))
160 return;
Zachary Turnerc1592652015-04-29 22:55:28 +0000161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 va_list args;
163 va_start(args, format);
164 VAPrintf(format, args);
165 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166}
167
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168//----------------------------------------------------------------------
169// Printing of errors that are not fatal.
170//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171void Log::Error(const char *format, ...) {
172 va_list args;
173 va_start(args, format);
174 VAError(format, args);
175 va_end(args);
Zachary Turner610e5292015-05-07 21:39:33 +0000176}
177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178void Log::VAError(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000179 llvm::SmallString<64> Content;
180 VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181
Zachary Turner6f9e6902017-03-03 20:56:28 +0000182 Printf("error: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183}
184
185//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186// Printing of warnings that are not fatal only if verbose mode is
187// enabled.
188//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189void Log::Verbose(const char *format, ...) {
190 if (!m_options.Test(LLDB_LOG_OPTION_VERBOSE))
191 return;
Zachary Turnerc1592652015-04-29 22:55:28 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 va_list args;
194 va_start(args, format);
195 VAPrintf(format, args);
196 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197}
198
199//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200// Printing of warnings that are not fatal.
201//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202void Log::Warning(const char *format, ...) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000203 llvm::SmallString<64> Content;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 va_list args;
205 va_start(args, format);
Zachary Turner6f9e6902017-03-03 20:56:28 +0000206 VASprintf(Content, format, args);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208
Zachary Turner6f9e6902017-03-03 20:56:28 +0000209 Printf("warning: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210}
211
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000212void Log::Register(llvm::StringRef name, Channel &channel) {
213 auto iter = g_channel_map->try_emplace(name, channel);
214 assert(iter.second == true);
215 (void)iter;
216}
217
218void Log::Unregister(llvm::StringRef name) {
219 auto iter = g_channel_map->find(name);
220 assert(iter != g_channel_map->end());
221 iter->second.channel.Disable(UINT32_MAX);
222 g_channel_map->erase(iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223}
224
Pavel Labath5fae71c2017-02-10 11:49:21 +0000225bool Log::EnableLogChannel(
226 const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
Pavel Labath5e336902017-03-01 10:08:40 +0000227 uint32_t log_options, llvm::StringRef channel,
228 llvm::ArrayRef<const char *> categories, Stream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000229 auto iter = g_channel_map->find(channel);
230 if (iter == g_channel_map->end()) {
231 error_stream.Format("Invalid log channel '{0}'.\n", channel);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 }
Pavel Labath5e336902017-03-01 10:08:40 +0000234 uint32_t flags = categories.empty()
235 ? iter->second.channel.default_flags
236 : GetFlags(error_stream, *iter, categories);
Pavel Labath88d081b2017-02-27 11:05:39 +0000237 iter->second.channel.Enable(iter->second.log, log_stream_sp, log_options,
238 flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000239 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240}
241
Pavel Labath5e336902017-03-01 10:08:40 +0000242bool Log::DisableLogChannel(llvm::StringRef channel,
243 llvm::ArrayRef<const char *> categories,
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000244 Stream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000245 auto iter = g_channel_map->find(channel);
246 if (iter == g_channel_map->end()) {
247 error_stream.Format("Invalid log channel '{0}'.\n", channel);
248 return false;
249 }
Pavel Labath5e336902017-03-01 10:08:40 +0000250 uint32_t flags = categories.empty()
251 ? UINT32_MAX
252 : GetFlags(error_stream, *iter, categories);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000253 iter->second.channel.Disable(flags);
254 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255}
256
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000257bool Log::ListChannelCategories(llvm::StringRef channel, Stream &stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000258 auto ch = g_channel_map->find(channel);
259 if (ch == g_channel_map->end()) {
260 stream.Format("Invalid log channel '{0}'.\n", channel);
261 return false;
262 }
263 ListCategories(stream, *ch);
264 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265}
266
267void Log::DisableAllLogChannels(Stream *feedback_strm) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000268 for (auto &entry : *g_channel_map)
269 entry.second.channel.Disable(UINT32_MAX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270}
271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272void Log::ListAllLogChannels(Stream *strm) {
Pavel Labath3474ebc2017-02-27 12:21:16 +0000273 if (g_channel_map->empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 strm->PutCString("No logging channels are currently registered.\n");
275 return;
276 }
277
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000278 for (const auto &channel : *g_channel_map)
279 ListCategories(*strm, channel);
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000280}
Pavel Labath0cfd7dc92017-01-13 10:41:59 +0000281bool Log::GetVerbose() const { return m_options.Test(LLDB_LOG_OPTION_VERBOSE); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282
Pavel Labath107d9bb2017-01-18 11:00:26 +0000283void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
284 llvm::StringRef function) {
285 static uint32_t g_sequence_id = 0;
286 // Add a sequence ID if requested
287 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
288 OS << ++g_sequence_id << " ";
289
290 // Timestamp if requested
291 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
292 auto now = std::chrono::duration<double>(
293 std::chrono::system_clock::now().time_since_epoch());
294 OS << llvm::formatv("{0:f9} ", now.count());
295 }
296
297 // Add the process and thread if requested
298 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
299 OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
Zachary Turner6f9e6902017-03-03 20:56:28 +0000300 llvm::get_threadid());
Pavel Labath107d9bb2017-01-18 11:00:26 +0000301
302 // Add the thread name if requested
303 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
304 llvm::SmallString<32> thread_name;
Zachary Turner6f9e6902017-03-03 20:56:28 +0000305 llvm::get_thread_name(thread_name);
Pavel Labath107d9bb2017-01-18 11:00:26 +0000306 if (!thread_name.empty())
307 OS << thread_name;
308 }
309
310 if (m_options.Test(LLDB_LOG_OPTION_BACKTRACE))
311 llvm::sys::PrintStackTrace(OS);
312
313 if (m_options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
314 (!file.empty() || !function.empty())) {
315 file = llvm::sys::path::filename(file).take_front(40);
316 function = function.take_front(40);
317 OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
318 }
319}
320
321void Log::WriteMessage(const std::string &message) {
322 // Make a copy of our stream shared pointer in case someone disables our
323 // log while we are logging and releases the stream
Pavel Labathba95a282017-02-21 09:58:23 +0000324 auto stream_sp = GetStream();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000325 if (!stream_sp)
326 return;
327
328 if (m_options.Test(LLDB_LOG_OPTION_THREADSAFE)) {
329 static std::recursive_mutex g_LogThreadedMutex;
330 std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex);
Pavel Labath5fae71c2017-02-10 11:49:21 +0000331 *stream_sp << message;
332 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000333 } else {
Pavel Labath5fae71c2017-02-10 11:49:21 +0000334 *stream_sp << message;
335 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000336 }
337}
338
339void Log::Format(llvm::StringRef file, llvm::StringRef function,
340 const llvm::formatv_object_base &payload) {
341 std::string message_string;
342 llvm::raw_string_ostream message(message_string);
343 WriteHeader(message, file, function);
344 message << payload << "\n";
345 WriteMessage(message.str());
346}