debug.h

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