1 /*++
2 
3 Copyright (c) 2004 - 2012, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution.  The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8 
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 
12 Module Name:
13 
14   EfiDebug.h
15 
16 Abstract:
17 
18   EFI Debug macros. The work needs tobe done in library. The Debug
19   macros them selves are standard for all files, including the core.
20 
21   There needs to be code linked in that produces the following macros:
22 
23   EfiDebugAssert(file, linenumber, assertion string) - worker function for
24       ASSERT. filename and line number of where this ASSERT() is located
25       is passed in along with the stringized version of the assertion.
26 
27   EfiDebugPrint - Worker function for debug print
28 
29   _DEBUG_SET_MEM(address, length, value) - Set memory at address to value
30     for legnth bytes. This macro is used to initialzed uninitialized memory
31     or memory that is free'ed, so it will not be used by mistake.
32 
33 --*/
34 
35 #ifndef _EFI_DEBUG_H_
36 #define _EFI_DEBUG_H_
37 
38 #ifdef EFI_DEBUG
39 
40   VOID
41   EfiDebugAssert (
42     IN CHAR8    *FileName,
43     IN INTN     LineNumber,
44     IN CHAR8    *Description
45     );
46 
47   VOID
48   EfiDebugPrint (
49     IN  UINTN ErrorLevel,
50     IN  CHAR8 *Format,
51     ...
52     );
53 
54   VOID
55   EfiDebugVPrint (
56     IN  UINTN   ErrorLevel,
57     IN  CHAR8   *Format,
58     IN  VA_LIST Marker
59     );
60 
61   //
62   // Define macros for the above functions so we can make them go away
63   // in non-debug builds.
64   //
65   #define EFI_DEBUG_VPRINT(ErrorLevel, Format, Marker) \
66                       EfiDebugVPrint(ErrorLevel, Format, Marker)
67 
68   #define EFI_DEBUG_ASSERT(FileName, LineNumber, Description)  \
69                       EfiDebugAssert (FileName, LineNumber, Description)
70 
71   #define _DEBUG_ASSERT(assertion)  \
72             EfiDebugAssert (__FILE__, __LINE__, #assertion)
73 
74   #define _DEBUG(arg) DebugPrint arg
75 
76   //
77   // Define ASSERT() macro, if assertion is FALSE trigger the ASSERT
78   //
79   #define ASSERT(assertion)   if(!(assertion))  \
80                                 _DEBUG_ASSERT(assertion)
81 
82   #define ASSERT_LOCKED(l)    if(!(l)->Lock) _DEBUG_ASSERT(l not locked)
83 
84   //
85   // DEBUG((DebugLevel, "format string", ...)) - if DebugLevel is active do
86   //   the a debug print.
87   //
88   #define DEBUG(arg)        EfiDebugPrint arg
89 
90   #define DEBUG_CODE(code)  code
91 
92   #define CR(record, TYPE, field, signature)                    \
93             _CR(record, TYPE, field)->Signature != signature ?  \
94             (TYPE *) (_DEBUG_ASSERT("CR has Bad Signature"), record) :                        \
95             _CR(record, TYPE, field)
96 
97   #define _DEBUG_SET_MEM(address, length, data) EfiCommonLibSetMem(address, length, data)
98 
99   //
100   // Generate an ASSERT if the protocol specified by GUID is already installed on Handle.
101   // If Handle is NULL, then an ASSERT is generated if the protocol specified by GUID
102   // is present anywhere in the handle database
103   //
104   #define ASSERT_PROTOCOL_ALREADY_INSTALLED(Handle, Guid)               \
105     DEBUG_CODE ( {                                                      \
106     VOID  *Instance;                                                    \
107     if (Handle == NULL) {                                               \
108       ASSERT(EFI_ERROR(gBS->LocateProtocol (Guid, NULL, &Instance)));   \
109     } else {                                                            \
110       ASSERT(EFI_ERROR(gBS->HandleProtocol (Handle, Guid, &Instance))); \
111     } } )
112 
113 #else
114   #define ASSERT(a)
115   #define ASSERT_LOCKED(l)
116   #define DEBUG(arg)
117   #define DEBUG_CODE(code)
118   #define CR(Record, TYPE, Field, Signature)   \
119             _CR(Record, TYPE, Field)
120   #define _DEBUG_SET_MEM(address, length, data)
121   #define EFI_DEBUG_VPRINT(ErrorLevel, Format, Marker)
122   #define EFI_DEBUG_ASSERT(FileName, LineNumber, Description)
123   #define ASSERT_PROTOCOL_ALREADY_INSTALLED(Handle, Guid)
124 #endif
125 
126 //
127 // Generate an ASSERT if Status is an error code
128 //
129 //#define ASSERT_EFI_ERROR(status)  ASSERT(!EFI_ERROR(status))
130 #define ASSERT_EFI_ERROR(status)  if (EFI_ERROR(status))                \
131     DEBUG_CODE ( {                                                      \
132       DEBUG((EFI_D_ERROR, "\nASSERT!Status = 0x%x Info :",status));     \
133       ASSERT(!EFI_ERROR(status));                                \
134     } )
135 
136 #ifdef EFI_DEBUG_CLEAR_MEMORY
137   #define DEBUG_SET_MEMORY(address,length)  \
138             _DEBUG_SET_MEM(address, length, EFI_BAD_POINTER_AS_BYTE)
139 #else
140   #define DEBUG_SET_MEMORY(address,length)
141 #endif
142 
143 #define EFI_D_INIT        0x00000001          // Initialization style messages
144 #define EFI_D_WARN        0x00000002          // Warnings
145 #define EFI_D_LOAD        0x00000004          // Load events
146 #define EFI_D_FS          0x00000008          // EFI File system
147 #define EFI_D_POOL        0x00000010          // Alloc & Free's
148 #define EFI_D_PAGE        0x00000020          // Alloc & Free's
149 #define EFI_D_INFO        0x00000040          // Informational debug messages
150 #define EFI_D_VARIABLE    0x00000100          // Variable
151 #define EFI_D_BM          0x00000400          // Boot Manager (BDS)
152 #define EFI_D_BLKIO       0x00001000          // BlkIo Driver
153 #define EFI_D_NET         0x00004000          // SNI Driver
154 #define EFI_D_UNDI        0x00010000          // UNDI Driver
155 #define EFI_D_LOADFILE    0x00020000          // UNDI Driver
156 #define EFI_D_EVENT       0x00080000          // Event messages
157 #define EFI_D_VERBOSE     0x00400000          // Detailed debug messages that may significantly impact boot performance
158 #define EFI_D_ERROR       0x80000000          // Error
159 
160 #define EFI_D_GENERIC (EFI_D_ERROR | EFI_D_INIT | EFI_D_WARN | EFI_D_INFO | \
161                         EFI_D_BLKIO | EFI_D_NET | EFI_D_UNDI | EFI_D_VERBOSE)
162 
163 #define EFI_D_INTRINSIC ( EFI_D_EVENT | EFI_D_POOL | EFI_D_PAGE | \
164                           EFI_D_BM | EFI_D_LOAD | EFI_D_VARIABLE )
165 
166 #define EFI_D_RESERVED  (EFI_D_GENERIC | EFI_D_INTRINSIC)
167 
168 #define EFI_DBUG_MASK   (EFI_D_ERROR | EFI_D_INFO | EFI_D_WARN)
169 
170 #endif
171