]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Include/Library/DebugLib.h
Synchronize the MdePkg\Include\Library\BaseMemoryLib.h,
[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
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
73 Prints a debug message to the debug output device if the specified error level is enabled.
74
75 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print
76 the message specified by Format and the associated variable argument list to
77 the debug output device.
78
79 If Format is NULL, then ASSERT().
80
81 @param ErrorLevel The error level of the debug message.
82 @param Format Format string for the debug message to print.
83 @param ... Variable argument list whose contents are accessed
84 based on the format string specified by Format.
85
86 **/
87 VOID
88 EFIAPI
89 DebugPrint (
90 IN UINTN ErrorLevel,
91 IN CONST CHAR8 *Format,
92 ...
93 );
94
95
96 /**
97
98 Prints an assert message containing a filename, line number, and description.
99 This may be followed by a breakpoint or a dead loop.
100
101 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
102 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
103 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
104 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
105 CpuDeadLoop() is called. If neither of these bits are set, then this function
106 returns immediately after the message is printed to the debug output device.
107 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
108 processing another DebugAssert(), then DebugAssert() must return immediately.
109
110 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
111
112 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
113
114 @param FileName Pointer to the name of the source file that generated the assert condition.
115 @param LineNumber The line number in the source file that generated the assert condition
116 @param Description Pointer to the description of the assert condition.
117
118 **/
119 VOID
120 EFIAPI
121 DebugAssert (
122 IN CONST CHAR8 *FileName,
123 IN UINTN LineNumber,
124 IN CONST CHAR8 *Description
125 );
126
127
128 /**
129
130 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
131
132 This function fills Length bytes of Buffer with the value specified by
133 PcdDebugClearMemoryValue, and returns Buffer.
134
135 If Buffer is NULL, then ASSERT().
136
137 If Length is greater than (MAX_ADDRESS -Buffer + 1), then ASSERT().
138
139 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
140 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
141
142 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
143
144 **/
145 VOID *
146 EFIAPI
147 DebugClearMemory (
148 OUT VOID *Buffer,
149 IN UINTN Length
150 );
151
152
153 /**
154
155 Returns TRUE if ASSERT() macros are enabled.
156
157 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
158 PcdDebugProperyMask is set. Otherwise FALSE is returned.
159
160 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
161 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
162
163 **/
164 BOOLEAN
165 EFIAPI
166 DebugAssertEnabled (
167 VOID
168 );
169
170
171 /**
172
173 Returns TRUE if DEBUG()macros are enabled.
174
175 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
176 PcdDebugProperyMask is set. Otherwise FALSE is returned.
177
178 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
179 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
180
181 **/
182 BOOLEAN
183 EFIAPI
184 DebugPrintEnabled (
185 VOID
186 );
187
188
189 /**
190
191 Returns TRUE if DEBUG_CODE()macros are enabled.
192
193 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
194 PcdDebugProperyMask is set. Otherwise FALSE is returned.
195
196 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
197 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
198
199 **/
200 BOOLEAN
201 EFIAPI
202 DebugCodeEnabled (
203 VOID
204 );
205
206
207 /**
208
209 Returns TRUE if DEBUG_CLEAR_MEMORY()macro is enabled.
210
211 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
212 PcdDebugProperyMask is set. Otherwise FALSE is returned.
213
214 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
215 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
216
217 **/
218 BOOLEAN
219 EFIAPI
220 DebugClearMemoryEnabled (
221 VOID
222 );
223
224
225 /**
226
227 Internal worker macro that calls DebugAssert().
228
229 This macro calls DebugAssert() passing in the filename, line number, and
230 expression that evailated to FALSE.
231
232 @param Expression Boolean expression that evailated to FALSE
233
234 **/
235 #define _ASSERT(Expression) DebugAssert (__FILE__, __LINE__, #Expression)
236
237
238 /**
239
240 Internal worker macro that calls DebugPrint().
241
242 This macro calls DebugPrint() passing in the debug error level, a format
243 string, and a variable argument list.
244
245 @param Expression Expression containing an error level, a format string,
246 and a variable argument list based on the format string.
247
248 **/
249 #define _DEBUG(Expression) DebugPrint Expression
250
251
252 /**
253
254 Macro that calls DebugAssert() if a expression evaluates to FALSE.
255
256 If the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set,
257 then this macro evaluates the Boolean expression specified by Expression. If
258 Expression evaluates to FALSE, then DebugAssert() is called passing in the
259 source filename, source line number, and Expression.
260
261 @param Expression Boolean expression
262
263 **/
264 #define ASSERT(Expression) \
265 do { \
266 if (DebugAssertEnabled ()) { \
267 if (!(Expression)) { \
268 _ASSERT (Expression); \
269 } \
270 } \
271 } while (FALSE)
272
273
274 /**
275
276 Macro that calls DebugPrint().
277
278 If the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set,
279 then this macro passes Expression to DebugPrint().
280
281 @param Expression Expression containing an error level, a format string,
282 and a variable argument list based on the format string.
283
284
285 **/
286 #define DEBUG(Expression) \
287 do { \
288 if (DebugPrintEnabled ()) { \
289 _DEBUG (Expression); \
290 } \
291 } while (FALSE)
292
293
294 /**
295
296 Macro that calls DebugAssert() if an EFI_STATUS evaluates to an error code.
297
298 If the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set,
299 then this macro evaluates the EFI_STATUS value specified by StatusParameter.
300 If StatusParameter is an error code, then DebugAssert() is called passing in
301 the source filename, source line number, and StatusParameter.
302
303 @param StatusParameter EFI_STATUS value to evaluate.
304
305 **/
306 #define ASSERT_EFI_ERROR(StatusParameter) \
307 do { \
308 if (DebugAssertEnabled ()) { \
309 if (EFI_ERROR (StatusParameter)) { \
310 DEBUG ((EFI_D_ERROR, "\nASSERT_EFI_ERROR (Status = %r)\n", StatusParameter)); \
311 _ASSERT (!EFI_ERROR (StatusParameter)); \
312 } \
313 } \
314 } while (FALSE)
315
316
317 /**
318
319 Macro that calls DebugAssert() if a protocol is already installed in the
320 handle database.
321
322 If the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear,
323 then return.
324
325 If Handle is NULL, then a check is made to see if the protocol specified by Guid
326 is present on any handle in the handle database. If Handle is not NULL, then
327 a check is made to see if the protocol specified by Guid is present on the
328 handle specified by Handle. If the check finds the protocol, then DebugAssert()
329 is called passing in the source filename, source line number, and Guid.
330
331 If Guid is NULL, then ASSERT().
332
333 @param Handle The handle to check for the protocol. This is an optional
334 parameter that may be NULL. If it is NULL, then the entire
335 handle database is searched.
336
337 @param Guid Pointer to a protocol GUID.
338
339 **/
340 #define ASSERT_PROTOCOL_ALREADY_INSTALLED(Handle, Guid) \
341 do { \
342 if (DebugAssertEnabled ()) { \
343 VOID *Instance; \
344 ASSERT (Guid != NULL); \
345 if (Handle == NULL) { \
346 if (!EFI_ERROR (gBS->LocateProtocol ((EFI_GUID *)Guid, NULL, &Instance))) { \
347 _ASSERT (Guid already installed in database); \
348 } \
349 } else { \
350 if (!EFI_ERROR (gBS->HandleProtocol (Handle, (EFI_GUID *)Guid, &Instance))) { \
351 _ASSERT (Guid already installed on Handle); \
352 } \
353 } \
354 } \
355 } while (FALSE)
356
357
358 /**
359 Macro that marks the beginning of debug source code.
360
361 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
362 then this macro marks the beginning of source code that is included in a module.
363 Otherwise, the source lines between DEBUG_CODE_BEGIN() and DEBUG_CODE_END()
364 are not included in a module.
365
366 **/
367 #define DEBUG_CODE_BEGIN() do { if (DebugCodeEnabled ()) { UINT8 __DebugCodeLocal
368
369
370 /**
371
372 Macro that marks the end of debug source code.
373
374 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
375 then this macro marks the end of source code that is included in a module.
376 Otherwise, the source lines between DEBUG_CODE_BEGIN() and DEBUG_CODE_END()
377 are not included in a module.
378
379 **/
380 #define DEBUG_CODE_END() __DebugCodeLocal = 0; __DebugCodeLocal++; } } while (FALSE)
381
382
383 /**
384
385 Macro that declares a section of debug source code.
386
387 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
388 then the source code specified by Expression is included in a module.
389 Otherwise, the source specified by Expression is not included in a module.
390
391 **/
392 #define DEBUG_CODE(Expression) \
393 DEBUG_CODE_BEGIN (); \
394 Expression \
395 DEBUG_CODE_END ()
396
397
398 /**
399
400 Macro that calls DebugClearMemory() to clear a buffer to a default value.
401
402 If the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set,
403 then this macro calls DebugClearMemory() passing in Address and Length.
404
405 @param Address Pointer to a buffer.
406 @param Length The number of bytes in the buffer to set.
407
408 **/
409 #define DEBUG_CLEAR_MEMORY(Address, Length) \
410 do { \
411 if (DebugClearMemoryEnabled ()) { \
412 DebugClearMemory (Address, Length); \
413 } \
414 } while (FALSE)
415
416
417 /**
418
419 Macro that calls DebugAssert() if the containing record does not have a
420 matching signature. If the signatures matches, then a pointer to the data
421 structure that contains a specified field of that data structure is returned.
422 This is a light weight method hide information by placing a public data
423 structure inside a larger private data structure and using a pointer to the
424 public data structure to retrieve a pointer to the private data structure.
425
426 If the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear,
427 then this macro computes the offset, in bytes, of field specified by Field
428 from the beginning of the data structure specified by TYPE. This offset is
429 subtracted from Record, and is used to return a pointer to a data structure
430 of the type specified by TYPE.
431
432 If the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set,
433 then this macro computes the offset, in bytes, of field specified by Field from
434 the beginning of the data structure specified by TYPE. This offset is
435 subtracted from Record, and is used to compute a pointer to a data structure of
436 the type specified by TYPE. The Signature field of the data structure specified
437 by TYPE is compared to TestSignature. If the signatures match, then a pointer
438 to the pointer to a data structure of the type specified by TYPE is returned.
439 If the signatures do not match, then DebugAssert() is called with a description
440 of "CR has a bad signature" and Record is returned.
441
442 If the data type specified by TYPE does not contain the field specified by Field,
443 then the module will not compile.
444
445 If TYPE does not contain a field called Signature, then the module will not
446 compile.
447
448 @param Record Pointer to the field specified by Field within a data
449 structure of type TYPE.
450
451 @param TYPE The name of the data structure type to return This
452 data structure must contain the field specified by Field.
453
454 @param Field The name of the field in the data structure specified
455 by TYPE to which Record points.
456
457 @param TestSignature The 32-bit signature value to match.
458
459 **/
460 #define CR(Record, TYPE, Field, TestSignature) \
461 (DebugAssertEnabled () && (_CR (Record, TYPE, Field)->Signature != TestSignature)) ? \
462 (TYPE *) (_ASSERT (CR has Bad Signature), Record) : \
463 _CR (Record, TYPE, Field)
464
465 #endif