1 /*
2 * Percepio Trace Recorder for Tracealyzer v4.6.6
3 * Copyright 2021 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 #define TRC_ASSERT_BUFFER_SIZE (sizeof(TraceEntryHandle_t))
72 
73 typedef struct TraceAssertBuffer
74 {
75 	uint8_t buffer[TRC_ASSERT_BUFFER_SIZE];
76 } TraceAssertBuffer_t;
77 
78 /**
79  * @internal Initializes assert system
80  *
81  * @param[in] pxBuffer The assert data buffer
82  *
83  * @retval TRC_FAIL Failure
84  * @retval TRC_SUCCESS Success
85  */
86 traceResult xTraceAssertInitialize(TraceAssertBuffer_t *pxBuffer);
87 
88 /**
89  * @internal Creates an assert
90  *
91  * @param[in] szFilePath File name
92  * @param[in] uxLineNumber Line number
93  *
94  * @retval TRC_FAIL Failure
95  * @retval TRC_SUCCESS Success
96  */
97 void prvTraceAssertCreate(const char* szFilePath, TraceUnsignedBaseType_t uxLineNumber);
98 
99 /**
100  * @brief Retrieves the assert and line number
101  *
102  * @param[out] pxFileNameStringHandle File name string handle
103  * @param[out] puxLineNumber Line number
104  *
105  * @retval TRC_FAIL Failure
106  * @retval TRC_SUCCESS Success
107  */
108 traceResult xTraceAssertGet(TraceStringHandle_t* pxFileNameStringHandle, TraceUnsignedBaseType_t* puxLineNumber);
109 
110 #else /* ((TRC_CFG_USE_TRACE_ASSERT) == 1) */
111 
112 #define TRC_ASSERT(__condition)
113 
114 #define TRC_ASSERT_ALWAYS_EVALUATE(__condition) (void)(__condition)
115 
116 #define TRC_ASSERT_CUSTOM_ON_FAIL(__condition, __custom_on_fail)
117 
118 #define TRC_ASSERT_CUSTOM_ON_FAIL_ALWAYS_EVALUATE(__condition, __custom_on_fail) (__condition)
119 
120 #define TRC_ASSERT_EQUAL_SIZE(x, y)
121 
122 typedef struct TraceAssertBuffer
123 {
124 	uint32_t buffer[1];
125 } TraceAssertBuffer_t;
126 
127 #define xTraceAssertInitialize(pxBuffer) ((void)pxBuffer, TRC_SUCCESS)
128 
129 #define xTraceAssertGet(pxFileNameStringHandle, puxLineNumber) ((void)pxFileNameStringHandle, (void)puxLineNumber, TRC_FAIL)
130 
131 #endif /* ((TRC_CFG_USE_TRACE_ASSERT) == 1) */
132 
133 /** @} */
134 
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
140 
141 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
142 
143 #endif /* TRC_ASSERT_H */
144