Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- Log.cpp -----------------------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 9 | #include "lldb/Utility/Log.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 10 | #include "lldb/Utility/VASPrintf.h" |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 11 | |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SmallString.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/Twine.h" |
| 14 | #include "llvm/ADT/iterator.h" |
Zachary Turner | 4479ac1 | 2017-04-06 18:12:24 +0000 | [diff] [blame] | 15 | |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Chrono.h" |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ManagedStatic.h" |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Signals.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Threading.h" |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 23 | #include <chrono> |
Eugene Zelenko | a74f37a | 2016-03-10 23:57:12 +0000 | [diff] [blame] | 24 | #include <cstdarg> |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 25 | #include <mutex> |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 26 | #include <utility> |
Zachary Turner | 4479ac1 | 2017-04-06 18:12:24 +0000 | [diff] [blame] | 27 | |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 28 | #include <assert.h> |
Nico Weber | b1cb0b79 | 2018-04-10 13:33:45 +0000 | [diff] [blame] | 29 | #if defined(_WIN32) |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 30 | #include <process.h> |
Zachary Turner | 4479ac1 | 2017-04-06 18:12:24 +0000 | [diff] [blame] | 31 | #else |
| 32 | #include <unistd.h> |
Pavel Labath | d813309 | 2017-10-23 19:41:17 +0000 | [diff] [blame] | 33 | #include <pthread.h> |
Zachary Turner | 4479ac1 | 2017-04-06 18:12:24 +0000 | [diff] [blame] | 34 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | using namespace lldb_private; |
| 37 | |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 38 | llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map; |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 39 | |
Raphael Isemann | 6ba63d8 | 2019-09-24 07:18:09 +0000 | [diff] [blame] | 40 | void 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 Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 45 | for (const auto &category : entry.second.m_channel.categories) |
Raphael Isemann | 6ba63d8 | 2019-09-24 07:18:09 +0000 | [diff] [blame] | 46 | lambda(category.name, category.description); |
| 47 | } |
| 48 | |
| 49 | void Log::ListCategories(llvm::raw_ostream &stream, |
| 50 | const ChannelMap::value_type &entry) { |
Raphael Isemann | 8126340 | 2019-09-24 08:20:05 +0000 | [diff] [blame] | 51 | stream << llvm::formatv("Logging categories for '{0}':\n", entry.first()); |
Raphael Isemann | 6ba63d8 | 2019-09-24 07:18:09 +0000 | [diff] [blame] | 52 | ForEachCategory(entry, |
| 53 | [&stream](llvm::StringRef name, llvm::StringRef description) { |
| 54 | stream << llvm::formatv(" {0} - {1}\n", name, description); |
| 55 | }); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 58 | uint32_t Log::GetFlags(llvm::raw_ostream &stream, const ChannelMap::value_type &entry, |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 59 | llvm::ArrayRef<const char *> categories) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 60 | bool list_categories = false; |
| 61 | uint32_t flags = 0; |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 62 | for (const char *category : categories) { |
| 63 | if (llvm::StringRef("all").equals_lower(category)) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 64 | flags |= UINT32_MAX; |
| 65 | continue; |
| 66 | } |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 67 | if (llvm::StringRef("default").equals_lower(category)) { |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 68 | flags |= entry.second.m_channel.default_flags; |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 69 | continue; |
| 70 | } |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 71 | auto cat = llvm::find_if( |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 72 | entry.second.m_channel.categories, |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 73 | [&](const Log::Category &c) { return c.name.equals_lower(category); }); |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 74 | if (cat != entry.second.m_channel.categories.end()) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 75 | flags |= cat->flag; |
| 76 | continue; |
| 77 | } |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 78 | stream << llvm::formatv("error: unrecognized log category '{0}'\n", |
| 79 | category); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 80 | list_categories = true; |
| 81 | } |
| 82 | if (list_categories) |
| 83 | ListCategories(stream, entry); |
| 84 | return flags; |
| 85 | } |
| 86 | |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 87 | void 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 Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 99 | void 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 Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 109 | const Flags Log::GetOptions() const { |
| 110 | return m_options.load(std::memory_order_relaxed); |
| 111 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 113 | const Flags Log::GetMask() const { |
| 114 | return m_mask.load(std::memory_order_relaxed); |
| 115 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | void Log::PutCString(const char *cstr) { Printf("%s", cstr); } |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 118 | void Log::PutString(llvm::StringRef str) { PutCString(str.str().c_str()); } |
Zachary Turner | c159265 | 2015-04-29 22:55:28 +0000 | [diff] [blame] | 119 | |
Zachary Turner | c159265 | 2015-04-29 22:55:28 +0000 | [diff] [blame] | 120 | // Simple variable argument logging with flags. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | void Log::Printf(const char *format, ...) { |
| 122 | va_list args; |
| 123 | va_start(args, format); |
| 124 | VAPrintf(format, args); |
| 125 | va_end(args); |
Zachary Turner | c159265 | 2015-04-29 22:55:28 +0000 | [diff] [blame] | 126 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 127 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 128 | // 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | void Log::VAPrintf(const char *format, va_list args) { |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 132 | llvm::SmallString<64> FinalMessage; |
| 133 | llvm::raw_svector_ostream Stream(FinalMessage); |
| 134 | WriteHeader(Stream, "", ""); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 136 | llvm::SmallString<64> Content; |
| 137 | lldb_private::VASprintf(Content, format, args); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 139 | Stream << Content << "\n"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame^] | 141 | WriteMessage(std::string(FinalMessage.str())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | // Printing of errors that are not fatal. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | void Log::Error(const char *format, ...) { |
| 146 | va_list args; |
| 147 | va_start(args, format); |
| 148 | VAError(format, args); |
| 149 | va_end(args); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | void Log::VAError(const char *format, va_list args) { |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 153 | llvm::SmallString<64> Content; |
| 154 | VASprintf(Content, format, args); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 156 | Printf("error: %s", Content.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 159 | // Printing of warnings that are not fatal only if verbose mode is enabled. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | void Log::Verbose(const char *format, ...) { |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 161 | if (!GetVerbose()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | return; |
Zachary Turner | c159265 | 2015-04-29 22:55:28 +0000 | [diff] [blame] | 163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | va_list args; |
| 165 | va_start(args, format); |
| 166 | VAPrintf(format, args); |
| 167 | va_end(args); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 170 | // Printing of warnings that are not fatal. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | void Log::Warning(const char *format, ...) { |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 172 | llvm::SmallString<64> Content; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | va_list args; |
| 174 | va_start(args, format); |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 175 | VASprintf(Content, format, args); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 176 | va_end(args); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 177 | |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 178 | Printf("warning: %s", Content.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Pavel Labath | d813309 | 2017-10-23 19:41:17 +0000 | [diff] [blame] | 181 | void Log::Initialize() { |
| 182 | #ifdef LLVM_ON_UNIX |
| 183 | pthread_atfork(nullptr, nullptr, &Log::DisableLoggingChild); |
| 184 | #endif |
| 185 | InitializeLldbChannel(); |
| 186 | } |
| 187 | |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 188 | void 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 | |
| 194 | void Log::Unregister(llvm::StringRef name) { |
| 195 | auto iter = g_channel_map->find(name); |
| 196 | assert(iter != g_channel_map->end()); |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 197 | iter->second.Disable(UINT32_MAX); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 198 | g_channel_map->erase(iter); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 201 | bool Log::EnableLogChannel( |
| 202 | const std::shared_ptr<llvm::raw_ostream> &log_stream_sp, |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 203 | uint32_t log_options, llvm::StringRef channel, |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 204 | llvm::ArrayRef<const char *> categories, llvm::raw_ostream &error_stream) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 205 | auto iter = g_channel_map->find(channel); |
| 206 | if (iter == g_channel_map->end()) { |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 207 | error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | } |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 210 | uint32_t flags = categories.empty() |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 211 | ? iter->second.m_channel.default_flags |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 212 | : GetFlags(error_stream, *iter, categories); |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 213 | iter->second.Enable(log_stream_sp, log_options, flags); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 214 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 217 | bool Log::DisableLogChannel(llvm::StringRef channel, |
| 218 | llvm::ArrayRef<const char *> categories, |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 219 | llvm::raw_ostream &error_stream) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 220 | auto iter = g_channel_map->find(channel); |
| 221 | if (iter == g_channel_map->end()) { |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 222 | error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 223 | return false; |
| 224 | } |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 225 | uint32_t flags = categories.empty() |
| 226 | ? UINT32_MAX |
| 227 | : GetFlags(error_stream, *iter, categories); |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 228 | iter->second.Disable(flags); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 229 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 232 | bool Log::ListChannelCategories(llvm::StringRef channel, |
| 233 | llvm::raw_ostream &stream) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 234 | auto ch = g_channel_map->find(channel); |
| 235 | if (ch == g_channel_map->end()) { |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 236 | stream << llvm::formatv("Invalid log channel '{0}'.\n", channel); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 237 | return false; |
| 238 | } |
| 239 | ListCategories(stream, *ch); |
| 240 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 243 | void Log::DisableAllLogChannels() { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 244 | for (auto &entry : *g_channel_map) |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 245 | entry.second.Disable(UINT32_MAX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Raphael Isemann | 6ba63d8 | 2019-09-24 07:18:09 +0000 | [diff] [blame] | 248 | void 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 | |
| 258 | std::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 Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 265 | void Log::ListAllLogChannels(llvm::raw_ostream &stream) { |
Pavel Labath | 3474ebc | 2017-02-27 12:21:16 +0000 | [diff] [blame] | 266 | if (g_channel_map->empty()) { |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 267 | stream << "No logging channels are currently registered.\n"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | return; |
| 269 | } |
| 270 | |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 271 | for (const auto &channel : *g_channel_map) |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 272 | ListCategories(stream, channel); |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 273 | } |
Pavel Labath | 775588c | 2017-03-15 09:06:58 +0000 | [diff] [blame] | 274 | |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 275 | bool Log::GetVerbose() const { |
| 276 | return m_options.load(std::memory_order_relaxed) & LLDB_LOG_OPTION_VERBOSE; |
| 277 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 278 | |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 279 | void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file, |
| 280 | llvm::StringRef function) { |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 281 | Flags options = GetOptions(); |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 282 | static uint32_t g_sequence_id = 0; |
| 283 | // Add a sequence ID if requested |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 284 | if (options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE)) |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 285 | OS << ++g_sequence_id << " "; |
| 286 | |
| 287 | // Timestamp if requested |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 288 | if (options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) { |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 289 | 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 Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 295 | if (options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD)) |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 296 | OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(), |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 297 | llvm::get_threadid()); |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 298 | |
| 299 | // Add the thread name if requested |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 300 | if (options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) { |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 301 | llvm::SmallString<32> thread_name; |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 302 | llvm::get_thread_name(thread_name); |
Tatyana Krasnukha | 0b8bea3 | 2018-07-13 11:49:28 +0000 | [diff] [blame] | 303 | |
| 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 Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 310 | if (options.Test(LLDB_LOG_OPTION_BACKTRACE)) |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 311 | llvm::sys::PrintStackTrace(OS); |
| 312 | |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 313 | if (options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) && |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 314 | (!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 | |
| 321 | void Log::WriteMessage(const std::string &message) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 322 | // Make a copy of our stream shared pointer in case someone disables our log |
| 323 | // while we are logging and releases the stream |
Pavel Labath | ba95a28 | 2017-02-21 09:58:23 +0000 | [diff] [blame] | 324 | auto stream_sp = GetStream(); |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 325 | if (!stream_sp) |
| 326 | return; |
| 327 | |
Pavel Labath | f5aaa99 | 2017-03-09 10:16:07 +0000 | [diff] [blame] | 328 | Flags options = GetOptions(); |
| 329 | if (options.Test(LLDB_LOG_OPTION_THREADSAFE)) { |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 330 | static std::recursive_mutex g_LogThreadedMutex; |
| 331 | std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex); |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 332 | *stream_sp << message; |
| 333 | stream_sp->flush(); |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 334 | } else { |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 335 | *stream_sp << message; |
| 336 | stream_sp->flush(); |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | void 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 Labath | d813309 | 2017-10-23 19:41:17 +0000 | [diff] [blame] | 348 | |
| 349 | void 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 | } |