ChibiOS 21.11.5
chdebug.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/chdebug.c
21 * @brief Debug support code.
22 *
23 * @addtogroup checks_assertions
24 * @details Debug APIs and services:
25 * - Runtime system state and call protocol check. The following
26 * panic messages can be generated:
27 * - SV#1, misplaced @p chSysDisable().
28 * - Called from an ISR.
29 * - Called from a critical zone.
30 * .
31 * - SV#2, misplaced @p chSysSuspend()
32 * - Called from an ISR.
33 * - Called from a critical zone.
34 * .
35 * - SV#3, misplaced @p chSysEnable().
36 * - Called from an ISR.
37 * - Called from a critical zone.
38 * .
39 * - SV#4, misplaced @p chSysLock().
40 * - Called from an ISR.
41 * - Called from a critical zone.
42 * .
43 * - SV#5, misplaced @p chSysUnlock().
44 * - Called from an ISR.
45 * - Not called from a critical zone.
46 * .
47 * - SV#6, misplaced @p chSysLockFromISR().
48 * - Not called from an ISR.
49 * - Called from a critical zone.
50 * .
51 * - SV#7, misplaced @p chSysUnlockFromISR().
52 * - Not called from an ISR.
53 * - Not called from a critical zone.
54 * .
55 * - SV#8, misplaced @p CH_IRQ_PROLOGUE().
56 * - Not called at ISR begin.
57 * - Called from a critical zone.
58 * .
59 * - SV#9, misplaced @p CH_IRQ_EPILOGUE().
60 * - @p CH_IRQ_PROLOGUE() missing.
61 * - Not called at ISR end.
62 * - Called from a critical zone.
63 * .
64 * - SV#10, misplaced I-class function.
65 * - I-class function not called from within a critical zone.
66 * .
67 * - SV#11, misplaced S-class function.
68 * - S-class function not called from within a critical zone.
69 * - Called from an ISR.
70 * .
71 * - Parameters check.
72 * - Kernel assertions.
73 * .
74 * @note Stack checks are not implemented in this module but in the port
75 * layer in an architecture-dependent way.
76 * @{
77 */
78
79#include "ch.h"
80
81/*===========================================================================*/
82/* Module local definitions. */
83/*===========================================================================*/
84
85/*===========================================================================*/
86/* Module exported variables. */
87/*===========================================================================*/
88
89/*===========================================================================*/
90/* Module local types. */
91/*===========================================================================*/
92
93/*===========================================================================*/
94/* Module local variables. */
95/*===========================================================================*/
96
97/*===========================================================================*/
98/* Module local functions. */
99/*===========================================================================*/
100
101/*===========================================================================*/
102/* Module exported functions. */
103/*===========================================================================*/
104
105#if (CH_DBG_SYSTEM_STATE_CHECK == TRUE) || defined(__DOXYGEN__)
106/**
107 * @brief Guard code for @p chSysDisable().
108 *
109 * @notapi
110 */
112 os_instance_t *oip = currcore;
113
114 if (unlikely((oip->dbg.isr_cnt != (cnt_t)0) ||
115 (oip->dbg.lock_cnt != (cnt_t)0))) {
116 chSysHalt("SV#1");
117 }
118}
119
120/**
121 * @brief Guard code for @p chSysSuspend().
122 *
123 * @notapi
124 */
126 os_instance_t *oip = currcore;
127
128 if (unlikely((oip->dbg.isr_cnt != (cnt_t)0) ||
129 (oip->dbg.lock_cnt != (cnt_t)0))) {
130 chSysHalt("SV#2");
131 }
132}
133
134/**
135 * @brief Guard code for @p chSysEnable().
136 *
137 * @notapi
138 */
140 os_instance_t *oip = currcore;
141
142 if (unlikely((oip->dbg.isr_cnt != (cnt_t)0) ||
143 (oip->dbg.lock_cnt != (cnt_t)0))) {
144 chSysHalt("SV#3");
145 }
146}
147
148/**
149 * @brief Guard code for @p chSysLock().
150 *
151 * @notapi
152 */
154 os_instance_t *oip = currcore;
155
156 if (unlikely((oip->dbg.isr_cnt != (cnt_t)0) ||
157 (oip->dbg.lock_cnt != (cnt_t)0))) {
158 chSysHalt("SV#4");
159 }
160 oip->dbg.lock_cnt = (cnt_t)1;
161}
162
163/**
164 * @brief Guard code for @p chSysUnlock().
165 *
166 * @notapi
167 */
169 os_instance_t *oip = currcore;
170
171 if (unlikely((oip->dbg.isr_cnt != (cnt_t)0) ||
172 (oip->dbg.lock_cnt <= (cnt_t)0))) {
173 chSysHalt("SV#5");
174 }
175 oip->dbg.lock_cnt = (cnt_t)0;
176}
177
178/**
179 * @brief Guard code for @p chSysLockFromIsr().
180 *
181 * @notapi
182 */
184 os_instance_t *oip = currcore;
185
186 if (unlikely((oip->dbg.isr_cnt <= (cnt_t)0) ||
187 (oip->dbg.lock_cnt != (cnt_t)0))) {
188 chSysHalt("SV#6");
189 }
190 oip->dbg.lock_cnt = (cnt_t)1;
191}
192
193/**
194 * @brief Guard code for @p chSysUnlockFromIsr().
195 *
196 * @notapi
197 */
199 os_instance_t *oip = currcore;
200
201 if (unlikely((oip->dbg.isr_cnt <= (cnt_t)0) ||
202 (oip->dbg.lock_cnt <= (cnt_t)0))) {
203 chSysHalt("SV#7");
204 }
205 oip->dbg.lock_cnt = (cnt_t)0;
206}
207
208/**
209 * @brief Guard code for @p CH_IRQ_PROLOGUE().
210 *
211 * @notapi
212 */
214 os_instance_t *oip = currcore;
215
217 if (unlikely((oip->dbg.isr_cnt < (cnt_t)0) ||
218 (oip->dbg.lock_cnt != (cnt_t)0))) {
219 chSysHalt("SV#8");
220 }
221 oip->dbg.isr_cnt++;
223}
224
225/**
226 * @brief Guard code for @p CH_IRQ_EPILOGUE().
227 *
228 * @notapi
229 */
231 os_instance_t *oip = currcore;
232
234 if (unlikely((oip->dbg.isr_cnt <= (cnt_t)0) ||
235 (oip->dbg.lock_cnt != (cnt_t)0))) {
236 chSysHalt("SV#9");
237 }
238 oip->dbg.isr_cnt--;
240}
241
242/**
243 * @brief I-class functions context check.
244 * @details Verifies that the system is in an appropriate state for invoking
245 * an I-class API function. A panic is generated if the state is
246 * not compatible.
247 *
248 * @api
249 */
251 os_instance_t *oip = currcore;
252
253 if (unlikely((oip->dbg.isr_cnt < (cnt_t)0) ||
254 (oip->dbg.lock_cnt <= (cnt_t)0))) {
255 chSysHalt("SV#10");
256 }
257}
258
259/**
260 * @brief S-class functions context check.
261 * @details Verifies that the system is in an appropriate state for invoking
262 * an S-class API function. A panic is generated if the state is
263 * not compatible.
264 *
265 * @api
266 */
268 os_instance_t *oip = currcore;
269
270 if (unlikely((oip->dbg.isr_cnt != (cnt_t)0) ||
271 (oip->dbg.lock_cnt <= (cnt_t)0))) {
272 chSysHalt("SV#11");
273 }
274}
275
276#endif /* CH_DBG_SYSTEM_STATE_CHECK == TRUE */
277
278/** @} */
void chDbgCheckClassS(void)
S-class functions context check.
Definition chdebug.c:267
void __dbg_check_disable(void)
Guard code for chSysDisable().
Definition chdebug.c:111
void __dbg_check_unlock(void)
Guard code for chSysUnlock().
Definition chdebug.c:168
void chDbgCheckClassI(void)
I-class functions context check.
Definition chdebug.c:250
void __dbg_check_lock(void)
Guard code for chSysLock().
Definition chdebug.c:153
void __dbg_check_leave_isr(void)
Guard code for CH_IRQ_EPILOGUE().
Definition chdebug.c:230
void __dbg_check_enable(void)
Guard code for chSysEnable().
Definition chdebug.c:139
void __dbg_check_lock_from_isr(void)
Guard code for chSysLockFromIsr().
Definition chdebug.c:183
void __dbg_check_unlock_from_isr(void)
Guard code for chSysUnlockFromIsr().
Definition chdebug.c:198
void __dbg_check_enter_isr(void)
Guard code for CH_IRQ_PROLOGUE().
Definition chdebug.c:213
void __dbg_check_suspend(void)
Guard code for chSysSuspend().
Definition chdebug.c:125
int32_t cnt_t
Definition chearly.h:91
struct ch_os_instance os_instance_t
Type of an OS instance structure.
Definition chearly.h:137
#define unlikely(x)
Marks a boolean expression as likely false.
Definition chearly.h:190
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 currcore
Access to current core's instance structure.
Definition chsys.h:89
void chSysHalt(const char *reason)
Halts the system.
Definition chsys.c:208
system_debug_t dbg
System debug.
Definition chobjects.h:442
cnt_t lock_cnt
Lock nesting level.
Definition chdebug.h:78
cnt_t isr_cnt
ISR nesting level.
Definition chdebug.h:74