Skip to content

Commit

Permalink
TCBZ1371: Add new DUMP_HEX macro
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeytdisco authored and kenlautner committed May 4, 2023
1 parent aa32c8a commit dddf77d
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions MdePkg/Include/Library/DebugLib.h
Expand Up @@ -631,4 +631,81 @@ UnitTestDebugAssert (
BASE_CR (Record, TYPE, Field)
#endif

/**
Log a formatted hexdump as the debug message on the specified debug
error level.
The hexdump is split into lines of 16 dumped bytes. Each line is
prefixed with a caller-provided string and offset. The full hexdump
is bracketed, and its byte ascii char also print. If the byte value
is not the ascii code, it will print as '.'
@param[in] ErrorLevel The error level of the debug message.
@param[in] Offset Offset to be display after PrefixFormat.
Offset will be increased for each print line.
@param[in] Data The data to dump.
@param[in] DataSize Number of bytes in Data.
@param[in] LinePrefixFormat Format string describing the prefix that is
printed at the beginning of each dump line,
including the bracket lines.
@param[in] ... Arguments for LinePrefixFormat.
**/
#if !defined (MDEPKG_NDEBUG)
#define DUMP_HEX(ErrorLevel, \
Offset, \
Data, \
DataSize, \
LinePrefixFormat, \
...) \
do { \
if (DebugPrintEnabled () && DebugPrintLevelEnabled (ErrorLevel)) { \
UINT8 *_DataToDump; \
UINT8 _Val[50]; \
UINT8 _Str[20]; \
UINT8 _TempByte; \
UINTN _Size; \
UINTN _DumpHexIndex; \
UINTN _LocalOffset; \
UINTN _LocalDataSize; \
CONST CHAR8 *_Hex = "0123456789ABCDEF"; \
_LocalOffset = (Offset); \
_LocalDataSize = (DataSize); \
_DataToDump = (UINT8 *)(Data); \
\
ASSERT (_DataToDump != NULL); \
\
while (_LocalDataSize != 0) { \
_Size = 16; \
if (_Size > _LocalDataSize) { \
_Size = _LocalDataSize; \
} \
\
for (_DumpHexIndex = 0; _DumpHexIndex < _Size; _DumpHexIndex += 1) { \
_TempByte = (UINT8) _DataToDump[_DumpHexIndex]; \
_Val[_DumpHexIndex * 3 + 0] = (UINT8) _Hex[_TempByte >> 4]; \
_Val[_DumpHexIndex * 3 + 1] = (UINT8) _Hex[_TempByte & 0xF]; \
_Val[_DumpHexIndex * 3 + 2] = \
(CHAR8) ((_DumpHexIndex == 7) ? '-' : ' '); \
_Str[_DumpHexIndex] = \
(CHAR8) ((_TempByte < ' ' || _TempByte > '~') ? '.' : _TempByte); \
} \
\
_Val[_DumpHexIndex * 3] = 0; \
_Str[_DumpHexIndex] = 0; \
\
DebugPrint(ErrorLevel, LinePrefixFormat, ##__VA_ARGS__); \
DebugPrint(ErrorLevel, "%08X: %-48a *%a*\r\n", _LocalOffset, _Val, _Str); \
_DataToDump = (UINT8 *)(((UINTN)_DataToDump) + _Size); \
_LocalOffset += _Size; \
_LocalDataSize -= _Size; \
} \
} \
} while (FALSE)
#else
#define DUMP_HEX(ErrorLevel, \
Offset, \
Data, \
DataSize, \
LinePrefixFormat, \
...)
#endif

#endif // __DEBUG_LIB_H__

0 comments on commit dddf77d

Please sign in to comment.