-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.c
More file actions
164 lines (147 loc) · 5.42 KB
/
trace.c
File metadata and controls
164 lines (147 loc) · 5.42 KB
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
/*
The MIT License (MIT)
Copyright (c) 2015-2017 Howard Chan
https://github.com/howard-chan/TRACE
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/********************* System Headers ************************/
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
/********************* Local Headers *************************/
#include "trace.h"
/*********************** Macros ******************************/
// NOTE: This macro creates the trace buffer objects in memory
#define TRACE_INSTANTIATE(trcName, hdrName, depth, isWrap) \
trace_line_t gaxTrace##trcName##Line[depth]; \
trace_t gxTrace##trcName = { \
.usDepth = depth, \
.bIsWrap = isWrap, \
.usIdx = 0, \
.ulCount = 0, \
.axLine = gaxTrace##trcName##Line, \
.name = hdrName, \
}; \
trace_t *gpxTrace##trcName = &gxTrace##trcName;
// NOTE: This macro is used to create an array of trace buffers
#define TRACE_ELEMENT(trcName, hdrName, depth, isWrap) &gxTrace##trcName,
/********************* Configuration *************************/
/********************* Global Variables **********************/
#ifdef TRACE_USE_CONFIG_FILE
// Instantiate the trace buffers
#undef TRACE_CONFIG
#define TRACE_CONFIG TRACE_INSTANTIATE
#include TRACE_USE_CONFIG_FILE
// Define the trace buffer array
#undef TRACE_CONFIG
#define TRACE_CONFIG TRACE_ELEMENT
trace_t * gapxTraceAll[] =
{
#include TRACE_USE_CONFIG_FILE
};
#endif // TRACE_USE_CONFIG
/********************* Exported Functions ********************/
void trace_init(trace_t *This, char *name, trace_line_t *pxLine, uint16_t usDepth, bool bIsWrap)
{
This->usDepth = usDepth;
This->bIsWrap = bIsWrap;
This->usIdx = 0;
This->ulCount = 0;
This->axLine = pxLine;
This->name = name;
}
void trace(trace_t *This, char *pcMessage, uint32_t ulValue)
{
// Step 1: Lock the resource
TRACE_LOCK();
// Step 2: Always count the number of trace calls
This->ulCount++;
// Only store if there is room in the buffer (i.e. no-wrap case)
if (This->usIdx < This->usDepth)
{
// Step 3.1: get the current index and increment
uint16_t usIdx = This->usIdx;
This->usIdx++;
// If Trace Wrap is enabled, then reset index
if (This->bIsWrap && This->usIdx >= This->usDepth)
{
This->usIdx = 0;
}
// Step 3.2: Unlock the resource
TRACE_UNLOCK();
// Step 3.3: Store the trace line. Assume there is enough time to write at slot before wrap
#if defined(TRACE_GET_TICK)
This->axLine[usIdx].ulTimeStamp = TRACE_GET_TICK(); // Tick [in ms]
#endif // defined(TRACE_GET_TICK)
This->axLine[usIdx].pcMessage = pcMessage;
This->axLine[usIdx].ulValue = ulValue;
}
else
{
// Step 3: Unlock the resource
TRACE_UNLOCK();
}
}
#ifdef TRACE_OUTPUT
void trace_dump(trace_t *This, bool bReset)
{
uint16_t usIdx, usIdy, usOffset;
#if defined(TRACE_GET_TICK)
uint32_t ulLastStamp = 0;
#endif // defined(TRACE_GET_TICK)
TRACE_DUMP_LOCK();
usOffset = (This->bIsWrap) ? This->usIdx : 0;
TRACE_OUTPUT(TRACE_NEWLINE "====TRACE[%s] (total:%d, depth:%d)==== " TRACE_NEWLINE, This->name, This->ulCount, This->usDepth);
// Output the trace buffer
for (usIdx = 0; usIdx < This->usDepth; usIdx++)
{
usIdy = (usOffset + usIdx) % This->usDepth;
// Only print valid messages
if (This->axLine[usIdy].pcMessage && usIdy < This->ulCount)
{
uint32_t ulValue = This->axLine[usIdy].ulValue;
#if defined(TRACE_GET_TICK)
uint32_t ulStamp = This->axLine[usIdy].ulTimeStamp;
TRACE_OUTPUT(" %6d|%10d ms| %s: 0x%08x (%d)" TRACE_NEWLINE, usIdy, ulStamp - ulLastStamp, This->axLine[usIdy].pcMessage, ulValue, ulValue);
ulLastStamp = ulStamp;
#else
TRACE_OUTPUT(" %6d| %s: 0x%08x (%d)" TRACE_NEWLINE, usIdy, This->axLine[usIdy].pcMessage, ulValue, ulValue);
#endif // defined(TRACE_GET_TICK)
}
}
// Reset the trace buffer for a new capture
if (bReset)
{
This->usIdx = 0;
This->ulCount = 0;
}
TRACE_DUMP_UNLOCK();
}
#ifdef TRACE_USE_CONFIG_FILE
void trace_dump_all(bool bReset)
{
int idx;
for (idx = 0; idx < sizeof(gapxTraceAll) / sizeof(gapxTraceAll[0]); idx++)
{
trace_dump(gapxTraceAll[idx], bReset);
}
}
#endif // TRACE_USE_CONFIG_FILE
#else
void trace_dump(trace_t *This, bool bReset) { This = This; bReset = bReset; }
void trace_dump_all(void) {}
#endif // TRACE_OUTPUT