blob: a80b106838bc58dd6735d499877e39a5492734d1 [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
Zachary Turner6f9e6902017-03-03 20:56:28 +000010#include "lldb/Utility/Log.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000011#include "lldb/Utility/VASPrintf.h"
Pavel Labath774103c2016-11-02 12:18:42 +000012
Pavel Labathfb0d22d2017-02-17 13:27:42 +000013#include "llvm/ADT/STLExtras.h"
Pavel Labath774103c2016-11-02 12:18:42 +000014#include "llvm/ADT/SmallString.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000015#include "llvm/ADT/Twine.h" // for operator+, Twine
16#include "llvm/ADT/iterator.h" // for iterator_facade_base
17
Pavel Labath774103c2016-11-02 12:18:42 +000018#include "llvm/Support/Chrono.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000019#include "llvm/Support/ManagedStatic.h" // for ManagedStatic
Pavel Labath107d9bb2017-01-18 11:00:26 +000020#include "llvm/Support/Path.h"
Pavel Labath774103c2016-11-02 12:18:42 +000021#include "llvm/Support/Signals.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000022#include "llvm/Support/Threading.h"
Pavel Labath774103c2016-11-02 12:18:42 +000023#include "llvm/Support/raw_ostream.h"
24
Zachary Turner4479ac12017-04-06 18:12:24 +000025#include <chrono> // for duration, system_clock, syst...
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000026#include <cstdarg>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000027#include <mutex>
Zachary Turner4479ac12017-04-06 18:12:24 +000028#include <utility> // for pair
29
30#include <assert.h> // for assert
31#if defined(LLVM_ON_WIN32)
32#include <process.h> // for getpid
33#else
34#include <unistd.h>
35#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037using namespace lldb_private;
38
Pavel Labathf5aaa992017-03-09 10:16:07 +000039llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;
Pavel Labathfb0d22d2017-02-17 13:27:42 +000040
Pavel Labath775588c2017-03-15 09:06:58 +000041void Log::ListCategories(llvm::raw_ostream &stream, const ChannelMap::value_type &entry) {
42 stream << llvm::formatv("Logging categories for '{0}':\n", entry.first());
43 stream << " all - all available logging categories\n";
44 stream << " default - default set of logging categories\n";
Pavel Labathf5aaa992017-03-09 10:16:07 +000045 for (const auto &category : entry.second.m_channel.categories)
Pavel Labath775588c2017-03-15 09:06:58 +000046 stream << llvm::formatv(" {0} - {1}\n", category.name,
47 category.description);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000048}
49
Pavel Labath775588c2017-03-15 09:06:58 +000050uint32_t Log::GetFlags(llvm::raw_ostream &stream, const ChannelMap::value_type &entry,
Pavel Labath5e336902017-03-01 10:08:40 +000051 llvm::ArrayRef<const char *> categories) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000052 bool list_categories = false;
53 uint32_t flags = 0;
Pavel Labath5e336902017-03-01 10:08:40 +000054 for (const char *category : categories) {
55 if (llvm::StringRef("all").equals_lower(category)) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000056 flags |= UINT32_MAX;
57 continue;
58 }
Pavel Labath5e336902017-03-01 10:08:40 +000059 if (llvm::StringRef("default").equals_lower(category)) {
Pavel Labathf5aaa992017-03-09 10:16:07 +000060 flags |= entry.second.m_channel.default_flags;
Pavel Labathfb0d22d2017-02-17 13:27:42 +000061 continue;
62 }
Pavel Labath5e336902017-03-01 10:08:40 +000063 auto cat = llvm::find_if(
Pavel Labathf5aaa992017-03-09 10:16:07 +000064 entry.second.m_channel.categories,
Pavel Labath5e336902017-03-01 10:08:40 +000065 [&](const Log::Category &c) { return c.name.equals_lower(category); });
Pavel Labathf5aaa992017-03-09 10:16:07 +000066 if (cat != entry.second.m_channel.categories.end()) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000067 flags |= cat->flag;
68 continue;
69 }
Pavel Labath775588c2017-03-15 09:06:58 +000070 stream << llvm::formatv("error: unrecognized log category '{0}'\n",
71 category);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000072 list_categories = true;
73 }
74 if (list_categories)
75 ListCategories(stream, entry);
76 return flags;
77}
78
Pavel Labathf5aaa992017-03-09 10:16:07 +000079void Log::Enable(const std::shared_ptr<llvm::raw_ostream> &stream_sp,
80 uint32_t options, uint32_t flags) {
81 llvm::sys::ScopedWriter lock(m_mutex);
82
83 uint32_t mask = m_mask.fetch_or(flags, std::memory_order_relaxed);
84 if (mask | flags) {
85 m_options.store(options, std::memory_order_relaxed);
86 m_stream_sp = stream_sp;
87 m_channel.log_ptr.store(this, std::memory_order_relaxed);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000088 }
89}
90
Pavel Labathf5aaa992017-03-09 10:16:07 +000091void Log::Disable(uint32_t flags) {
92 llvm::sys::ScopedWriter lock(m_mutex);
93
94 uint32_t mask = m_mask.fetch_and(~flags, std::memory_order_relaxed);
95 if (!(mask & ~flags)) {
96 m_stream_sp.reset();
97 m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000098 }
99}
100
Pavel Labathf5aaa992017-03-09 10:16:07 +0000101const Flags Log::GetOptions() const {
102 return m_options.load(std::memory_order_relaxed);
103}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104
Pavel Labathf5aaa992017-03-09 10:16:07 +0000105const Flags Log::GetMask() const {
106 return m_mask.load(std::memory_order_relaxed);
107}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109void Log::PutCString(const char *cstr) { Printf("%s", cstr); }
Zachary Turnerc1564272016-11-16 21:15:24 +0000110void Log::PutString(llvm::StringRef str) { PutCString(str.str().c_str()); }
Zachary Turnerc1592652015-04-29 22:55:28 +0000111
112//----------------------------------------------------------------------
113// Simple variable argument logging with flags.
114//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115void Log::Printf(const char *format, ...) {
116 va_list args;
117 va_start(args, format);
118 VAPrintf(format, args);
119 va_end(args);
Zachary Turnerc1592652015-04-29 22:55:28 +0000120}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121
122//----------------------------------------------------------------------
123// All logging eventually boils down to this function call. If we have
124// a callback registered, then we call the logging callback. If we have
125// a valid file handle, we also log to the file.
126//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127void Log::VAPrintf(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000128 llvm::SmallString<64> FinalMessage;
129 llvm::raw_svector_ostream Stream(FinalMessage);
130 WriteHeader(Stream, "", "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131
Zachary Turner6f9e6902017-03-03 20:56:28 +0000132 llvm::SmallString<64> Content;
133 lldb_private::VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134
Zachary Turner6f9e6902017-03-03 20:56:28 +0000135 Stream << Content << "\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136
Zachary Turner6f9e6902017-03-03 20:56:28 +0000137 WriteMessage(FinalMessage.str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138}
139
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141// Printing of errors that are not fatal.
142//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143void Log::Error(const char *format, ...) {
144 va_list args;
145 va_start(args, format);
146 VAError(format, args);
147 va_end(args);
Zachary Turner610e5292015-05-07 21:39:33 +0000148}
149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150void Log::VAError(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000151 llvm::SmallString<64> Content;
152 VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153
Zachary Turner6f9e6902017-03-03 20:56:28 +0000154 Printf("error: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155}
156
157//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158// Printing of warnings that are not fatal only if verbose mode is
159// enabled.
160//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161void Log::Verbose(const char *format, ...) {
Pavel Labathf5aaa992017-03-09 10:16:07 +0000162 if (!GetVerbose())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 return;
Zachary Turnerc1592652015-04-29 22:55:28 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 va_list args;
166 va_start(args, format);
167 VAPrintf(format, args);
168 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169}
170
171//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172// Printing of warnings that are not fatal.
173//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174void Log::Warning(const char *format, ...) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000175 llvm::SmallString<64> Content;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 va_list args;
177 va_start(args, format);
Zachary Turner6f9e6902017-03-03 20:56:28 +0000178 VASprintf(Content, format, args);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180
Zachary Turner6f9e6902017-03-03 20:56:28 +0000181 Printf("warning: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182}
183
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000184void Log::Register(llvm::StringRef name, Channel &channel) {
185 auto iter = g_channel_map->try_emplace(name, channel);
186 assert(iter.second == true);
187 (void)iter;
188}
189
190void Log::Unregister(llvm::StringRef name) {
191 auto iter = g_channel_map->find(name);
192 assert(iter != g_channel_map->end());
Pavel Labathf5aaa992017-03-09 10:16:07 +0000193 iter->second.Disable(UINT32_MAX);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000194 g_channel_map->erase(iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195}
196
Pavel Labath5fae71c2017-02-10 11:49:21 +0000197bool Log::EnableLogChannel(
198 const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
Pavel Labath5e336902017-03-01 10:08:40 +0000199 uint32_t log_options, llvm::StringRef channel,
Pavel Labath775588c2017-03-15 09:06:58 +0000200 llvm::ArrayRef<const char *> categories, llvm::raw_ostream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000201 auto iter = g_channel_map->find(channel);
202 if (iter == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000203 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 }
Pavel Labath5e336902017-03-01 10:08:40 +0000206 uint32_t flags = categories.empty()
Pavel Labathf5aaa992017-03-09 10:16:07 +0000207 ? iter->second.m_channel.default_flags
Pavel Labath5e336902017-03-01 10:08:40 +0000208 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000209 iter->second.Enable(log_stream_sp, log_options, flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000210 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211}
212
Pavel Labath5e336902017-03-01 10:08:40 +0000213bool Log::DisableLogChannel(llvm::StringRef channel,
214 llvm::ArrayRef<const char *> categories,
Pavel Labath775588c2017-03-15 09:06:58 +0000215 llvm::raw_ostream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000216 auto iter = g_channel_map->find(channel);
217 if (iter == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000218 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000219 return false;
220 }
Pavel Labath5e336902017-03-01 10:08:40 +0000221 uint32_t flags = categories.empty()
222 ? UINT32_MAX
223 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000224 iter->second.Disable(flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000225 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226}
227
Pavel Labath775588c2017-03-15 09:06:58 +0000228bool Log::ListChannelCategories(llvm::StringRef channel,
229 llvm::raw_ostream &stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000230 auto ch = g_channel_map->find(channel);
231 if (ch == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000232 stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000233 return false;
234 }
235 ListCategories(stream, *ch);
236 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237}
238
Pavel Labath775588c2017-03-15 09:06:58 +0000239void Log::DisableAllLogChannels() {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000240 for (auto &entry : *g_channel_map)
Pavel Labathf5aaa992017-03-09 10:16:07 +0000241 entry.second.Disable(UINT32_MAX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242}
243
Pavel Labath775588c2017-03-15 09:06:58 +0000244void Log::ListAllLogChannels(llvm::raw_ostream &stream) {
Pavel Labath3474ebc2017-02-27 12:21:16 +0000245 if (g_channel_map->empty()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000246 stream << "No logging channels are currently registered.\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 return;
248 }
249
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000250 for (const auto &channel : *g_channel_map)
Pavel Labath775588c2017-03-15 09:06:58 +0000251 ListCategories(stream, channel);
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000252}
Pavel Labath775588c2017-03-15 09:06:58 +0000253
Pavel Labathf5aaa992017-03-09 10:16:07 +0000254bool Log::GetVerbose() const {
255 return m_options.load(std::memory_order_relaxed) & LLDB_LOG_OPTION_VERBOSE;
256}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257
Pavel Labath107d9bb2017-01-18 11:00:26 +0000258void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
259 llvm::StringRef function) {
Pavel Labathf5aaa992017-03-09 10:16:07 +0000260 Flags options = GetOptions();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000261 static uint32_t g_sequence_id = 0;
262 // Add a sequence ID if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000263 if (options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000264 OS << ++g_sequence_id << " ";
265
266 // Timestamp if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000267 if (options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000268 auto now = std::chrono::duration<double>(
269 std::chrono::system_clock::now().time_since_epoch());
270 OS << llvm::formatv("{0:f9} ", now.count());
271 }
272
273 // Add the process and thread if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000274 if (options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000275 OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
Zachary Turner6f9e6902017-03-03 20:56:28 +0000276 llvm::get_threadid());
Pavel Labath107d9bb2017-01-18 11:00:26 +0000277
278 // Add the thread name if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000279 if (options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000280 llvm::SmallString<32> thread_name;
Zachary Turner6f9e6902017-03-03 20:56:28 +0000281 llvm::get_thread_name(thread_name);
Pavel Labath107d9bb2017-01-18 11:00:26 +0000282 if (!thread_name.empty())
283 OS << thread_name;
284 }
285
Pavel Labathf5aaa992017-03-09 10:16:07 +0000286 if (options.Test(LLDB_LOG_OPTION_BACKTRACE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000287 llvm::sys::PrintStackTrace(OS);
288
Pavel Labathf5aaa992017-03-09 10:16:07 +0000289 if (options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
Pavel Labath107d9bb2017-01-18 11:00:26 +0000290 (!file.empty() || !function.empty())) {
291 file = llvm::sys::path::filename(file).take_front(40);
292 function = function.take_front(40);
293 OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
294 }
295}
296
297void Log::WriteMessage(const std::string &message) {
298 // Make a copy of our stream shared pointer in case someone disables our
299 // log while we are logging and releases the stream
Pavel Labathba95a282017-02-21 09:58:23 +0000300 auto stream_sp = GetStream();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000301 if (!stream_sp)
302 return;
303
Pavel Labathf5aaa992017-03-09 10:16:07 +0000304 Flags options = GetOptions();
305 if (options.Test(LLDB_LOG_OPTION_THREADSAFE)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000306 static std::recursive_mutex g_LogThreadedMutex;
307 std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex);
Pavel Labath5fae71c2017-02-10 11:49:21 +0000308 *stream_sp << message;
309 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000310 } else {
Pavel Labath5fae71c2017-02-10 11:49:21 +0000311 *stream_sp << message;
312 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000313 }
314}
315
316void Log::Format(llvm::StringRef file, llvm::StringRef function,
317 const llvm::formatv_object_base &payload) {
318 std::string message_string;
319 llvm::raw_string_ostream message(message_string);
320 WriteHeader(message, file, function);
321 message << payload << "\n";
322 WriteMessage(message.str());
323}