ChibiOS/RT  5.1.0
chtrace.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 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; either version 3 of the License, or
9  (at your option) any later version.
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 chtrace.c
22  * @brief Tracer code.
23  *
24  * @addtogroup trace
25  * @details System events tracing service.
26  * @{
27  */
28 
29 #include "ch.h"
30 
31 /*===========================================================================*/
32 /* Module local definitions. */
33 /*===========================================================================*/
34 
35 /*===========================================================================*/
36 /* Module exported variables. */
37 /*===========================================================================*/
38 
39 /*===========================================================================*/
40 /* Module local types. */
41 /*===========================================================================*/
42 
43 /*===========================================================================*/
44 /* Module local variables. */
45 /*===========================================================================*/
46 
47 /*===========================================================================*/
48 /* Module local functions. */
49 /*===========================================================================*/
50 
51 #if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
52 /**
53  * @brief Writes a time stamp and increases the trace buffer pointer.
54  *
55  * @notapi
56  */
57 static NOINLINE void trace_next(void) {
58 
60 #if PORT_SUPPORTS_RT == TRUE
62 #else
63  ch.dbg.trace_buffer.ptr->rtstamp = (rtcnt_t)0;
64 #endif
65 
66  /* Trace hook, useful in order to interface debug tools.*/
68 
69  if (++ch.dbg.trace_buffer.ptr >=
72  }
73 }
74 #endif
75 
76 /*===========================================================================*/
77 /* Module exported functions. */
78 /*===========================================================================*/
79 
80 #if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
81 /**
82  * @brief Trace circular buffer subsystem initialization.
83  * @note Internal use only.
84  */
85 void _trace_init(void) {
86  unsigned i;
87 
88  ch.dbg.trace_buffer.suspended = (uint16_t)~CH_DBG_TRACE_MASK;
91  for (i = 0U; i < (unsigned)CH_DBG_TRACE_BUFFER_SIZE; i++) {
92  ch.dbg.trace_buffer.buffer[i].type = CH_TRACE_TYPE_UNUSED;
93  }
94 }
95 
96 /**
97  * @brief Inserts in the circular debug trace buffer a context switch record.
98  *
99  * @param[in] ntp the thread being switched in
100  * @param[in] otp the thread being switched out
101  *
102  * @notapi
103  */
104 void _trace_switch(thread_t *ntp, thread_t *otp) {
105 
106  (void)ntp;
107 
108  if ((ch.dbg.trace_buffer.suspended & CH_DBG_TRACE_MASK_SWITCH) == 0U) {
109  ch.dbg.trace_buffer.ptr->type = CH_TRACE_TYPE_SWITCH;
110  ch.dbg.trace_buffer.ptr->state = (uint8_t)otp->state;
111  ch.dbg.trace_buffer.ptr->u.sw.ntp = currp;
112  ch.dbg.trace_buffer.ptr->u.sw.wtobjp = otp->u.wtobjp;
113  trace_next();
114  }
115 }
116 
117 /**
118  * @brief Inserts in the circular debug trace buffer an ISR-enter record.
119  *
120  * @param[in] isr name of the isr
121  *
122  * @notapi
123  */
124 void _trace_isr_enter(const char *isr) {
125 
126  if ((ch.dbg.trace_buffer.suspended & CH_DBG_TRACE_MASK_ISR) == 0U) {
127  port_lock_from_isr();
128  ch.dbg.trace_buffer.ptr->type = CH_TRACE_TYPE_ISR_ENTER;
129  ch.dbg.trace_buffer.ptr->state = 0U;
130  ch.dbg.trace_buffer.ptr->u.isr.name = isr;
131  trace_next();
132  port_unlock_from_isr();
133  }
134 }
135 
136 /**
137  * @brief Inserts in the circular debug trace buffer an ISR-leave record.
138  *
139  * @param[in] isr name of the isr
140  *
141  * @notapi
142  */
143 void _trace_isr_leave(const char *isr) {
144 
145  if ((ch.dbg.trace_buffer.suspended & CH_DBG_TRACE_MASK_ISR) == 0U) {
146  port_lock_from_isr();
147  ch.dbg.trace_buffer.ptr->type = CH_TRACE_TYPE_ISR_LEAVE;
148  ch.dbg.trace_buffer.ptr->state = 0U;
149  ch.dbg.trace_buffer.ptr->u.isr.name = isr;
150  trace_next();
151  port_unlock_from_isr();
152  }
153 }
154 
155 /**
156  * @brief Inserts in the circular debug trace buffer an halt record.
157  *
158  * @param[in] reason the halt error string
159  *
160  * @notapi
161  */
162 void _trace_halt(const char *reason) {
163 
164  if ((ch.dbg.trace_buffer.suspended & CH_DBG_TRACE_MASK_HALT) == 0U) {
165  ch.dbg.trace_buffer.ptr->type = CH_TRACE_TYPE_HALT;
166  ch.dbg.trace_buffer.ptr->state = 0;
167  ch.dbg.trace_buffer.ptr->u.halt.reason = reason;
168  trace_next();
169  }
170 }
171 
172 /**
173  * @brief Adds an user trace record to the trace buffer.
174  *
175  * @param[in] up1 user parameter 1
176  * @param[in] up2 user parameter 2
177  *
178  * @iclass
179  */
180 void chDbgWriteTraceI(void *up1, void *up2) {
181 
183 
184  if ((ch.dbg.trace_buffer.suspended & CH_DBG_TRACE_MASK_USER) == 0U) {
185  ch.dbg.trace_buffer.ptr->type = CH_TRACE_TYPE_USER;
186  ch.dbg.trace_buffer.ptr->state = 0;
187  ch.dbg.trace_buffer.ptr->u.user.up1 = up1;
188  ch.dbg.trace_buffer.ptr->u.user.up2 = up2;
189  trace_next();
190  }
191 }
192 
193 /**
194  * @brief Adds an user trace record to the trace buffer.
195  *
196  * @param[in] up1 user parameter 1
197  * @param[in] up2 user parameter 2
198  *
199  * @api
200  */
201 void chDbgWriteTrace(void *up1, void *up2) {
202 
203  chSysLock();
204  chDbgWriteTraceI(up1, up2);
205  chSysUnlock();
206 }
207 
208 /**
209  * @brief Suspends one or more trace events.
210  *
211  * @param[in] mask mask of the trace events to be suspended
212  *
213  * @iclass
214  */
215 void chDbgSuspendTraceI(uint16_t mask) {
216 
218 
219  ch.dbg.trace_buffer.suspended |= mask;
220 }
221 
222 /**
223  * @brief Suspends one or more trace events.
224  *
225  * @param[in] mask mask of the trace events to be suspended
226  *
227  * @api
228  */
229 void chDbgSuspendTrace(uint16_t mask) {
230 
231  chSysLock();
232  chDbgSuspendTraceI(mask);
233  chSysUnlock();
234 }
235 
236 /**
237  * @brief Resumes one or more trace events.
238  *
239  * @param[in] mask mask of the trace events to be resumed
240  *
241  * @iclass
242  */
243 void chDbgResumeTraceI(uint16_t mask) {
244 
246 
247  ch.dbg.trace_buffer.suspended &= ~mask;
248 }
249 
250 /**
251  * @brief Resumes one or more trace events.
252  *
253  * @param[in] mask mask of the trace events to be resumed
254  *
255  * @api
256  */
257 void chDbgResumeTrace(uint16_t mask) {
258 
259  chSysLock();
260  chDbgResumeTraceI(mask);
261  chSysUnlock();
262 }
263 #endif /* CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED */
264 
265 /** @} */
static NOINLINE void trace_next(void)
Writes a time stamp and increases the trace buffer pointer.
Definition: chtrace.c:57
#define chSysGetRealtimeCounterX()
Returns the current value of the system real time counter.
Definition: chsys.h:253
ch_trace_buffer_t trace_buffer
Public trace buffer.
Definition: chschd.h:402
static systime_t chVTGetSystemTimeX(void)
Current system time.
Definition: chvt.h:115
ch_trace_event_t * ptr
Pointer to the buffer front.
Definition: chtrace.h:187
void chDbgWriteTrace(void *up1, void *up2)
Adds an user trace record to the trace buffer.
Definition: chtrace.c:201
static void chSysLock(void)
Enters the kernel lock state.
Definition: chsys.h:353
system_debug_t dbg
System debug.
Definition: chschd.h:423
struct ch_trace_event_t::@1::@2 sw
Structure representing a context 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
#define currp
Current thread pointer access macro.
Definition: chschd.h:459
static void chSysUnlock(void)
Leaves the kernel lock state.
Definition: chsys.h:365
void chDbgWriteTraceI(void *up1, void *up2)
Adds an user trace record to the trace buffer.
Definition: chtrace.c:180
void _trace_halt(const char *reason)
Inserts in the circular debug trace buffer an halt record.
Definition: chtrace.c:162
void chDbgCheckClassI(void)
I-class functions context check.
Definition: chdebug.c:235
void chDbgSuspendTrace(uint16_t mask)
Suspends one or more trace events.
Definition: chtrace.c:229
struct ch_trace_event_t::@1::@5 user
User trace structure.
uint32_t rtstamp
Accurate time stamp.
Definition: chtrace.h:118
void _trace_init(void)
Trace circular buffer subsystem initialization.
Definition: chtrace.c:85
uint16_t suspended
Suspended trace sources mask.
Definition: chtrace.h:179
struct ch_trace_event_t::@1::@4 halt
Structure representing an halt.
uint32_t state
Switched out thread state.
Definition: chtrace.h:112
ch_system_t ch
System data structures.
Definition: chschd.c:42
ch_trace_event_t buffer[CH_DBG_TRACE_BUFFER_SIZE]
Ring buffer.
Definition: chtrace.h:191
uint16_t size
Trace buffer size (entries).
Definition: chtrace.h:183
tstate_t state
Current thread state.
Definition: chschd.h:180
void * wtobjp
Pointer to a generic "wait" object.
Definition: chschd.h:230
void chDbgResumeTraceI(uint16_t mask)
Resumes one or more trace events.
Definition: chtrace.c:243
void chDbgResumeTrace(uint16_t mask)
Resumes one or more trace events.
Definition: chtrace.c:257
union ch_thread::@0 u
State-specific fields.
systime_t time
System time stamp of the switch event.
Definition: chtrace.h:122
uint32_t type
Record type.
Definition: chtrace.h:108
#define CH_DBG_TRACE_BUFFER_SIZE
Trace buffer entries.
Definition: chtrace.h:87
void _trace_isr_enter(const char *isr)
Inserts in the circular debug trace buffer an ISR-enter record.
Definition: chtrace.c:124
#define CH_CFG_TRACE_HOOK(tep)
Trace hook.
Definition: chconf.h:684
ChibiOS/RT main include file.
void _trace_isr_leave(const char *isr)
Inserts in the circular debug trace buffer an ISR-leave record.
Definition: chtrace.c:143
struct ch_trace_event_t::@1::@3 isr
Structure representing an ISR enter.
void chDbgSuspendTraceI(uint16_t mask)
Suspends one or more trace events.
Definition: chtrace.c:215
Structure representing a thread.
Definition: chschd.h:153