blob: 4df82f2bf4d7b4a22b7d4ec11dcbddbda57002e2 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- Log.cpp -----------------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
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 Labath774103c2016-11-02 12:18:42 +000012#include "llvm/ADT/SmallString.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000013#include "llvm/ADT/Twine.h"
14#include "llvm/ADT/iterator.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000015
Pavel Labath774103c2016-11-02 12:18:42 +000016#include "llvm/Support/Chrono.h"
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000017#include "llvm/Support/ManagedStatic.h"
Pavel Labath107d9bb2017-01-18 11:00:26 +000018#include "llvm/Support/Path.h"
Pavel Labath774103c2016-11-02 12:18:42 +000019#include "llvm/Support/Signals.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000020#include "llvm/Support/Threading.h"
Pavel Labath774103c2016-11-02 12:18:42 +000021#include "llvm/Support/raw_ostream.h"
22
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000023#include <chrono>
Eugene Zelenkoa74f37a2016-03-10 23:57:12 +000024#include <cstdarg>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000025#include <mutex>
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000026#include <utility>
Zachary Turner4479ac12017-04-06 18:12:24 +000027
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000028#include <assert.h>
Nico Weberb1cb0b792018-04-10 13:33:45 +000029#if defined(_WIN32)
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000030#include <process.h>
Zachary Turner4479ac12017-04-06 18:12:24 +000031#else
32#include <unistd.h>
Pavel Labathd8133092017-10-23 19:41:17 +000033#include <pthread.h>
Zachary Turner4479ac12017-04-06 18:12:24 +000034#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036using 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
Raphael Isemann6ba63d82019-09-24 07:18:09 +000040void Log::ForEachCategory(
41 const Log::ChannelMap::value_type &entry,
42 llvm::function_ref<void(llvm::StringRef, llvm::StringRef)> lambda) {
43 lambda("all", "all available logging categories");
44 lambda("default", "default set of logging categories");
Pavel Labathf5aaa992017-03-09 10:16:07 +000045 for (const auto &category : entry.second.m_channel.categories)
Raphael Isemann6ba63d82019-09-24 07:18:09 +000046 lambda(category.name, category.description);
47}
48
49void Log::ListCategories(llvm::raw_ostream &stream,
50 const ChannelMap::value_type &entry) {
Raphael Isemann81263402019-09-24 08:20:05 +000051 stream << llvm::formatv("Logging categories for '{0}':\n", entry.first());
Raphael Isemann6ba63d82019-09-24 07:18:09 +000052 ForEachCategory(entry,
53 [&stream](llvm::StringRef name, llvm::StringRef description) {
54 stream << llvm::formatv(" {0} - {1}\n", name, description);
55 });
Pavel Labathfb0d22d2017-02-17 13:27:42 +000056}
57
Pavel Labath775588c2017-03-15 09:06:58 +000058uint32_t Log::GetFlags(llvm::raw_ostream &stream, const ChannelMap::value_type &entry,
Pavel Labath5e336902017-03-01 10:08:40 +000059 llvm::ArrayRef<const char *> categories) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000060 bool list_categories = false;
61 uint32_t flags = 0;
Pavel Labath5e336902017-03-01 10:08:40 +000062 for (const char *category : categories) {
63 if (llvm::StringRef("all").equals_lower(category)) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000064 flags |= UINT32_MAX;
65 continue;
66 }
Pavel Labath5e336902017-03-01 10:08:40 +000067 if (llvm::StringRef("default").equals_lower(category)) {
Pavel Labathf5aaa992017-03-09 10:16:07 +000068 flags |= entry.second.m_channel.default_flags;
Pavel Labathfb0d22d2017-02-17 13:27:42 +000069 continue;
70 }
Pavel Labath5e336902017-03-01 10:08:40 +000071 auto cat = llvm::find_if(
Pavel Labathf5aaa992017-03-09 10:16:07 +000072 entry.second.m_channel.categories,
Pavel Labath5e336902017-03-01 10:08:40 +000073 [&](const Log::Category &c) { return c.name.equals_lower(category); });
Pavel Labathf5aaa992017-03-09 10:16:07 +000074 if (cat != entry.second.m_channel.categories.end()) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +000075 flags |= cat->flag;
76 continue;
77 }
Pavel Labath775588c2017-03-15 09:06:58 +000078 stream << llvm::formatv("error: unrecognized log category '{0}'\n",
79 category);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000080 list_categories = true;
81 }
82 if (list_categories)
83 ListCategories(stream, entry);
84 return flags;
85}
86
Pavel Labathf5aaa992017-03-09 10:16:07 +000087void Log::Enable(const std::shared_ptr<llvm::raw_ostream> &stream_sp,
88 uint32_t options, uint32_t flags) {
89 llvm::sys::ScopedWriter lock(m_mutex);
90
91 uint32_t mask = m_mask.fetch_or(flags, std::memory_order_relaxed);
92 if (mask | flags) {
93 m_options.store(options, std::memory_order_relaxed);
94 m_stream_sp = stream_sp;
95 m_channel.log_ptr.store(this, std::memory_order_relaxed);
Pavel Labathfb0d22d2017-02-17 13:27:42 +000096 }
97}
98
Pavel Labathf5aaa992017-03-09 10:16:07 +000099void Log::Disable(uint32_t flags) {
100 llvm::sys::ScopedWriter lock(m_mutex);
101
102 uint32_t mask = m_mask.fetch_and(~flags, std::memory_order_relaxed);
103 if (!(mask & ~flags)) {
104 m_stream_sp.reset();
105 m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000106 }
107}
108
Pavel Labathf5aaa992017-03-09 10:16:07 +0000109const Flags Log::GetOptions() const {
110 return m_options.load(std::memory_order_relaxed);
111}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112
Pavel Labathf5aaa992017-03-09 10:16:07 +0000113const Flags Log::GetMask() const {
114 return m_mask.load(std::memory_order_relaxed);
115}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117void Log::PutCString(const char *cstr) { Printf("%s", cstr); }
Zachary Turnerc1564272016-11-16 21:15:24 +0000118void Log::PutString(llvm::StringRef str) { PutCString(str.str().c_str()); }
Zachary Turnerc1592652015-04-29 22:55:28 +0000119
Zachary Turnerc1592652015-04-29 22:55:28 +0000120// Simple variable argument logging with flags.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121void Log::Printf(const char *format, ...) {
122 va_list args;
123 va_start(args, format);
124 VAPrintf(format, args);
125 va_end(args);
Zachary Turnerc1592652015-04-29 22:55:28 +0000126}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127
Adrian Prantl05097242018-04-30 16:49:04 +0000128// All logging eventually boils down to this function call. If we have a
129// callback registered, then we call the logging callback. If we have a valid
130// file handle, we also log to the file.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131void Log::VAPrintf(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000132 llvm::SmallString<64> FinalMessage;
133 llvm::raw_svector_ostream Stream(FinalMessage);
134 WriteHeader(Stream, "", "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135
Zachary Turner6f9e6902017-03-03 20:56:28 +0000136 llvm::SmallString<64> Content;
137 lldb_private::VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138
Zachary Turner6f9e6902017-03-03 20:56:28 +0000139 Stream << Content << "\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140
Benjamin Krameradcd0262020-01-28 20:23:46 +0100141 WriteMessage(std::string(FinalMessage.str()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142}
143
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144// Printing of errors that are not fatal.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145void Log::Error(const char *format, ...) {
146 va_list args;
147 va_start(args, format);
148 VAError(format, args);
149 va_end(args);
Zachary Turner610e5292015-05-07 21:39:33 +0000150}
151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152void Log::VAError(const char *format, va_list args) {
Zachary Turner6f9e6902017-03-03 20:56:28 +0000153 llvm::SmallString<64> Content;
154 VASprintf(Content, format, args);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155
Zachary Turner6f9e6902017-03-03 20:56:28 +0000156 Printf("error: %s", Content.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157}
158
Adrian Prantl05097242018-04-30 16:49:04 +0000159// Printing of warnings that are not fatal only if verbose mode is enabled.
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
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170// Printing of warnings that are not fatal.
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 Labathd8133092017-10-23 19:41:17 +0000181void Log::Initialize() {
182#ifdef LLVM_ON_UNIX
183 pthread_atfork(nullptr, nullptr, &Log::DisableLoggingChild);
184#endif
185 InitializeLldbChannel();
186}
187
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000188void Log::Register(llvm::StringRef name, Channel &channel) {
189 auto iter = g_channel_map->try_emplace(name, channel);
190 assert(iter.second == true);
191 (void)iter;
192}
193
194void Log::Unregister(llvm::StringRef name) {
195 auto iter = g_channel_map->find(name);
196 assert(iter != g_channel_map->end());
Pavel Labathf5aaa992017-03-09 10:16:07 +0000197 iter->second.Disable(UINT32_MAX);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000198 g_channel_map->erase(iter);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199}
200
Pavel Labath5fae71c2017-02-10 11:49:21 +0000201bool Log::EnableLogChannel(
202 const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
Pavel Labath5e336902017-03-01 10:08:40 +0000203 uint32_t log_options, llvm::StringRef channel,
Pavel Labath775588c2017-03-15 09:06:58 +0000204 llvm::ArrayRef<const char *> categories, llvm::raw_ostream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000205 auto iter = g_channel_map->find(channel);
206 if (iter == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000207 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 }
Pavel Labath5e336902017-03-01 10:08:40 +0000210 uint32_t flags = categories.empty()
Pavel Labathf5aaa992017-03-09 10:16:07 +0000211 ? iter->second.m_channel.default_flags
Pavel Labath5e336902017-03-01 10:08:40 +0000212 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000213 iter->second.Enable(log_stream_sp, log_options, flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000214 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215}
216
Pavel Labath5e336902017-03-01 10:08:40 +0000217bool Log::DisableLogChannel(llvm::StringRef channel,
218 llvm::ArrayRef<const char *> categories,
Pavel Labath775588c2017-03-15 09:06:58 +0000219 llvm::raw_ostream &error_stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000220 auto iter = g_channel_map->find(channel);
221 if (iter == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000222 error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000223 return false;
224 }
Pavel Labath5e336902017-03-01 10:08:40 +0000225 uint32_t flags = categories.empty()
226 ? UINT32_MAX
227 : GetFlags(error_stream, *iter, categories);
Pavel Labathf5aaa992017-03-09 10:16:07 +0000228 iter->second.Disable(flags);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000229 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230}
231
Pavel Labath775588c2017-03-15 09:06:58 +0000232bool Log::ListChannelCategories(llvm::StringRef channel,
233 llvm::raw_ostream &stream) {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000234 auto ch = g_channel_map->find(channel);
235 if (ch == g_channel_map->end()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000236 stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000237 return false;
238 }
239 ListCategories(stream, *ch);
240 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241}
242
Pavel Labath775588c2017-03-15 09:06:58 +0000243void Log::DisableAllLogChannels() {
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000244 for (auto &entry : *g_channel_map)
Pavel Labathf5aaa992017-03-09 10:16:07 +0000245 entry.second.Disable(UINT32_MAX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246}
247
Raphael Isemann6ba63d82019-09-24 07:18:09 +0000248void Log::ForEachChannelCategory(
249 llvm::StringRef channel,
250 llvm::function_ref<void(llvm::StringRef, llvm::StringRef)> lambda) {
251 auto ch = g_channel_map->find(channel);
252 if (ch == g_channel_map->end())
253 return;
254
255 ForEachCategory(*ch, lambda);
256}
257
258std::vector<llvm::StringRef> Log::ListChannels() {
259 std::vector<llvm::StringRef> result;
260 for (const auto &channel : *g_channel_map)
261 result.push_back(channel.first());
262 return result;
263}
264
Pavel Labath775588c2017-03-15 09:06:58 +0000265void Log::ListAllLogChannels(llvm::raw_ostream &stream) {
Pavel Labath3474ebc2017-02-27 12:21:16 +0000266 if (g_channel_map->empty()) {
Pavel Labath775588c2017-03-15 09:06:58 +0000267 stream << "No logging channels are currently registered.\n";
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 return;
269 }
270
Pavel Labathfb0d22d2017-02-17 13:27:42 +0000271 for (const auto &channel : *g_channel_map)
Pavel Labath775588c2017-03-15 09:06:58 +0000272 ListCategories(stream, channel);
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000273}
Pavel Labath775588c2017-03-15 09:06:58 +0000274
Pavel Labathf5aaa992017-03-09 10:16:07 +0000275bool Log::GetVerbose() const {
276 return m_options.load(std::memory_order_relaxed) & LLDB_LOG_OPTION_VERBOSE;
277}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278
Pavel Labath107d9bb2017-01-18 11:00:26 +0000279void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
280 llvm::StringRef function) {
Pavel Labathf5aaa992017-03-09 10:16:07 +0000281 Flags options = GetOptions();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000282 static uint32_t g_sequence_id = 0;
283 // Add a sequence ID if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000284 if (options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000285 OS << ++g_sequence_id << " ";
286
287 // Timestamp if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000288 if (options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000289 auto now = std::chrono::duration<double>(
290 std::chrono::system_clock::now().time_since_epoch());
291 OS << llvm::formatv("{0:f9} ", now.count());
292 }
293
294 // Add the process and thread if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000295 if (options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000296 OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
Zachary Turner6f9e6902017-03-03 20:56:28 +0000297 llvm::get_threadid());
Pavel Labath107d9bb2017-01-18 11:00:26 +0000298
299 // Add the thread name if requested
Pavel Labathf5aaa992017-03-09 10:16:07 +0000300 if (options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000301 llvm::SmallString<32> thread_name;
Zachary Turner6f9e6902017-03-03 20:56:28 +0000302 llvm::get_thread_name(thread_name);
Tatyana Krasnukha0b8bea32018-07-13 11:49:28 +0000303
304 llvm::SmallString<12> format_str;
305 llvm::raw_svector_ostream format_os(format_str);
306 format_os << "{0,-" << llvm::alignTo<16>(thread_name.size()) << "} ";
307 OS << llvm::formatv(format_str.c_str(), thread_name);
Pavel Labath107d9bb2017-01-18 11:00:26 +0000308 }
309
Pavel Labathf5aaa992017-03-09 10:16:07 +0000310 if (options.Test(LLDB_LOG_OPTION_BACKTRACE))
Pavel Labath107d9bb2017-01-18 11:00:26 +0000311 llvm::sys::PrintStackTrace(OS);
312
Pavel Labathf5aaa992017-03-09 10:16:07 +0000313 if (options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
Pavel Labath107d9bb2017-01-18 11:00:26 +0000314 (!file.empty() || !function.empty())) {
315 file = llvm::sys::path::filename(file).take_front(40);
316 function = function.take_front(40);
317 OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
318 }
319}
320
321void Log::WriteMessage(const std::string &message) {
Adrian Prantl05097242018-04-30 16:49:04 +0000322 // Make a copy of our stream shared pointer in case someone disables our log
323 // while we are logging and releases the stream
Pavel Labathba95a282017-02-21 09:58:23 +0000324 auto stream_sp = GetStream();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000325 if (!stream_sp)
326 return;
327
Pavel Labathf5aaa992017-03-09 10:16:07 +0000328 Flags options = GetOptions();
329 if (options.Test(LLDB_LOG_OPTION_THREADSAFE)) {
Pavel Labath107d9bb2017-01-18 11:00:26 +0000330 static std::recursive_mutex g_LogThreadedMutex;
331 std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex);
Pavel Labath5fae71c2017-02-10 11:49:21 +0000332 *stream_sp << message;
333 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000334 } else {
Pavel Labath5fae71c2017-02-10 11:49:21 +0000335 *stream_sp << message;
336 stream_sp->flush();
Pavel Labath107d9bb2017-01-18 11:00:26 +0000337 }
338}
339
340void Log::Format(llvm::StringRef file, llvm::StringRef function,
341 const llvm::formatv_object_base &payload) {
342 std::string message_string;
343 llvm::raw_string_ostream message(message_string);
344 WriteHeader(message, file, function);
345 message << payload << "\n";
346 WriteMessage(message.str());
347}
Pavel Labathd8133092017-10-23 19:41:17 +0000348
349void Log::DisableLoggingChild() {
350 // Disable logging by clearing out the atomic variable after forking -- if we
351 // forked while another thread held the channel mutex, we would deadlock when
352 // trying to write to the log.
353 for (auto &c: *g_channel_map)
354 c.second.m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
355}