xref: /aosp_15_r20/external/gmmlib/Source/util/gfxDebug.h (revision 35ffd701415c9e32e53136d61a677a8d0a8fc4a5)
1 /*==============================================================================
2 Copyright(c) 2017 Intel Corporation
3 
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files(the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and / or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 OTHER DEALINGS IN THE SOFTWARE.
21 ============================================================================
22 **
23 ** File Name: gfxDebug.h
24 **
25 ** Description:
26 **
27 ** Environment:
28 **
29 ** Notes:
30 **
31 ============================================================================*/
32 
33 #pragma once
34 
35 #ifndef _GFXDEBUG_H_
36 #define _GFXDEBUG_H_
37 
38 #include <stdint.h>
39 
40 #if defined(D3D) && !defined (USERMODE_DRIVER)
41 // The D3D XP Kernel Mode driver will continue to use the old scheme until it
42 // has been updated.
43 #else
44 
45 // This portion is used by new stile debug scheme.
46 // NOTE: There is some overlap between defines.
47 
48 
49 //========================================================================
50 // Controlling Debug Messages: There are two types of output messages -
51 // debug or release output message. These are controlled through the two
52 // flags listed below.
53 
54 #ifdef _DEBUG
55   #define __DEBUG_MESSAGE   1
56   #define __RELEASE_MESSAGE 1
57 #else
58   #define __DEBUG_MESSAGE   0
59   #define __RELEASE_MESSAGE 1
60 #endif
61 
62 // !!! HAVE TO FIGURE OOUT HOW TO INCLUDE THIS CORRECTLY
63 // #include "video.h"
64 
65 //==== Define global debug message routine ===================
66 
67 #if __DEBUG_MESSAGE || __RELEASE_MESSAGE
68 #ifdef __UMD
69 #include "stdlib.h"
70 
71 #define MESSAGE_FUNCTION(FUNCTION_NAME,COMPONENT_ID)                        \
72                                                                             \
73     void FUNCTION_NAME(unsigned long MessageLevel, const char *MessageFmt, ...)     \
74 {                                                                           \
75     uint32_t Length = 0;                                                       \
76     char *PrintBuffer = NULL;                                               \
77     char *Prefix = NULL;                                                    \
78     va_list ArgList;                                                        \
79                                                                             \
80     unsigned long ComponentId   = COMPONENT_ID;                                     \
81     unsigned long ComponentMask = 1 << COMPONENT_ID;                                \
82                                                                             \
83     /* Ensure that CRITICAL messages are printed if the setting of the */   \
84     /* global debug variables flag is NORMAL or VERBOSE. Similarly, if */   \
85     /* the setting of the debug variables flag is NORMAL, ensure that  */   \
86     /* the VERBOSE message are printed out too!                        */   \
87                                                                             \
88     if (MessageLevel & GFXDBG_CRITICAL)                                     \
89     {                                                                       \
90         MessageLevel |= GFXDBG_NORMAL | GFXDBG_VERBOSE;                     \
91     }                                                                       \
92     else if (MessageLevel & GFXDBG_NORMAL)                                  \
93     {                                                                       \
94         MessageLevel |= GFXDBG_VERBOSE;                                     \
95     }                                                                       \
96                                                                             \
97     /* Some of routines need to call the debug message functionality    */  \
98     /* before the DebugControl variable has been initialized. Hence, if */  \
99     /* pDebugControl is NULL, unconditionally print the debug message.  */  \
100                                                                             \
101     if ((pDebugControl == NULL) ||                                          \
102     ((pDebugControl->DebugEnableMask & ComponentMask) &&                    \
103     (MessageLevel & pDebugControl->DebugLevel[ComponentId])))               \
104     {                                                                       \
105         va_start (ArgList, MessageFmt);                                     \
106         Length = _vscprintf(MessageFmt, ArgList) + 1;                       \
107         PrintBuffer = malloc(Length * sizeof(char));                        \
108         if (PrintBuffer)                                                    \
109         {                                                                   \
110             vsprintf_s(PrintBuffer, Length, MessageFmt, ArgList);           \
111             Prefix = ComponentIdStrings[ComponentId];                       \
112             __MESSAGE_PRINT(Prefix, PrintBuffer);                           \
113             free(PrintBuffer);                                              \
114         }                                                                   \
115         else                                                                \
116         {                                                                   \
117             __MESSAGE_PRINT("INTC DEBUG: ",                                 \
118                             "Can not allocate print buffer\n");             \
119             __debugbreak();                                                 \
120         }                                                                   \
121         va_end(ArgList);                                                    \
122     }                                                                       \
123 }
124 
125 #else
126 
127 #ifndef __linux__
128     #include "igdKrnlEtwMacros.h"
129 #endif
130 
131 #define MESSAGE_FUNCTION(FUNCTION_NAME,COMPONENT_ID)                        \
132                                                                             \
133     void FUNCTION_NAME(unsigned long MessageLevel, const char *MessageFmt, ...)     \
134 {                                                                           \
135     int32_t Length = 0;                                                       \
136     char PrintBuffer[GFX_MAX_MESSAGE_LENGTH];                               \
137     char *Prefix = NULL;                                                    \
138     va_list ArgList;                                                        \
139     unsigned long GfxDbgLvl = MessageLevel;                                         \
140     unsigned long ComponentId   = COMPONENT_ID;                                     \
141     unsigned long ComponentMask = 1 << COMPONENT_ID;                                \
142                                                                             \
143     /* Ensure that CRITICAL messages are printed if the setting of the */   \
144     /* global debug variables flag is NORMAL or VERBOSE. Similarly, if */   \
145     /* the setting of the debug variables flag is NORMAL, ensure that  */   \
146     /* the VERBOSE message are printed out too!                        */   \
147                                                                             \
148     if (MessageLevel & GFXDBG_CRITICAL)                                     \
149     {                                                                       \
150         MessageLevel |= GFXDBG_NORMAL | GFXDBG_VERBOSE;                     \
151     }                                                                       \
152     else if (MessageLevel & GFXDBG_NORMAL)                                  \
153     {                                                                       \
154         MessageLevel |= GFXDBG_VERBOSE;                                     \
155     }                                                                       \
156                                                                             \
157     /* Some of routines need to call the debug message functionality    */  \
158     /* before the DebugControl variable has been initialized. Hence, if */  \
159     /* pDebugControl is NULL, unconditionally print the debug message.  */  \
160                                                                             \
161     va_start(ArgList, MessageFmt);                                          \
162     Length = _vsnprintf_s(PrintBuffer, GFX_MAX_MESSAGE_LENGTH,              \
163                         sizeof(PrintBuffer),                                \
164                         MessageFmt, ArgList);                               \
165     if (Length >= 0)                                                        \
166     {                                                                       \
167         EtwDebugPrint((uint16_t)GfxDbgLvl, (uint16_t)ComponentId, PrintBuffer); \
168                                                                             \
169         if ((pDebugControl == NULL) ||                                      \
170         ((pDebugControl->DebugEnableMask & ComponentMask) &&                \
171         (MessageLevel & pDebugControl->DebugLevel[ComponentId])))           \
172         {                                                                   \
173             Prefix = ComponentIdStrings[ComponentId];                       \
174             __MESSAGE_PRINT(Prefix, PrintBuffer);                           \
175         }                                                                   \
176     }                                                                       \
177     else                                                                    \
178     {                                                                       \
179         __MESSAGE_PRINT("INTC DEBUG: ",                                     \
180                         "The print buffer is to small\n");                  \
181         __debugbreak();                                                     \
182     }                                                                       \
183     va_end(ArgList);                                                        \
184 }                                                                           \
185 
186 #endif
187 #endif // __DEBUG_MESSAGE || __RELEASE_MESSAGE
188 
189 // Define a max size for the intermediate buffer for storing the
190 // debug error message string.
191 
192 #define GFX_MAX_MESSAGE_LENGTH        (512)
193 
194 //==============================================================================
195 // From winnt.h
196 //
197 // C_ASSERT() can be used to perform many COMPILE-TIME assertions:
198 //            type sizes, field offsets, etc.
199 //
200 // An assertion failure results in error C2118: negative subscript.
201 //
202 // When this assertion is to be used in the middle of a code block,
203 // use it within {} - e.g. {__GL_C_ASSERT (__GL_NUM == 0);}
204 //
205 // Since all components may not want to include "winnt.h", define a
206 // C_ASSERT that can be used by all components.
207 #ifndef GFX_C_ASSERT
208 #define GFX_C_ASSERT(e) typedef char __GFX_C_ASSERT__[(e)?1:-1]
209 #endif
210 
211 
212 // Unfortunately, we cannot include "g_debug.h" before this structure
213 // definition since it needs this structure - but then this structure
214 // requires the number of components. We get around this by using a
215 // large enough number for the component count.
216 
217 #define MAX_COMPONENT_COUNT_DONOTUSE        (20)
218 
219 
220 //------------------------------------------------------------------------------
221 // Debug and assert control structure. Note that each component has
222 // a separate variable to control its debug level.
223 //------------------------------------------------------------------------------
224 
225 typedef struct GFX_DEBUG_CONTROL_REC
226 {
227     unsigned long   Version;
228     unsigned long   Size;
229     unsigned long   AssertEnableMask;
230     unsigned long   EnableDebugFileDump;
231     unsigned long   DebugEnableMask;
232     unsigned long   RingBufDbgMask;
233     unsigned long   ReportAssertEnable;
234     unsigned long   AssertBreakDisable;
235 
236 #ifndef __UMD
237     unsigned long   DebugLevel[MAX_COMPONENT_COUNT_DONOTUSE];
238 #endif
239 
240 } GFX_DEBUG_CONTROL, *PGFX_DEBUG_CONTROL;
241 
242 #ifdef __cplusplus
243     extern "C" {
244 #endif
245 #ifdef __GFXDEBUG_C__          // Only to be defined by "gfxDebug.c" file.
246     GFX_DEBUG_CONTROL  *pDebugControl = NULL;
247 #else
248     extern GFX_DEBUG_CONTROL  *pDebugControl;
249 #endif
250 #ifdef __cplusplus
251     }
252 #endif
253 
254     void InitializeDebugVariables(PGFX_DEBUG_CONTROL pDebugControl);
255 
256 #if (defined(_DEBUG) || defined( _RELEASE_INTERNAL ))
257 
258 #if ( !defined(REPORT_ASSERT) || !defined(REPORT_ASSERT_ETW)) && defined( _WIN32 )
259 #include "AssertTracer.h"
260 #elif !defined(REPORT_ASSERT)
261 #define REPORT_ASSERT( expr )
262 #define REPORT_ASSERT_ETW(compId, componentMask, expr)
263 #endif
264 #endif
265 
266 #ifdef _DEBUG
267 
268     // This function doesn't do anything, it exists so you can put
269     // a break point at the XXX_ASSERT and when you step into it
270     // you will step into this function and then you can
271     // disable/enable the assert by setting AssertOn to FALSE/TRUE.
272 #ifdef _MSC_VER
ToggleAssert(int AssertOn)273     static __inline int ToggleAssert(int AssertOn)
274 #else
275     static inline int ToggleAssert(int AssertOn)
276 #endif
277     {
278         return AssertOn;
279     };
280 
281 #define __ASSERT(pDebugControl, compId, componentMask, expr)                    \
282     {                                                                           \
283         static int __assertOn = TRUE;                                           \
284         __assertOn = ToggleAssert(__assertOn);                                  \
285         REPORT_ASSERT_ETW(compId, componentMask, expr);                         \
286         if (__assertOn)                                                         \
287         {                                                                       \
288             if(!(pDebugControl))                                                \
289             {                                                                   \
290                 if(!(expr))                                                     \
291                 {                                                               \
292                     REPORT_ASSERT(expr);                                        \
293                     __debugbreak();                                             \
294                 }                                                               \
295             }                                                                   \
296             else                                                                \
297             {                                                                   \
298                 if (((componentMask) & pDebugControl->AssertEnableMask) &&      \
299                 !(expr))                                                        \
300                 {                                                               \
301                     if(pDebugControl->ReportAssertEnable)                       \
302                     {                                                           \
303                         REPORT_ASSERT(expr);                                    \
304                     }                                                           \
305                     if(!pDebugControl->AssertBreakDisable)                      \
306                     {                                                           \
307                         __debugbreak();                                         \
308                     }                                                           \
309                 }                                                               \
310                                                                                 \
311             }                                                                   \
312         }                                                                       \
313     }
314 
315 #define __ASSERTPTR(pDebugControl, compId, componentMask, expr, ret)            \
316     {                                                                           \
317         __ASSERT(pDebugControl, compId, componentMask, expr);                   \
318         if (!expr)                                                              \
319         {                                                                       \
320             return ret;                                                         \
321         }                                                                       \
322     }
323 
324 #elif defined(_RELEASE_INTERNAL)
325 
326 #define __ASSERT(pDebugControl, compId, compMask, expr)                     \
327         {                                                                   \
328             REPORT_ASSERT_ETW(compId, compMask, expr);                      \
329         }
330 
331 #define __ASSERTPTR(pDebugControl, compId, compMask, expr, ret)             \
332         {                                                                   \
333             __ASSERT(pDebugControl, compId, compMask, expr);                \
334             if (!expr)                                                      \
335             {                                                               \
336                 return ret;                                                 \
337             }                                                               \
338         }
339 
340 #else
341     #define __ASSERT(pDebugControl, compId, compMask, expr)
342     #define __ASSERTPTR(pDebugControl, compId, compMask ,expr, ret)
343 #endif // _DEBUG
344 
345 
346 #define __RELEASEASSERT(pDebugControl, componentMask, expr)                  \
347 {                                                                            \
348     if (((componentMask) & pDebugControl->AssertEnableMask) && !(expr))      \
349         {                                                                    \
350             __debugbreak();                                                  \
351         }                                                                    \
352 }
353 
354 
355 // The common code may use a macro such as "GFXASSERT". This is defined
356 // to be UMD or KMD ASSERT based on the component being compiled.
357 
358 #ifdef __UMD
359     #define GFXASSERT       UMDASSERT
360 #else
361     #define GFXASSERT       KM_ASSERT
362 #endif
363 
364 #include <stdint.h>
365 #include "g_gfxDebug.h"     // Include automatically generated component
366                             // specific macros etc.
367 
368 
369 GFX_C_ASSERT(MAX_COMPONENT_COUNT_DONOTUSE >= GFX_COMPONENT_COUNT);
370 
371 // !!! WE MIGHT BE ABLE TO DELETE THE FOLLOWING, DOUBLE CHECK FIRST
372 
373 // Ring buffer proxy
374 #define ENABLE_RINGBUF_PROXY_IN_RELEASE_BUILD       0
375 
376 #endif   // D3D not defined
377 
378 #endif // _GFXDEBUG_H_
379