blob: e7c6a852011d97ac0af0c47ddf56bb3d67c4f67e [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Log.cpp -------------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turner6f9e6902017-03-03 20:56:28 +00009#include "lldb/Utility/Log.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000010#include "lldb/Utility/VASPrintf.h"
Pavel Labath774103c2016-11-02 12:18:42 +000011
Pavel Labathfb0d22d2017-02-17 13:27:42 +000012#include "llvm/ADT/STLExtras.h"
Pavel Labath774103c2016-11-02 12:18:42 +000013#include "llvm/ADT/SmallString.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000014#include "llvm/ADT/Twine.h"
15#include "llvm/ADT/iterator.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000016
Pavel Labath774103c2016-11-02 12:18:42 +000017#include "llvm/Support/Chrono.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000018#include "llvm/Support/ManagedStatic.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
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000024#include <chrono>
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000025#include <cstdarg>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000026#include <mutex>
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000027#include <utility>
Zachary Turner4479ac12017-04-06 18:12:24 +000028
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000029#include <assert.h>
Nico Weberb1cb0b792018-04-10 13:33:45 +000030#if defined(_WIN32)
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000031#include <process.h>
Zachary Turner4479ac12017-04-06 18:12:24 +000032#else
33#include <unistd.h>
Pavel Labathd8133092017-10-23 19:41:17 +000034#include <pthread.h>
Zachary Turner4479ac12017-04-06 18:12:24 +000035#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//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000123// All logging eventually boils down to this function call. If we have a
124// callback registered, then we call the logging callback. If we have a valid
125// file handle, we also log to the file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126//----------------------------------------------------------------------
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//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +0000158// Printing of warnings that are not fatal only if verbose mode is enabled.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160void Log::Verbose(const char *format, ...) {
Pavel Labathf5aaa992017-03-09 10:16:07 +0000161 if (!GetVerbose())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 return;
Zachary Turnerc1592652015-04-29 22:55:28 +0000163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 va_list args;
165 va_start(args, format);
166 VAPrintf(format, args);
167 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168}
169
170//----------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171// Printing of warnings that are not fatal.
172//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173void Log::Warning(const char *format, ...) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000174 llvm::SmallString<64> Content;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 va_list args;
176 va_start(args, format);
Zachary Turner6f9e6902017-03-03 20:56:28 +0000177 VASprintf(Content, format, args);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 va_end(args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179
Zachary Turner6f9e6902017-03-03 20:56:28 +0000180 Printf("warning: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181}
182
Pavel Labathd8133092017-10-23 19:41:17 +0000183void Log::Initialize() {
184#ifdef LLVM_ON_UNIX
185 pthread_atfork(nullptr, nullptr, &Log::DisableLoggingChild);
186#endif
187 InitializeLldbChannel();
188}
189
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000190void Log::Register(llvm::StringRef name, Channel &channel) {
191 auto iter = g_channel_map->try_emplace(name, channel);
192 assert(iter.second == true);
193 (void)iter;
194}
195
196void Log::Unregister(llvm::StringRef name) {
197 auto iter = g_channel_map->find(name);
198 assert(iter != g_channel_map->end());
Pavel Labathf5aaa992017-03-09 10:16:07 +0000199 iter->second.Disable(UINT32_MAX);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000200 g_channel_map->erase(iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201}
202
Pavel Labath5fae71c2017-02-10 11:49:21 +0000203bool Log::EnableLogChannel(
204 const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
Pavel Labath5e336902017-03-01 10:08:40 +0000205 uint32_t log_options, llvm::StringRef channel,
Pavel Labath775588c2017-03-15 09:06:58 +0000206 llvm::ArrayRef<const char *> categories, llvm::raw_ostream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000207 auto iter = g_channel_map->find(channel);
208 if (iter == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000209 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 }
Pavel Labath5e336902017-03-01 10:08:40 +0000212 uint32_t flags = categories.empty()
Pavel Labathf5aaa992017-03-09 10:16:07 +0000213 ? iter->second.m_channel.default_flags
Pavel Labath5e336902017-03-01 10:08:40 +0000214 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000215 iter->second.Enable(log_stream_sp, log_options, flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000216 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217}
218
Pavel Labath5e336902017-03-01 10:08:40 +0000219bool Log::DisableLogChannel(llvm::StringRef channel,
220 llvm::ArrayRef<const char *> categories,
Pavel Labath775588c2017-03-15 09:06:58 +0000221 llvm::raw_ostream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000222 auto iter = g_channel_map->find(channel);
223 if (iter == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000224 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000225 return false;
226 }
Pavel Labath5e336902017-03-01 10:08:40 +0000227 uint32_t flags = categories.empty()
228 ? UINT32_MAX
229 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000230 iter->second.Disable(flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000231 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000232}
233
Pavel Labath775588c2017-03-15 09:06:58 +0000234bool Log::ListChannelCategories(llvm::StringRef channel,
235 llvm::raw_ostream &stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000236 auto ch = g_channel_map->find(channel);
237 if (ch == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000238 stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000239 return false;
240 }
241 ListCategories(stream, *ch);
242 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243}
244
Pavel Labath775588c2017-03-15 09:06:58 +0000245void Log::DisableAllLogChannels() {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000246 for (auto &entry : *g_channel_map)
Pavel Labathf5aaa992017-03-09 10:16:07 +0000247 entry.second.Disable(UINT32_MAX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248}
249
Pavel Labath775588c2017-03-15 09:06:58 +0000250void Log::ListAllLogChannels(llvm::raw_ostream &stream) {
Pavel Labath3474ebc2017-02-27 12:21:16 +0000251 if (g_channel_map->empty()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000252 stream << "No logging channels are currently registered.\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 return;
254 }
255
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000256 for (const auto &channel : *g_channel_map)
Pavel Labath775588c2017-03-15 09:06:58 +0000257 ListCategories(stream, channel);
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000258}
Pavel Labath775588c2017-03-15 09:06:58 +0000259
Pavel Labathf5aaa992017-03-09 10:16:07 +0000260bool Log::GetVerbose() const {
261 return m_options.load(std::memory_order_relaxed) & LLDB_LOG_OPTION_VERBOSE;
262}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263
Pavel Labath107d9bb2017-01-18 11:00:26 +0000264void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
265 llvm::StringRef function) {
Pavel Labathf5aaa992017-03-09 10:16:07 +0000266 Flags options = GetOptions();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000267 static uint32_t g_sequence_id = 0;
268 // Add a sequence ID if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000269 if (options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000270 OS << ++g_sequence_id << " ";
271
272 // Timestamp if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000273 if (options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000274 auto now = std::chrono::duration<double>(
275 std::chrono::system_clock::now().time_since_epoch());
276 OS << llvm::formatv("{0:f9} ", now.count());
277 }
278
279 // Add the process and thread if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000280 if (options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000281 OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
Zachary Turner6f9e6902017-03-03 20:56:28 +0000282 llvm::get_threadid());
Pavel Labath107d9bb2017-01-18 11:00:26 +0000283
284 // Add the thread name if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000285 if (options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000286 llvm::SmallString<32> thread_name;
Zachary Turner6f9e6902017-03-03 20:56:28 +0000287 llvm::get_thread_name(thread_name);
Tatyana Krasnukha0b8bea32018-07-13 11:49:28 +0000288
289 llvm::SmallString<12> format_str;
290 llvm::raw_svector_ostream format_os(format_str);
291 format_os << "{0,-" << llvm::alignTo<16>(thread_name.size()) << "} ";
292 OS << llvm::formatv(format_str.c_str(), thread_name);
Pavel Labath107d9bb2017-01-18 11:00:26 +0000293 }
294
Pavel Labathf5aaa992017-03-09 10:16:07 +0000295 if (options.Test(LLDB_LOG_OPTION_BACKTRACE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000296 llvm::sys::PrintStackTrace(OS);
297
Pavel Labathf5aaa992017-03-09 10:16:07 +0000298 if (options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
Pavel Labath107d9bb2017-01-18 11:00:26 +0000299 (!file.empty() || !function.empty())) {
300 file = llvm::sys::path::filename(file).take_front(40);
301 function = function.take_front(40);
302 OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
303 }
304}
305
306void Log::WriteMessage(const std::string &message) {
Adrian Prantl05097242018-04-30 16:49:04 +0000307 // Make a copy of our stream shared pointer in case someone disables our log
308 // while we are logging and releases the stream
Pavel Labathba95a282017-02-21 09:58:23 +0000309 auto stream_sp = GetStream();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000310 if (!stream_sp)
311 return;
312
Pavel Labathf5aaa992017-03-09 10:16:07 +0000313 Flags options = GetOptions();
314 if (options.Test(LLDB_LOG_OPTION_THREADSAFE)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000315 static std::recursive_mutex g_LogThreadedMutex;
316 std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex);
Pavel Labath5fae71c2017-02-10 11:49:21 +0000317 *stream_sp << message;
318 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000319 } else {
Pavel Labath5fae71c2017-02-10 11:49:21 +0000320 *stream_sp << message;
321 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000322 }
323}
324
325void Log::Format(llvm::StringRef file, llvm::StringRef function,
326 const llvm::formatv_object_base &payload) {
327 std::string message_string;
328 llvm::raw_string_ostream message(message_string);
329 WriteHeader(message, file, function);
330 message << payload << "\n";
331 WriteMessage(message.str());
332}
Pavel Labathd8133092017-10-23 19:41:17 +0000333
334void Log::DisableLoggingChild() {
335 // Disable logging by clearing out the atomic variable after forking -- if we
336 // forked while another thread held the channel mutex, we would deadlock when
337 // trying to write to the log.
338 for (auto &c: *g_channel_map)
339 c.second.m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
340}