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