1 /*
2  * Percepio Trace Recorder SDK for Tracealyzer v4.5.1
3  * Copyright 2021 Percepio AB
4  * www.percepio.com
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * Percepio Tracealyzer Recorder SDK
9  */
10 
11 #include <stdio.h>
12 #include "trcSDK.h"
13 
14 extern uint16_t CurrentFilterMask;
15 
16 extern uint16_t CurrentFilterGroup;
17 
18 TRACE_ALLOC_CRITICAL_SECTION();
19 
20 void vTraceSDKSetObjectFilter();
21 
22 /*******************************************************************************
23 * xTraceSDKRegisterObject
24 *
25 * This function will register an object in the trace.
26 *
27 * @param uiEventCode The event code.
28 * @param pxObject The pointer to the object.
29 * @param uiData The object's initial data.
30 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
31 ******************************************************************************/
xTraceSDKRegisterObject(uint32_t uiEventCode,void * pxObject,uint32_t uiData)32 traceResult xTraceSDKRegisterObject(uint32_t uiEventCode, void* pxObject, uint32_t uiData)
33 {
34 	traceResult xResult = TRACE_FAIL;
35 
36 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
37 	prvTraceSaveObjectData(pxObject, uiData); \
38 	if (prvTraceBeginStoreEvent(uiEventCode, sizeof(uint32_t) + sizeof(uint32_t)) == TRACE_SUCCESS)
39 	{
40 		prvTraceStoreEventPayload32((uint32_t)pxObject);
41 		prvTraceStoreEventPayload32(uiData);
42 		prvTraceEndStoreEvent();
43 		xResult = TRACE_SUCCESS;
44 	}
45 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
46 
47 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
48 	(void)uiEventCode;
49 	(void)pxObject;
50 	(void)uiData;
51 	xResult = TRACE_SUCCESS;
52 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
53 
54 	return xResult;
55 }
56 
57 /*******************************************************************************
58 * xTraceSDKUnregisterObject
59 *
60 * This function will unregister an object in the trace.
61 *
62 * @param uiEventCode The event code.
63 * @param pxObject The pointer to the object.
64 * @param uiData The object's end data.
65 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
66 ******************************************************************************/
xTraceSDKUnregisterObject(uint32_t uiEventCode,void * pxObject,uint32_t uiData)67 traceResult xTraceSDKUnregisterObject(uint32_t uiEventCode, void* pxObject, uint32_t uiData)
68 {
69 	traceResult xResult = TRACE_FAIL;
70 
71 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
72 	if (prvTraceBeginStoreEvent(uiEventCode, sizeof(uint32_t) + sizeof(uint32_t)) == TRACE_SUCCESS)
73 	{
74 		prvTraceStoreEventPayload32((uint32_t)pxObject);
75 		prvTraceStoreEventPayload32(uiData);
76 		prvTraceEndStoreEvent();
77 		xResult = TRACE_SUCCESS;
78 	}
79 	/* Only remove the symbol entry */
80 	prvTraceDeleteSymbol(pxObject);
81 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
82 
83 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
84 	(void)uiEventCode;
85 	(void)pxObject;
86 	(void)uiData;
87 	xResult = TRACE_SUCCESS;
88 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
89 
90 	return xResult;
91 }
92 
93 /*******************************************************************************
94 * xTraceSDKSetObjectName
95 *
96 * This function will set an object's name.
97 *
98 * @param pxObject The pointer to the object.
99 * @param pszName The task name.
100 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
101 ******************************************************************************/
xTraceSDKSetObjectName(void * pxObject,const char * pszName)102 traceResult xTraceSDKSetObjectName(void* pxObject, const char* pszName)
103 {
104 	traceResult xResult = TRACE_FAIL;
105 
106 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
107 	prvTraceSaveObjectSymbol(pxObject, (const char*)pszName);
108 	prvTraceStoreStringEvent(1, PSF_EVENT_OBJ_NAME, (const char*)pszName, (uint32_t)pxObject);
109 	xResult = TRACE_SUCCESS;
110 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
111 
112 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
113 	(void)pxObject;
114 	(void)pszName;
115 	xResult = TRACE_SUCCESS;
116 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
117 
118 	return xResult;
119 }
120 
121 /*******************************************************************************
122 * xTraceSDKSetObjectData
123 *
124 * This function will register a task in the trace.
125 *
126 * @param pxObject The pointer to the object.
127 * @param uiData The object's data.
128 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
129 ******************************************************************************/
xTraceSDKSetObjectData(void * pxObject,uint32_t uiData)130 traceResult xTraceSDKSetObjectData(void* pxObject, uint32_t uiData)
131 {
132 	traceResult xResult = TRACE_FAIL;
133 
134 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
135 	prvTraceSaveObjectData(pxObject, uiData);
136 	xResult = TRACE_SUCCESS;
137 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
138 
139 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
140 	(void)pxObject;
141 	(void)uiData;
142 	xResult = TRACE_SUCCESS;
143 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
144 
145 	return xResult;
146 }
147 
148 /*******************************************************************************
149 * xTraceSDKTaskSwitch
150 *
151 * This function will register a task switch and priority.
152 *
153 * @param pxTCB The pointer to the TCB.
154 * @param uiPriority The task priority.
155 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
156 ******************************************************************************/
xTraceSDKTaskSwitch(void * pxTCB,uint32_t uiPriority)157 traceResult xTraceSDKTaskSwitch(void* pxTCB, uint32_t uiPriority)
158 {
159 	traceResult xResult = TRACE_FAIL;
160 
161 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
162 	extern volatile uint32_t uiTraceSystemState;
163 	uiTraceSystemState = TRC_STATE_IN_TASKSWITCH;
164 	if (prvTraceGetCurrentTask() != (uint32_t)pxTCB)
165 	{
166 		prvTraceSetCurrentTask((uint32_t)pxTCB);
167 		if (prvTraceBeginStoreEvent(PSF_EVENT_TASK_ACTIVATE, sizeof(uint32_t)) == TRACE_SUCCESS)
168 		{
169 			prvTraceStoreEventPayload32((uint32_t)pxTCB);
170 			prvTraceStoreEventPayload32(uiPriority);
171 			prvTraceEndStoreEvent();
172 			xResult = TRACE_SUCCESS;
173 		}
174 	}
175 	uiTraceSystemState = TRC_STATE_IN_APPLICATION;
176 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
177 
178 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
179 	(void)pxTCB;
180 	(void)uiPriority;
181 	xResult = TRACE_SUCCESS;
182 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
183 
184 	return xResult;
185 }
186 
187 /*******************************************************************************
188 * xTraceSDKTaskReady
189 *
190 * This function will set a task to ready in the trace.
191 *
192 * @param pxTCB The pointer to the TCB.
193 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
194 ******************************************************************************/
xTraceSDKTaskReady(void * pxTCB)195 traceResult xTraceSDKTaskReady(void* pxTCB)
196 {
197 	traceResult xResult = TRACE_FAIL;
198 
199 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
200 	if (prvTraceBeginStoreEvent(PSF_EVENT_TASK_READY, sizeof(uint32_t)) == TRACE_SUCCESS)
201 	{
202 		prvTraceStoreEventPayload32((uint32_t)pxTCB);
203 		prvTraceEndStoreEvent();
204 		xResult = TRACE_SUCCESS;
205 	}
206 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
207 
208 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
209 	(void)pxTCB;
210 	xResult = TRACE_SUCCESS;
211 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
212 
213 	return xResult;
214 }
215 
216 /*******************************************************************************
217 * xTraceSDKEventBegin
218 *
219 * This function will begin an event and allow for payload to be added via the
220 * xTraceSDKEventAdd****() functions.
221 *
222 * @param uiEventCode The event code.
223 * @param uiPayloadSize The expected payload size in bytes.
224 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
225 ******************************************************************************/
xTraceSDKEventBegin(uint32_t uiEventCode,uint32_t uiPayloadSize)226 traceResult xTraceSDKEventBegin(uint32_t uiEventCode, uint32_t uiPayloadSize)
227 {
228 	TRACE_ENTER_CRITICAL_SECTION();
229 
230 	traceResult xResult = TRACE_FAIL;
231 
232 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
233 	xResult = prvTraceBeginStoreEvent(uiEventCode, uiPayloadSize);
234 
235 	if (xResult == TRACE_FAIL)
236 	{
237 		TRACE_EXIT_CRITICAL_SECTION();
238 	}
239 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
240 
241 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
242 	(void)uiEventCode;
243 	(void)uiPayloadSize;
244 	xResult = TRACE_SUCCESS;
245 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
246 
247 	return xResult;
248 }
249 
250 /*******************************************************************************
251 * xTraceSDKEventEnd
252 *
253 * This function ends an event.
254 *
255 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
256 ******************************************************************************/
xTraceSDKEventEnd()257 traceResult xTraceSDKEventEnd()
258 {
259 	traceResult xResult = TRACE_FAIL;
260 
261 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
262 	xResult = prvTraceEndStoreEvent();
263 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
264 
265 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
266 	xResult = TRACE_SUCCESS;
267 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
268 
269 	TRACE_EXIT_CRITICAL_SECTION();
270 
271 	return xResult;
272 }
273 
274 /*******************************************************************************
275 * xTraceSDKEventAddData
276 *
277 * This function will add a variable size of data as payload.
278 *
279 * @param pvData The pointer to the data.
280 * @param uiSize The data size.
281 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
282 ******************************************************************************/
xTraceSDKEventAddData(void * pvData,uint32_t uiSize)283 traceResult xTraceSDKEventAddData(void* pvData, uint32_t uiSize)
284 {
285 	traceResult xResult = TRACE_FAIL;
286 
287 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
288 	xResult = prvTraceStoreEventPayload(pvData, uiSize);
289 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
290 
291 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
292 	(void)pvData;
293 	(void)uiSize;
294 	xResult = TRACE_SUCCESS;
295 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
296 
297 	return xResult;
298 }
299 
300 /*******************************************************************************
301 * xTraceSDKEventAddObject
302 *
303 * This function will add an object as payload.
304 *
305 * @param pxObject The pointer to the object.
306 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
307 ******************************************************************************/
xTraceSDKEventAddObject(void * pxObject)308 traceResult xTraceSDKEventAddObject(void* pxObject)
309 {
310 	traceResult xResult = TRACE_FAIL;
311 
312 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
313 	xResult = xTraceSDKEventAdd32((uint32_t)pxObject);
314 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
315 
316 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
317 	(void)pxObject;
318 	xResult = TRACE_SUCCESS;
319 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
320 
321 	return xResult;
322 }
323 
324 /*******************************************************************************
325 * xTraceSDKEventAdd32
326 *
327 * This function will add a 32-bit value as payload.
328 *
329 * @param value The value.
330 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
331 ******************************************************************************/
xTraceSDKEventAdd32(uint32_t value)332 traceResult xTraceSDKEventAdd32(uint32_t value)
333 {
334 	traceResult xResult = TRACE_FAIL;
335 
336 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
337 	xResult = prvTraceStoreEventPayload32(value);
338 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
339 
340 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
341 	(void)value;
342 	xResult = TRACE_SUCCESS;
343 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
344 
345 	return xResult;
346 }
347 
348 /*******************************************************************************
349 * xTraceSDKEventAdd16
350 *
351 * This function will add an uint16_t as payload.
352 *
353 * @param value The value.
354 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
355 ******************************************************************************/
xTraceSDKEventAdd16(uint16_t value)356 traceResult xTraceSDKEventAdd16(uint16_t value)
357 {
358 	traceResult xResult = TRACE_FAIL;
359 
360 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
361 	xResult = prvTraceStoreEventPayload16(value);
362 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
363 
364 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
365 	(void)value;
366 	xResult = TRACE_SUCCESS;
367 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
368 
369 	return xResult;
370 }
371 
372 /*******************************************************************************
373 * xTraceSDKEventAdd8
374 *
375 * This function will add an uint8_t as payload.
376 *
377 * @param value The value.
378 * @return Function result. TRACE_SUCCESS or TRACE_FAIL.
379 ******************************************************************************/
xTraceSDKEventAdd8(uint8_t value)380 traceResult xTraceSDKEventAdd8(uint8_t value)
381 {
382 	traceResult xResult = TRACE_FAIL;
383 
384 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
385 	xResult = prvTraceStoreEventPayload8(value);
386 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
387 
388 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
389 	(void)value;
390 	xResult = TRACE_SUCCESS;
391 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
392 
393 	return xResult;
394 }
395