BeRTOS
debug.h
Go to the documentation of this file.
00001 
00052 #ifndef BERTOS_DEBUG_H
00053 #define BERTOS_DEBUG_H
00054 
00055 #include <cfg/os.h>
00056 #include <cfg/compiler.h>
00057 
00058 #include "cfg/cfg_debug.h"   /* CONFIG_KDEBUG_* */
00059 
00060 /*
00061  * Defaults for rarely used config stuff.
00062  */
00063 #ifndef CONFIG_KDEBUG_TRACE
00064 #define CONFIG_KDEBUG_TRACE  1
00065 #endif
00066 
00067 #ifndef CONFIG_KDEBUG_VERBOSE_ASSERT
00068 #define CONFIG_KDEBUG_VERBOSE_ASSERT  1
00069 #endif
00070 
00071 #ifndef CONFIG_KDEBUG_WALLS
00072 #define CONFIG_KDEBUG_WALLS  1
00073 #endif
00074 
00075 #if defined(__doxygen__)
00076 
00085     #define _DEBUG 1
00086 #endif
00087 
00088 #ifdef _DEBUG
00089     // STLport specific: enable extra checks
00090     #define __STL_DEBUG 1
00091 
00092     // MSVC specific: Enable memory allocation debug
00093     #if defined(_MSC_VER)
00094         #include <crtdbg.h>
00095     #endif
00096 
00097     /*
00098      * On UNIX systems the extabilished practice is to define
00099      * NDEBUG for release builds and nothing for debug builds.
00100      */
00101     #ifdef NDEBUG
00102     #undef NDEBUG
00103     #endif
00104 
00110     #ifndef THIS_FILE
00111         #define THIS_FILE  __FILE__
00112     #endif
00113 
00133     #define DB(x) x
00134 
00135     #include <cpu/attr.h>        /* CPU_HARVARD */
00136 
00137     /* These are implemented in drv/kdebug.c */
00138     void kdbg_init(void);
00139     void kputchar(char c);
00140     int kputnum(int num);
00141     void kdump(const void *buf, size_t len);
00142     void __init_wall(long *wall, int size);
00143 
00144     #if CPU_HARVARD
00145         #include <cpu/pgm.h>
00146         void kputs_P(const char *PROGMEM str);
00147         void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
00148         int __bassert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
00149         void __trace_P(const char *func);
00150         void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
00151         int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
00152         int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
00153         #define kputs(str)  kputs_P(PSTR(str))
00154         #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
00155         #define __bassert(cond, file, line)  __bassert_P(PSTR(cond), PSTR(file), (line))
00156         #define __trace(func)  __trace_P(func)
00157         #define __tracemsg(func, fmt, ...)  __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
00158         #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
00159         #define __check_wall(wall, size, name, file, line)  __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
00160     #else /* !CPU_HARVARD */
00161         void kputs(const char *str);
00162         void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
00163         int __bassert(const char *cond, const char *file, int line);
00164         void __trace(const char *func);
00165         void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
00166         int __invalid_ptr(void *p, const char *name, const char *file, int line);
00167         int __check_wall(long *wall, int size, const char *name, const char *file, int line);
00168     #endif /* !CPU_HARVARD */
00169 
00170     #if CONFIG_KDEBUG_VERBOSE_ASSERT
00171 
00174         #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __bassert(#x, THIS_FILE, __LINE__)))
00175 
00178         #define ASSERT2(x, help)  ((void)(LIKELY(x) ? 0 : __bassert(help " (" #x ")", THIS_FILE, __LINE__)))
00179     #else
00180         #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __bassert("", THIS_FILE, __LINE__)))
00181         #define ASSERT2(x, help)  ((void)ASSERT(x))
00182     #endif
00183 
00184     #define IS_VALID_PTR(p) (LIKELY((void *)(p) >= (void *)CPU_RAM_START))
00185 
00194     #define ASSERT_VALID_PTR(p) (IS_VALID_PTR(p) \
00195         ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__))
00196 
00206     #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) \
00207         || ((void *)(p) >= (void *)CPU_RAM_START)) \
00208         ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
00209 
00210     #if CONFIG_KDEBUG_TRACE
00211         #define TRACE  __trace(__func__)
00212         #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
00213     #else
00214         #define TRACE  do {} while(0)
00215         #define TRACEMSG(...)  do {} while(0)
00216     #endif
00217 
00222     #define ASSERT_VALID_OBJ(_t, _o) do { \
00223         ASSERT_VALID_PTR((_o)); \
00224         ASSERT(dynamic_cast<_t>((_o)) != NULL); \
00225     }
00226 
00265     #define NEW_INSTANCE(CLASS)                do { ++CLASS::__instances } while (0)
00266     #define DELETE_INSTANCE(CLASS)             do { --CLASS::__instances } while (0)
00267     #define ASSERT_ZERO_INSTANCES(CLASS)       ASSERT(CLASS::__instances == 0)
00268     #define GET_INSTANCE_COUNT(CLASS)          (CLASS::__instances)
00269     #define DECLARE_INSTANCE_TRACKING(CLASS)   static int __instances
00270     #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
00271     /*\}*/
00272 
00273 #else /* !_DEBUG */
00274 
00275     /*
00276      * On UNIX systems the extabilished practice is to define
00277      * NDEBUG for release builds and nothing for debug builds.
00278      */
00279     #ifndef NDEBUG
00280     #define NDEBUG 1
00281     #endif
00282 
00283     #define DB(x)  /* nothing */
00284     #ifndef ASSERT
00285         #define ASSERT(x)  ((void)0)
00286     #endif /* ASSERT */
00287     #define ASSERT2(x, help)             ((void)0)
00288     #define IS_VALID_PTR(p)              (1)
00289     #define ASSERT_VALID_PTR(p)          ((void)0)
00290     #define ASSERT_VALID_PTR_OR_NULL(p)  ((void)0)
00291     #define ASSERT_VALID_OBJ(_t, _o)     ((void)0)
00292     #define TRACE                        do {} while (0)
00293     #if COMPILER_VARIADIC_MACROS
00294         #define TRACEMSG(x, ...)         do {} while (0)
00295     #else
00296         INLINE void TRACEMSG(UNUSED_ARG(const char *, msg), ...)
00297         {
00298             /* NOP */
00299         }
00300     #endif
00301 
00302     #define NEW_INSTANCE(CLASS)                do {} while (0)
00303     #define DELETE_INSTANCE(CLASS)             do {} while (0)
00304     #define ASSERT_ZERO_INSTANCES(CLASS)       do {} while (0)
00305     #define GET_INSTANCE_COUNT(CLASS)          ERROR_ONLY_FOR_DEBUG
00306     #define DECLARE_INSTANCE_TRACKING(CLASS)
00307     #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
00308 
00309     INLINE void kdbg_init(void) { /* nop */ }
00310     INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
00311     INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
00312     INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
00313     INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
00314 
00315     #if defined(__cplusplus) && COMPILER_VARIADIC_MACROS
00316         /* G++ can't inline functions with variable arguments... */
00317         #define kprintf(fmt,...) do { (void)(fmt); } while(0)
00318     #else
00319         /* ...but GCC can. */
00320         INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
00321     #endif
00322 
00323 #endif /* _DEBUG */
00324 
00325 #if CONFIG_KDEBUG_WALLS
00326 
00330     #define WALL_SIZE                    8
00331     #define WALL_VALUE                   (long)0xABADCAFEL
00332     #define DECLARE_WALL(name,size)      long name[(size) / sizeof(long)];
00333     #define FWD_DECLARE_WALL(name,size)  extern long name[(size) / sizeof(long)];
00334     #define INIT_WALL(name)              __init_wall((name), countof(name))
00335     #define CHECK_WALL(name)             __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
00336     /*\}*/
00337 #else
00338     #define DECLARE_WALL(name, size)     /* nothing */
00339     #define FWD_DECLARE_WALL(name, size) /* nothing */
00340     #define INIT_WALL(name)              do {} while (0)
00341     #define CHECK_WALL(name)             do {} while (0)
00342 #endif
00343  // defgroup debug
00345 
00346 #endif /* BERTOS_DEBUG_H */