xref: /aosp_15_r20/external/angle/src/common/debug.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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.h: Debugging utilities. A lot of the logging code is adapted from Chromium's
8 // base/logging.h.
9 
10 #ifndef COMMON_DEBUG_H_
11 #define COMMON_DEBUG_H_
12 
13 #include <assert.h>
14 #include <stdio.h>
15 
16 #include <iomanip>
17 #include <ios>
18 #include <mutex>
19 #include <sstream>
20 #include <string>
21 
22 #include "common/SimpleMutex.h"
23 #include "common/angleutils.h"
24 #include "common/entry_points_enum_autogen.h"
25 #include "common/log_utils.h"
26 #include "common/platform.h"
27 
28 #if defined(ANGLE_PLATFORM_WINDOWS)
29 #    include <sal.h>
30 typedef unsigned long DWORD;
31 typedef _Return_type_success_(return >= 0) long HRESULT;
32 #endif
33 
34 #if !defined(TRACE_OUTPUT_FILE)
35 #    define TRACE_OUTPUT_FILE "angle_debug.txt"
36 #endif
37 
38 namespace gl
39 {
40 class Context;
41 
42 // Pairs a begin event with an end event.
43 class [[nodiscard]] ScopedPerfEventHelper : angle::NonCopyable
44 {
45   public:
46     ScopedPerfEventHelper(Context *context, angle::EntryPoint entryPoint);
47     ~ScopedPerfEventHelper();
48     ANGLE_FORMAT_PRINTF(2, 3)
49     void begin(const char *format, ...);
50 
51   private:
52     gl::Context *mContext;
53     const angle::EntryPoint mEntryPoint;
54     const char *mFunctionName;
55     bool mCalledBeginEvent;
56 };
57 
58 // Wraps the API/Platform-specific debug annotation functions.
59 // Also handles redirecting logging destination.
60 class DebugAnnotator : angle::NonCopyable
61 {
62   public:
DebugAnnotator()63     DebugAnnotator() {}
~DebugAnnotator()64     virtual ~DebugAnnotator() {}
65     virtual void beginEvent(gl::Context *context,
66                             angle::EntryPoint entryPoint,
67                             const char *eventName,
68                             const char *eventMessage)                    = 0;
69     virtual void endEvent(gl::Context *context,
70                           const char *eventName,
71                           angle::EntryPoint entryPoint)                  = 0;
72     virtual void setMarker(gl::Context *context, const char *markerName) = 0;
73     virtual bool getStatus(const gl::Context *context)                   = 0;
74     // Log Message Handler that gets passed every log message,
75     // when debug annotations are initialized,
76     // replacing default handling by LogMessage.
77     virtual void logMessage(const LogMessage &msg) const = 0;
78 };
79 
80 void InitializeDebugAnnotations(DebugAnnotator *debugAnnotator);
81 void UninitializeDebugAnnotations();
82 bool DebugAnnotationsActive(const gl::Context *context);
83 bool DebugAnnotationsInitialized();
84 
85 void InitializeDebugMutexIfNeeded();
86 
87 angle::SimpleMutex &GetDebugMutex();
88 }  // namespace gl
89 
90 // The state tracked by ANGLE will be validated with the driver state before each call
91 #if defined(ANGLE_ENABLE_DEBUG_TRACE)
92 #    define ANGLE_STATE_VALIDATION_ENABLED
93 #endif
94 
95 #if defined(__GNUC__)
96 #    define ANGLE_CRASH() __builtin_trap()
97 #else
98 #    define ANGLE_CRASH() ((void)(*(volatile char *)0 = 0)), __assume(0)
99 #endif
100 
101 #define ANGLE_UNUSED_VARIABLE(variable) (static_cast<void>(variable))
102 
103 // Defining ANGLE_ENABLE_STRUCT_PADDING_WARNINGS will enable warnings when members are added to
104 // structs to enforce packing. This is helpful for diagnosing unexpected struct sizes when making
105 // fast cache variables.
106 #if defined(__clang__)
107 #    define ANGLE_ENABLE_STRUCT_PADDING_WARNINGS \
108         _Pragma("clang diagnostic push") _Pragma("clang diagnostic error \"-Wpadded\"")
109 #    define ANGLE_DISABLE_STRUCT_PADDING_WARNINGS _Pragma("clang diagnostic pop")
110 #elif defined(__GNUC__)
111 #    define ANGLE_ENABLE_STRUCT_PADDING_WARNINGS \
112         _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic error \"-Wpadded\"")
113 #    define ANGLE_DISABLE_STRUCT_PADDING_WARNINGS _Pragma("GCC diagnostic pop")
114 #elif defined(_MSC_VER)
115 #    define ANGLE_ENABLE_STRUCT_PADDING_WARNINGS \
116         __pragma(warning(push)) __pragma(warning(error : 4820))
117 #    define ANGLE_DISABLE_STRUCT_PADDING_WARNINGS __pragma(warning(pop))
118 #else
119 #    define ANGLE_ENABLE_STRUCT_PADDING_WARNINGS
120 #    define ANGLE_DISABLE_STRUCT_PADDING_WARNINGS
121 #endif
122 
123 #if defined(__clang__)
124 #    define ANGLE_DISABLE_SUGGEST_OVERRIDE_WARNINGS                               \
125         _Pragma("clang diagnostic push")                                          \
126             _Pragma("clang diagnostic ignored \"-Wsuggest-destructor-override\"") \
127                 _Pragma("clang diagnostic ignored \"-Wsuggest-override\"")
128 #    define ANGLE_REENABLE_SUGGEST_OVERRIDE_WARNINGS _Pragma("clang diagnostic pop")
129 #else
130 #    define ANGLE_DISABLE_SUGGEST_OVERRIDE_WARNINGS
131 #    define ANGLE_REENABLE_SUGGEST_OVERRIDE_WARNINGS
132 #endif
133 
134 #if defined(__clang__)
135 #    define ANGLE_DISABLE_EXTRA_SEMI_WARNING \
136         _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wextra-semi\"")
137 #    define ANGLE_REENABLE_EXTRA_SEMI_WARNING _Pragma("clang diagnostic pop")
138 #else
139 #    define ANGLE_DISABLE_EXTRA_SEMI_WARNING
140 #    define ANGLE_REENABLE_EXTRA_SEMI_WARNING
141 #endif
142 
143 #if defined(__clang__)
144 #    define ANGLE_DISABLE_EXTRA_SEMI_STMT_WARNING \
145         _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wextra-semi-stmt\"")
146 #    define ANGLE_REENABLE_EXTRA_SEMI_STMT_WARNING _Pragma("clang diagnostic pop")
147 #else
148 #    define ANGLE_DISABLE_EXTRA_SEMI_STMT_WARNING
149 #    define ANGLE_REENABLE_EXTRA_SEMI_STMT_WARNING
150 #endif
151 
152 #if defined(__clang__)
153 #    define ANGLE_DISABLE_SHADOWING_WARNING \
154         _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wshadow-field\"")
155 #    define ANGLE_REENABLE_SHADOWING_WARNING _Pragma("clang diagnostic pop")
156 #else
157 #    define ANGLE_DISABLE_SHADOWING_WARNING
158 #    define ANGLE_REENABLE_SHADOWING_WARNING
159 #endif
160 
161 #if defined(__clang__)
162 #    define ANGLE_DISABLE_DESTRUCTOR_OVERRIDE_WARNING \
163         _Pragma("clang diagnostic push")              \
164             _Pragma("clang diagnostic ignored \"-Winconsistent-missing-destructor-override\"")
165 #    define ANGLE_REENABLE_DESTRUCTOR_OVERRIDE_WARNING _Pragma("clang diagnostic pop")
166 #else
167 #    define ANGLE_DISABLE_DESTRUCTOR_OVERRIDE_WARNING
168 #    define ANGLE_REENABLE_DESTRUCTOR_OVERRIDE_WARNING
169 #endif
170 
171 #if defined(__clang__)
172 #    define ANGLE_DISABLE_UNUSED_FUNCTION_WARNING \
173         _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wunused-function\"")
174 #    define ANGLE_REENABLE_UNUSED_FUNCTION_WARNING _Pragma("clang diagnostic pop")
175 #else
176 #    define ANGLE_DISABLE_UNUSED_FUNCTION_WARNING
177 #    define ANGLE_REENABLE_UNUSED_FUNCTION_WARNING
178 #endif
179 
180 #endif  // COMMON_DEBUG_H_
181