debug.h

Go to the documentation of this file.
00001 
00043 #ifndef BERTOS_DEBUG_H
00044 #define BERTOS_DEBUG_H
00045 
00046 #include <cfg/os.h>
00047 #include <cfg/compiler.h>
00048 
00049 
00050 /*
00051  * Defaults for rarely used config stuff.
00052  */
00053 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
00054 #define CONFIG_KDEBUG_DISABLE_TRACE  0
00055 #endif
00056 
00057 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
00058 #define CONFIG_KDEBUG_ASSERT_NO_TEXT  0
00059 #endif
00060 
00061 #if defined(__doxygen__)
00062 
00071     #define _DEBUG 1
00072 #endif
00073 
00074 #ifdef _DEBUG
00075     // STLport specific: enable extra checks
00076     #define __STL_DEBUG 1
00077 
00078     // MSVC specific: Enable memory allocation debug
00079     #if defined(_MSC_VER)
00080         #include <crtdbg.h>
00081     #endif
00082 
00083     /*
00084      * On UNIX systems the extabilished practice is to define
00085      * NDEBUG for release builds and nothing for debug builds.
00086      */
00087     #ifdef NDEBUG
00088     #undef NDEBUG
00089     #endif
00090 
00096     #ifndef THIS_FILE
00097         #define THIS_FILE  __FILE__
00098     #endif
00099 
00119     #define DB(x) x
00120 
00121     #include "cfg/cfg_debug.h"   /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
00122     #include <cpu/attr.h>        /* CPU_HARVARD */
00123 
00124     /* These are implemented in drv/kdebug.c */
00125     void kdbg_init(void);
00126     void kputchar(char c);
00127     int kputnum(int num);
00128     void kdump(const void *buf, size_t len);
00129     void __init_wall(long *wall, int size);
00130 
00131     #if CPU_HARVARD
00132         #include <cpu/pgm.h>
00133         void kputs_P(const char *PROGMEM str);
00134         void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
00135         int __bassert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
00136         void __trace_P(const char *func);
00137         void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
00138         int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
00139         int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
00140         #define kputs(str)  kputs_P(PSTR(str))
00141         #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
00142         #define __bassert(cond, file, line)  __bassert_P(PSTR(cond), PSTR(file), (line))
00143         #define __trace(func)  __trace_P(func)
00144         #define __tracemsg(func, fmt, ...)  __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
00145         #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
00146         #define __check_wall(wall, size, name, file, line)  __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
00147     #else /* !CPU_HARVARD */
00148         void kputs(const char *str);
00149         void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
00150         int __bassert(const char *cond, const char *file, int line);
00151         void __trace(const char *func);
00152         void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
00153         int __invalid_ptr(void *p, const char *name, const char *file, int line);
00154         int __check_wall(long *wall, int size, const char *name, const char *file, int line);
00155     #endif /* !CPU_HARVARD */
00156 
00157     #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
00158         #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __bassert(#x, THIS_FILE, __LINE__)))
00159         #define ASSERT2(x, help)  ((void)(LIKELY(x) ? 0 : __bassert(help " (" #x ")", THIS_FILE, __LINE__)))
00160     #else
00161         #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __bassert("", THIS_FILE, __LINE__)))
00162         #define ASSERT2(x, help)  ((void)ASSERT(x))
00163     #endif
00164 
00174     #define ASSERT_VALID_PTR(p) ((void)(LIKELY((void *)(p) >= (void *)CPU_RAM_START) \
00175         ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
00176 
00186     #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) \
00187         || ((void *)(p) >= (void *)CPU_RAM_START)) \
00188         ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
00189 
00190     #if !CONFIG_KDEBUG_DISABLE_TRACE
00191         #define TRACE  __trace(__func__)
00192         #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
00193     #else
00194         #define TRACE  do {} while(0)
00195         #define TRACEMSG(...)  do {} while(0)
00196     #endif
00197 
00198 
00203     #define WALL_SIZE                    8
00204     #define WALL_VALUE                   (long)0xABADCAFEL
00205     #define DECLARE_WALL(name,size)      long name[(size) / sizeof(long)];
00206     #define FWD_DECLARE_WALL(name,size)  extern long name[(size) / sizeof(long)];
00207     #define INIT_WALL(name)              __init_wall((name), countof(name))
00208     #define CHECK_WALL(name)             __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
00209     /*\}*/
00210 
00215     #define ASSERT_VALID_OBJ(_t, _o) do { \
00216         ASSERT_VALID_PTR((_o)); \
00217         ASSERT(dynamic_cast<_t>((_o)) != NULL); \
00218     }
00219 
00258     #define NEW_INSTANCE(CLASS)                do { ++CLASS::__instances } while (0)
00259     #define DELETE_INSTANCE(CLASS)             do { --CLASS::__instances } while (0)
00260     #define ASSERT_ZERO_INSTANCES(CLASS)       ASSERT(CLASS::__instances == 0)
00261     #define GET_INSTANCE_COUNT(CLASS)          (CLASS::__instances)
00262     #define DECLARE_INSTANCE_TRACKING(CLASS)   static int __instances
00263     #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
00264     /*\}*/
00265 
00266 #else /* !_DEBUG */
00267 
00268     /*
00269      * On UNIX systems the extabilished practice is to define
00270      * NDEBUG for release builds and nothing for debug builds.
00271      */
00272     #ifndef NDEBUG
00273     #define NDEBUG 1
00274     #endif
00275 
00276     #define DB(x)  /* nothing */
00277     #ifndef ASSERT
00278         #define ASSERT(x)  ((void)0)
00279     #endif /* ASSERT */
00280     #define ASSERT2(x, help)             ((void)0)
00281     #define ASSERT_VALID_PTR(p)          ((void)0)
00282     #define ASSERT_VALID_PTR_OR_NULL(p)  ((void)0)
00283     #define ASSERT_VALID_OBJ(_t, _o)     ((void)0)
00284     #define TRACE                        do {} while (0)
00285     #if COMPILER_VARIADIC_MACROS
00286         #define TRACEMSG(x, ...)         do {} while (0)
00287     #else
00288         INLINE void TRACEMSG(UNUSED_ARG(const char *, msg), ...)
00289         {
00290             /* NOP */
00291         }
00292     #endif
00293 
00294     #define DECLARE_WALL(name, size)     /* nothing */
00295     #define FWD_DECLARE_WALL(name, size) /* nothing */
00296     #define INIT_WALL(name)              do {} while (0)
00297     #define CHECK_WALL(name)             do {} while (0)
00298 
00299     #define NEW_INSTANCE(CLASS)                do {} while (0)
00300     #define DELETE_INSTANCE(CLASS)             do {} while (0)
00301     #define ASSERT_ZERO_INSTANCES(CLASS)       do {} while (0)
00302     #define GET_INSTANCE_COUNT(CLASS)          ERROR_ONLY_FOR_DEBUG
00303     #define DECLARE_INSTANCE_TRACKING(CLASS)
00304     #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
00305 
00306     INLINE void kdbg_init(void) { /* nop */ }
00307     INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
00308     INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
00309     INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
00310     INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
00311 
00312     #if defined(__cplusplus) && COMPILER_VARIADIC_MACROS
00313         /* G++ can't inline functions with variable arguments... */
00314         #define kprintf(fmt,...) do { (void)(fmt); } while(0)
00315     #else
00316         /* ...but GCC can. */
00317         INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
00318     #endif
00319 
00320 #endif /* _DEBUG */
00321 
00322 #endif /* BERTOS_DEBUG_H */