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 * 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 
xTraceStringRegister(const char * szString,TraceStringHandle_t * pString)17 traceResult xTraceStringRegister(const char* szString, TraceStringHandle_t *pString)
18 {
19 	TraceEntryHandle_t xEntryHandle;
20 	TraceEventHandle_t xEventHandle = 0;
21 	uint32_t i = 0, uiLength = 0, uiValue = 0;
22 
23 	/* This should never fail */
24 	TRC_ASSERT(szString != 0);
25 
26 	/* This should never fail */
27 	TRC_ASSERT(pString != 0);
28 
29 	/* We need to check this */
30 	if (xTraceEntryCreate(&xEntryHandle) == TRC_FAIL)
31 	{
32 		return TRC_FAIL;
33 	}
34 
35 	/* The address to the available symbol table slot is the address we use */
36 	/* This should never fail */
37 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetSymbol(xEntryHandle, szString) == TRC_SUCCESS);
38 
39 	*pString = (TraceStringHandle_t)xEntryHandle;
40 
41 	for (i = 0; (szString[i] != 0) && (i < (TRC_ENTRY_TABLE_SLOT_SYMBOL_SIZE)); i++) {}
42 
43 	uiLength = i;
44 
45 	/* We need to check this */
46 	if (xTraceEventBegin(PSF_EVENT_OBJ_NAME, sizeof(void*) + uiLength, &xEventHandle) == TRC_SUCCESS)
47 	{
48 		xTraceEventAddPointer(xEventHandle, (void*)xEntryHandle);
49 		xTraceEventAddData(xEventHandle, (void*)szString, uiLength);
50 
51 		/* Check if we can truncate */
52 		xTraceEventPayloadRemaining(xEventHandle, &uiValue);
53 		if (uiValue > 0)
54 		{
55 			xTraceEventAdd8(xEventHandle, 0);
56 		}
57 
58 		xTraceEventEnd(xEventHandle);
59 	}
60 
61 	return TRC_SUCCESS;
62 }
63 
xTraceRegisterString(const char * name)64 TraceStringHandle_t xTraceRegisterString(const char *name)
65 {
66 	TraceStringHandle_t trcStr = 0;
67 	xTraceStringRegister(name, &trcStr);
68 
69 	return trcStr;
70 }
71 
72 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
73 
74 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
75