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 * The implementation for tasks.
9 */
10 
11 #include <trcRecorder.h>
12 
13 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
14 
15 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
16 
17 #ifndef TRC_KERNEL_PORT_KERNEL_CAN_SWITCH_TO_SAME_TASK
18 #define TRC_KERNEL_PORT_KERNEL_CAN_SWITCH_TO_SAME_TASK 1
19 #endif
20 
21 /* Code used for "task address" when no task has started, to indicate "(startup)".
22  * This value was used since NULL/0 was already reserved for the idle task. */
23 #define TRACE_HANDLE_NO_TASK ((void*)2UL)
24 
25 #define TRC_TASK_STATE_INDEX_PRIORITY		0u
26 
27 TraceTaskData_t* pxTraceTaskData TRC_CFG_RECORDER_DATA_ATTRIBUTE;
28 
xTraceTaskInitialize(TraceTaskData_t * pxBuffer)29 traceResult xTraceTaskInitialize(TraceTaskData_t *pxBuffer)
30 {
31 	int32_t i;
32 
33 	/* This should never fail */
34 	TRC_ASSERT(pxBuffer != (void*)0);
35 
36 	pxTraceTaskData= pxBuffer;
37 
38 	for (i = 0; i < (TRC_CFG_CORE_COUNT); i++)
39 	{
40 		pxTraceTaskData->coreTasks[i] = TRACE_HANDLE_NO_TASK;  /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 !MISRAC2012-Rule-11.6 Suppress conversion from pointer to integer check*/
41 	}
42 
43 	xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_TASK);
44 
45 	return TRC_SUCCESS;
46 }
47 
xTraceTaskUnregister(TraceTaskHandle_t xTaskHandle,TraceUnsignedBaseType_t uxPriority)48 traceResult xTraceTaskUnregister(TraceTaskHandle_t xTaskHandle, TraceUnsignedBaseType_t uxPriority)
49 {
50 	void* pvTask = (void*)0;
51 
52 	/* This should never fail */
53 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntryGetAddress((TraceEntryHandle_t)xTaskHandle, &pvTask) == TRC_SUCCESS);
54 
55 	xTraceStackMonitorRemove(pvTask);
56 
57 	return xTraceObjectUnregister((TraceObjectHandle_t)xTaskHandle, PSF_EVENT_TASK_DELETE, uxPriority);
58 }
59 
xTraceTaskSetPriority(TraceTaskHandle_t xTaskHandle,TraceUnsignedBaseType_t uxPriority)60 traceResult xTraceTaskSetPriority(TraceTaskHandle_t xTaskHandle, TraceUnsignedBaseType_t uxPriority)
61 {
62 	void *pvTask = (void*)0;
63 
64 	/* This should never fail */
65 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceObjectSetSpecificState((TraceObjectHandle_t)xTaskHandle, TRC_TASK_STATE_INDEX_PRIORITY, uxPriority) == TRC_SUCCESS);
66 
67 	/* This should never fail */
68 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntryGetAddress((TraceEntryHandle_t)xTaskHandle, &pvTask) == TRC_SUCCESS);
69 
70 	(void)xTraceEventCreate2(PSF_EVENT_TASK_PRIORITY, (TraceUnsignedBaseType_t)pvTask, uxPriority);  /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 !MISRAC2012-Rule-11.6 Suppress conversion from pointer to integer check*/
71 
72 	return TRC_SUCCESS;
73 }
74 
xTraceTaskSetPriorityWithoutHandle(void * pvTask,TraceUnsignedBaseType_t uxPriority)75 traceResult xTraceTaskSetPriorityWithoutHandle(void* pvTask, TraceUnsignedBaseType_t uxPriority)
76 {
77 	TraceEntryHandle_t xEntryHandle;
78 
79 	if (xTraceEntryFind(pvTask, &xEntryHandle) == TRC_FAIL)
80 	{
81 		return TRC_FAIL;
82 	}
83 
84 	/* This should never fail */
85 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceObjectSetSpecificState((TraceObjectHandle_t)xEntryHandle, TRC_TASK_STATE_INDEX_PRIORITY, uxPriority) == TRC_SUCCESS);
86 
87 	/* We need to check this */
88 	(void)xTraceEventCreate2(PSF_EVENT_TASK_PRIORITY, (TraceUnsignedBaseType_t)pvTask, uxPriority);  /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 !MISRAC2012-Rule-11.6 Suppress conversion from pointer to integer check*/
89 
90 	return TRC_SUCCESS;
91 }
92 
xTraceTaskSwitch(void * pvTask,TraceUnsignedBaseType_t uxPriority)93 traceResult xTraceTaskSwitch(void *pvTask, TraceUnsignedBaseType_t uxPriority)
94 {
95 	traceResult xResult = TRC_FAIL;
96 #if (TRC_KERNEL_PORT_KERNEL_CAN_SWITCH_TO_SAME_TASK == 1)
97 	void* pvCurrent = (void*)0;
98 #endif
99 
100 	TRACE_ALLOC_CRITICAL_SECTION();
101 
102 	(void)pvTask;
103 	(void)uxPriority;
104 
105 	if (!xTraceIsRecorderInitialized())
106 	{
107 		return xResult;
108 	}
109 
110 	if (!xTraceIsRecorderEnabled())
111 	{
112 		/* Make sure we store the current task, even while recorder isn't enabled */
113 		xTraceTaskSetCurrent(pvTask);
114 
115 		return xResult;
116 	}
117 
118 	xTraceStateSet(TRC_STATE_IN_TASKSWITCH);
119 
120 	TRACE_ENTER_CRITICAL_SECTION();
121 
122 #if (TRC_KERNEL_PORT_KERNEL_CAN_SWITCH_TO_SAME_TASK == 1)
123 	xTraceTaskGetCurrent(&pvCurrent);
124 	if (pvCurrent != pvTask)
125 #endif
126 	{
127 		xTraceTaskSetCurrent(pvTask);
128 
129 		xResult = xTraceEventCreate2(PSF_EVENT_TASK_ACTIVATE, (TraceUnsignedBaseType_t)pvTask, uxPriority);  /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 !MISRAC2012-Rule-11.6 Suppress conversion from pointer to integer check*/
130 	}
131 
132 	xTraceStateSet(TRC_STATE_IN_APPLICATION);
133 
134 	TRACE_EXIT_CRITICAL_SECTION();
135 
136 	return xResult;
137 }
138 
139 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
140 
141 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
142