ChibiOS/RT
2.5.1
chdebug.h
Go to the documentation of this file.
00001 /*
00002     ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
00003                  2011,2012 Giovanni Di Sirio.
00004 
00005     This file is part of ChibiOS/RT.
00006 
00007     ChibiOS/RT is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 3 of the License, or
00010     (at your option) any later version.
00011 
00012     ChibiOS/RT is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 */
00020 
00021 /**
00022  * @file    chdebug.h
00023  * @brief   Debug macros and structures.
00024  *
00025  * @addtogroup debug
00026  * @{
00027  */
00028 
00029 #ifndef _CHDEBUG_H_
00030 #define _CHDEBUG_H_
00031 
00032 #if CH_DBG_ENABLE_ASSERTS     || CH_DBG_ENABLE_CHECKS      ||               \
00033     CH_DBG_ENABLE_STACK_CHECK || CH_DBG_SYSTEM_STATE_CHECK
00034 #define CH_DBG_ENABLED              TRUE
00035 #else
00036 #define CH_DBG_ENABLED              FALSE
00037 #endif
00038 
00039 #define __QUOTE_THIS(p) #p
00040 
00041 /*===========================================================================*/
00042 /**
00043  * @name    Debug related settings
00044  * @{
00045  */
00046 /*===========================================================================*/
00047 
00048 /**
00049  * @brief   Trace buffer entries.
00050  */
00051 #ifndef CH_TRACE_BUFFER_SIZE
00052 #define CH_TRACE_BUFFER_SIZE        64
00053 #endif
00054 
00055 /**
00056  * @brief   Fill value for thread stack area in debug mode.
00057  */
00058 #ifndef CH_STACK_FILL_VALUE
00059 #define CH_STACK_FILL_VALUE         0x55
00060 #endif
00061 
00062 /**
00063  * @brief   Fill value for thread area in debug mode.
00064  * @note    The chosen default value is 0xFF in order to make evident which
00065  *          thread fields were not initialized when inspecting the memory with
00066  *          a debugger. A uninitialized field is not an error in itself but it
00067  *          better to know it.
00068  */
00069 #ifndef CH_THREAD_FILL_VALUE
00070 #define CH_THREAD_FILL_VALUE        0xFF
00071 #endif
00072 
00073 /** @} */
00074 
00075 /*===========================================================================*/
00076 /* System state checker related code and variables.                          */
00077 /*===========================================================================*/
00078 
00079 #if !CH_DBG_SYSTEM_STATE_CHECK
00080 #define dbg_enter_lock()
00081 #define dbg_leave_lock()
00082 #define dbg_check_disable()
00083 #define dbg_check_suspend()
00084 #define dbg_check_enable()
00085 #define dbg_check_lock()
00086 #define dbg_check_unlock()
00087 #define dbg_check_lock_from_isr()
00088 #define dbg_check_unlock_from_isr()
00089 #define dbg_check_enter_isr()
00090 #define dbg_check_leave_isr()
00091 #define chDbgCheckClassI();
00092 #define chDbgCheckClassS();
00093 #else
00094 #define dbg_enter_lock() (dbg_lock_cnt = 1)
00095 #define dbg_leave_lock() (dbg_lock_cnt = 0)
00096 #endif
00097 
00098 /*===========================================================================*/
00099 /* Trace related structures and macros.                                      */
00100 /*===========================================================================*/
00101 
00102 #if CH_DBG_ENABLE_TRACE || defined(__DOXYGEN__)
00103 /**
00104  * @brief   Trace buffer record.
00105  */
00106 typedef struct {
00107   systime_t             se_time;    /**< @brief Time of the switch event.   */
00108   Thread                *se_tp;     /**< @brief Switched in thread.         */
00109   void                  *se_wtobjp; /**< @brief Object where going to sleep.*/
00110   uint8_t               se_state;   /**< @brief Switched out thread state.  */
00111 } ch_swc_event_t;
00112 
00113 /**
00114  * @brief   Trace buffer header.
00115  */
00116 typedef struct {
00117   unsigned              tb_size;    /**< @brief Trace buffer size (entries).*/
00118   ch_swc_event_t        *tb_ptr;    /**< @brief Pointer to the buffer front.*/
00119   /** @brief Ring buffer.*/
00120   ch_swc_event_t        tb_buffer[CH_TRACE_BUFFER_SIZE];
00121 } ch_trace_buffer_t;
00122 
00123 #if !defined(__DOXYGEN__)
00124 extern ch_trace_buffer_t dbg_trace_buffer;
00125 #endif
00126 
00127 #endif /* CH_DBG_ENABLE_TRACE */
00128 
00129 #if !CH_DBG_ENABLE_TRACE
00130 /* When the trace feature is disabled this function is replaced by an empty
00131    macro.*/
00132 #define dbg_trace(otp)
00133 #endif
00134 
00135 /*===========================================================================*/
00136 /* Parameters checking related macros.                                       */
00137 /*===========================================================================*/
00138 
00139 #if CH_DBG_ENABLE_CHECKS || defined(__DOXYGEN__)
00140 /**
00141  * @name    Macro Functions
00142  * @{
00143  */
00144 /**
00145  * @brief   Function parameter check.
00146  * @details If the condition check fails then the kernel panics and halts.
00147  * @note    The condition is tested only if the @p CH_DBG_ENABLE_CHECKS switch
00148  *          is specified in @p chconf.h else the macro does nothing.
00149  *
00150  * @param[in] c         the condition to be verified to be true
00151  * @param[in] func      the undecorated function name
00152  *
00153  * @api
00154  */
00155 #if !defined(chDbgCheck)
00156 #define chDbgCheck(c, func) {                                               \
00157   if (!(c))                                                                 \
00158     chDbgPanic(__QUOTE_THIS(func)"()");                                     \
00159 }
00160 #endif /* !defined(chDbgCheck) */
00161 /** @} */
00162 #else /* !CH_DBG_ENABLE_CHECKS */
00163 #define chDbgCheck(c, func) {                                               \
00164   (void)(c), (void)__QUOTE_THIS(func)"()";                                  \
00165 }
00166 #endif /* !CH_DBG_ENABLE_CHECKS */
00167 
00168 /*===========================================================================*/
00169 /* Assertions related macros.                                                */
00170 /*===========================================================================*/
00171 
00172 #if CH_DBG_ENABLE_ASSERTS || defined(__DOXYGEN__)
00173 /**
00174  * @name    Macro Functions
00175  * @{
00176  */
00177 /**
00178  * @brief   Condition assertion.
00179  * @details If the condition check fails then the kernel panics with the
00180  *          specified message and halts.
00181  * @note    The condition is tested only if the @p CH_DBG_ENABLE_ASSERTS switch
00182  *          is specified in @p chconf.h else the macro does nothing.
00183  * @note    The convention for the message is the following:<br>
00184  *          @<function_name@>(), #@<assert_number@>
00185  * @note    The remark string is not currently used except for putting a
00186  *          comment in the code about the assertion.
00187  *
00188  * @param[in] c         the condition to be verified to be true
00189  * @param[in] m         the text message
00190  * @param[in] r         a remark string
00191  *
00192  * @api
00193  */
00194 #if !defined(chDbgAssert)
00195 #define chDbgAssert(c, m, r) {                                              \
00196   if (!(c))                                                                 \
00197     chDbgPanic(m);                                                          \
00198 }
00199 #endif /* !defined(chDbgAssert) */
00200 /** @} */
00201 #else /* !CH_DBG_ENABLE_ASSERTS */
00202 #define chDbgAssert(c, m, r) {(void)(c);}
00203 #endif /* !CH_DBG_ENABLE_ASSERTS */
00204 
00205 /*===========================================================================*/
00206 /* Panic related macros.                                                     */
00207 /*===========================================================================*/
00208 
00209 #if !CH_DBG_ENABLED
00210 /* When the debug features are disabled this function is replaced by an empty
00211    macro.*/
00212 #define chDbgPanic(msg) {}
00213 #endif
00214 
00215 #ifdef __cplusplus
00216 extern "C" {
00217 #endif
00218 #if CH_DBG_SYSTEM_STATE_CHECK
00219   extern cnt_t dbg_isr_cnt;
00220   extern cnt_t dbg_lock_cnt;
00221   void dbg_check_disable(void);
00222   void dbg_check_suspend(void);
00223   void dbg_check_enable(void);
00224   void dbg_check_lock(void);
00225   void dbg_check_unlock(void);
00226   void dbg_check_lock_from_isr(void);
00227   void dbg_check_unlock_from_isr(void);
00228   void dbg_check_enter_isr(void);
00229   void dbg_check_leave_isr(void);
00230   void chDbgCheckClassI(void);
00231   void chDbgCheckClassS(void);
00232 #endif
00233 #if CH_DBG_ENABLE_TRACE || defined(__DOXYGEN__)
00234   void _trace_init(void);
00235   void dbg_trace(Thread *otp);
00236 #endif
00237 #if CH_DBG_ENABLED
00238   extern const char *dbg_panic_msg;
00239   void chDbgPanic(const char *msg);
00240 #endif
00241 #ifdef __cplusplus
00242 }
00243 #endif
00244 
00245 #endif /* _CHDEBUG_H_ */
00246 
00247 /** @} */