1 /*
2 * Percepio Trace Recorder for Tracealyzer v4.9.2
3 * Copyright 2023 Percepio AB
4 * www.percepio.com
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 /**
10 * @file
11 *
12 * @brief Public trace assert APIs.
13 */
14
15 #ifndef TRC_ASSERT_H
16 #define TRC_ASSERT_H
17
18 #if (TRC_USE_TRACEALYZER_RECORDER == 1) && (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
19
20 #include <trcTypes.h>
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 /**
27 * @defgroup trace_assert_apis Trace Asserts APIs
28 * @ingroup trace_recorder_apis
29 * @{
30 */
31
32 #ifndef TRC_CFG_USE_TRACE_ASSERT
33 #error "TRC_CFG_USE_TRACE_ASSERT is not defined. Please define it in trcConfig.h"
34 #endif
35
36 #if ((TRC_CFG_USE_TRACE_ASSERT) == 1)
37
38 /* Standard assert */
39 #define TRC_ASSERT(__condition) if (!(__condition)) { prvTraceAssertCreate(__FILE__, __LINE__); return TRC_FAIL; }
40
41 #define TRC_ASSERT_ALWAYS_EVALUATE TRC_ASSERT
42
43 /* Standard assert with custom on fail actions */
44 #define TRC_ASSERT_CUSTOM_ON_FAIL(__condition, __custom_on_fail) if (!(__condition)) { prvTraceAssertCreate(__FILE__, __LINE__); __custom_on_fail; }
45
46 #define TRC_ASSERT_CUSTOM_ON_FAIL_ALWAYS_EVALUATE TRC_ASSERT_CUSTOM_ON_FAIL
47
48 #if (defined(TRC_CFG_TEST_MODE) && (TRC_CFG_TEST_MODE) == 1)
49
50 /* Asserts that two types have an equal size. Condition passed to function to avoid compilers warning about unreachable code due to constant value. */
51 #define TRC_ASSERT_EQUAL_SIZE(x, y) if (!prvTraceAssertCheckCondition((TraceBaseType_t)(sizeof(x) == sizeof(y)))) { prvTraceAssertCreate(__FILE__, __LINE__); return TRC_FAIL; }
52
53 /**
54 * @brief Inlined condition check to get around some compiler warnings for unused variables.
55 *
56 * @param[in] condition The condition
57 */
prvTraceAssertCheckCondition(TraceBaseType_t condition)58 inline TraceBaseType_t prvTraceAssertCheckCondition(TraceBaseType_t condition)
59 {
60 return condition;
61 }
62
63 #else
64
65 #define TRC_ASSERT_EQUAL_SIZE(x, y)
66
67 #endif
68
69
70 typedef struct TraceAssertData /* Aligned */
71 {
72 TraceEntryHandle_t xEntry;
73 } TraceAssertData_t;
74
75 /**
76 * @internal Initializes assert system
77 *
78 * @param[in] pxBuffer The assert data buffer
79 *
80 * @retval TRC_FAIL Failure
81 * @retval TRC_SUCCESS Success
82 */
83 traceResult xTraceAssertInitialize(TraceAssertData_t *pxBuffer);
84
85 /**
86 * @internal Creates an assert
87 *
88 * @param[in] szFilePath File name
89 * @param[in] uxLineNumber Line number
90 *
91 * @retval TRC_FAIL Failure
92 * @retval TRC_SUCCESS Success
93 */
94 void prvTraceAssertCreate(const char* szFilePath, TraceUnsignedBaseType_t uxLineNumber);
95
96 /**
97 * @brief Retrieves the assert and line number
98 *
99 * @param[out] pxFileNameStringHandle File name string handle
100 * @param[out] puxLineNumber Line number
101 *
102 * @retval TRC_FAIL Failure
103 * @retval TRC_SUCCESS Success
104 */
105 traceResult xTraceAssertGet(TraceStringHandle_t* pxFileNameStringHandle, TraceUnsignedBaseType_t* puxLineNumber);
106
107 #else /* ((TRC_CFG_USE_TRACE_ASSERT) == 1) */
108
109 #define TRC_ASSERT(__condition)
110
111 #define TRC_ASSERT_ALWAYS_EVALUATE(__condition) (void)(__condition)
112
113 #define TRC_ASSERT_CUSTOM_ON_FAIL(__condition, __custom_on_fail)
114
115 #define TRC_ASSERT_CUSTOM_ON_FAIL_ALWAYS_EVALUATE(__condition, __custom_on_fail) (__condition)
116
117 #define TRC_ASSERT_EQUAL_SIZE(x, y)
118
119 typedef struct TraceAssertData
120 {
121 TraceUnsignedBaseType_t buffer[1];
122 } TraceAssertData_t;
123
124 #define xTraceAssertInitialize(pxBuffer) ((void)pxBuffer, TRC_SUCCESS)
125
126 #define xTraceAssertGet(pxFileNameStringHandle, puxLineNumber) ((void)pxFileNameStringHandle, (void)puxLineNumber, TRC_FAIL)
127
128 #endif /* ((TRC_CFG_USE_TRACE_ASSERT) == 1) */
129
130 /** @} */
131
132 #ifdef __cplusplus
133 }
134 #endif
135
136 #endif
137
138 #endif /* TRC_ASSERT_H */
139