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