ChibiOS  21.6.0
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_READY 1U
41 #define CH_TRACE_TYPE_SWITCH 2U
42 #define CH_TRACE_TYPE_ISR_ENTER 3U
43 #define CH_TRACE_TYPE_ISR_LEAVE 4U
44 #define CH_TRACE_TYPE_HALT 5U
45 #define CH_TRACE_TYPE_USER 6U
46 /** @} */
47 
48 /**
49  * @name Events to trace
50  * @{
51  */
52 #define CH_DBG_TRACE_MASK_DISABLED 255U
53 #define CH_DBG_TRACE_MASK_NONE 0U
54 #define CH_DBG_TRACE_MASK_READY 1U
55 #define CH_DBG_TRACE_MASK_SWITCH 2U
56 #define CH_DBG_TRACE_MASK_ISR 4U
57 #define CH_DBG_TRACE_MASK_HALT 8U
58 #define CH_DBG_TRACE_MASK_USER 16U
59 #define CH_DBG_TRACE_MASK_SLOW (CH_DBG_TRACE_MASK_READY | \
60  CH_DBG_TRACE_MASK_SWITCH | \
61  CH_DBG_TRACE_MASK_HALT | \
62  CH_DBG_TRACE_MASK_USER)
63 #define CH_DBG_TRACE_MASK_ALL (CH_DBG_TRACE_MASK_READY | \
64  CH_DBG_TRACE_MASK_SWITCH | \
65  CH_DBG_TRACE_MASK_ISR | \
66  CH_DBG_TRACE_MASK_HALT | \
67  CH_DBG_TRACE_MASK_USER)
68 /** @} */
69 
70 /*===========================================================================*/
71 /* Module pre-compile time settings. */
72 /*===========================================================================*/
73 
74 /**
75  * @name Debug related settings
76  * @{
77  */
78 /**
79  * @brief Trace buffer entries.
80  */
81 #if !defined(CH_DBG_TRACE_MASK) || defined(__DOXYGEN__)
82 #define CH_DBG_TRACE_MASK CH_DBG_TRACE_MASK_DISABLED
83 #endif
84 
85 /**
86  * @brief Trace buffer entries.
87  * @note The trace buffer is only allocated if @p CH_DBG_TRACE_MASK is
88  * different from @p CH_DBG_TRACE_MASK_DISABLED.
89  */
90 #if !defined(CH_DBG_TRACE_BUFFER_SIZE) || defined(__DOXYGEN__)
91 #define CH_DBG_TRACE_BUFFER_SIZE 128
92 #endif
93 /** @} */
94 
95 /*===========================================================================*/
96 /* Derived constants and error checks. */
97 /*===========================================================================*/
98 
99 /*===========================================================================*/
100 /* Module data structures and types. */
101 /*===========================================================================*/
102 
103 #if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
104 /*lint -save -e46 [6.1] An uint32_t is required.*/
105 /**
106  * @brief Trace buffer record.
107  */
108 typedef struct {
109  /**
110  * @brief Record type.
111  */
112  uint32_t type:3;
113  /**
114  * @brief Switched out thread state.
115  */
116  uint32_t state:5;
117  /**
118  * @brief Accurate time stamp.
119  * @note This field only available if the post supports
120  * @p PORT_SUPPORTS_RT else it is set to zero.
121  */
122  uint32_t rtstamp:24;
123  /**
124  * @brief System time stamp of the switch event.
125  */
127  union {
128  /**
129  * @brief Structure representing a context switch.
130  */
131  struct {
132  /**
133  * @brief Switched in thread.
134  */
136  /**
137  * @brief Object where going to sleep.
138  */
139  void *wtobjp;
140  } sw;
141  /**
142  * @brief Structure representing a thread becoming ready.
143  */
144  struct {
145  /**
146  * @brief Thread made ready.
147  */
149  /**
150  * @brief Ready message.
151  */
153  } rdy;
154  /**
155  * @brief Structure representing an ISR enter.
156  */
157  struct {
158  /**
159  * @brief ISR function name taken using @p __func__.
160  */
161  const char *name;
162  } isr;
163  /**
164  * @brief Structure representing an halt.
165  */
166  struct {
167  /**
168  * @brief Halt error string.
169  */
170  const char *reason;
171  } halt;
172  /**
173  * @brief User trace structure.
174  */
175  struct {
176  /**
177  * @brief Trace user parameter 1.
178  */
179  void *up1;
180  /**
181  * @brief Trace user parameter 2.
182  */
183  void *up2;
184  } user;
185  } u;
186 } trace_event_t;
187 /*lint -restore*/
188 
189 /**
190  * @brief Trace buffer header.
191  */
192 typedef struct {
193  /**
194  * @brief Suspended trace sources mask.
195  */
196  uint16_t suspended;
197  /**
198  * @brief Trace buffer size (entries).
199  */
200  uint16_t size;
201  /**
202  * @brief Pointer to the buffer front.
203  */
205  /**
206  * @brief Ring buffer.
207  */
210 #endif /* CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED */
211 
212 /*===========================================================================*/
213 /* Module macros. */
214 /*===========================================================================*/
215 
216 /* When a trace feature is disabled the associated functions are replaced by
217  an empty macro. Note that the macros can be externally redefined in
218  order to interface 3rd parties tracing tools.*/
219 #if CH_DBG_TRACE_MASK == CH_DBG_TRACE_MASK_DISABLED
220 #if !defined(__trace_ready)
221 #define __trace_ready(tp, msg)
222 #endif
223 #if !defined(__trace_switch)
224 #define __trace_switch(ntp, otp)
225 #endif
226 #if !defined(__trace_isr_enter)
227 #define __trace_isr_enter(isr)
228 #endif
229 #if !defined(__trace_isr_leave)
230 #define __trace_isr_leave(isr)
231 #endif
232 #if !defined(__trace_halt)
233 #define __trace_halt(reason)
234 #endif
235 #if !defined(chDbgWriteTraceI)
236 #define chDbgWriteTraceI(up1, up2)
237 #endif
238 #if !defined(chDbgWriteTrace)
239 #define chDbgWriteTrace(up1, up2)
240 #endif
241 #endif /* CH_DBG_TRACE_MASK == CH_DBG_TRACE_MASK_DISABLED */
242 
243 /*===========================================================================*/
244 /* External declarations. */
245 /*===========================================================================*/
246 
247 #ifdef __cplusplus
248 extern "C" {
249 #endif
250 #if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
252  void __trace_ready(thread_t *tp, msg_t msg);
253  void __trace_switch(thread_t *ntp, thread_t *otp);
254  void __trace_isr_enter(const char *isr);
255  void __trace_isr_leave(const char *isr);
256  void __trace_halt(const char *reason);
257  void chTraceWriteI(void *up1, void *up2);
258  void chTraceWrite(void *up1, void *up2);
259  void chTraceSuspendI(uint16_t mask);
260  void chTraceSuspend(uint16_t mask);
261  void chTraceIResume(uint16_t mask);
262  void chTraceResume(uint16_t mask);
263 #endif /* CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED */
264 #ifdef __cplusplus
265 }
266 #endif
267 
268 /*===========================================================================*/
269 /* Module inline functions. */
270 /*===========================================================================*/
271 
272 #endif /* CHTRACE_H */
273 
274 /** @} */
chTraceWriteI
void chTraceWriteI(void *up1, void *up2)
Adds an user trace record to the trace buffer.
Definition: chtrace.c:205
CH_DBG_TRACE_BUFFER_SIZE
#define CH_DBG_TRACE_BUFFER_SIZE
Trace buffer entries.
Definition: chtrace.h:91
systime_t
uint64_t systime_t
Type of system time.
Definition: chtime.h:107
trace_event_t
Trace buffer record.
Definition: chtrace.h:108
__trace_halt
void __trace_halt(const char *reason)
Inserts in the circular debug trace buffer an halt record.
Definition: chtrace.c:186
msg_t
int32_t msg_t
Definition: chearly.h:88
trace_event_t::reason
const char * reason
Halt error string.
Definition: chtrace.h:170
trace_event_t::ntp
thread_t * ntp
Switched in thread.
Definition: chtrace.h:135
trace_event_t::up1
void * up1
Trace user parameter 1.
Definition: chtrace.h:179
trace_buffer_t::size
uint16_t size
Trace buffer size (entries).
Definition: chtrace.h:200
trace_event_t::rtstamp
uint32_t rtstamp
Accurate time stamp.
Definition: chtrace.h:122
trace_event_t::time
systime_t time
System time stamp of the switch event.
Definition: chtrace.h:126
ch_thread
Structure representing a thread.
Definition: chobjects.h:156
trace_event_t::type
uint32_t type
Record type.
Definition: chtrace.h:112
trace_event_t::msg
msg_t msg
Ready message.
Definition: chtrace.h:152
chTraceSuspend
void chTraceSuspend(uint16_t mask)
Suspends one or more trace events.
Definition: chtrace.c:255
trace_buffer_t
Trace buffer header.
Definition: chtrace.h:192
trace_event_t::state
uint32_t state
Switched out thread state.
Definition: chtrace.h:116
trace_event_t::name
const char * name
ISR function name taken using func.
Definition: chtrace.h:161
chTraceResume
void chTraceResume(uint16_t mask)
Resumes one or more trace events.
Definition: chtrace.c:283
__trace_isr_leave
void __trace_isr_leave(const char *isr)
Inserts in the circular debug trace buffer an ISR-leave record.
Definition: chtrace.c:166
__trace_isr_enter
void __trace_isr_enter(const char *isr)
Inserts in the circular debug trace buffer an ISR-enter record.
Definition: chtrace.c:146
__trace_ready
void __trace_ready(thread_t *tp, msg_t msg)
Inserts in the circular debug trace buffer a ready record.
Definition: chtrace.c:107
chTraceWrite
void chTraceWrite(void *up1, void *up2)
Adds an user trace record to the trace buffer.
Definition: chtrace.c:227
trace_buffer_t::suspended
uint16_t suspended
Suspended trace sources mask.
Definition: chtrace.h:196
__trace_object_init
void __trace_object_init(trace_buffer_t *tbp)
Circular trace buffer initialization.
Definition: chtrace.c:88
trace_event_t::up2
void * up2
Trace user parameter 2.
Definition: chtrace.h:183
trace_event_t::wtobjp
void * wtobjp
Object where going to sleep.
Definition: chtrace.h:139
trace_buffer_t::ptr
trace_event_t * ptr
Pointer to the buffer front.
Definition: chtrace.h:204
__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:127
chTraceSuspendI
void chTraceSuspendI(uint16_t mask)
Suspends one or more trace events.
Definition: chtrace.c:241
trace_event_t::tp
thread_t * tp
Thread made ready.
Definition: chtrace.h:148