debug.h

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