debug.h

Go to the documentation of this file.
00001 
00043 #ifndef DEVLIB_DEBUG_H
00044 #define DEVLIB_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 
00076     // STLport specific: enable extra checks
00077     #define __STL_DEBUG 1
00078 
00079     // MSVC specific: Enable memory allocation debug
00080     #if defined(_MSC_VER)
00081         #include <crtdbg.h>
00082     #endif
00083 
00084     /*
00085      * On UNIX systems the extabilished practice is to define
00086      * NDEBUG for release builds and nothing for debug builds.
00087      */
00088     #ifdef NDEBUG
00089     #undef NDEBUG
00090     #endif
00091 
00097     #ifndef THIS_FILE
00098         #define THIS_FILE  __FILE__
00099     #endif
00100 
00120     #define DB(x) x
00121 
00122     #include <appconfig.h>  /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
00123     #include <cpu/attr.h>  /* CPU_HARVARD */
00124 
00125     /* These are implemented in drv/kdebug.c */
00126     void kdbg_init(void);
00127     void kputchar(char c);
00128     int kputnum(int num);
00129     void kdump(const void *buf, size_t len);
00130     void __init_wall(long *wall, int size);
00131 
00132     #if CPU_HARVARD
00133         #include <mware/pgm.h>
00134         void kputs_P(const char *PROGMEM str);
00135         void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
00136         int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
00137         void __trace_P(const char *func);
00138         void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
00139         int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
00140         int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
00141         #define kputs(str)  kputs_P(PSTR(str))
00142         #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
00143         #define __assert(cond, file, line)  __assert_P(PSTR(cond), PSTR(file), (line))
00144         #define __trace(func)  __trace_P(func)
00145         #define __tracemsg(func, fmt, ...)  __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
00146         #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
00147         #define __check_wall(wall, size, name, file, line)  __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
00148     #else /* !CPU_HARVARD */
00149         void kputs(const char *str);
00150         void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
00151         int __assert(const char *cond, const char *file, int line);
00152         void __trace(const char *func);
00153         void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
00154         int __invalid_ptr(void *p, const char *name, const char *file, int line);
00155         int __check_wall(long *wall, int size, const char *name, const char *file, int line);
00156     #endif /* !CPU_HARVARD */
00157 
00158     #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
00159         #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
00160         #define ASSERT2(x, help)  ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
00161     #else
00162         #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
00163         #define ASSERT2(x, help)  ((void)ASSERT(x))
00164     #endif
00165 
00173     #define ASSERT_VALID_PTR(p)         ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
00174 
00180     #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
00181 
00182     #if !CONFIG_KDEBUG_DISABLE_TRACE
00183         #define TRACE  __trace(__func__)
00184         #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
00185     #else
00186         #define TRACE  do {} while(0)
00187         #define TRACEMSG(...)  do {} while(0)
00188     #endif
00189 
00194     #define WALL_SIZE                    8
00195     #define WALL_VALUE                   (long)0xABADCAFEL
00196     #define DECLARE_WALL(name,size)      long name[(size) / sizeof(long)];
00197     #define FWD_DECLARE_WALL(name,size)  extern long name[(size) / sizeof(long)];
00198     #define INIT_WALL(name)              __init_wall((name), countof(name))
00199     #define CHECK_WALL(name)             __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
00200     /*\}*/
00201 
00206     #define ASSERT_VALID_OBJ(_t, _o) do { \
00207         ASSERT_VALID_PTR((_o)); \
00208         ASSERT(dynamic_cast<_t>((_o)) != NULL); \
00209     }
00210 
00249     #define NEW_INSTANCE(CLASS)                do { ++CLASS::__instances } while (0)
00250     #define DELETE_INSTANCE(CLASS)             do { --CLASS::__instances } while (0)
00251     #define ASSERT_ZERO_INSTANCES(CLASS)       ASSERT(CLASS::__instances == 0)
00252     #define GET_INSTANCE_COUNT(CLASS)          (CLASS::__instances)
00253     #define DECLARE_INSTANCE_TRACKING(CLASS)   static int __instances
00254     #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
00255     /*\}*/
00256 
00257 #else /* !_DEBUG */
00258 
00259     /*
00260      * On UNIX systems the extabilished practice is to define
00261      * NDEBUG for release builds and nothing for debug builds.
00262      */
00263     #ifndef NDEBUG
00264     #define NDEBUG 1
00265     #endif
00266 
00267     #define DB(x)  /* nothing */
00268     #ifndef ASSERT
00269         #define ASSERT(x)  ((void)0)
00270     #endif /* ASSERT */
00271     #define ASSERT2(x, help)             ((void)0)
00272     #define ASSERT_VALID_PTR(p)          ((void)0)
00273     #define ASSERT_VALID_PTR_OR_NULL(p)  ((void)0)
00274     #define ASSERT_VALID_OBJ(_t, _o)     ((void)0)
00275     #define TRACE                        do {} while (0)
00276     #if COMPILER_VARIADIC_MACROS
00277         #define TRACEMSG(x, ...)         do {} while (0)
00278     #else
00279         INLINE void TRACEMSG(UNUSED_ARG(const char *, msg), ...)
00280         {
00281             /* NOP */
00282         }
00283     #endif
00284 
00285     #define DECLARE_WALL(name, size)     /* nothing */
00286     #define FWD_DECLARE_WALL(name, size) /* nothing */
00287     #define INIT_WALL(name)              do {} while (0)
00288     #define CHECK_WALL(name)             do {} while (0)
00289 
00290     #define NEW_INSTANCE(CLASS)                do {} while (0)
00291     #define DELETE_INSTANCE(CLASS)             do {} while (0)
00292     #define ASSERT_ZERO_INSTANCES(CLASS)       do {} while (0)
00293     #define GET_INSTANCE_COUNT(CLASS)          ERROR_ONLY_FOR_DEBUG
00294     #define DECLARE_INSTANCE_TRACKING(CLASS)
00295     #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
00296 
00297     INLINE void kdbg_init(void) { /* nop */ }
00298     INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
00299     INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
00300     INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
00301     INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
00302 
00303     #if defined(__cplusplus) && COMPILER_VARIADIC_MACROS
00304         /* G++ can't inline functions with variable arguments... */
00305         #define kprintf(fmt,...) do { (void)(fmt); } while(0)
00306     #else
00307         /* ...but GCC can. */
00308         INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
00309     #endif
00310 
00311 #endif /* _DEBUG */
00312 
00313 #endif /* DEVLIB_DEBUG_H */