TianoCore EDK2 master
Loading...
Searching...
No Matches
DebugLib.c
Go to the documentation of this file.
1
9#include <Uefi.h>
10
11#include <Library/DebugLib.h>
12#include <Library/PrintLib.h>
13#include <Library/PcdLib.h>
14#include <Library/BaseLib.h>
17
18#include <Protocol/DebugPort.h>
19
20//
21// Define the maximum debug and assert message length that this library supports
22//
23#define MAX_DEBUG_MESSAGE_LENGTH 0x100
24
25//
26// Define the timeout for EFI_DEBUGPORT_PROTOCOL.Write
27//
28#define WRITE_TIMEOUT 1000
29
30EFI_DEBUGPORT_PROTOCOL *mDebugPort = NULL;
31
32//
33// VA_LIST can not initialize to NULL for all compiler, so we use this to
34// indicate a null VA_LIST
35//
36VA_LIST mVaListNull;
37
38extern BOOLEAN mPostEBS;
39extern EFI_BOOT_SERVICES *mDebugBS;
40
51VOID
53 IN CONST CHAR8 *Buffer,
54 IN UINTN BufferLength
55 )
56{
57 UINTN Length;
58 EFI_STATUS Status;
59
60 if (!mPostEBS) {
61 //
62 // If mDebugPort is NULL, initialize first.
63 //
64 if (mDebugPort == NULL) {
65 Status = mDebugBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **)&mDebugPort);
66 if (EFI_ERROR (Status)) {
67 return;
68 }
69
70 mDebugPort->Reset (mDebugPort);
71 }
72
73 //
74 // EFI_DEBUGPORT_PROTOCOL.Write is called until all message is sent.
75 //
76 while (BufferLength > 0) {
77 Length = BufferLength;
78
79 Status = mDebugPort->Write (mDebugPort, WRITE_TIMEOUT, &Length, (VOID *)Buffer);
80 if (EFI_ERROR (Status) || (BufferLength < Length)) {
81 break;
82 }
83
84 Buffer += Length;
85 BufferLength -= Length;
86 }
87 }
88}
89
105VOID
106EFIAPI
108 IN UINTN ErrorLevel,
109 IN CONST CHAR8 *Format,
110 ...
111 )
112{
113 VA_LIST Marker;
114
115 VA_START (Marker, Format);
116 DebugVPrint (ErrorLevel, Format, Marker);
117 VA_END (Marker);
118}
119
137VOID
139 IN UINTN ErrorLevel,
140 IN CONST CHAR8 *Format,
141 IN VA_LIST VaListMarker,
142 IN BASE_LIST BaseListMarker
143 )
144{
145 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
146
147 if (!mPostEBS) {
148 //
149 // If Format is NULL, then ASSERT().
150 //
151 ASSERT (Format != NULL);
152
153 //
154 // Check driver debug mask value and global mask
155 //
156 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {
157 return;
158 }
159
160 //
161 // Convert the DEBUG() message to an ASCII String
162 //
163 if (BaseListMarker == NULL) {
164 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);
165 } else {
166 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);
167 }
168
169 //
170 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
171 //
173 }
174}
175
191VOID
192EFIAPI
194 IN UINTN ErrorLevel,
195 IN CONST CHAR8 *Format,
196 IN VA_LIST VaListMarker
197 )
198{
199 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);
200}
201
219VOID
220EFIAPI
222 IN UINTN ErrorLevel,
223 IN CONST CHAR8 *Format,
224 IN BASE_LIST BaseListMarker
225 )
226{
227 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);
228}
229
253VOID
254EFIAPI
256 IN CONST CHAR8 *FileName,
257 IN UINTN LineNumber,
258 IN CONST CHAR8 *Description
259 )
260{
261 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
262
263 if (!mPostEBS) {
264 //
265 // Generate the ASSERT() message in ASCII format
266 //
268 Buffer,
269 sizeof (Buffer),
270 "ASSERT [%a] %a(%d): %a\n",
271 gEfiCallerBaseName,
272 FileName,
273 LineNumber,
274 Description
275 );
276
277 //
278 // Send the print string to EFI_DEBUGPORT_PROTOCOL.Write.
279 //
281
282 //
283 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
284 //
285 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
286 CpuBreakpoint ();
287 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
288 CpuDeadLoop ();
289 }
290 }
291}
292
308VOID *
309EFIAPI
311 OUT VOID *Buffer,
312 IN UINTN Length
313 )
314{
315 //
316 // If Buffer is NULL, then ASSERT().
317 //
318 ASSERT (Buffer != NULL);
319
320 //
321 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
322 //
323 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));
324}
325
336BOOLEAN
337EFIAPI
339 VOID
340 )
341{
342 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
343}
344
355BOOLEAN
356EFIAPI
358 VOID
359 )
360{
361 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
362}
363
374BOOLEAN
375EFIAPI
377 VOID
378 )
379{
380 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
381}
382
393BOOLEAN
394EFIAPI
396 VOID
397 )
398{
399 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
400}
401
411BOOLEAN
412EFIAPI
414 IN CONST UINTN ErrorLevel
415 )
416{
417 return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);
418}
UINT64 UINTN
VOID EFIAPI DebugAssert(IN CONST CHAR8 *FileName, IN UINTN LineNumber, IN CONST CHAR8 *Description)
Definition: DebugLib.c:188
VOID EFIAPI DebugVPrint(IN UINTN ErrorLevel, IN CONST CHAR8 *Format, IN VA_LIST VaListMarker)
Definition: DebugLib.c:126
VOID DebugPrintMarker(IN UINTN ErrorLevel, IN CONST CHAR8 *Format, IN VA_LIST VaListMarker, IN BASE_LIST BaseListMarker)
Definition: DebugLib.c:76
VOID EFIAPI DebugBPrint(IN UINTN ErrorLevel, IN CONST CHAR8 *Format, IN BASE_LIST BaseListMarker)
Definition: DebugLib.c:154
VOID *EFIAPI DebugClearMemory(OUT VOID *Buffer, IN UINTN Length)
Definition: DebugLib.c:232
VOID EFIAPI DebugPrint(IN UINTN ErrorLevel, IN CONST CHAR8 *Format,...)
Definition: DebugLib.c:45
BOOLEAN EFIAPI DebugCodeEnabled(VOID)
Definition: DebugLib.c:301
BOOLEAN EFIAPI DebugClearMemoryEnabled(VOID)
Definition: DebugLib.c:321
BOOLEAN EFIAPI DebugPrintEnabled(VOID)
Definition: DebugLib.c:281
BOOLEAN EFIAPI DebugAssertEnabled(VOID)
Definition: DebugLib.c:261
BOOLEAN EFIAPI DebugPrintLevelEnabled(IN CONST UINTN ErrorLevel)
Definition: DebugLib.c:350
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
VOID EFIAPI CpuDeadLoop(VOID)
Definition: CpuDeadLoop.c:25
VOID EFIAPI CpuBreakpoint(VOID)
Definition: CpuBreakpoint.c:26
VOID *EFIAPI SetMem(OUT VOID *Buffer, IN UINTN Length, IN UINT8 Value)
Definition: SetMemWrapper.c:38
UINT32 EFIAPI GetDebugPrintErrorLevel(VOID)
UINTN EFIAPI AsciiBSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString, IN BASE_LIST Marker)
Definition: PrintLib.c:763
UINTN EFIAPI AsciiVSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString, IN VA_LIST Marker)
Definition: PrintLib.c:702
UINTN EFIAPI AsciiSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition: PrintLib.c:813
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define VA_START(Marker, Parameter)
Definition: Base.h:661
CHAR8 * VA_LIST
Definition: Base.h:643
#define IN
Definition: Base.h:279
UINTN * BASE_LIST
Definition: Base.h:711
#define OUT
Definition: Base.h:284
#define VA_END(Marker)
Definition: Base.h:691
VOID UefiDebugLibDebugPortProtocolWrite(IN CONST CHAR8 *Buffer, IN UINTN BufferLength)
Definition: DebugLib.c:52
#define PcdGet8(TokenName)
Definition: PcdLib.h:336
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29