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 strings.
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 #define TRC_RUNNABLE_STATE_INDEX_OWNER_TASK 0UL
18 
19 /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/
xTraceRunnableRegister(const char * szName,TraceRunnableRegisterMethod_t uxRegisterMethod,TraceRunnableHandle_t * pxRunnableHandle)20 traceResult xTraceRunnableRegister(const char* szName, TraceRunnableRegisterMethod_t uxRegisterMethod, TraceRunnableHandle_t *pxRunnableHandle)
21 {
22 	TraceEntryHandle_t xEntryHandle;
23 	TraceEventHandle_t xEventHandle = 0;
24 	int32_t i;
25 	uint32_t uiLength, uiValue = 0u;
26 
27 	/* This should never fail */
28 	TRC_ASSERT(szName != (void*)0);
29 
30 	/* This should never fail */
31 	TRC_ASSERT(pxRunnableHandle != (void*)0);
32 
33 	for (i = 0; (szName[i] != (char)0) && (i < (int32_t)(TRC_ENTRY_TABLE_SLOT_SYMBOL_SIZE)); i++) {} /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/ /*cstat !MISRAC2004-17.4_b We need to access every character in the string*/
34 
35 	uiLength = (uint32_t)i;
36 
37 	if (uxRegisterMethod == TRC_RUNNABLE_REGISTER_METHOD_USE_ENTRY_TABLE)
38 	{
39 		/* Check if we have already created an entry previously */
40 		if (*pxRunnableHandle == (void*)0)
41 		{
42 			/* We need to check this */
43 			if (xTraceEntryCreate(&xEntryHandle) == TRC_FAIL)
44 			{
45 				return TRC_FAIL;
46 			}
47 
48 			TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetOptions(xEntryHandle, TRC_ENTRY_OPTION_RUNNABLE) == TRC_SUCCESS);
49 
50 			TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetState(xEntryHandle, TRC_RUNNABLE_STATE_INDEX_OWNER_TASK, (TraceUnsignedBaseType_t)xTraceTaskGetCurrentReturn()) == TRC_SUCCESS); /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 !MISRAC2012-Rule-11.6 We need the address of the task*/
51 
52 			/* The address to the available symbol table slot is the address we use */
53 			/* This should never fail */
54 			TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetSymbol(xEntryHandle, szName, uiLength) == TRC_SUCCESS);
55 
56 			*pxRunnableHandle = (TraceRunnableHandle_t)xEntryHandle;
57 		}
58 	}
59 	else if (uxRegisterMethod == TRC_RUNNABLE_REGISTER_METHOD_USE_STRING_ADDRESS)
60 	{
61 		*pxRunnableHandle = (TraceRunnableHandle_t)szName; /*cstat !MISRAC2004-11.5 !MISRAC2012-Rule-11.8 We need the address of the string*/
62 	}
63 	else if (uxRegisterMethod == TRC_RUNNABLE_REGISTER_METHOD_USE_HANDLE_ADDRESS)
64 	{
65 		/* The handle address should be a unique value that we can use as handle */
66 		*pxRunnableHandle = (TraceRunnableHandle_t)pxRunnableHandle;
67 	}
68 	else
69 	{
70 		return TRC_FAIL;
71 	}
72 
73 	/* We need to check this */
74 	if (xTraceEventBegin(PSF_EVENT_RUNNABLE_REGISTER, sizeof(void*) + uiLength, &xEventHandle) == TRC_SUCCESS)
75 	{
76 		(void)xTraceEventAddPointer(xEventHandle, (void*)*pxRunnableHandle);
77 		(void)xTraceEventAddString(xEventHandle, szName, uiLength);
78 
79 		/* Check if we can truncate */
80 		(void)xTraceEventPayloadRemaining(xEventHandle, &uiValue);
81 		if (uiValue > 0u)
82 		{
83 			(void)xTraceEventAdd8(xEventHandle, 0u);
84 		}
85 
86 		(void)xTraceEventEnd(xEventHandle); /*cstat !MISRAC2012-Rule-17.7 Suppress ignored return value check (inside macro)*/
87 	}
88 
89 	return TRC_SUCCESS;
90 }
91 
92 #endif
93 
94 #endif
95