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