-
Notifications
You must be signed in to change notification settings - Fork 33
/
trcPrint.c
336 lines (289 loc) · 11.7 KB
/
trcPrint.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Percepio Trace Recorder for Tracealyzer v4.9.2
* Copyright 2023 Percepio AB
* www.percepio.com
*
* SPDX-License-Identifier: Apache-2.0
*
* The implementation for print.
*/
#include <trcRecorder.h>
#if (TRC_USE_TRACEALYZER_RECORDER == 1) && (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) && (TRC_CFG_INCLUDE_USER_EVENTS == 1)
#include <stdarg.h>
static traceResult prvTraceVPrintF(const TraceStringHandle_t xChannel, const char* szFormat, uint32_t uiLength, uint32_t uiArgs, va_list* pxVariableList);
static TracePrintData_t *pxPrintData TRC_CFG_RECORDER_DATA_ATTRIBUTE;
traceResult xTracePrintInitialize(TracePrintData_t *pxBuffer)
{
/* This should never fail */
TRC_ASSERT(pxBuffer != (void*)0);
pxPrintData = pxBuffer;
pxPrintData->defaultChannel = 0;
pxPrintData->consoleChannel = 0;
xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_PRINT);
return TRC_SUCCESS;
}
/*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/
traceResult xTracePrint(TraceStringHandle_t xChannel, const char* szString)
{
uint32_t uiLength;
uint32_t i;
/* We need to check this */
if (xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_PRINT) == 0U)
{
return TRC_FAIL;
}
if (szString == (void*)0)
{
szString = ""; /*cstat !MISRAC2012-Rule-17.8 Suppress modified function parameter check*/
}
for (i = 0u; (szString[i] != (char)0) && (i < 128u); i++) {} /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress char base type usage checks*/ /*cstat !MISRAC2004-17.4_b We need to access every character in the string*/
uiLength = i + 1u; /* Null termination */
return prvTraceVPrintF(xChannel, szString, uiLength, 0u, (va_list*)0); /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 Suppress conversion of pointer to integer check*/ /*cstat !MISRAC2012-Rule-11.9 Suppress NULL recommendation*/
}
/*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/ /*cstat !MISRAC2004-16.1 Suppress variable parameter count check*/
traceResult xTraceConsoleChannelPrintF(const char* szFormat, ...)
{
traceResult xResult;
va_list xVariableList; /*cstat !MISRAC2012-Rule-17.1 Suppress stdarg usage check*/
/* We need to check this */
if (xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_PRINT) == 0U)
{
return TRC_FAIL;
}
if (pxPrintData->consoleChannel == 0)
{
if (xTraceStringRegister("Debug Console", &pxPrintData->consoleChannel) == TRC_FAIL)
{
return TRC_FAIL;
}
}
va_start(xVariableList, szFormat);
xResult = xTraceVPrintF(pxPrintData->consoleChannel, szFormat, &xVariableList);
va_end(xVariableList);
return xResult;
}
/*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/ /*cstat !MISRAC2004-16.1 Suppress variable parameter count check*/
traceResult xTracePrintCompactF(const char* szChannel, const char* szFormat, ...)
{
uint32_t i;
uint32_t uiArgs = 0u;
traceResult xResult;
va_list xVariableList; /*cstat !MISRAC2012-Rule-17.1 Suppress stdarg usage check*/
TraceUnsignedBaseType_t uxArg1, uxArg2, uxArg3, uxArg4;
/* We need to check this */
if (xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_PRINT) == 0U)
{
return TRC_FAIL;
}
if (szFormat == (void*)0)
{
szFormat = ""; /*cstat !MISRAC2012-Rule-17.8 Suppress modified function parameter check*/
}
/* Count the number of arguments in the format string (e.g., %d) */
for (i = 0u; (szFormat[i] != (char)0) && (i < 128u); i++) /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress char base type usage checks*/ /*cstat !MISRAC2012-Rule-14.2 Suppress malformed for loop due to i being incremented inside*/ /*cstat !MISRAC2004-17.4_b We need to access every character in the string*/
{
if (szFormat[i] == '%') /*cstat !MISRAC2004-17.4_b We need to access a specific character in the string*/
{
if (szFormat[i + 1u] == (char)0) /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/ /*cstat !MISRAC2004-17.4_b We need to access a specific character in the string*/
{
/* Found end of string, let for loop detect it */
continue; /*cstat !MISRAC2004-14.5 Suppress continue usage check*/
}
if (szFormat[i + 1u] != '%') /*cstat !MISRAC2004-17.4_b We need to access a specific character in the string*/
{
uiArgs++; /* Found an argument */
}
/* Move past format specifier or non-argument '%' */
i++; /*cstat !MISRAC2004-13.6 Suppress i increment inside for loop check*/
}
}
va_start(xVariableList, szFormat);
switch(uiArgs)
{
case 0:
xResult = xTraceEventCreate2(PSF_EVENT_USER_EVENT_FIXED, (TraceUnsignedBaseType_t)szChannel, (TraceUnsignedBaseType_t)szFormat); /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 We need the addresses*/
break;
case 1:
uxArg1 = va_arg(xVariableList, TraceUnsignedBaseType_t);
xResult = xTraceEventCreate3(PSF_EVENT_USER_EVENT_FIXED + 1UL, (TraceUnsignedBaseType_t)szChannel, (TraceUnsignedBaseType_t)szFormat, uxArg1); /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 We need the addresses*/
break;
case 2:
uxArg1 = va_arg(xVariableList, TraceUnsignedBaseType_t);
uxArg2 = va_arg(xVariableList, TraceUnsignedBaseType_t);
xResult = xTraceEventCreate4(PSF_EVENT_USER_EVENT_FIXED + 2UL, (TraceUnsignedBaseType_t)szChannel, (TraceUnsignedBaseType_t)szFormat, uxArg1, uxArg2); /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 We need the addresses*/
break;
case 3:
uxArg1 = va_arg(xVariableList, TraceUnsignedBaseType_t);
uxArg2 = va_arg(xVariableList, TraceUnsignedBaseType_t);
uxArg3 = va_arg(xVariableList, TraceUnsignedBaseType_t);
xResult = xTraceEventCreate5(PSF_EVENT_USER_EVENT_FIXED + 3UL, (TraceUnsignedBaseType_t)szChannel, (TraceUnsignedBaseType_t)szFormat, uxArg1, uxArg2, uxArg3); /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 We need the addresses*/
break;
case 4:
uxArg1 = va_arg(xVariableList, TraceUnsignedBaseType_t);
uxArg2 = va_arg(xVariableList, TraceUnsignedBaseType_t);
uxArg3 = va_arg(xVariableList, TraceUnsignedBaseType_t);
uxArg4 = va_arg(xVariableList, TraceUnsignedBaseType_t);
xResult = xTraceEventCreate6(PSF_EVENT_USER_EVENT_FIXED + 4UL, (TraceUnsignedBaseType_t)szChannel, (TraceUnsignedBaseType_t)szFormat, uxArg1, uxArg2, uxArg3, uxArg4); /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 We need the addresses*/
break;
default:
xResult = TRC_FAIL;
break;
}
va_end(xVariableList);
return xResult;
}
/*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/ /*cstat !MISRAC2004-16.1 Suppress variable parameter count check*/
traceResult xTracePrintF(TraceStringHandle_t xChannel, const char* szFormat, ...)
{
traceResult xResult;
va_list xVariableList; /*cstat !MISRAC2012-Rule-17.1 Suppress stdarg usage check*/
/* We need to check this */
if (xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_PRINT) == 0U)
{
return TRC_FAIL;
}
va_start(xVariableList, szFormat);
xResult = xTraceVPrintF(xChannel, szFormat, &xVariableList);
va_end(xVariableList);
return xResult;
}
/*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/ /*cstat !MISRAC2012-Rule-17.1 Suppress stdarg usage check*/
traceResult xTraceVPrintF(TraceStringHandle_t xChannel, const char* szFormat, va_list* pxVariableList)
{
uint32_t i;
uint32_t uiArgs = 0u;
uint32_t uiLength;
/* We need to check this */
if (xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_PRINT) == 0U)
{
return TRC_FAIL;
}
if (szFormat == (void*)0)
{
szFormat = ""; /*cstat !MISRAC2012-Rule-17.8 Suppress modified function parameter check*/
}
/* Count the number of arguments in the format string (e.g., %d) */
for (i = 0u; (szFormat[i] != (char)0) && (i < 128u); i++) /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress char base type usage checks*/ /*cstat !MISRAC2012-Rule-14.2 Suppress malformed for loop due to i being incremented inside*/ /*cstat !MISRAC2004-17.4_b We need to access every character in the string*/
{
if (szFormat[i] == '%') /*cstat !MISRAC2004-17.4_b We need to access a specific character in the string*/
{
if (szFormat[i + 1u] == (char)0) /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/ /*cstat !MISRAC2004-17.4_b We need to access a specific character in the string*/
{
/* Found end of string, let for loop detect it */
continue; /*cstat !MISRAC2004-14.5 Suppress continue usage check*/
}
if (szFormat[i + 1u] != '%') /*cstat !MISRAC2004-17.4_b We need to access a specific character in the string*/
{
uiArgs++; /* Found an argument */
}
/* Move past format specifier or non-argument '%' */
i++; /*cstat !MISRAC2004-13.6 Suppress i increment inside for loop check*/
}
}
uiLength = i + 1u; /* Null termination */
return prvTraceVPrintF(xChannel, szFormat, uiLength, uiArgs, pxVariableList);
}
/*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/ /*cstat !MISRAC2012-Rule-17.1 Suppress stdarg usage check*/
static traceResult prvTraceVPrintF(TraceStringHandle_t xChannel, const char* szFormat, uint32_t uiLength, uint32_t uiArgs, va_list* pxVariableList)
{
const uint32_t uiEventCode = PSF_EVENT_USER_EVENT + 1u + uiArgs; /* Add channel (1) */
traceResult xResult;
TraceUnsignedBaseType_t uxParam1;
TraceUnsignedBaseType_t uxParam2;
TraceUnsignedBaseType_t uxParam3;
TraceUnsignedBaseType_t uxParam4;
TraceUnsignedBaseType_t uxParam5;
if (xChannel == 0)
{
if (pxPrintData->defaultChannel == 0)
{
/* Channel is not present */
if (xTraceStringRegister("Default", &pxPrintData->defaultChannel) == TRC_FAIL)
{
return TRC_FAIL;
}
}
xChannel = pxPrintData->defaultChannel; /*cstat !MISRAC2012-Rule-17.8 Suppress modified function parameter check*/
}
switch (uiArgs)
{
case 0:
xResult = xTraceEventCreateData1(uiEventCode, (TraceUnsignedBaseType_t)xChannel, (TraceUnsignedBaseType_t*)szFormat, uiLength);
break;
case 1:
uxParam1 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
xResult = xTraceEventCreateData2(
uiEventCode,
(TraceUnsignedBaseType_t)xChannel,
uxParam1,
(TraceUnsignedBaseType_t*)szFormat,
uiLength
);
break;
case 2:
uxParam1 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
uxParam2 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
xResult = xTraceEventCreateData3(
uiEventCode,
(TraceUnsignedBaseType_t)xChannel,
uxParam1,
uxParam2,
(TraceUnsignedBaseType_t*)szFormat,
uiLength
);
break;
case 3:
uxParam1 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
uxParam2 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
uxParam3 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
xResult = xTraceEventCreateData4(
uiEventCode,
(TraceUnsignedBaseType_t)xChannel,
uxParam1,
uxParam2,
uxParam3,
(TraceUnsignedBaseType_t*)szFormat,
uiLength
);
break;
case 4:
uxParam1 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
uxParam2 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
uxParam3 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
uxParam4 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
xResult = xTraceEventCreateData5(
uiEventCode,
(TraceUnsignedBaseType_t)xChannel,
uxParam1,
uxParam2,
uxParam3,
uxParam4,
(TraceUnsignedBaseType_t*)szFormat,
uiLength
);
break;
case 5:
uxParam1 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
uxParam2 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
uxParam3 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
uxParam4 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
uxParam5 = va_arg(*pxVariableList, TraceUnsignedBaseType_t);
xResult = xTraceEventCreateData6(
uiEventCode,
(TraceUnsignedBaseType_t)xChannel,
uxParam1,
uxParam2,
uxParam3,
uxParam4,
uxParam5,
(TraceUnsignedBaseType_t*)szFormat,
uiLength
);
break;
default:
xResult = TRC_FAIL;
break;
}
return xResult;
}
#endif