ChibiOS/RT  6.1.4
chtrace.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
3  2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
4 
5  This file is part of ChibiOS.
6 
7  ChibiOS is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation version 3 of the License.
10 
11  ChibiOS is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /**
21  * @file rt/include/chtrace.h
22  * @brief Tracer macros and structures.
23  *
24  * @addtogroup trace
25  * @{
26  */
27 
28 #ifndef CHTRACE_H
29 #define CHTRACE_H
30 
31 /*===========================================================================*/
32 /* Module constants. */
33 /*===========================================================================*/
34 
35 /**
36  * @name Trace record types
37  * @{
38  */
39 #define CH_TRACE_TYPE_UNUSED 0U
40 #define CH_TRACE_TYPE_SWITCH 1U
41 #define CH_TRACE_TYPE_ISR_ENTER 2U
42 #define CH_TRACE_TYPE_ISR_LEAVE 3U
43 #define CH_TRACE_TYPE_HALT 4U
44 #define CH_TRACE_TYPE_USER 5U
45 /** @} */
46 
47 /**
48  * @name Events to trace
49  * @{
50  */
51 #define CH_DBG_TRACE_MASK_DISABLED 255U
52 #define CH_DBG_TRACE_MASK_NONE 0U
53 #define CH_DBG_TRACE_MASK_SWITCH 1U
54 #define CH_DBG_TRACE_MASK_ISR 2U
55 #define CH_DBG_TRACE_MASK_HALT 4U
56 #define CH_DBG_TRACE_MASK_USER 8U
57 #define CH_DBG_TRACE_MASK_SLOW (CH_DBG_TRACE_MASK_SWITCH | \
58  CH_DBG_TRACE_MASK_HALT | \
59  CH_DBG_TRACE_MASK_USER)
60 #define CH_DBG_TRACE_MASK_ALL (CH_DBG_TRACE_MASK_SWITCH | \
61  CH_DBG_TRACE_MASK_ISR | \
62  CH_DBG_TRACE_MASK_HALT | \
63  CH_DBG_TRACE_MASK_USER)
64 /** @} */
65 
66 /*===========================================================================*/
67 /* Module pre-compile time settings. */
68 /*===========================================================================*/
69 
70 /**
71  * @name Debug related settings
72  * @{
73  */
74 /**
75  * @brief Trace buffer entries.
76  */
77 #if !defined(CH_DBG_TRACE_MASK) || defined(__DOXYGEN__)
78 #define CH_DBG_TRACE_MASK CH_DBG_TRACE_MASK_DISABLED
79 #endif
80 
81 /**
82  * @brief Trace buffer entries.
83  * @note The trace buffer is only allocated if @p CH_DBG_TRACE_MASK is
84  * different from @p CH_DBG_TRACE_MASK_DISABLED.
85  */
86 #if !defined(CH_DBG_TRACE_BUFFER_SIZE) || defined(__DOXYGEN__)
87 #define CH_DBG_TRACE_BUFFER_SIZE 128
88 #endif
89 /** @} */
90 
91 /*===========================================================================*/
92 /* Derived constants and error checks. */
93 /*===========================================================================*/
94 
95 /*===========================================================================*/
96 /* Module data structures and types. */
97 /*===========================================================================*/
98 
99 #if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
100 /*lint -save -e46 [6.1] An uint32_t is required.*/
101 /**
102  * @brief Trace buffer record.
103  */
104 typedef struct {
105  /**
106  * @brief Record type.
107  */
108  uint32_t type:3;
109  /**
110  * @brief Switched out thread state.
111  */
112  uint32_t state:5;
113  /**
114  * @brief Accurate time stamp.
115  * @note This field only available if the post supports
116  * @p PORT_SUPPORTS_RT else it is set to zero.
117  */
118  uint32_t rtstamp:24;
119  /**
120  * @brief System time stamp of the switch event.
121  */
123  union {
124  /**
125  * @brief Structure representing a context switch.
126  */
127  struct {
128  /**
129  * @brief Switched in thread.
130  */
132  /**
133  * @brief Object where going to sleep.
134  */
135  void *wtobjp;
136  } sw;
137  /**
138  * @brief Structure representing an ISR enter.
139  */
140  struct {
141  /**
142  * @brief ISR function name taken using @p __func__.
143  */
144  const char *name;
145  } isr;
146  /**
147  * @brief Structure representing an halt.
148  */
149  struct {
150  /**
151  * @brief Halt error string.
152  */
153  const char *reason;
154  } halt;
155  /**
156  * @brief User trace structure.
157  */
158  struct {
159  /**
160  * @brief Trace user parameter 1.
161  */
162  void *up1;
163  /**
164  * @brief Trace user parameter 2.
165  */
166  void *up2;
167  } user;
168  } u;
170 /*lint -restore*/
171 
172 /**
173  * @brief Trace buffer header.
174  */
175 typedef struct {
176  /**
177  * @brief Suspended trace sources mask.
178  */
179  uint16_t suspended;
180  /**
181  * @brief Trace buffer size (entries).
182  */
183  uint16_t size;
184  /**
185  * @brief Pointer to the buffer front.
186  */
188  /**
189  * @brief Ring buffer.
190  */
193 #endif /* CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED */
194 
195 /*===========================================================================*/
196 /* Module macros. */
197 /*===========================================================================*/
198 
199 /* When a trace feature is disabled the associated functions are replaced by
200  an empty macro. Note that the macros can be externally redefined in
201  order to interface 3rd parties tracing tools.*/
202 #if CH_DBG_TRACE_MASK == CH_DBG_TRACE_MASK_DISABLED
203 #if !defined(_trace_init)
204 #define _trace_init()
205 #endif
206 #if !defined(_trace_switch)
207 #define _trace_switch(ntp, otp)
208 #endif
209 #if !defined(_trace_isr_enter)
210 #define _trace_isr_enter(isr)
211 #endif
212 #if !defined(_trace_isr_leave)
213 #define _trace_isr_leave(isr)
214 #endif
215 #if !defined(_trace_halt)
216 #define _trace_halt(reason)
217 #endif
218 #if !defined(chDbgWriteTraceI)
219 #define chDbgWriteTraceI(up1, up2)
220 #endif
221 #if !defined(chDbgWriteTrace)
222 #define chDbgWriteTrace(up1, up2)
223 #endif
224 #endif /* CH_DBG_TRACE_MASK == CH_DBG_TRACE_MASK_DISABLED */
225 
226 /*===========================================================================*/
227 /* External declarations. */
228 /*===========================================================================*/
229 
230 #ifdef __cplusplus
231 extern "C" {
232 #endif
233 #if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
234  void _trace_init(void);
235  void _trace_switch(thread_t *ntp, thread_t *otp);
236  void _trace_isr_enter(const char *isr);
237  void _trace_isr_leave(const char *isr);
238  void _trace_halt(const char *reason);
239  void chDbgWriteTraceI(void *up1, void *up2);
240  void chDbgWriteTrace(void *up1, void *up2);
241  void chDbgSuspendTraceI(uint16_t mask);
242  void chDbgSuspendTrace(uint16_t mask);
243  void chDbgResumeTraceI(uint16_t mask);
244  void chDbgResumeTrace(uint16_t mask);
245 #endif /* CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED */
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 /*===========================================================================*/
251 /* Module inline functions. */
252 /*===========================================================================*/
253 
254 #endif /* CHTRACE_H */
255 
256 /** @} */
ch_trace_buffer_t::ptr
ch_trace_event_t * ptr
Pointer to the buffer front.
Definition: chtrace.h:187
ch_trace_buffer_t::size
uint16_t size
Trace buffer size (entries).
Definition: chtrace.h:183
CH_DBG_TRACE_BUFFER_SIZE
#define CH_DBG_TRACE_BUFFER_SIZE
Trace buffer entries.
Definition: chtrace.h:87
systime_t
uint64_t systime_t
Type of system time.
Definition: chtime.h:107
_trace_isr_enter
void _trace_isr_enter(const char *isr)
Inserts in the circular debug trace buffer an ISR-enter record.
Definition: chtrace.c:124
ch_trace_event_t::wtobjp
void * wtobjp
Object where going to sleep.
Definition: chtrace.h:135
ch_trace_event_t::up2
void * up2
Trace user parameter 2.
Definition: chtrace.h:166
ch_trace_event_t::ntp
thread_t * ntp
Switched in thread.
Definition: chtrace.h:131
chDbgWriteTraceI
void chDbgWriteTraceI(void *up1, void *up2)
Adds an user trace record to the trace buffer.
Definition: chtrace.c:180
_trace_init
void _trace_init(void)
Trace circular buffer subsystem initialization.
Definition: chtrace.c:85
ch_trace_event_t::time
systime_t time
System time stamp of the switch event.
Definition: chtrace.h:122
chDbgSuspendTraceI
void chDbgSuspendTraceI(uint16_t mask)
Suspends one or more trace events.
Definition: chtrace.c:215
ch_trace_event_t::type
uint32_t type
Record type.
Definition: chtrace.h:108
ch_trace_event_t::name
const char * name
ISR function name taken using func.
Definition: chtrace.h:144
ch_thread
Structure representing a thread.
Definition: chschd.h:134
chDbgSuspendTrace
void chDbgSuspendTrace(uint16_t mask)
Suspends one or more trace events.
Definition: chtrace.c:229
ch_trace_event_t::rtstamp
uint32_t rtstamp
Accurate time stamp.
Definition: chtrace.h:118
ch_trace_event_t::reason
const char * reason
Halt error string.
Definition: chtrace.h:153
_trace_halt
void _trace_halt(const char *reason)
Inserts in the circular debug trace buffer an halt record.
Definition: chtrace.c:162
chDbgResumeTraceI
void chDbgResumeTraceI(uint16_t mask)
Resumes one or more trace events.
Definition: chtrace.c:243
ch_trace_event_t::state
uint32_t state
Switched out thread state.
Definition: chtrace.h:112
ch_trace_buffer_t::suspended
uint16_t suspended
Suspended trace sources mask.
Definition: chtrace.h:179
ch_trace_event_t::up1
void * up1
Trace user parameter 1.
Definition: chtrace.h:162
ch_trace_event_t
Trace buffer record.
Definition: chtrace.h:104
chDbgResumeTrace
void chDbgResumeTrace(uint16_t mask)
Resumes one or more trace events.
Definition: chtrace.c:257
_trace_switch
void _trace_switch(thread_t *ntp, thread_t *otp)
Inserts in the circular debug trace buffer a context switch record.
Definition: chtrace.c:104
chDbgWriteTrace
void chDbgWriteTrace(void *up1, void *up2)
Adds an user trace record to the trace buffer.
Definition: chtrace.c:201
ch_trace_buffer_t
Trace buffer header.
Definition: chtrace.h:175
_trace_isr_leave
void _trace_isr_leave(const char *isr)
Inserts in the circular debug trace buffer an ISR-leave record.
Definition: chtrace.c:143