ChibiOS/RT  6.0.3
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 checks_assertions
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  * - Parameters check.
73  * - Kernel assertions.
74  * .
75  * @note Stack checks are not implemented in this module but in the port
76  * layer in an architecture-dependent way.
77  * @{
78  */
79 
80 #include "ch.h"
81 
82 /*===========================================================================*/
83 /* Module local definitions. */
84 /*===========================================================================*/
85 
86 /*===========================================================================*/
87 /* Module exported variables. */
88 /*===========================================================================*/
89 
90 /*===========================================================================*/
91 /* Module local types. */
92 /*===========================================================================*/
93 
94 /*===========================================================================*/
95 /* Module local variables. */
96 /*===========================================================================*/
97 
98 /*===========================================================================*/
99 /* Module local functions. */
100 /*===========================================================================*/
101 
102 /*===========================================================================*/
103 /* Module exported functions. */
104 /*===========================================================================*/
105 
106 #if (CH_DBG_SYSTEM_STATE_CHECK == TRUE) || defined(__DOXYGEN__)
107 /**
108  * @brief Guard code for @p chSysDisable().
109  *
110  * @notapi
111  */
112 void _dbg_check_disable(void) {
113 
114  if ((ch.dbg.isr_cnt != (cnt_t)0) || (ch.dbg.lock_cnt != (cnt_t)0)) {
115  chSysHalt("SV#1");
116  }
117 }
118 
119 /**
120  * @brief Guard code for @p chSysSuspend().
121  *
122  * @notapi
123  */
124 void _dbg_check_suspend(void) {
125 
126  if ((ch.dbg.isr_cnt != (cnt_t)0) || (ch.dbg.lock_cnt != (cnt_t)0)) {
127  chSysHalt("SV#2");
128  }
129 }
130 
131 /**
132  * @brief Guard code for @p chSysEnable().
133  *
134  * @notapi
135  */
136 void _dbg_check_enable(void) {
137 
138  if ((ch.dbg.isr_cnt != (cnt_t)0) || (ch.dbg.lock_cnt != (cnt_t)0)) {
139  chSysHalt("SV#3");
140  }
141 }
142 
143 /**
144  * @brief Guard code for @p chSysLock().
145  *
146  * @notapi
147  */
148 void _dbg_check_lock(void) {
149 
150  if ((ch.dbg.isr_cnt != (cnt_t)0) || (ch.dbg.lock_cnt != (cnt_t)0)) {
151  chSysHalt("SV#4");
152  }
153  _dbg_enter_lock();
154 }
155 
156 /**
157  * @brief Guard code for @p chSysUnlock().
158  *
159  * @notapi
160  */
161 void _dbg_check_unlock(void) {
162 
163  if ((ch.dbg.isr_cnt != (cnt_t)0) || (ch.dbg.lock_cnt <= (cnt_t)0)) {
164  chSysHalt("SV#5");
165  }
166  _dbg_leave_lock();
167 }
168 
169 /**
170  * @brief Guard code for @p chSysLockFromIsr().
171  *
172  * @notapi
173  */
175 
176  if ((ch.dbg.isr_cnt <= (cnt_t)0) || (ch.dbg.lock_cnt != (cnt_t)0)) {
177  chSysHalt("SV#6");
178  }
179  _dbg_enter_lock();
180 }
181 
182 /**
183  * @brief Guard code for @p chSysUnlockFromIsr().
184  *
185  * @notapi
186  */
188 
189  if ((ch.dbg.isr_cnt <= (cnt_t)0) || (ch.dbg.lock_cnt <= (cnt_t)0)) {
190  chSysHalt("SV#7");
191  }
192  _dbg_leave_lock();
193 }
194 
195 /**
196  * @brief Guard code for @p CH_IRQ_PROLOGUE().
197  *
198  * @notapi
199  */
201 
202  port_lock_from_isr();
203  if ((ch.dbg.isr_cnt < (cnt_t)0) || (ch.dbg.lock_cnt != (cnt_t)0)) {
204  chSysHalt("SV#8");
205  }
206  ch.dbg.isr_cnt++;
207  port_unlock_from_isr();
208 }
209 
210 /**
211  * @brief Guard code for @p CH_IRQ_EPILOGUE().
212  *
213  * @notapi
214  */
216 
217  port_lock_from_isr();
218  if ((ch.dbg.isr_cnt <= (cnt_t)0) || (ch.dbg.lock_cnt != (cnt_t)0)) {
219  chSysHalt("SV#9");
220  }
221  ch.dbg.isr_cnt--;
222  port_unlock_from_isr();
223 }
224 
225 /**
226  * @brief I-class functions context check.
227  * @details Verifies that the system is in an appropriate state for invoking
228  * an I-class API function. A panic is generated if the state is
229  * not compatible.
230  *
231  * @api
232  */
233 void chDbgCheckClassI(void) {
234 
235  if ((ch.dbg.isr_cnt < (cnt_t)0) || (ch.dbg.lock_cnt <= (cnt_t)0)) {
236  chSysHalt("SV#10");
237  }
238 }
239 
240 /**
241  * @brief S-class functions context check.
242  * @details Verifies that the system is in an appropriate state for invoking
243  * an S-class API function. A panic is generated if the state is
244  * not compatible.
245  *
246  * @api
247  */
248 void chDbgCheckClassS(void) {
249 
250  if ((ch.dbg.isr_cnt != (cnt_t)0) || (ch.dbg.lock_cnt <= (cnt_t)0)) {
251  chSysHalt("SV#11");
252  }
253 }
254 
255 #endif /* CH_DBG_SYSTEM_STATE_CHECK == TRUE */
256 
257 /** @} */
cnt_t lock_cnt
Lock nesting level.
Definition: chschd.h:396
system_debug_t dbg
System debug.
Definition: chschd.h:423
void _dbg_check_disable(void)
Guard code for chSysDisable().
Definition: chdebug.c:112
void _dbg_check_lock_from_isr(void)
Guard code for chSysLockFromIsr().
Definition: chdebug.c:174
void _dbg_check_enable(void)
Guard code for chSysEnable().
Definition: chdebug.c:136
void _dbg_check_unlock(void)
Guard code for chSysUnlock().
Definition: chdebug.c:161
void chSysHalt(const char *reason)
Halts the system.
Definition: chsys.c:198
void _dbg_check_suspend(void)
Guard code for chSysSuspend().
Definition: chdebug.c:124
void _dbg_check_enter_isr(void)
Guard code for CH_IRQ_PROLOGUE().
Definition: chdebug.c:200
ch_system_t ch
System data structures.
Definition: chschd.c:42
void _dbg_check_unlock_from_isr(void)
Guard code for chSysUnlockFromIsr().
Definition: chdebug.c:187
cnt_t isr_cnt
ISR nesting level.
Definition: chschd.h:392
void chDbgCheckClassI(void)
I-class functions context check.
Definition: chdebug.c:233
void _dbg_check_lock(void)
Guard code for chSysLock().
Definition: chdebug.c:148
void _dbg_check_leave_isr(void)
Guard code for CH_IRQ_EPILOGUE().
Definition: chdebug.c:215
void chDbgCheckClassS(void)
S-class functions context check.
Definition: chdebug.c:248
ChibiOS/RT main include file.