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