-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathtraceDecoder.c
More file actions
167 lines (141 loc) · 5.67 KB
/
traceDecoder.c
File metadata and controls
167 lines (141 loc) · 5.67 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
165
166
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* TRACE Decoder Module
* ====================
*
* Implementation of ITM/DWT decode according to the specification in Appendix D4
* of the ARMv7-M Architecture Refrence Manual document available
* from https://static.docs.arm.com/ddi0403/e/DDI0403E_B_armv7m_arm.pdf
*/
#include <string.h>
#include <assert.h>
#include "msgDecoder.h"
#include "traceDecoder.h"
#include "generics.h"
/* Individual trace decoders defined in their own file */
extern struct TRACEDecoderEngine *ETM35DecoderPumpCreate( void );
extern struct TRACEDecoderEngine *MTBDecoderPumpCreate( void );
extern struct TRACEDecoderEngine *ETM4DecoderPumpCreate( void );
static struct TRACEDecoderEngine *( *_engine[TRACE_PROT_NUM] )( void ) = { ETM35DecoderPumpCreate, MTBDecoderPumpCreate, ETM4DecoderPumpCreate };
static const char *TRACEprotocolString[TRACE_PROT_NUM] = { TRACEprotocolStrings };
// ====================================================================================================
// ====================================================================================================
// ====================================================================================================
// Externally available routines
// ====================================================================================================
// ====================================================================================================
// ====================================================================================================
const char *TRACEDecodeGetProtocolName( enum TRACEprotocol protocol )
{
assert( protocol < TRACE_PROT_LIST_END );
return TRACEprotocolString[ protocol ];
}
// ====================================================================================================
void TRACEDecoderZeroStats( struct TRACEDecoder *i )
{
assert( i );
memset( &i->stats, 0, sizeof( struct TRACEDecoderStats ) );
}
// ====================================================================================================
bool TRACEDecoderIsSynced( struct TRACEDecoder *i )
{
assert( i );
assert( i->engine );
return i->engine->synced( i->engine );
}
// ====================================================================================================
struct TRACEDecoderStats *TRACEDecoderGetStats( struct TRACEDecoder *i )
{
assert( i );
return &i->stats;
}
// ====================================================================================================
struct TRACECPUState *TRACECPUState( struct TRACEDecoder *i )
{
return &i->cpu;
}
// ====================================================================================================
const char *TRACEExceptionName( int exceptionNumber )
{
return ( ( char *[] )
{"???", "PE Reset", "NMI", "HardFault", "MemManage", "BusFault", "UsageFault", "SecureFault", "???", "???", "???", "SVC", "Debug Monitor", "???", "PendSV", "SysTick", "IRQ"
} )[( exceptionNumber < 16 ) ? exceptionNumber : 16];
}
// ====================================================================================================
bool TRACEStateChanged( struct TRACEDecoder *i, enum TRACEchanges c )
{
bool r = ( i->cpu.changeRecord & ( 1 << c ) ) != 0;
i->cpu.changeRecord &= ~( 1 << c );
return r;
}
// ====================================================================================================
void TRACEDecoderForceSync( struct TRACEDecoder *i, bool isSynced )
/* Force the decoder into a specific sync state */
{
assert( i );
assert( i->engine );
if ( isSynced )
{
i->stats.syncCount++;
}
else
{
if ( TRACEDecoderIsSynced( i ) )
{
i->stats.lostSyncCount++;
}
}
i->engine->forceSync( i->engine, isSynced );
}
// ====================================================================================================
void TRACEDecoderPump( struct TRACEDecoder *i, uint8_t *buf, int len, traceDecodeCB cb, void *d )
{
assert( i );
assert( buf );
assert( cb );
/* len can arrive as 0 for the case of an unwrapped buffer */
if ( i->engine->action )
{
while ( len-- )
{
if ( i->engine->action( i->engine, &i->cpu, *( buf++ ) ) )
{
/* Something worthy of being reported happened */
cb( d );
}
}
}
else if ( i->engine->actionPair )
{
while ( len > 7 )
{
/* MTB processes two words at a time...a from and to address */
/* (yes, that could be +1 on a uint32_t increment, but I prefer being explicit) */
if ( i->engine->actionPair( i->engine, &i->cpu, *( uint32_t * )buf, *( uint32_t * )( buf + 4 ) ) )
{
/* Something worthy of being reported happened */
cb( d );
}
buf += 8;
len -= 8;
}
}
}
// ====================================================================================================
void TRACEDecoderInit( struct TRACEDecoder *i, enum TRACEprotocol protocol, bool usingAltAddrEncodeSet, genericsReportCB report )
/* Reset a TRACEDecoder instance */
{
assert( protocol < TRACE_PROT_NUM );
memset( i, 0, sizeof( struct TRACEDecoder ) );
TRACEDecoderZeroStats( i );
i->cpu.addr = ADDRESS_UNKNOWN;
i->cpu.cycleCount = COUNT_UNKNOWN;
i->cpu.report = report;
i->protocol = protocol;
i->engine = _engine[ protocol ]();
if ( i->engine->altAddrEncode )
{
i->engine->altAddrEncode( i->engine, usingAltAddrEncodeSet );
}
}
// ====================================================================================================