Line data Source code
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 : #include <base.h>
8 :
9 : #include <stdio.h>
10 : #include <stdlib.h>
11 : #include <string.h>
12 : #include <assert.h>
13 : #include <stdarg.h>
14 :
15 : #include "library/debuglib.h"
16 :
17 : #if LIBSPDM_DEBUG_ASSERT_ENABLE
18 : #define LIBSPDM_DEBUG_LIBSPDM_ASSERT_NATIVE 0
19 : #define LIBSPDM_DEBUG_LIBSPDM_ASSERT_DEADLOOP 1
20 : #define LIBSPDM_DEBUG_LIBSPDM_ASSERT_BREAKPOINT 2
21 : #define LIBSPDM_DEBUG_LIBSPDM_ASSERT_EXIT 3
22 :
23 : #ifndef LIBSPDM_DEBUG_LIBSPDM_ASSERT_CONFIG
24 : #define LIBSPDM_DEBUG_LIBSPDM_ASSERT_CONFIG LIBSPDM_DEBUG_LIBSPDM_ASSERT_DEADLOOP
25 : #endif
26 :
27 0 : void libspdm_debug_assert(const char *file_name, size_t line_number, const char *description)
28 : {
29 0 : printf("LIBSPDM_ASSERT: %s(%zu): %s\n", file_name, line_number, description);
30 :
31 : #if (LIBSPDM_DEBUG_LIBSPDM_ASSERT_CONFIG == LIBSPDM_DEBUG_LIBSPDM_ASSERT_DEADLOOP)
32 : {
33 0 : volatile int32_t ___i = 1;
34 0 : while (___i)
35 : ;
36 : }
37 : #elif (LIBSPDM_DEBUG_LIBSPDM_ASSERT_CONFIG == LIBSPDM_DEBUG_LIBSPDM_ASSERT_BREAKPOINT)
38 : #if defined(_MSC_EXTENSIONS)
39 : __debugbreak();
40 : #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
41 : __asm__ __volatile__ ("int $3");
42 : #else
43 : #error Breakpoint asserts are not available on this architecture and/or compiler
44 : #endif
45 : #elif (LIBSPDM_DEBUG_LIBSPDM_ASSERT_CONFIG == LIBSPDM_DEBUG_LIBSPDM_ASSERT_EXIT)
46 : exit(1);
47 : #else
48 : assert(false);
49 : #endif
50 0 : }
51 : #endif /* LIBSPDM_DEBUG_ASSERT_ENABLE */
52 :
53 : #if LIBSPDM_DEBUG_PRINT_ENABLE
54 :
55 : /* Define the maximum debug and assert message length that this library supports. */
56 : #define LIBSPDM_MAX_DEBUG_MESSAGE_LENGTH 0x100
57 :
58 : #ifndef LIBSPDM_DEBUG_LEVEL_CONFIG
59 : #define LIBSPDM_DEBUG_LEVEL_CONFIG (LIBSPDM_DEBUG_INFO | LIBSPDM_DEBUG_ERROR)
60 : #endif
61 :
62 4270712 : void libspdm_debug_print(size_t error_level, const char *format, ...)
63 : {
64 : char buffer[LIBSPDM_MAX_DEBUG_MESSAGE_LENGTH];
65 : va_list marker;
66 : int status;
67 :
68 4270712 : if ((error_level & LIBSPDM_DEBUG_LEVEL_CONFIG) == 0) {
69 0 : return;
70 : }
71 :
72 4270712 : va_start(marker, format);
73 4270712 : status = vsnprintf(buffer, sizeof(buffer), format, marker);
74 4270712 : va_end(marker);
75 :
76 : /* If status is negative then an encoding error has occurred. */
77 4270712 : assert(status >= 0);
78 : /* If status is greater than or equal to the size of the buffer then the buffer is not
79 : * large enough to handle the formatted string. */
80 4270712 : assert(status < sizeof(buffer));
81 :
82 4270712 : printf("%s", buffer);
83 : }
84 : #endif /* LIBSPDM_DEBUG_PRINT_ENABLE */
|