blob: 6c27ba228e57addd3518a628e215a473ebced075 [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"
Zachary Turner6f9e6902017-03-03 20:56:28 +000012#include "lldb/Utility/VASPrintf.h"
Pavel Labath775588c2017-03-15 09:06:58 +000013#include "lldb/lldb-types.h"
Pavel Labath774103c2016-11-02 12:18:42 +000014
15// Other libraries and framework includes
Pavel Labathfb0d22d2017-02-17 13:27:42 +000016#include "llvm/ADT/STLExtras.h"
Pavel Labath774103c2016-11-02 12:18:42 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/Support/Chrono.h"
Pavel Labath107d9bb2017-01-18 11:00:26 +000019#include "llvm/Support/Path.h"
Pavel Labath774103c2016-11-02 12:18:42 +000020#include "llvm/Support/Signals.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000021#include "llvm/Support/Threading.h"
Pavel Labath774103c2016-11-02 12:18:42 +000022#include "llvm/Support/raw_ostream.h"
23
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024// C Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025// C++ Includes
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000026#include <cstdarg>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000027#include <cstdio>
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000028#include <cstdlib>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include <map>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000030#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include <string>
32
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033using namespace lldb_private;
34
Pavel Labathf5aaa992017-03-09 10:16:07 +000035llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;
Pavel Labathfb0d22d2017-02-17 13:27:42 +000036
Pavel Labath775588c2017-03-15 09:06:58 +000037void Log::ListCategories(llvm::raw_ostream &stream, const ChannelMap::value_type &entry) {
38 stream << llvm::formatv("Logging categories for '{0}':\n", entry.first());
39 stream << " all - all available logging categories\n";
40 stream << " default - default set of logging categories\n";
Pavel Labathf5aaa992017-03-09 10:16:07 +000041 for (const auto &category : entry.second.m_channel.categories)
Pavel Labath775588c2017-03-15 09:06:58 +000042 stream << llvm::formatv(" {0} - {1}\n", category.name,
43 category.description);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000044}
45
Pavel Labath775588c2017-03-15 09:06:58 +000046uint32_t Log::GetFlags(llvm::raw_ostream &stream, const ChannelMap::value_type &entry,
Pavel Labath5e336902017-03-01 10:08:40 +000047 llvm::ArrayRef<const char *> categories) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000048 bool list_categories = false;
49 uint32_t flags = 0;
Pavel Labath5e336902017-03-01 10:08:40 +000050 for (const char *category : categories) {
51 if (llvm::StringRef("all").equals_lower(category)) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000052 flags |= UINT32_MAX;
53 continue;
54 }
Pavel Labath5e336902017-03-01 10:08:40 +000055 if (llvm::StringRef("default").equals_lower(category)) {
Pavel Labathf5aaa992017-03-09 10:16:07 +000056 flags |= entry.second.m_channel.default_flags;
Pavel Labathfb0d22d2017-02-17 13:27:42 +000057 continue;
58 }
Pavel Labath5e336902017-03-01 10:08:40 +000059 auto cat = llvm::find_if(
Pavel Labathf5aaa992017-03-09 10:16:07 +000060 entry.second.m_channel.categories,
Pavel Labath5e336902017-03-01 10:08:40 +000061 [&](const Log::Category &c) { return c.name.equals_lower(category); });
Pavel Labathf5aaa992017-03-09 10:16:07 +000062 if (cat != entry.second.m_channel.categories.end()) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000063 flags |= cat->flag;
64 continue;
65 }
Pavel Labath775588c2017-03-15 09:06:58 +000066 stream << llvm::formatv("error: unrecognized log category '{0}'\n",
67 category);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000068 list_categories = true;
69 }
70 if (list_categories)
71 ListCategories(stream, entry);
72 return flags;
73}
74
Pavel Labathf5aaa992017-03-09 10:16:07 +000075void Log::Enable(const std::shared_ptr<llvm::raw_ostream> &stream_sp,
76 uint32_t options, uint32_t flags) {
77 llvm::sys::ScopedWriter lock(m_mutex);
78
79 uint32_t mask = m_mask.fetch_or(flags, std::memory_order_relaxed);
80 if (mask | flags) {
81 m_options.store(options, std::memory_order_relaxed);
82 m_stream_sp = stream_sp;
83 m_channel.log_ptr.store(this, std::memory_order_relaxed);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000084 }
85}
86
Pavel Labathf5aaa992017-03-09 10:16:07 +000087void Log::Disable(uint32_t flags) {
88 llvm::sys::ScopedWriter lock(m_mutex);
89
90 uint32_t mask = m_mask.fetch_and(~flags, std::memory_order_relaxed);
91 if (!(mask & ~flags)) {
92 m_stream_sp.reset();
93 m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000094 }
95}
96
Pavel Labathf5aaa992017-03-09 10:16:07 +000097const Flags Log::GetOptions() const {
98 return m_options.load(std::memory_order_relaxed);
99}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100
Pavel Labathf5aaa992017-03-09 10:16:07 +0000101const Flags Log::GetMask() const {
102 return m_mask.load(std::memory_order_relaxed);
103}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105void Log::PutCString(const char *cstr) { Printf("%s", cstr); }
Zachary Turnerc1564272016-11-16 21:15:24 +0000106void Log::PutString(llvm::StringRef str) { PutCString(str.str().c_str()); }
Zachary Turnerc1592652015-04-29 22:55:28 +0000107
108//----------------------------------------------------------------------
109// Simple variable argument logging with flags.
110//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111void Log::Printf(const char *format, ...) {
112 va_list args;
113 va_start(args, format);
114 VAPrintf(format, args);
115 va_end(args);
Zachary Turnerc1592652015-04-29 22:55:28 +0000116}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
118//----------------------------------------------------------------------
119// All logging eventually boils down to this function call. If we have
120// a callback registered, then we call the logging callback. If we have
121// a valid file handle, we also log to the file.
122//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123void Log::VAPrintf(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000124 llvm::SmallString<64> FinalMessage;
125 llvm::raw_svector_ostream Stream(FinalMessage);
126 WriteHeader(Stream, "", "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127
Zachary Turner6f9e6902017-03-03 20:56:28 +0000128 llvm::SmallString<64> Content;
129 lldb_private::VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130
Zachary Turner6f9e6902017-03-03 20:56:28 +0000131 Stream << Content << "\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132
Zachary Turner6f9e6902017-03-03 20:56:28 +0000133 WriteMessage(FinalMessage.str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134}
135
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137// Printing of errors that are not fatal.
138//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139void Log::Error(const char *format, ...) {
140 va_list args;
141 va_start(args, format);
142 VAError(format, args);
143 va_end(args);
Zachary Turner610e5292015-05-07 21:39:33 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146void Log::VAError(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000147 llvm::SmallString<64> Content;
148 VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149
Zachary Turner6f9e6902017-03-03 20:56:28 +0000150 Printf("error: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151}
152
153//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154// Printing of warnings that are not fatal only if verbose mode is
155// enabled.
156//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157void Log::Verbose(const char *format, ...) {
Pavel Labathf5aaa992017-03-09 10:16:07 +0000158 if (!GetVerbose())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 return;
Zachary Turnerc1592652015-04-29 22:55:28 +0000160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 va_list args;
162 va_start(args, format);
163 VAPrintf(format, args);
164 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
167//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168// Printing of warnings that are not fatal.
169//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170void Log::Warning(const char *format, ...) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000171 llvm::SmallString<64> Content;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 va_list args;
173 va_start(args, format);
Zachary Turner6f9e6902017-03-03 20:56:28 +0000174 VASprintf(Content, format, args);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176
Zachary Turner6f9e6902017-03-03 20:56:28 +0000177 Printf("warning: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178}
179
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000180void Log::Register(llvm::StringRef name, Channel &channel) {
181 auto iter = g_channel_map->try_emplace(name, channel);
182 assert(iter.second == true);
183 (void)iter;
184}
185
186void Log::Unregister(llvm::StringRef name) {
187 auto iter = g_channel_map->find(name);
188 assert(iter != g_channel_map->end());
Pavel Labathf5aaa992017-03-09 10:16:07 +0000189 iter->second.Disable(UINT32_MAX);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000190 g_channel_map->erase(iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191}
192
Pavel Labath5fae71c2017-02-10 11:49:21 +0000193bool Log::EnableLogChannel(
194 const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
Pavel Labath5e336902017-03-01 10:08:40 +0000195 uint32_t log_options, llvm::StringRef channel,
Pavel Labath775588c2017-03-15 09:06:58 +0000196 llvm::ArrayRef<const char *> categories, llvm::raw_ostream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000197 auto iter = g_channel_map->find(channel);
198 if (iter == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000199 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 }
Pavel Labath5e336902017-03-01 10:08:40 +0000202 uint32_t flags = categories.empty()
Pavel Labathf5aaa992017-03-09 10:16:07 +0000203 ? iter->second.m_channel.default_flags
Pavel Labath5e336902017-03-01 10:08:40 +0000204 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000205 iter->second.Enable(log_stream_sp, log_options, flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000206 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207}
208
Pavel Labath5e336902017-03-01 10:08:40 +0000209bool Log::DisableLogChannel(llvm::StringRef channel,
210 llvm::ArrayRef<const char *> categories,
Pavel Labath775588c2017-03-15 09:06:58 +0000211 llvm::raw_ostream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000212 auto iter = g_channel_map->find(channel);
213 if (iter == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000214 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000215 return false;
216 }
Pavel Labath5e336902017-03-01 10:08:40 +0000217 uint32_t flags = categories.empty()
218 ? UINT32_MAX
219 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000220 iter->second.Disable(flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000221 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222}
223
Pavel Labath775588c2017-03-15 09:06:58 +0000224bool Log::ListChannelCategories(llvm::StringRef channel,
225 llvm::raw_ostream &stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000226 auto ch = g_channel_map->find(channel);
227 if (ch == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000228 stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000229 return false;
230 }
231 ListCategories(stream, *ch);
232 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233}
234
Pavel Labath775588c2017-03-15 09:06:58 +0000235void Log::DisableAllLogChannels() {
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
Pavel Labath775588c2017-03-15 09:06:58 +0000240void Log::ListAllLogChannels(llvm::raw_ostream &stream) {
Pavel Labath3474ebc2017-02-27 12:21:16 +0000241 if (g_channel_map->empty()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000242 stream << "No logging channels are currently registered.\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 return;
244 }
245
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000246 for (const auto &channel : *g_channel_map)
Pavel Labath775588c2017-03-15 09:06:58 +0000247 ListCategories(stream, channel);
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000248}
Pavel Labath775588c2017-03-15 09:06:58 +0000249
Pavel Labathf5aaa992017-03-09 10:16:07 +0000250bool Log::GetVerbose() const {
251 return m_options.load(std::memory_order_relaxed) & LLDB_LOG_OPTION_VERBOSE;
252}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253
Pavel Labath107d9bb2017-01-18 11:00:26 +0000254void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
255 llvm::StringRef function) {
Pavel Labathf5aaa992017-03-09 10:16:07 +0000256 Flags options = GetOptions();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000257 static uint32_t g_sequence_id = 0;
258 // Add a sequence ID if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000259 if (options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000260 OS << ++g_sequence_id << " ";
261
262 // Timestamp if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000263 if (options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000264 auto now = std::chrono::duration<double>(
265 std::chrono::system_clock::now().time_since_epoch());
266 OS << llvm::formatv("{0:f9} ", now.count());
267 }
268
269 // Add the process and thread if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000270 if (options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000271 OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
Zachary Turner6f9e6902017-03-03 20:56:28 +0000272 llvm::get_threadid());
Pavel Labath107d9bb2017-01-18 11:00:26 +0000273
274 // Add the thread name if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000275 if (options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000276 llvm::SmallString<32> thread_name;
Zachary Turner6f9e6902017-03-03 20:56:28 +0000277 llvm::get_thread_name(thread_name);
Pavel Labath107d9bb2017-01-18 11:00:26 +0000278 if (!thread_name.empty())
279 OS << thread_name;
280 }
281
Pavel Labathf5aaa992017-03-09 10:16:07 +0000282 if (options.Test(LLDB_LOG_OPTION_BACKTRACE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000283 llvm::sys::PrintStackTrace(OS);
284
Pavel Labathf5aaa992017-03-09 10:16:07 +0000285 if (options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
Pavel Labath107d9bb2017-01-18 11:00:26 +0000286 (!file.empty() || !function.empty())) {
287 file = llvm::sys::path::filename(file).take_front(40);
288 function = function.take_front(40);
289 OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
290 }
291}
292
293void Log::WriteMessage(const std::string &message) {
294 // Make a copy of our stream shared pointer in case someone disables our
295 // log while we are logging and releases the stream
Pavel Labathba95a282017-02-21 09:58:23 +0000296 auto stream_sp = GetStream();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000297 if (!stream_sp)
298 return;
299
Pavel Labathf5aaa992017-03-09 10:16:07 +0000300 Flags options = GetOptions();
301 if (options.Test(LLDB_LOG_OPTION_THREADSAFE)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000302 static std::recursive_mutex g_LogThreadedMutex;
303 std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex);
Pavel Labath5fae71c2017-02-10 11:49:21 +0000304 *stream_sp << message;
305 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000306 } else {
Pavel Labath5fae71c2017-02-10 11:49:21 +0000307 *stream_sp << message;
308 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000309 }
310}
311
312void Log::Format(llvm::StringRef file, llvm::StringRef function,
313 const llvm::formatv_object_base &payload) {
314 std::string message_string;
315 llvm::raw_string_ostream message(message_string);
316 WriteHeader(message, file, function);
317 message << payload << "\n";
318 WriteMessage(message.str());
319}