-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
119 lines (105 loc) · 3.98 KB
/
example.c
File metadata and controls
119 lines (105 loc) · 3.98 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
/*
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.
*/
// Please refer to trace.h for the procedure reference
/********************* System Headers ************************/
#include <stdio.h>
// Step 4: Include trace.h to the source to be instrumented,
#include "trace.h"
/*********************** Constants ***************************/
#define TEST_TRACE_DEPTH 20
#define MANUAL_TRACE_DEPTH 5
/********************* Local Functions ***********************/
// Step 1a: This mimics a tick which is used for optional time stamping
uint32_t fake_tick(void)
{
static uint32_t ulTick = 0;
return ulTick += 10;
}
/* Step 5: Use the following macros in your code to instrument execution:
* TRACE_FILE(<Trace Buffer Name>)
* TRACE_FUNC(<Trace Buffer Name>)
* TRACE(<Trace Buffer Name>, <message>, <value>)
*/
void test_func1(void)
{
// Use TRACE_FUNC() to indicate the function and line number
TRACE_FUNC(Test);
static int cnt = 0;
// Simulate an error condition
if ((++cnt % 3) == 0)
{
// Use TRACE_FILE() to indicate the file name and line number
TRACE_FILE(Test);
// Use TRACE() to trace the first few errors
TRACE(Error, " Event @ func1", cnt);
}
}
void test_func2(void)
{
TRACE_FUNC(Test);
static int cnt = 0;
if ((++cnt % 5) == 0)
{
TRACE(Error, " Event @ func2", cnt);
}
}
void main(void)
{
int idx;
// Example 1: Manually setting up trace buffer
printf("\nExample 1: Manually setting up the trace buffer\n");
trace_line_t gaxTraceManual[MANUAL_TRACE_DEPTH];
trace_t gxTraceManual;
trace_t *gpxTraceManual = &gxTraceManual;
trace_init(&gxTraceManual, "Manual Example", gaxTraceManual, MANUAL_TRACE_DEPTH, false);
trace(&gxTraceManual, "Start Manual Tracing", 0);
TRACE(Manual, "Using TRACE_FILE Macro", 2);
TRACE_FILE(Manual);
TRACE(Manual, "Using TRACE_FUNC Macro", 2);
TRACE_FUNC(Manual);
trace_dump(&gxTraceManual, true);
// Example 2: Automatically setting up trace buffer using TRACE_USE_CONFIG_FILE
printf("\nExample 2: Automatically setting up trace buffer using TRACE_USE_CONFIG_FILE\n");
TRACE(Test, "Start", 0);
for (idx = 0; idx < TEST_TRACE_DEPTH + 6; idx++)
{
test_func1();
test_func2();
}
TRACE(Test, "Test End", idx);
/* Step 6: Dump the trace using TRACE_DUMP or inspect with JTAG
* a) Call TRACE_DUMP(<Trace Buffer Name>, <reset>) to dump the contents of the trace buffer
* b) With a JTAG debugger (e.g. JLinkDebugger), inspecting the global variables and
* expanding them in the debugger will show human redable messages
*/
TRACE_DUMP(Error, true);
TRACE_DUMP(Test, true);
// Try again with the buffers reset
TRACE(Test, "Test Start Again", 0);
for (idx = 0; idx < 4; idx++)
{
test_func1();
test_func2();
}
TRACE(Test, "Test End Again", idx);
// Dump all traces
TRACE_DUMP_ALL(true);
}