1 //
2 // Copyright 2002 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // debug.cpp: Debugging utilities.
8
9 #include "common/debug.h"
10
11 #include <stdarg.h>
12
13 #include <array>
14 #include <cstdio>
15 #include <cstring>
16 #include <fstream>
17 #include <ostream>
18 #include <vector>
19
20 #if defined(ANGLE_PLATFORM_ANDROID)
21 # include <android/log.h>
22 #endif
23
24 #if defined(ANGLE_PLATFORM_APPLE)
25 # include <os/log.h>
26 #endif
27
28 #if defined(ANGLE_PLATFORM_WINDOWS)
29 # include <windows.h>
30 #endif
31
32 #include "anglebase/no_destructor.h"
33 #include "common/Optional.h"
34 #include "common/angleutils.h"
35 #include "common/entry_points_enum_autogen.h"
36 #include "common/system_utils.h"
37
38 namespace gl
39 {
40
41 namespace
42 {
43
44 DebugAnnotator *g_debugAnnotator = nullptr;
45
46 std::mutex *g_debugMutex = nullptr;
47
48 constexpr std::array<const char *, LOG_NUM_SEVERITIES> g_logSeverityNames = {
49 {"EVENT", "INFO", "WARN", "ERR", "FATAL"}};
50
LogSeverityName(int severity)51 constexpr const char *LogSeverityName(int severity)
52 {
53 return (severity >= 0 && severity < LOG_NUM_SEVERITIES) ? g_logSeverityNames[severity]
54 : "UNKNOWN";
55 }
56
ShouldCreateLogMessage(LogSeverity severity)57 bool ShouldCreateLogMessage(LogSeverity severity)
58 {
59 #if defined(ANGLE_TRACE_ENABLED)
60 return true;
61 #elif defined(ANGLE_ENABLE_ASSERTS)
62 return severity == LOG_FATAL || severity == LOG_ERR || severity == LOG_WARN;
63 #else
64 return false;
65 #endif
66 }
67
68 } // namespace
69
70 namespace priv
71 {
72
ShouldCreatePlatformLogMessage(LogSeverity severity)73 bool ShouldCreatePlatformLogMessage(LogSeverity severity)
74 {
75 #if defined(ANGLE_TRACE_ENABLED)
76 return true;
77 #else
78 return severity != LOG_EVENT;
79 #endif
80 }
81
82 // This is never instantiated, it's just used for EAT_STREAM_PARAMETERS to an object of the correct
83 // type on the LHS of the unused part of the ternary operator.
84 std::ostream *gSwallowStream;
85 } // namespace priv
86
DebugAnnotationsActive()87 bool DebugAnnotationsActive()
88 {
89 #if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS) || defined(ANGLE_ENABLE_DEBUG_TRACE)
90 return g_debugAnnotator != nullptr && g_debugAnnotator->getStatus();
91 #else
92 return false;
93 #endif
94 }
95
ShouldBeginScopedEvent()96 bool ShouldBeginScopedEvent()
97 {
98 #if defined(ANGLE_ENABLE_ANNOTATOR_RUN_TIME_CHECKS)
99 return DebugAnnotationsActive();
100 #else
101 return true;
102 #endif // defined(ANGLE_ENABLE_ANNOTATOR_RUN_TIME_CHECKS)
103 }
104
DebugAnnotationsInitialized()105 bool DebugAnnotationsInitialized()
106 {
107 return g_debugAnnotator != nullptr;
108 }
109
InitializeDebugAnnotations(DebugAnnotator * debugAnnotator)110 void InitializeDebugAnnotations(DebugAnnotator *debugAnnotator)
111 {
112 UninitializeDebugAnnotations();
113 g_debugAnnotator = debugAnnotator;
114 }
115
UninitializeDebugAnnotations()116 void UninitializeDebugAnnotations()
117 {
118 // Pointer is not managed.
119 g_debugAnnotator = nullptr;
120 }
121
InitializeDebugMutexIfNeeded()122 void InitializeDebugMutexIfNeeded()
123 {
124 if (g_debugMutex == nullptr)
125 {
126 g_debugMutex = new std::mutex();
127 }
128 }
129
GetDebugMutex()130 std::mutex &GetDebugMutex()
131 {
132 ASSERT(g_debugMutex);
133 return *g_debugMutex;
134 }
135
ScopedPerfEventHelper(gl::Context * context,angle::EntryPoint entryPoint)136 ScopedPerfEventHelper::ScopedPerfEventHelper(gl::Context *context, angle::EntryPoint entryPoint)
137 : mContext(context), mEntryPoint(entryPoint), mFunctionName(nullptr), mCalledBeginEvent(false)
138 {}
139
~ScopedPerfEventHelper()140 ScopedPerfEventHelper::~ScopedPerfEventHelper()
141 {
142 // EGL_Initialize() and EGL_Terminate() can change g_debugAnnotator. Must check the value of
143 // g_debugAnnotator and whether ScopedPerfEventHelper::begin() initiated a begine that must be
144 // ended now.
145 if (DebugAnnotationsInitialized() && mCalledBeginEvent)
146 {
147 g_debugAnnotator->endEvent(mContext, mFunctionName, mEntryPoint);
148 }
149 }
150
begin(const char * format,...)151 void ScopedPerfEventHelper::begin(const char *format, ...)
152 {
153 mFunctionName = GetEntryPointName(mEntryPoint);
154
155 va_list vararg;
156 va_start(vararg, format);
157
158 std::vector<char> buffer;
159 size_t len = FormatStringIntoVector(format, vararg, buffer);
160 va_end(vararg);
161
162 ANGLE_LOG(EVENT) << std::string(&buffer[0], len);
163 if (DebugAnnotationsInitialized())
164 {
165 mCalledBeginEvent = true;
166 g_debugAnnotator->beginEvent(mContext, mEntryPoint, mFunctionName, buffer.data());
167 }
168 }
169
LogMessage(const char * file,const char * function,int line,LogSeverity severity)170 LogMessage::LogMessage(const char *file, const char *function, int line, LogSeverity severity)
171 : mFile(file), mFunction(function), mLine(line), mSeverity(severity)
172 {
173 // INFO() and EVENT() do not require additional function(line) info.
174 if (mSeverity > LOG_INFO)
175 {
176 const char *slash = std::max(strrchr(mFile, '/'), strrchr(mFile, '\\'));
177 mStream << (slash ? (slash + 1) : mFile) << ":" << mLine << " (" << mFunction << "): ";
178 }
179 }
180
~LogMessage()181 LogMessage::~LogMessage()
182 {
183 std::unique_lock<std::mutex> lock;
184 if (g_debugMutex != nullptr)
185 {
186 lock = std::unique_lock<std::mutex>(*g_debugMutex);
187 }
188
189 if (DebugAnnotationsInitialized() && (mSeverity > LOG_INFO))
190 {
191 g_debugAnnotator->logMessage(*this);
192 }
193 else
194 {
195 Trace(getSeverity(), getMessage().c_str());
196 }
197
198 if (mSeverity == LOG_FATAL)
199 {
200 if (angle::IsDebuggerAttached())
201 {
202 angle::BreakDebugger();
203 }
204 else
205 {
206 ANGLE_CRASH();
207 }
208 }
209 }
210
Trace(LogSeverity severity,const char * message)211 void Trace(LogSeverity severity, const char *message)
212 {
213 if (!ShouldCreateLogMessage(severity))
214 {
215 return;
216 }
217
218 std::string str(message);
219
220 if (DebugAnnotationsActive())
221 {
222
223 switch (severity)
224 {
225 case LOG_EVENT:
226 // Debugging logging done in ScopedPerfEventHelper
227 break;
228 default:
229 g_debugAnnotator->setMarker(message);
230 break;
231 }
232 }
233
234 if (severity == LOG_FATAL || severity == LOG_ERR || severity == LOG_WARN ||
235 #if defined(ANGLE_ENABLE_TRACE_ANDROID_LOGCAT)
236 severity == LOG_EVENT ||
237 #endif
238 severity == LOG_INFO)
239 {
240 #if defined(ANGLE_PLATFORM_ANDROID)
241 android_LogPriority android_priority = ANDROID_LOG_ERROR;
242 switch (severity)
243 {
244 case LOG_INFO:
245 case LOG_EVENT:
246 android_priority = ANDROID_LOG_INFO;
247 break;
248 case LOG_WARN:
249 android_priority = ANDROID_LOG_WARN;
250 break;
251 case LOG_ERR:
252 android_priority = ANDROID_LOG_ERROR;
253 break;
254 case LOG_FATAL:
255 android_priority = ANDROID_LOG_FATAL;
256 break;
257 default:
258 UNREACHABLE();
259 }
260 __android_log_print(android_priority, "ANGLE", "%s: %s\n", LogSeverityName(severity),
261 str.c_str());
262 #elif defined(ANGLE_PLATFORM_APPLE)
263 if (__builtin_available(macOS 10.12, iOS 10.0, *))
264 {
265 os_log_type_t apple_log_type = OS_LOG_TYPE_DEFAULT;
266 switch (severity)
267 {
268 case LOG_INFO:
269 apple_log_type = OS_LOG_TYPE_INFO;
270 break;
271 case LOG_WARN:
272 apple_log_type = OS_LOG_TYPE_DEFAULT;
273 break;
274 case LOG_ERR:
275 apple_log_type = OS_LOG_TYPE_ERROR;
276 break;
277 case LOG_FATAL:
278 // OS_LOG_TYPE_FAULT is too severe - grabs the entire process tree.
279 apple_log_type = OS_LOG_TYPE_ERROR;
280 break;
281 default:
282 UNREACHABLE();
283 }
284 os_log_with_type(OS_LOG_DEFAULT, apple_log_type, "ANGLE: %s: %s\n",
285 LogSeverityName(severity), str.c_str());
286 }
287 #else
288 // Note: we use fprintf because <iostream> includes static initializers.
289 fprintf((severity >= LOG_WARN) ? stderr : stdout, "%s: %s\n", LogSeverityName(severity),
290 str.c_str());
291 #endif
292 }
293
294 #if defined(ANGLE_PLATFORM_WINDOWS) && \
295 (defined(ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER) || !defined(NDEBUG))
296 # if !defined(ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER)
297 if (severity >= LOG_ERR)
298 # endif // !defined(ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER)
299 {
300 OutputDebugStringA(str.c_str());
301 OutputDebugStringA("\n");
302 }
303 #endif
304
305 #if defined(ANGLE_ENABLE_DEBUG_TRACE)
306 # if defined(NDEBUG)
307 if (severity == LOG_EVENT || severity == LOG_WARN || severity == LOG_INFO)
308 {
309 return;
310 }
311 # endif // defined(NDEBUG)
312 static angle::base::NoDestructor<std::ofstream> file(TRACE_OUTPUT_FILE, std::ofstream::app);
313 if (file->good())
314 {
315 if (severity > LOG_EVENT)
316 {
317 *file << LogSeverityName(severity) << ": ";
318 }
319 *file << str << "\n";
320 file->flush();
321 }
322 #endif // defined(ANGLE_ENABLE_DEBUG_TRACE)
323 }
324
getSeverity() const325 LogSeverity LogMessage::getSeverity() const
326 {
327 return mSeverity;
328 }
329
getMessage() const330 std::string LogMessage::getMessage() const
331 {
332 return mStream.str();
333 }
334
335 #if defined(ANGLE_PLATFORM_WINDOWS)
FmtHR(HRESULT value)336 priv::FmtHexHelper<HRESULT> FmtHR(HRESULT value)
337 {
338 return priv::FmtHexHelper<HRESULT>("HRESULT: ", value);
339 }
340
FmtErr(DWORD value)341 priv::FmtHexHelper<DWORD> FmtErr(DWORD value)
342 {
343 return priv::FmtHexHelper<DWORD>("error: ", value);
344 }
345 #endif // defined(ANGLE_PLATFORM_WINDOWS)
346
347 } // namespace gl
348