blob: 2e8570b762aff9a42af87fc3d84beeb93be086e7 [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"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000015#include "llvm/ADT/Twine.h"
16#include "llvm/ADT/iterator.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000017
Pavel Labath774103c2016-11-02 12:18:42 +000018#include "llvm/Support/Chrono.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000019#include "llvm/Support/ManagedStatic.h"
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
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000025#include <chrono>
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000026#include <cstdarg>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000027#include <mutex>
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000028#include <utility>
Zachary Turner4479ac12017-04-06 18:12:24 +000029
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000030#include <assert.h>
Nico Weberb1cb0b792018-04-10 13:33:45 +000031#if defined(_WIN32)
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000032#include <process.h>
Zachary Turner4479ac12017-04-06 18:12:24 +000033#else
34#include <unistd.h>
Pavel Labathd8133092017-10-23 19:41:17 +000035#include <pthread.h>
Zachary Turner4479ac12017-04-06 18:12:24 +000036#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038using namespace lldb_private;
39
Pavel Labathf5aaa992017-03-09 10:16:07 +000040llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;
Pavel Labathfb0d22d2017-02-17 13:27:42 +000041
Pavel Labath775588c2017-03-15 09:06:58 +000042void Log::ListCategories(llvm::raw_ostream &stream, const ChannelMap::value_type &entry) {
43 stream << llvm::formatv("Logging categories for '{0}':\n", entry.first());
44 stream << " all - all available logging categories\n";
45 stream << " default - default set of logging categories\n";
Pavel Labathf5aaa992017-03-09 10:16:07 +000046 for (const auto &category : entry.second.m_channel.categories)
Pavel Labath775588c2017-03-15 09:06:58 +000047 stream << llvm::formatv(" {0} - {1}\n", category.name,
48 category.description);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000049}
50
Pavel Labath775588c2017-03-15 09:06:58 +000051uint32_t Log::GetFlags(llvm::raw_ostream &stream, const ChannelMap::value_type &entry,
Pavel Labath5e336902017-03-01 10:08:40 +000052 llvm::ArrayRef<const char *> categories) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000053 bool list_categories = false;
54 uint32_t flags = 0;
Pavel Labath5e336902017-03-01 10:08:40 +000055 for (const char *category : categories) {
56 if (llvm::StringRef("all").equals_lower(category)) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000057 flags |= UINT32_MAX;
58 continue;
59 }
Pavel Labath5e336902017-03-01 10:08:40 +000060 if (llvm::StringRef("default").equals_lower(category)) {
Pavel Labathf5aaa992017-03-09 10:16:07 +000061 flags |= entry.second.m_channel.default_flags;
Pavel Labathfb0d22d2017-02-17 13:27:42 +000062 continue;
63 }
Pavel Labath5e336902017-03-01 10:08:40 +000064 auto cat = llvm::find_if(
Pavel Labathf5aaa992017-03-09 10:16:07 +000065 entry.second.m_channel.categories,
Pavel Labath5e336902017-03-01 10:08:40 +000066 [&](const Log::Category &c) { return c.name.equals_lower(category); });
Pavel Labathf5aaa992017-03-09 10:16:07 +000067 if (cat != entry.second.m_channel.categories.end()) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000068 flags |= cat->flag;
69 continue;
70 }
Pavel Labath775588c2017-03-15 09:06:58 +000071 stream << llvm::formatv("error: unrecognized log category '{0}'\n",
72 category);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000073 list_categories = true;
74 }
75 if (list_categories)
76 ListCategories(stream, entry);
77 return flags;
78}
79
Pavel Labathf5aaa992017-03-09 10:16:07 +000080void Log::Enable(const std::shared_ptr<llvm::raw_ostream> &stream_sp,
81 uint32_t options, uint32_t flags) {
82 llvm::sys::ScopedWriter lock(m_mutex);
83
84 uint32_t mask = m_mask.fetch_or(flags, std::memory_order_relaxed);
85 if (mask | flags) {
86 m_options.store(options, std::memory_order_relaxed);
87 m_stream_sp = stream_sp;
88 m_channel.log_ptr.store(this, std::memory_order_relaxed);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000089 }
90}
91
Pavel Labathf5aaa992017-03-09 10:16:07 +000092void Log::Disable(uint32_t flags) {
93 llvm::sys::ScopedWriter lock(m_mutex);
94
95 uint32_t mask = m_mask.fetch_and(~flags, std::memory_order_relaxed);
96 if (!(mask & ~flags)) {
97 m_stream_sp.reset();
98 m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000099 }
100}
101
Pavel Labathf5aaa992017-03-09 10:16:07 +0000102const Flags Log::GetOptions() const {
103 return m_options.load(std::memory_order_relaxed);
104}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105
Pavel Labathf5aaa992017-03-09 10:16:07 +0000106const Flags Log::GetMask() const {
107 return m_mask.load(std::memory_order_relaxed);
108}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110void Log::PutCString(const char *cstr) { Printf("%s", cstr); }
Zachary Turnerc1564272016-11-16 21:15:24 +0000111void Log::PutString(llvm::StringRef str) { PutCString(str.str().c_str()); }
Zachary Turnerc1592652015-04-29 22:55:28 +0000112
113//----------------------------------------------------------------------
114// Simple variable argument logging with flags.
115//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116void Log::Printf(const char *format, ...) {
117 va_list args;
118 va_start(args, format);
119 VAPrintf(format, args);
120 va_end(args);
Zachary Turnerc1592652015-04-29 22:55:28 +0000121}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122
123//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000124// All logging eventually boils down to this function call. If we have a
125// callback registered, then we call the logging callback. If we have a valid
126// file handle, we also log to the file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128void Log::VAPrintf(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000129 llvm::SmallString<64> FinalMessage;
130 llvm::raw_svector_ostream Stream(FinalMessage);
131 WriteHeader(Stream, "", "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132
Zachary Turner6f9e6902017-03-03 20:56:28 +0000133 llvm::SmallString<64> Content;
134 lldb_private::VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135
Zachary Turner6f9e6902017-03-03 20:56:28 +0000136 Stream << Content << "\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137
Zachary Turner6f9e6902017-03-03 20:56:28 +0000138 WriteMessage(FinalMessage.str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139}
140
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142// Printing of errors that are not fatal.
143//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144void Log::Error(const char *format, ...) {
145 va_list args;
146 va_start(args, format);
147 VAError(format, args);
148 va_end(args);
Zachary Turner610e5292015-05-07 21:39:33 +0000149}
150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151void Log::VAError(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000152 llvm::SmallString<64> Content;
153 VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154
Zachary Turner6f9e6902017-03-03 20:56:28 +0000155 Printf("error: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156}
157
158//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000159// Printing of warnings that are not fatal only if verbose mode is enabled.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160//----------------------------------------------------------------------
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 Labathd8133092017-10-23 19:41:17 +0000184void Log::Initialize() {
185#ifdef LLVM_ON_UNIX
186 pthread_atfork(nullptr, nullptr, &Log::DisableLoggingChild);
187#endif
188 InitializeLldbChannel();
189}
190
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000191void Log::Register(llvm::StringRef name, Channel &channel) {
192 auto iter = g_channel_map->try_emplace(name, channel);
193 assert(iter.second == true);
194 (void)iter;
195}
196
197void Log::Unregister(llvm::StringRef name) {
198 auto iter = g_channel_map->find(name);
199 assert(iter != g_channel_map->end());
Pavel Labathf5aaa992017-03-09 10:16:07 +0000200 iter->second.Disable(UINT32_MAX);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000201 g_channel_map->erase(iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202}
203
Pavel Labath5fae71c2017-02-10 11:49:21 +0000204bool Log::EnableLogChannel(
205 const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
Pavel Labath5e336902017-03-01 10:08:40 +0000206 uint32_t log_options, llvm::StringRef channel,
Pavel Labath775588c2017-03-15 09:06:58 +0000207 llvm::ArrayRef<const char *> categories, llvm::raw_ostream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000208 auto iter = g_channel_map->find(channel);
209 if (iter == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000210 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 }
Pavel Labath5e336902017-03-01 10:08:40 +0000213 uint32_t flags = categories.empty()
Pavel Labathf5aaa992017-03-09 10:16:07 +0000214 ? iter->second.m_channel.default_flags
Pavel Labath5e336902017-03-01 10:08:40 +0000215 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000216 iter->second.Enable(log_stream_sp, log_options, flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000217 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218}
219
Pavel Labath5e336902017-03-01 10:08:40 +0000220bool Log::DisableLogChannel(llvm::StringRef channel,
221 llvm::ArrayRef<const char *> categories,
Pavel Labath775588c2017-03-15 09:06:58 +0000222 llvm::raw_ostream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000223 auto iter = g_channel_map->find(channel);
224 if (iter == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000225 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000226 return false;
227 }
Pavel Labath5e336902017-03-01 10:08:40 +0000228 uint32_t flags = categories.empty()
229 ? UINT32_MAX
230 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000231 iter->second.Disable(flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000232 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233}
234
Pavel Labath775588c2017-03-15 09:06:58 +0000235bool Log::ListChannelCategories(llvm::StringRef channel,
236 llvm::raw_ostream &stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000237 auto ch = g_channel_map->find(channel);
238 if (ch == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000239 stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000240 return false;
241 }
242 ListCategories(stream, *ch);
243 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244}
245
Pavel Labath775588c2017-03-15 09:06:58 +0000246void Log::DisableAllLogChannels() {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000247 for (auto &entry : *g_channel_map)
Pavel Labathf5aaa992017-03-09 10:16:07 +0000248 entry.second.Disable(UINT32_MAX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249}
250
Pavel Labath775588c2017-03-15 09:06:58 +0000251void Log::ListAllLogChannels(llvm::raw_ostream &stream) {
Pavel Labath3474ebc2017-02-27 12:21:16 +0000252 if (g_channel_map->empty()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000253 stream << "No logging channels are currently registered.\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 return;
255 }
256
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000257 for (const auto &channel : *g_channel_map)
Pavel Labath775588c2017-03-15 09:06:58 +0000258 ListCategories(stream, channel);
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000259}
Pavel Labath775588c2017-03-15 09:06:58 +0000260
Pavel Labathf5aaa992017-03-09 10:16:07 +0000261bool Log::GetVerbose() const {
262 return m_options.load(std::memory_order_relaxed) & LLDB_LOG_OPTION_VERBOSE;
263}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264
Pavel Labath107d9bb2017-01-18 11:00:26 +0000265void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
266 llvm::StringRef function) {
Pavel Labathf5aaa992017-03-09 10:16:07 +0000267 Flags options = GetOptions();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000268 static uint32_t g_sequence_id = 0;
269 // Add a sequence ID if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000270 if (options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000271 OS << ++g_sequence_id << " ";
272
273 // Timestamp if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000274 if (options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000275 auto now = std::chrono::duration<double>(
276 std::chrono::system_clock::now().time_since_epoch());
277 OS << llvm::formatv("{0:f9} ", now.count());
278 }
279
280 // Add the process and thread if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000281 if (options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000282 OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
Zachary Turner6f9e6902017-03-03 20:56:28 +0000283 llvm::get_threadid());
Pavel Labath107d9bb2017-01-18 11:00:26 +0000284
285 // Add the thread name if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000286 if (options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000287 llvm::SmallString<32> thread_name;
Zachary Turner6f9e6902017-03-03 20:56:28 +0000288 llvm::get_thread_name(thread_name);
Tatyana Krasnukha0b8bea32018-07-13 11:49:28 +0000289
290 llvm::SmallString<12> format_str;
291 llvm::raw_svector_ostream format_os(format_str);
292 format_os << "{0,-" << llvm::alignTo<16>(thread_name.size()) << "} ";
293 OS << llvm::formatv(format_str.c_str(), thread_name);
Pavel Labath107d9bb2017-01-18 11:00:26 +0000294 }
295
Pavel Labathf5aaa992017-03-09 10:16:07 +0000296 if (options.Test(LLDB_LOG_OPTION_BACKTRACE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000297 llvm::sys::PrintStackTrace(OS);
298
Pavel Labathf5aaa992017-03-09 10:16:07 +0000299 if (options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
Pavel Labath107d9bb2017-01-18 11:00:26 +0000300 (!file.empty() || !function.empty())) {
301 file = llvm::sys::path::filename(file).take_front(40);
302 function = function.take_front(40);
303 OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
304 }
305}
306
307void Log::WriteMessage(const std::string &message) {
Adrian Prantl05097242018-04-30 16:49:04 +0000308 // Make a copy of our stream shared pointer in case someone disables our log
309 // while we are logging and releases the stream
Pavel Labathba95a282017-02-21 09:58:23 +0000310 auto stream_sp = GetStream();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000311 if (!stream_sp)
312 return;
313
Pavel Labathf5aaa992017-03-09 10:16:07 +0000314 Flags options = GetOptions();
315 if (options.Test(LLDB_LOG_OPTION_THREADSAFE)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000316 static std::recursive_mutex g_LogThreadedMutex;
317 std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex);
Pavel Labath5fae71c2017-02-10 11:49:21 +0000318 *stream_sp << message;
319 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000320 } else {
Pavel Labath5fae71c2017-02-10 11:49:21 +0000321 *stream_sp << message;
322 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000323 }
324}
325
326void Log::Format(llvm::StringRef file, llvm::StringRef function,
327 const llvm::formatv_object_base &payload) {
328 std::string message_string;
329 llvm::raw_string_ostream message(message_string);
330 WriteHeader(message, file, function);
331 message << payload << "\n";
332 WriteMessage(message.str());
333}
Pavel Labathd8133092017-10-23 19:41:17 +0000334
335void Log::DisableLoggingChild() {
336 // Disable logging by clearing out the atomic variable after forking -- if we
337 // forked while another thread held the channel mutex, we would deadlock when
338 // trying to write to the log.
339 for (auto &c: *g_channel_map)
340 c.second.m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
341}