summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Source/FreeRTOS-Plus-Trace/trcAssert.c
blob: 8d40867d6d60a266f0c4c844964b7427438442e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Percepio Trace Recorder for Tracealyzer v4.6.0
* Copyright 2021 Percepio AB
* www.percepio.com
*
* SPDX-License-Identifier: Apache-2.0
*
* The implementation for errors.
*/

#include <trcRecorder.h>

#if (TRC_USE_TRACEALYZER_RECORDER == 1)

#if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)

#if ((TRC_CFG_USE_TRACE_ASSERT) == 1)

#if (defined(TRC_CFG_TEST_MODE) && (TRC_CFG_TEST_MODE) == 1)

extern inline TraceBaseType_t prvTraceAssertCheckCondition(TraceBaseType_t condition);

#endif

#define TRC_ASSERT_STATE_INDEX_LINE_NUMBER 0

typedef struct TraceAssertInfo
{
	TraceEntryHandle_t xEntry;
} TraceAssertInfo_t;

static TraceAssertInfo_t* pxAssertInfo;

traceResult xTraceAssertInitialize(TraceAssertBuffer_t *pxBuffer)
{
	TRC_ASSERT_EQUAL_SIZE(TraceAssertBuffer_t, TraceAssertInfo_t);

	TRC_ASSERT(pxBuffer != 0);

	pxAssertInfo = (TraceAssertInfo_t*)pxBuffer;
	pxAssertInfo->xEntry = 0;

	xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_ASSERT);

	return TRC_SUCCESS;
}

void prvTraceAssertCreate(const char* szFilePath, TraceUnsignedBaseType_t uxLineNumber)
{
	TraceBaseType_t i, xLength;
	TraceUnsignedBaseType_t uxEntryLineNumber = 0xFFFFFFFF;
	static TraceUnsignedBaseType_t uxRecursionGuard = 0;

	if (uxRecursionGuard == 0)
	{
		/* Recursion can only get here once */
		uxRecursionGuard = 1;

		if (!xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_ASSERT))
		{
			return;
		}

		if (pxAssertInfo->xEntry == 0)
		{
			if (xTraceEntryCreate(&pxAssertInfo->xEntry) == TRC_FAIL)
			{
				return;
			}
		}

		xTraceEntryGetState(pxAssertInfo->xEntry, TRC_ASSERT_STATE_INDEX_LINE_NUMBER, &uxEntryLineNumber);

		/* We only save the first ASSERT information */
		if (uxEntryLineNumber == 0)
		{
			/* Find length */
			for (i = 0; (szFilePath[i] != 0) && (i < 128); i++) {}

			xLength = i;

			/* Find last slash or backslash */
			for (i = xLength - 1; (i >= 0) && ((szFilePath[i] != '\\') && (szFilePath[i] != '/')); i--) {}

			/* We treat the entry as an object and set it's name and state */
			xTraceObjectSetName((TraceObjectHandle_t)pxAssertInfo->xEntry, &szFilePath[i + 1]);
			xTraceObjectSetState((TraceObjectHandle_t)pxAssertInfo->xEntry, uxLineNumber);

			xTraceError(TRC_ERROR_ASSERT);
		}
	}

	xTraceDiagnosticsIncrease(TRC_DIAGNOSTICS_ASSERTS_TRIGGERED);
}

traceResult xTraceAssertGet(TraceStringHandle_t *pxFileNameStringHandle, TraceUnsignedBaseType_t *puxLineNumber)
{
	TRC_ASSERT(pxFileNameStringHandle != 0);
	
	TRC_ASSERT(puxLineNumber != 0);

	if (!xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_ASSERT))
	{
		return TRC_FAIL;
	}

	*puxLineNumber = 0;
	xTraceEntryGetState(pxAssertInfo->xEntry, TRC_ASSERT_STATE_INDEX_LINE_NUMBER, puxLineNumber);

	if (*puxLineNumber == 0)
	{
		return TRC_FAIL;
	}
	
	/* The string handle can be set to the entry handle */
	*pxFileNameStringHandle = (TraceStringHandle_t)pxAssertInfo->xEntry;

	return TRC_SUCCESS;
}

#endif /* ((TRC_CFG_USE_TRACE_ASSERT) == 1) */

#endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */

#endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */