blob: 49de39f72bf5b4216668e105715a4e036c3099dc [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 Labath107d9bb2017-01-18 11:00:26 +000021#include "llvm/Support/Path.h"
Pavel Labath774103c2016-11-02 12:18:42 +000022#include "llvm/Support/Signals.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000023#include "llvm/Support/Threading.h"
Pavel Labath774103c2016-11-02 12:18:42 +000024#include "llvm/Support/raw_ostream.h"
25
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026// C Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027// C++ Includes
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000028#include <cstdarg>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000029#include <cstdio>
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000030#include <cstdlib>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include <map>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000032#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include <string>
34
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035using namespace lldb;
36using namespace lldb_private;
37
Pavel Labathf5aaa992017-03-09 10:16:07 +000038llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;
Pavel Labathfb0d22d2017-02-17 13:27:42 +000039
Pavel Labathf5aaa992017-03-09 10:16:07 +000040void Log::ListCategories(Stream &stream, const ChannelMap::value_type &entry) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000041 stream.Format("Logging categories for '{0}':\n", entry.first());
42 stream.Format(" all - all available logging categories\n");
43 stream.Format(" default - default set of logging categories\n");
Pavel Labathf5aaa992017-03-09 10:16:07 +000044 for (const auto &category : entry.second.m_channel.categories)
Pavel Labathfb0d22d2017-02-17 13:27:42 +000045 stream.Format(" {0} - {1}\n", category.name, category.description);
46}
47
Pavel Labathf5aaa992017-03-09 10:16:07 +000048uint32_t Log::GetFlags(Stream &stream, const ChannelMap::value_type &entry,
Pavel Labath5e336902017-03-01 10:08:40 +000049 llvm::ArrayRef<const char *> categories) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000050 bool list_categories = false;
51 uint32_t flags = 0;
Pavel Labath5e336902017-03-01 10:08:40 +000052 for (const char *category : categories) {
53 if (llvm::StringRef("all").equals_lower(category)) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000054 flags |= UINT32_MAX;
55 continue;
56 }
Pavel Labath5e336902017-03-01 10:08:40 +000057 if (llvm::StringRef("default").equals_lower(category)) {
Pavel Labathf5aaa992017-03-09 10:16:07 +000058 flags |= entry.second.m_channel.default_flags;
Pavel Labathfb0d22d2017-02-17 13:27:42 +000059 continue;
60 }
Pavel Labath5e336902017-03-01 10:08:40 +000061 auto cat = llvm::find_if(
Pavel Labathf5aaa992017-03-09 10:16:07 +000062 entry.second.m_channel.categories,
Pavel Labath5e336902017-03-01 10:08:40 +000063 [&](const Log::Category &c) { return c.name.equals_lower(category); });
Pavel Labathf5aaa992017-03-09 10:16:07 +000064 if (cat != entry.second.m_channel.categories.end()) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000065 flags |= cat->flag;
66 continue;
67 }
Pavel Labath5e336902017-03-01 10:08:40 +000068 stream.Format("error: unrecognized log category '{0}'\n", category);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000069 list_categories = true;
70 }
71 if (list_categories)
72 ListCategories(stream, entry);
73 return flags;
74}
75
Pavel Labathf5aaa992017-03-09 10:16:07 +000076void Log::Enable(const std::shared_ptr<llvm::raw_ostream> &stream_sp,
77 uint32_t options, uint32_t flags) {
78 llvm::sys::ScopedWriter lock(m_mutex);
79
80 uint32_t mask = m_mask.fetch_or(flags, std::memory_order_relaxed);
81 if (mask | flags) {
82 m_options.store(options, std::memory_order_relaxed);
83 m_stream_sp = stream_sp;
84 m_channel.log_ptr.store(this, std::memory_order_relaxed);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000085 }
86}
87
Pavel Labathf5aaa992017-03-09 10:16:07 +000088void Log::Disable(uint32_t flags) {
89 llvm::sys::ScopedWriter lock(m_mutex);
90
91 uint32_t mask = m_mask.fetch_and(~flags, std::memory_order_relaxed);
92 if (!(mask & ~flags)) {
93 m_stream_sp.reset();
94 m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000095 }
96}
97
Pavel Labathf5aaa992017-03-09 10:16:07 +000098const Flags Log::GetOptions() const {
99 return m_options.load(std::memory_order_relaxed);
100}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101
Pavel Labathf5aaa992017-03-09 10:16:07 +0000102const Flags Log::GetMask() const {
103 return m_mask.load(std::memory_order_relaxed);
104}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106void Log::PutCString(const char *cstr) { Printf("%s", cstr); }
Zachary Turnerc1564272016-11-16 21:15:24 +0000107void Log::PutString(llvm::StringRef str) { PutCString(str.str().c_str()); }
Zachary Turnerc1592652015-04-29 22:55:28 +0000108
109//----------------------------------------------------------------------
110// Simple variable argument logging with flags.
111//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112void Log::Printf(const char *format, ...) {
113 va_list args;
114 va_start(args, format);
115 VAPrintf(format, args);
116 va_end(args);
Zachary Turnerc1592652015-04-29 22:55:28 +0000117}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118
119//----------------------------------------------------------------------
120// All logging eventually boils down to this function call. If we have
121// a callback registered, then we call the logging callback. If we have
122// a valid file handle, we also log to the file.
123//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124void Log::VAPrintf(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000125 llvm::SmallString<64> FinalMessage;
126 llvm::raw_svector_ostream Stream(FinalMessage);
127 WriteHeader(Stream, "", "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128
Zachary Turner6f9e6902017-03-03 20:56:28 +0000129 llvm::SmallString<64> Content;
130 lldb_private::VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131
Zachary Turner6f9e6902017-03-03 20:56:28 +0000132 Stream << Content << "\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133
Zachary Turner6f9e6902017-03-03 20:56:28 +0000134 WriteMessage(FinalMessage.str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135}
136
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138// Printing of errors that are not fatal.
139//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140void Log::Error(const char *format, ...) {
141 va_list args;
142 va_start(args, format);
143 VAError(format, args);
144 va_end(args);
Zachary Turner610e5292015-05-07 21:39:33 +0000145}
146
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147void Log::VAError(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000148 llvm::SmallString<64> Content;
149 VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150
Zachary Turner6f9e6902017-03-03 20:56:28 +0000151 Printf("error: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152}
153
154//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155// Printing of warnings that are not fatal only if verbose mode is
156// enabled.
157//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158void Log::Verbose(const char *format, ...) {
Pavel Labathf5aaa992017-03-09 10:16:07 +0000159 if (!GetVerbose())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 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
168//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169// Printing of warnings that are not fatal.
170//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171void Log::Warning(const char *format, ...) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000172 llvm::SmallString<64> Content;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 va_list args;
174 va_start(args, format);
Zachary Turner6f9e6902017-03-03 20:56:28 +0000175 VASprintf(Content, format, args);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177
Zachary Turner6f9e6902017-03-03 20:56:28 +0000178 Printf("warning: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179}
180
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000181void Log::Register(llvm::StringRef name, Channel &channel) {
182 auto iter = g_channel_map->try_emplace(name, channel);
183 assert(iter.second == true);
184 (void)iter;
185}
186
187void Log::Unregister(llvm::StringRef name) {
188 auto iter = g_channel_map->find(name);
189 assert(iter != g_channel_map->end());
Pavel Labathf5aaa992017-03-09 10:16:07 +0000190 iter->second.Disable(UINT32_MAX);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000191 g_channel_map->erase(iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192}
193
Pavel Labath5fae71c2017-02-10 11:49:21 +0000194bool Log::EnableLogChannel(
195 const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
Pavel Labath5e336902017-03-01 10:08:40 +0000196 uint32_t log_options, llvm::StringRef channel,
197 llvm::ArrayRef<const char *> categories, Stream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000198 auto iter = g_channel_map->find(channel);
199 if (iter == g_channel_map->end()) {
200 error_stream.Format("Invalid log channel '{0}'.\n", channel);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 }
Pavel Labath5e336902017-03-01 10:08:40 +0000203 uint32_t flags = categories.empty()
Pavel Labathf5aaa992017-03-09 10:16:07 +0000204 ? iter->second.m_channel.default_flags
Pavel Labath5e336902017-03-01 10:08:40 +0000205 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000206 iter->second.Enable(log_stream_sp, log_options, flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000207 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208}
209
Pavel Labath5e336902017-03-01 10:08:40 +0000210bool Log::DisableLogChannel(llvm::StringRef channel,
211 llvm::ArrayRef<const char *> categories,
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000212 Stream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000213 auto iter = g_channel_map->find(channel);
214 if (iter == g_channel_map->end()) {
215 error_stream.Format("Invalid log channel '{0}'.\n", channel);
216 return false;
217 }
Pavel Labath5e336902017-03-01 10:08:40 +0000218 uint32_t flags = categories.empty()
219 ? UINT32_MAX
220 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000221 iter->second.Disable(flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000222 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223}
224
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000225bool Log::ListChannelCategories(llvm::StringRef channel, Stream &stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000226 auto ch = g_channel_map->find(channel);
227 if (ch == g_channel_map->end()) {
228 stream.Format("Invalid log channel '{0}'.\n", channel);
229 return false;
230 }
231 ListCategories(stream, *ch);
232 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233}
234
235void Log::DisableAllLogChannels(Stream *feedback_strm) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000236 for (auto &entry : *g_channel_map)
Pavel Labathf5aaa992017-03-09 10:16:07 +0000237 entry.second.Disable(UINT32_MAX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238}
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240void Log::ListAllLogChannels(Stream *strm) {
Pavel Labath3474ebc2017-02-27 12:21:16 +0000241 if (g_channel_map->empty()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 strm->PutCString("No logging channels are currently registered.\n");
243 return;
244 }
245
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000246 for (const auto &channel : *g_channel_map)
247 ListCategories(*strm, channel);
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000248}
Pavel Labathf5aaa992017-03-09 10:16:07 +0000249bool Log::GetVerbose() const {
250 return m_options.load(std::memory_order_relaxed) & LLDB_LOG_OPTION_VERBOSE;
251}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252
Pavel Labath107d9bb2017-01-18 11:00:26 +0000253void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
254 llvm::StringRef function) {
Pavel Labathf5aaa992017-03-09 10:16:07 +0000255 Flags options = GetOptions();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000256 static uint32_t g_sequence_id = 0;
257 // Add a sequence ID if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000258 if (options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000259 OS << ++g_sequence_id << " ";
260
261 // Timestamp if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000262 if (options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000263 auto now = std::chrono::duration<double>(
264 std::chrono::system_clock::now().time_since_epoch());
265 OS << llvm::formatv("{0:f9} ", now.count());
266 }
267
268 // Add the process and thread if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000269 if (options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000270 OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
Zachary Turner6f9e6902017-03-03 20:56:28 +0000271 llvm::get_threadid());
Pavel Labath107d9bb2017-01-18 11:00:26 +0000272
273 // Add the thread name if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000274 if (options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000275 llvm::SmallString<32> thread_name;
Zachary Turner6f9e6902017-03-03 20:56:28 +0000276 llvm::get_thread_name(thread_name);
Pavel Labath107d9bb2017-01-18 11:00:26 +0000277 if (!thread_name.empty())
278 OS << thread_name;
279 }
280
Pavel Labathf5aaa992017-03-09 10:16:07 +0000281 if (options.Test(LLDB_LOG_OPTION_BACKTRACE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000282 llvm::sys::PrintStackTrace(OS);
283
Pavel Labathf5aaa992017-03-09 10:16:07 +0000284 if (options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
Pavel Labath107d9bb2017-01-18 11:00:26 +0000285 (!file.empty() || !function.empty())) {
286 file = llvm::sys::path::filename(file).take_front(40);
287 function = function.take_front(40);
288 OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
289 }
290}
291
292void Log::WriteMessage(const std::string &message) {
293 // Make a copy of our stream shared pointer in case someone disables our
294 // log while we are logging and releases the stream
Pavel Labathba95a282017-02-21 09:58:23 +0000295 auto stream_sp = GetStream();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000296 if (!stream_sp)
297 return;
298
Pavel Labathf5aaa992017-03-09 10:16:07 +0000299 Flags options = GetOptions();
300 if (options.Test(LLDB_LOG_OPTION_THREADSAFE)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000301 static std::recursive_mutex g_LogThreadedMutex;
302 std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex);
Pavel Labath5fae71c2017-02-10 11:49:21 +0000303 *stream_sp << message;
304 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000305 } else {
Pavel Labath5fae71c2017-02-10 11:49:21 +0000306 *stream_sp << message;
307 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000308 }
309}
310
311void Log::Format(llvm::StringRef file, llvm::StringRef function,
312 const llvm::formatv_object_base &payload) {
313 std::string message_string;
314 llvm::raw_string_ostream message(message_string);
315 WriteHeader(message, file, function);
316 message << payload << "\n";
317 WriteMessage(message.str());
318}