ChibiOS 21.11.5
chtrace.c
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/src/chtrace.c
21 * @brief Tracer code.
22 *
23 * @addtogroup trace
24 * @details System events tracing service.
25 * @{
26 */
27
28#include "ch.h"
29
30/*===========================================================================*/
31/* Module local definitions. */
32/*===========================================================================*/
33
34/*===========================================================================*/
35/* Module exported variables. */
36/*===========================================================================*/
37
38/*===========================================================================*/
39/* Module local types. */
40/*===========================================================================*/
41
42/*===========================================================================*/
43/* Module local variables. */
44/*===========================================================================*/
45
46/*===========================================================================*/
47/* Module local functions. */
48/*===========================================================================*/
49
50#if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
51/**
52 * @brief Writes a time stamp and increases the trace buffer pointer.
53 *
54 * @notapi
55 */
57
59#if PORT_SUPPORTS_RT == TRUE
61#else
63#endif
64
65 /* Trace hook, useful in order to interface debug tools.*/
67
69 oip->trace_buffer.ptr = &oip->trace_buffer.buffer[0];
70 }
71}
72#endif
73
74/*===========================================================================*/
75/* Module exported functions. */
76/*===========================================================================*/
77
78#if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
79/**
80 * @brief Circular trace buffer initialization.
81 * @note Internal use only.
82 *
83 * @param[out] tbp pointer to the @p trace_buffer_t structure
84 *
85 * @notapi
86 */
88 unsigned i;
89
90 tbp->suspended = (uint16_t)~CH_DBG_TRACE_MASK;
92 tbp->ptr = &tbp->buffer[0];
93 for (i = 0U; i < (unsigned)CH_DBG_TRACE_BUFFER_SIZE; i++) {
95 }
96}
97
98/**
99 * @brief Inserts in the circular debug trace buffer a ready record.
100 *
101 * @param[in] tp the thread that just become ready
102 * @param[in] msg the thread ready message
103 *
104 * @notapi
105 */
107 os_instance_t *oip = currcore;
108
111 oip->trace_buffer.ptr->state = (uint8_t)tp->state;
112 oip->trace_buffer.ptr->u.rdy.tp = tp;
113 oip->trace_buffer.ptr->u.rdy.msg = msg;
114 trace_next(oip);
115 }
116}
117
118/**
119 * @brief Inserts in the circular debug trace buffer a context switch record.
120 *
121 * @param[in] ntp the thread being switched in
122 * @param[in] otp the thread being switched out
123 *
124 * @notapi
125 */
127 os_instance_t *oip = currcore;
128
131 oip->trace_buffer.ptr->state = (uint8_t)otp->state;
132 oip->trace_buffer.ptr->u.sw.ntp = ntp;
133 oip->trace_buffer.ptr->u.sw.wtobjp = otp->u.wtobjp;
134 trace_next(oip);
135 }
136}
137
138/**
139 * @brief Inserts in the circular debug trace buffer an ISR-enter record.
140 *
141 * @param[in] isr name of the isr
142 *
143 * @notapi
144 */
145void __trace_isr_enter(const char *isr) {
146 os_instance_t *oip = currcore;
147
148 if ((oip->trace_buffer.suspended & CH_DBG_TRACE_MASK_ISR) == 0U) {
151 oip->trace_buffer.ptr->state = 0U;
152 oip->trace_buffer.ptr->u.isr.name = isr;
153 trace_next(oip);
155 }
156}
157
158/**
159 * @brief Inserts in the circular debug trace buffer an ISR-leave record.
160 *
161 * @param[in] isr name of the isr
162 *
163 * @notapi
164 */
165void __trace_isr_leave(const char *isr) {
166 os_instance_t *oip = currcore;
167
168 if ((oip->trace_buffer.suspended & CH_DBG_TRACE_MASK_ISR) == 0U) {
171 oip->trace_buffer.ptr->state = 0U;
172 oip->trace_buffer.ptr->u.isr.name = isr;
173 trace_next(oip);
175 }
176}
177
178/**
179 * @brief Inserts in the circular debug trace buffer an halt record.
180 *
181 * @param[in] reason the halt error string
182 *
183 * @notapi
184 */
185void __trace_halt(const char *reason) {
186 os_instance_t *oip = currcore;
187
188 if ((oip->trace_buffer.suspended & CH_DBG_TRACE_MASK_HALT) == 0U) {
190 oip->trace_buffer.ptr->state = 0;
191 oip->trace_buffer.ptr->u.halt.reason = reason;
192 trace_next(oip);
193 }
194}
195
196/**
197 * @brief Adds an user trace record to the trace buffer.
198 *
199 * @param[in] up1 user parameter 1
200 * @param[in] up2 user parameter 2
201 *
202 * @iclass
203 */
204void chTraceWriteI(void *up1, void *up2) {
205 os_instance_t *oip = currcore;
206
208
209 if ((oip->trace_buffer.suspended & CH_DBG_TRACE_MASK_USER) == 0U) {
211 oip->trace_buffer.ptr->state = 0;
212 oip->trace_buffer.ptr->u.user.up1 = up1;
213 oip->trace_buffer.ptr->u.user.up2 = up2;
214 trace_next(oip);
215 }
216}
217
218/**
219 * @brief Adds an user trace record to the trace buffer.
220 *
221 * @param[in] up1 user parameter 1
222 * @param[in] up2 user parameter 2
223 *
224 * @api
225 */
226void chTraceWrite(void *up1, void *up2) {
227
228 chSysLock();
229 chTraceWriteI(up1, up2);
230 chSysUnlock();
231}
232
233/**
234 * @brief Suspends one or more trace events.
235 *
236 * @param[in] mask mask of the trace events to be suspended
237 *
238 * @iclass
239 */
240void chTraceSuspendI(uint16_t mask) {
241
243
244 currcore->trace_buffer.suspended |= mask;
245}
246
247/**
248 * @brief Suspends one or more trace events.
249 *
250 * @param[in] mask mask of the trace events to be suspended
251 *
252 * @api
253 */
254void chTraceSuspend(uint16_t mask) {
255
256 chSysLock();
257 chTraceSuspendI(mask);
258 chSysUnlock();
259}
260
261/**
262 * @brief Resumes one or more trace events.
263 *
264 * @param[in] mask mask of the trace events to be resumed
265 *
266 * @iclass
267 */
268void chTraceResumeI(uint16_t mask) {
269
271
272 currcore->trace_buffer.suspended &= ~mask;
273}
274
275/**
276 * @brief Resumes one or more trace events.
277 *
278 * @param[in] mask mask of the trace events to be resumed
279 *
280 * @api
281 */
282void chTraceResume(uint16_t mask) {
283
284 chSysLock();
285 chTraceResumeI(mask);
286 chSysUnlock();
287}
288#endif /* CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED */
289
290/** @} */
#define chSysUnlock()
Leaves the kernel lock state.
#define chSysLock()
Enters the kernel lock state.
#define chVTGetSystemTimeX()
Current system time.
#define chDbgCheckClassI()
Definition chdebug.h:98
#define CH_CFG_TRACE_HOOK(tep)
Trace hook.
int32_t msg_t
Definition chearly.h:87
struct ch_os_instance os_instance_t
Type of an OS instance structure.
Definition chearly.h:137
port_rtcnt_t rtcnt_t
Definition chearly.h:76
struct ch_thread thread_t
Type of a thread structure.
Definition chearly.h:132
static void port_unlock_from_isr(void)
Kernel-unlock action from an interrupt handler.
Definition chcore.h:367
static void port_lock_from_isr(void)
Kernel-lock action from an interrupt handler.
Definition chcore.h:357
#define NOINLINE
Makes functions not inlineable.
Definition chtypes.h:90
#define CH_DBG_TRACE_MASK
#define chSysGetRealtimeCounterX()
Returns the current value of the system real time counter.
Definition chsys.h:291
#define currcore
Access to current core's instance structure.
Definition chsys.h:89
#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
#define CH_TRACE_TYPE_HALT
Definition chtrace.h:43
#define CH_DBG_TRACE_MASK_ISR
Definition chtrace.h:55
#define CH_TRACE_TYPE_USER
Definition chtrace.h:44
void chTraceResumeI(uint16_t mask)
Resumes one or more trace events.
Definition chtrace.c:268
#define CH_TRACE_TYPE_SWITCH
Definition chtrace.h:40
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
#define CH_TRACE_TYPE_READY
Definition chtrace.h:39
#define CH_DBG_TRACE_MASK_USER
Definition chtrace.h:57
void chTraceWriteI(void *up1, void *up2)
Adds an user trace record to the trace buffer.
Definition chtrace.c:204
#define CH_DBG_TRACE_MASK_HALT
Definition chtrace.h:56
static NOINLINE void trace_next(os_instance_t *oip)
Writes a time stamp and increases the trace buffer pointer.
Definition chtrace.c:56
#define CH_DBG_TRACE_MASK_READY
Definition chtrace.h:53
#define CH_TRACE_TYPE_ISR_ENTER
Definition chtrace.h:41
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
#define CH_TRACE_TYPE_UNUSED
Definition chtrace.h:38
#define CH_TRACE_TYPE_ISR_LEAVE
Definition chtrace.h:42
void __trace_object_init(trace_buffer_t *tbp)
Circular trace buffer initialization.
Definition chtrace.c:87
#define CH_DBG_TRACE_MASK_SWITCH
Definition chtrace.h:54
#define __trace_isr_leave(isr)
Definition chtrace.h:229
trace_buffer_t trace_buffer
Trace buffer.
Definition chobjects.h:447
void * wtobjp
Pointer to a generic "wait" object.
Definition chobjects.h:253
tstate_t state
Current thread state.
Definition chobjects.h:203
union ch_thread::@250330312022121344252011223135034045240103044261 u
State-specific fields.
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
void * up1
Trace user parameter 1.
Definition chtrace.h:178
struct trace_event_t::@260131021033123353245355077257223364340026005263::@202217350115266301345267210242210042027166125212 isr
Structure representing an ISR enter.
uint32_t type
Record type.
Definition chtrace.h:111
union trace_event_t::@260131021033123353245355077257223364340026005263 u
thread_t * ntp
Switched in thread.
Definition chtrace.h:134
struct trace_event_t::@260131021033123353245355077257223364340026005263::@251024233242046004237167317141111357111213363177 rdy
Structure representing a thread becoming ready.
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
struct trace_event_t::@260131021033123353245355077257223364340026005263::@356321027224211063007027066114155207152210227304 halt
Structure representing an halt.
struct trace_event_t::@260131021033123353245355077257223364340026005263::@362362056063017021234245241062043041075034014237 user
User trace structure.
void * up2
Trace user parameter 2.
Definition chtrace.h:182
struct trace_event_t::@260131021033123353245355077257223364340026005263::@164062373271014110227054175141026212176372123043 sw
Structure representing a context switch.