]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Include/Library/DebugLib.h
e25713f5c572f42d610f1a1f2fe7224470af5e26
[mirror_edk2.git] / MdePkg / Include / Library / DebugLib.h
1 /** @file
2 Provides services to print debug and assert messages to a debug output device.
3
4 The Debug library supports debug print and asserts based on a combination of macros and code.
5 The debug library can be turned on and off so that the debug code does not increase the size of an image.
6
7 Copyright (c) 2006 - 2008, Intel Corporation<BR>
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #ifndef __DEBUG_LIB_H__
19 #define __DEBUG_LIB_H__
20
21 //
22 // Declare bits for PcdDebugPropertyMask
23 //
24 #define DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED 0x01
25 #define DEBUG_PROPERTY_DEBUG_PRINT_ENABLED 0x02
26 #define DEBUG_PROPERTY_DEBUG_CODE_ENABLED 0x04
27 #define DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED 0x08
28 #define DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED 0x10
29 #define DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED 0x20
30
31 //
32 // Declare bits for PcdDebugPrintErrorLevel and the ErrorLevel parameter of DebugPrint()
33 //
34 #define DEBUG_INIT 0x00000001 // Initialization
35 #define DEBUG_WARN 0x00000002 // Warnings
36 #define DEBUG_LOAD 0x00000004 // Load events
37 #define DEBUG_FS 0x00000008 // EFI File system
38 #define DEBUG_POOL 0x00000010 // Alloc & Free's
39 #define DEBUG_PAGE 0x00000020 // Alloc & Free's
40 #define DEBUG_INFO 0x00000040 // Verbose
41 #define DEBUG_DISPATCH 0x00000080 // PEI/DXE Dispatchers
42 #define DEBUG_VARIABLE 0x00000100 // Variable
43 #define DEBUG_BM 0x00000400 // Boot Manager
44 #define DEBUG_BLKIO 0x00001000 // BlkIo Driver
45 #define DEBUG_NET 0x00004000 // SNI Driver
46 #define DEBUG_UNDI 0x00010000 // UNDI Driver
47 #define DEBUG_LOADFILE 0x00020000 // UNDI Driver
48 #define DEBUG_EVENT 0x00080000 // Event messages
49 #define DEBUG_ERROR 0x80000000 // Error
50
51 //
52 // Aliases of debug message mask bits
53 //
54 #define EFI_D_INIT DEBUG_INIT
55 #define EFI_D_WARN DEBUG_WARN
56 #define EFI_D_LOAD DEBUG_LOAD
57 #define EFI_D_FS DEBUG_FS
58 #define EFI_D_POOL DEBUG_POOL
59 #define EFI_D_PAGE DEBUG_PAGE
60 #define EFI_D_INFO DEBUG_INFO
61 #define EFI_D_DISPATCH DEBUG_DISPATCH
62 #define EFI_D_VARIABLE DEBUG_VARIABLE
63 #define EFI_D_BM DEBUG_BM
64 #define EFI_D_BLKIO DEBUG_BLKIO
65 #define EFI_D_NET DEBUG_NET
66 #define EFI_D_UNDI DEBUG_UNDI
67 #define EFI_D_LOADFILE DEBUG_LOADFILE
68 #define EFI_D_EVENT DEBUG_EVENT
69 #define EFI_D_ERROR DEBUG_ERROR
70
71 /**
72 Prints a debug message to the debug output device if the specified error level is enabled.
73
74 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print
75 the message specified by Format and the associated variable argument list to
76 the debug output device.
77
78 If Format is NULL, then ASSERT().
79
80 @param ErrorLevel The error level of the debug message.
81 @param Format Format string for the debug message to print.
82 @param ... Variable argument list whose contents are accessed
83 based on the format string specified by Format.
84
85 **/
86 VOID
87 EFIAPI
88 DebugPrint (
89 IN UINTN ErrorLevel,
90 IN CONST CHAR8 *Format,
91 ...
92 );
93
94
95 /**
96 Prints an assert message containing a filename, line number, and description.
97 This may be followed by a breakpoint or a dead loop.
98
99 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
100 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
101 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
102 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
103 CpuDeadLoop() is called. If neither of these bits are set, then this function
104 returns immediately after the message is printed to the debug output device.
105 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
106 processing another DebugAssert(), then DebugAssert() must return immediately.
107
108 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
109 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
110
111 @param FileName Pointer to the name of the source file that generated the assert condition.
112 @param LineNumber The line number in the source file that generated the assert condition
113 @param Description Pointer to the description of the assert condition.
114
115 **/
116 VOID
117 EFIAPI
118 DebugAssert (
119 IN CONST CHAR8 *FileName,
120 IN UINTN LineNumber,
121 IN CONST CHAR8 *Description
122 );
123
124
125 /**
126 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
127
128 This function fills Length bytes of Buffer with the value specified by
129 PcdDebugClearMemoryValue, and returns Buffer.
130
131 If Buffer is NULL, then ASSERT().
132 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
133
134 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
135 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
136
137 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
138
139 **/
140 VOID *
141 EFIAPI
142 DebugClearMemory (
143 OUT VOID *Buffer,
144 IN UINTN Length
145 );
146
147
148 /**
149 Returns TRUE if ASSERT() macros are enabled.
150
151 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
152 PcdDebugProperyMask is set. Otherwise FALSE is returned.
153
154 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
155 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
156
157 **/
158 BOOLEAN
159 EFIAPI
160 DebugAssertEnabled (
161 VOID
162 );
163
164
165 /**
166 Returns TRUE if DEBUG() macros are enabled.
167
168 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
169 PcdDebugProperyMask is set. Otherwise FALSE is returned.
170
171 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
172 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
173
174 **/
175 BOOLEAN
176 EFIAPI
177 DebugPrintEnabled (
178 VOID
179 );
180
181
182 /**
183 Returns TRUE if DEBUG_CODE() macros are enabled.
184
185 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
186 PcdDebugProperyMask is set. Otherwise FALSE is returned.
187
188 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
189 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
190
191 **/
192 BOOLEAN
193 EFIAPI
194 DebugCodeEnabled (
195 VOID
196 );
197
198
199 /**
200 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
201
202 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
203 PcdDebugProperyMask is set. Otherwise FALSE is returned.
204
205 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
206 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
207
208 **/
209 BOOLEAN
210 EFIAPI
211 DebugClearMemoryEnabled (
212 VOID
213 );
214
215
216 /**
217 Internal worker macro that calls DebugAssert().
218
219 This macro calls DebugAssert() passing in the filename, line number, and
220 expression that evailated to FALSE.
221
222 @param Expression Boolean expression that evailated to FALSE
223
224 **/
225 #define _ASSERT(Expression) DebugAssert (__FILE__, __LINE__, #Expression)
226
227
228 /**
229 Internal worker macro that calls DebugPrint().
230
231 This macro calls DebugPrint() passing in the debug error level, a format
232 string, and a variable argument list.
233
234 @param Expression Expression containing an error level, a format string,
235 and a variable argument list based on the format string.
236
237 **/
238 #define _DEBUG(Expression) DebugPrint Expression
239
240
241 /**
242 Macro that calls DebugAssert() if a expression evaluates to FALSE.
243
244 If the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set,
245 then this macro evaluates the Boolean expression specified by Expression. If
246 Expression evaluates to FALSE, then DebugAssert() is called passing in the
247 source filename, source line number, and Expression.
248
249 @param Expression Boolean expression
250
251 **/
252 #define ASSERT(Expression) \
253 do { \
254 if (DebugAssertEnabled ()) { \
255 if (!(Expression)) { \
256 _ASSERT (Expression); \
257 } \
258 } \
259 } while (FALSE)
260
261
262 /**
263 Macro that calls DebugPrint().
264
265 If the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set,
266 then this macro passes Expression to DebugPrint().
267
268 @param Expression Expression containing an error level, a format string,
269 and a variable argument list based on the format string.
270
271
272 **/
273 #define DEBUG(Expression) \
274 do { \
275 if (DebugPrintEnabled ()) { \
276 _DEBUG (Expression); \
277 } \
278 } while (FALSE)
279
280
281 /**
282 Macro that calls DebugAssert() if an EFI_STATUS evaluates to an error code.
283
284 If the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set,
285 then this macro evaluates the EFI_STATUS value specified by StatusParameter.
286 If StatusParameter is an error code, then DebugAssert() is called passing in
287 the source filename, source line number, and StatusParameter.
288
289 @param StatusParameter EFI_STATUS value to evaluate.
290
291 **/
292 #define ASSERT_EFI_ERROR(StatusParameter) \
293 do { \
294 if (DebugAssertEnabled ()) { \
295 if (EFI_ERROR (StatusParameter)) { \
296 DEBUG ((EFI_D_ERROR, "\nASSERT_EFI_ERROR (Status = %r)\n", StatusParameter)); \
297 _ASSERT (!EFI_ERROR (StatusParameter)); \
298 } \
299 } \
300 } while (FALSE)
301
302
303 /**
304 Macro that calls DebugAssert() if a protocol is already installed in the
305 handle database.
306
307 If the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear,
308 then return.
309
310 If Handle is NULL, then a check is made to see if the protocol specified by Guid
311 is present on any handle in the handle database. If Handle is not NULL, then
312 a check is made to see if the protocol specified by Guid is present on the
313 handle specified by Handle. If the check finds the protocol, then DebugAssert()
314 is called passing in the source filename, source line number, and Guid.
315
316 If Guid is NULL, then ASSERT().
317
318 @param Handle The handle to check for the protocol. This is an optional
319 parameter that may be NULL. If it is NULL, then the entire
320 handle database is searched.
321
322 @param Guid Pointer to a protocol GUID.
323
324 **/
325 #define ASSERT_PROTOCOL_ALREADY_INSTALLED(Handle, Guid) \
326 do { \
327 if (DebugAssertEnabled ()) { \
328 VOID *Instance; \
329 ASSERT (Guid != NULL); \
330 if (Handle == NULL) { \
331 if (!EFI_ERROR (gBS->LocateProtocol ((EFI_GUID *)Guid, NULL, &Instance))) { \
332 _ASSERT (Guid already installed in database); \
333 } \
334 } else { \
335 if (!EFI_ERROR (gBS->HandleProtocol (Handle, (EFI_GUID *)Guid, &Instance))) { \
336 _ASSERT (Guid already installed on Handle); \
337 } \
338 } \
339 } \
340 } while (FALSE)
341
342
343 /**
344 Macro that marks the beginning of debug source code.
345
346 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
347 then this macro marks the beginning of source code that is included in a module.
348 Otherwise, the source lines between DEBUG_CODE_BEGIN() and DEBUG_CODE_END()
349 are not included in a module.
350
351 **/
352 #define DEBUG_CODE_BEGIN() do { if (DebugCodeEnabled ()) { UINT8 __DebugCodeLocal
353
354
355 /**
356 Macro that marks the end of debug source code.
357
358 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
359 then this macro marks the end of source code that is included in a module.
360 Otherwise, the source lines between DEBUG_CODE_BEGIN() and DEBUG_CODE_END()
361 are not included in a module.
362
363 **/
364 #define DEBUG_CODE_END() __DebugCodeLocal = 0; __DebugCodeLocal++; } } while (FALSE)
365
366
367 /**
368 Macro that declares a section of debug source code.
369
370 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
371 then the source code specified by Expression is included in a module.
372 Otherwise, the source specified by Expression is not included in a module.
373
374 **/
375 #define DEBUG_CODE(Expression) \
376 DEBUG_CODE_BEGIN (); \
377 Expression \
378 DEBUG_CODE_END ()
379
380
381 /**
382 Macro that calls DebugClearMemory() to clear a buffer to a default value.
383
384 If the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set,
385 then this macro calls DebugClearMemory() passing in Address and Length.
386
387 @param Address Pointer to a buffer.
388 @param Length The number of bytes in the buffer to set.
389
390 **/
391 #define DEBUG_CLEAR_MEMORY(Address, Length) \
392 do { \
393 if (DebugClearMemoryEnabled ()) { \
394 DebugClearMemory (Address, Length); \
395 } \
396 } while (FALSE)
397
398
399 /**
400 Macro that calls DebugAssert() if the containing record does not have a
401 matching signature. If the signatures matches, then a pointer to the data
402 structure that contains a specified field of that data structure is returned.
403 This is a light weight method hide information by placing a public data
404 structure inside a larger private data structure and using a pointer to the
405 public data structure to retrieve a pointer to the private data structure.
406
407 If the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear,
408 then this macro computes the offset, in bytes, of field specified by Field
409 from the beginning of the data structure specified by TYPE. This offset is
410 subtracted from Record, and is used to return a pointer to a data structure
411 of the type specified by TYPE.
412
413 If the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set,
414 then this macro computes the offset, in bytes, of field specified by Field from
415 the beginning of the data structure specified by TYPE. This offset is
416 subtracted from Record, and is used to compute a pointer to a data structure of
417 the type specified by TYPE. The Signature field of the data structure specified
418 by TYPE is compared to TestSignature. If the signatures match, then a pointer
419 to the pointer to a data structure of the type specified by TYPE is returned.
420 If the signatures do not match, then DebugAssert() is called with a description
421 of "CR has a bad signature" and Record is returned.
422
423 If the data type specified by TYPE does not contain the field specified by Field,
424 then the module will not compile.
425
426 If TYPE does not contain a field called Signature, then the module will not
427 compile.
428
429 @param Record Pointer to the field specified by Field within a data
430 structure of type TYPE.
431
432 @param TYPE The name of the data structure type to return This
433 data structure must contain the field specified by Field.
434
435 @param Field The name of the field in the data structure specified
436 by TYPE to which Record points.
437
438 @param TestSignature The 32-bit signature value to match.
439
440 **/
441 #define CR(Record, TYPE, Field, TestSignature) \
442 (DebugAssertEnabled () && (BASE_CR (Record, TYPE, Field)->Signature != TestSignature)) ? \
443 (TYPE *) (_ASSERT (CR has Bad Signature), Record) : \
444 BASE_CR (Record, TYPE, Field)
445
446 #endif