ChibiOS  21.6.0
chdebug.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
3  2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
4 
5  This file is part of ChibiOS.
6 
7  ChibiOS is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation version 3 of the License.
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 rt/include/chdebug.h
22  * @brief Debug support macros and structures.
23  *
24  * @addtogroup checks_assertions
25  * @{
26  */
27 
28 #ifndef CHDEBUG_H
29 #define CHDEBUG_H
30 
31 /*===========================================================================*/
32 /* Module constants. */
33 /*===========================================================================*/
34 
35 /*===========================================================================*/
36 /* Module pre-compile time settings. */
37 /*===========================================================================*/
38 
39 /**
40  * @name Debug related settings
41  * @{
42  */
43 /**
44  * @brief Fill value for thread stack area in debug mode.
45  */
46 #if !defined(CH_DBG_STACK_FILL_VALUE) || defined(__DOXYGEN__)
47 #define CH_DBG_STACK_FILL_VALUE 0x55
48 #endif
49 /** @} */
50 
51 /*===========================================================================*/
52 /* Derived constants and error checks. */
53 /*===========================================================================*/
54 
55 /*===========================================================================*/
56 /* Module data structures and types. */
57 /*===========================================================================*/
58 
59 /**
60  * @brief System debug data structure.
61  */
62 typedef struct ch_system_debug {
63  /**
64  * @brief Pointer to the panic message.
65  * @details This pointer is meant to be accessed through the debugger, it is
66  * written once and then the system is halted.
67  * @note Accesses to this pointer must never be optimized out so the
68  * field itself is declared volatile.
69  */
70  const char * volatile panic_msg;
71 #if (CH_DBG_SYSTEM_STATE_CHECK == TRUE) || defined(__DOXYGEN__)
72  /**
73  * @brief ISR nesting level.
74  */
76  /**
77  * @brief Lock nesting level.
78  */
80 #endif
82 
83 /*===========================================================================*/
84 /* Module macros. */
85 /*===========================================================================*/
86 
87 /* When the state checker feature is disabled then the following functions
88  are replaced by an empty macro.*/
89 #if CH_DBG_SYSTEM_STATE_CHECK == FALSE
90 #define __dbg_check_disable()
91 #define __dbg_check_suspend()
92 #define __dbg_check_enable()
93 #define __dbg_check_lock()
94 #define __dbg_check_unlock()
95 #define __dbg_check_lock_from_isr()
96 #define __dbg_check_unlock_from_isr()
97 #define __dbg_check_enter_isr()
98 #define __dbg_check_leave_isr()
99 #define chDbgCheckClassI()
100 #define chDbgCheckClassS()
101 #endif
102 
103 /**
104  * @name Macro Functions
105  * @{
106  */
107 /**
108  * @brief Function parameters check.
109  * @details If the condition check fails then the kernel panics and halts.
110  * @note The condition is tested only if the @p CH_DBG_ENABLE_CHECKS switch
111  * is specified in @p chconf.h else the macro does nothing.
112  *
113  * @param[in] c the condition to be verified to be true
114  *
115  * @api
116  */
117 #if !defined(chDbgCheck)
118 #define chDbgCheck(c) do { \
119  /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
120  if (CH_DBG_ENABLE_CHECKS != FALSE) { \
121  if (unlikely(!(c))) { \
122  /*lint -restore*/ \
123  chSysHalt(__func__); \
124  } \
125  } \
126 } while (false)
127 #endif /* !defined(chDbgCheck) */
128 
129 /**
130  * @brief Condition assertion.
131  * @details If the condition check fails then the kernel panics with a
132  * message and halts.
133  * @note The condition is tested only if the @p CH_DBG_ENABLE_ASSERTS switch
134  * is specified in @p chconf.h else the macro does nothing.
135  * @note The remark string is not currently used except for putting a
136  * comment in the code about the assertion.
137  *
138  * @param[in] c the condition to be verified to be true
139  * @param[in] r a remark string
140  *
141  * @api
142  */
143 #if !defined(chDbgAssert)
144 #define chDbgAssert(c, r) do { \
145  /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \
146  if (CH_DBG_ENABLE_ASSERTS != FALSE) { \
147  if (unlikely(!(c))) { \
148  /*lint -restore*/ \
149  chSysHalt(__func__); \
150  } \
151  } \
152 } while (false)
153 #endif /* !defined(chDbgAssert) */
154 /** @} */
155 
156 /*===========================================================================*/
157 /* External declarations. */
158 /*===========================================================================*/
159 
160 #ifdef __cplusplus
161 extern "C" {
162 #endif
163 #if CH_DBG_SYSTEM_STATE_CHECK == TRUE
164  void __dbg_check_disable(void);
165  void __dbg_check_suspend(void);
166  void __dbg_check_enable(void);
167  void __dbg_check_lock(void);
168  void __dbg_check_unlock(void);
169  void __dbg_check_lock_from_isr(void);
170  void __dbg_check_unlock_from_isr(void);
171  void __dbg_check_enter_isr(void);
172  void __dbg_check_leave_isr(void);
173  void chDbgCheckClassI(void);
174  void chDbgCheckClassS(void);
175 #endif
176 #ifdef __cplusplus
177 }
178 #endif
179 
180 /*===========================================================================*/
181 /* Module inline functions. */
182 /*===========================================================================*/
183 
184 /**
185  * @brief Debug support initialization.
186  * @note Internal use only.
187  *
188  * @param[out] sdp pointer to the @p system_debug_t structure
189  *
190  * @notapi
191  */
192 static inline void __dbg_object_init(system_debug_t *sdp) {
193 
194  sdp->panic_msg = NULL;
195 
196 #if CH_DBG_SYSTEM_STATE_CHECK == TRUE
197  /* The initial state is assumed to be within a critical zone.*/
198  sdp->isr_cnt = (cnt_t)0;
199  sdp->lock_cnt = (cnt_t)1;
200 #endif
201 }
202 
203 #endif /* CHDEBUG_H */
204 
205 /** @} */
__dbg_object_init
static void __dbg_object_init(system_debug_t *sdp)
Debug support initialization.
Definition: chdebug.h:192
cnt_t
int32_t cnt_t
Definition: chearly.h:92
ch_system_debug::isr_cnt
cnt_t isr_cnt
ISR nesting level.
Definition: chdebug.h:75
ch_system_debug
System debug data structure.
Definition: chdebug.h:62
system_debug_t
struct ch_system_debug system_debug_t
System debug data structure.
ch_system_debug::panic_msg
const char *volatile panic_msg
Pointer to the panic message.
Definition: chdebug.h:70
ch_system_debug::lock_cnt
cnt_t lock_cnt
Lock nesting level.
Definition: chdebug.h:79