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