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