1 /** 2 * Copyright Notice: 3 * Copyright 2021-2022 DMTF. All rights reserved. 4 * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md 5 **/ 6 7 #ifndef DEBUG_LIB_H 8 #define DEBUG_LIB_H 9 10 /** @file 11 * Provides services to print debug and assert messages to a debug output device. 12 * 13 * The debug library supports debug print and asserts based on a combination of macros and code. 14 * The debug library can be turned on and off so that the debug code does not increase the size of 15 * an image. 16 **/ 17 18 #include "hal/base.h" 19 #include "internal/libspdm_lib_config.h" 20 21 #if LIBSPDM_DEBUG_PRINT_ENABLE 22 23 /* Declare bits for the error_level parameter of libspdm_debug_print(). */ 24 #define LIBSPDM_DEBUG_INFO 0x00000040 25 #define LIBSPDM_DEBUG_ERROR 0x80000000 26 27 /** 28 * Prints a debug message to the debug output device if the specified error level is enabled. 29 * 30 * @param error_level The error level of the debug message, either LIBSPDM_DEBUG_INFO or 31 * LIBSPDM_DEBUG_ERROR. 32 * @param format The format string for the debug message to print. 33 * @param ... The variable argument list whose contents are accessed 34 * based on the format string specified by format. 35 **/ 36 extern void libspdm_debug_print(size_t error_level, const char *format, ...); 37 #endif /* LIBSPDM_DEBUG_PRINT_ENABLE */ 38 39 #if LIBSPDM_DEBUG_ASSERT_ENABLE 40 /** 41 * Prints an assert message containing a filename, line number, and description. 42 * This may be followed by a breakpoint or a dead loop. 43 * 44 * @param file_name The pointer to the name of the source file that generated the assert 45 * condition. 46 * @param line_number The line number in the source file that generated the assert condition 47 * @param description The pointer to the description of the assert condition. 48 * 49 **/ 50 extern void libspdm_debug_assert(const char *file_name, size_t line_number, 51 const char *description); 52 #endif /* LIBSPDM_DEBUG_ASSERT_ENABLE */ 53 54 /** 55 * LIBSPDM_ASSERT(expression) - Macro that calls libspdm_debug_assert() if an expression evaluates 56 * to false. It is enabled through the LIBSPDM_DEBUG_ASSERT_ENABLE 57 * macro. 58 * 59 * @param expression Boolean expression. 60 **/ 61 #if LIBSPDM_DEBUG_ASSERT_ENABLE 62 #define LIBSPDM_ASSERT(expression) \ 63 do { \ 64 if (!(expression)) { \ 65 libspdm_debug_assert(__FILE__, __LINE__, #expression); \ 66 } \ 67 } while (false) 68 #else 69 #define LIBSPDM_ASSERT(expression) 70 #endif 71 72 /** 73 * LIBSPDM_DEBUG(level, format_string, argument_list...) - Macro that calls libspdm_debug_print(). 74 * It is enabled through the 75 * LIBSPDM_PRINT_ENABLE macro. 76 * @param print_level Either LIBSPDM_DEBUG_INFO or LIBSPDM_DEBUG_ERROR. 77 * @param format_string Formatted string. 78 * @param argument_list List of arguments. 79 * 80 * Note that format_string and argument_list are the same as those defined by the C printf function. 81 **/ 82 #if LIBSPDM_DEBUG_PRINT_ENABLE 83 #define LIBSPDM_DEBUG(expression) \ 84 do { \ 85 LIBSPDM_DEBUG_INTERNAL(expression); \ 86 } while (false) 87 88 #define LIBSPDM_DEBUG_PRINT_INTERNAL(print_level, ...) \ 89 do { \ 90 libspdm_debug_print(print_level, ## __VA_ARGS__); \ 91 } while (false) 92 93 #define LIBSPDM_DEBUG_INTERNAL(expression) LIBSPDM_DEBUG_PRINT_INTERNAL expression 94 #else 95 #define LIBSPDM_DEBUG(expression) 96 #endif 97 98 /** 99 * LIBSPDM_DEBUG_CODE(expression) - Macro that defines a section of debug source code. 100 * It is enabled through the LIBSPDM_BLOCK_ENABLE macro. 101 * 102 * @param expression One or more lines of expressions that are used for debugging and should not 103 * affect the behavior of functional code. 104 **/ 105 #if LIBSPDM_DEBUG_BLOCK_ENABLE 106 #define LIBSPDM_DEBUG_CODE(expression) \ 107 do { \ 108 uint8_t __debug_code_local; \ 109 expression \ 110 __debug_code_local = 0; \ 111 __debug_code_local++; \ 112 } while (false) 113 #else 114 #define LIBSPDM_DEBUG_CODE(expression) 115 #endif 116 117 #endif /* DEBUG_LIB_H */ 118