Provide defaults for CONFIG_KDEBUG_DISABLE_TRACE and CONFIG_KDEBUG_ASSERT_NO_TEXT.
[bertos.git] / debug.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief Simple debug facilities for hosted and embedded C/C++ applications.
9  *
10  * Debug output goes to stderr in hosted applications.
11  * Freestanding (AKA embedded) applications use \c drv/kdebug.c to output
12  * diagnostic messages to a serial terminal or a JTAG debugger.
13  *
14  * \version $Id$
15  * \author Bernardo Innocenti <bernie@develer.com>
16  */
17
18 /*#*
19  *#* $Log$
20  *#* Revision 1.13  2005/03/01 23:23:58  bernie
21  *#* Provide defaults for CONFIG_KDEBUG_DISABLE_TRACE and CONFIG_KDEBUG_ASSERT_NO_TEXT.
22  *#*
23  *#* Revision 1.12  2005/02/18 11:18:33  bernie
24  *#* Fixes for Harvard processors from project_ks.
25  *#*
26  *#* Revision 1.11  2005/02/16 20:29:48  bernie
27  *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
28  *#*
29  *#* Revision 1.10  2005/02/09 21:50:28  bernie
30  *#* Declare dummy ASSERT* macros as ((void)0) to work around a warning I can't remember any more.
31  *#*
32  *#* Revision 1.9  2005/01/25 08:36:40  bernie
33  *#* kputnum(): Export.
34  *#*
35  *#* Revision 1.8  2005/01/11 18:08:08  aleph
36  *#* Add empty kdump definition for debug off
37  *#*
38  *#* Revision 1.7  2004/12/31 17:43:09  bernie
39  *#* Use UNUSED_ARG instead of obsolete UNUSED macro.
40  *#*
41  *#* Revision 1.6  2004/12/08 08:52:00  bernie
42  *#* Save some more RAM on AVR.
43  *#*/
44 #ifndef DEVLIB_DEBUG_H
45 #define DEVLIB_DEBUG_H
46
47 /*
48  * Try to guess whether we're running in a hosted or embedded environment.
49  */
50 #ifndef OS_HOSTED
51         #if defined(__unix__) || defined(_WIN32)
52                 #define OS_HOSTED  1
53         #else
54                 #define OS_HOSTED  0
55         #endif
56 #endif /* !OS_HOSTED */
57
58 #if OS_HOSTED
59         /*
60          * For now, avoid dependency on compiler.h
61          */
62         #ifndef FORMAT
63         #define FORMAT(x,y,z) /* nothing */
64         #endif
65         #ifndef INLINE
66         #define INLINE static inline
67         #endif
68         #ifndef UNUSED_ARG
69         #ifdef __cplusplus
70                 #define UNUSED_ARG(type,name) type
71         #else
72                 #define UNUSED_ARG(type,name) type name
73         #endif
74         #endif
75 #else /* !OS_HOSTED */
76         #include <compiler.h>
77 #endif /* !OS_HOSTED */
78
79
80 /*
81  * Defaults for rarely used config stuff.
82  */
83 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
84 #define CONFIG_KDEBUG_DISABLE_TRACE  0
85 #endif
86
87 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
88 #define CONFIG_KDEBUG_ASSERT_NO_TEXT  0
89 #endif
90
91
92 /*!
93  * \def _DEBUG
94  *
95  * This preprocessor symbol is defined only in debug builds.
96  *
97  * The build infrastructure must arrange for _DEBUG to
98  * be prepredefined for all source files being compiled.
99  *
100  * This is compatible with the Microsoft convention for
101  * the default Debug and Release targets.
102  */
103 #ifdef _DEBUG
104
105         // STLport specific: enable extra checks
106         #define __STL_DEBUG 1
107
108         // MSVC specific: Enable memory allocation debug
109         #if defined(_MSC_VER)
110                 #include <crtdbg.h>
111         #endif
112
113         /*
114          * On UNIX systems the extabilished practice is to define
115          * NDEBUG for release builds and nothing for debug builds.
116          */
117         #ifdef NDEBUG
118         #undef NDEBUG
119         #endif
120
121         /*!
122          * This macro duplicates the old MSVC trick of redefining
123          * THIS_FILE locally to avoid the overhead of many duplicate
124          * strings in ASSERT().
125          */
126         #ifndef THIS_FILE
127                 #define THIS_FILE  __FILE__
128         #endif
129
130         /*!
131          * This macro can be used to conditionally exclude one or more
132          * statements conditioned on \c _DEBUG, avoiding the clutter
133          * of #ifdef/#endif pairs.
134          *
135          * \code
136          *     struct FooBar
137          *     {
138          *         int foo;
139          *         bool bar;
140          *         DB(int ref_count;) // Track number of users
141          *
142          *         void release()
143          *         {
144          *             DB(--ref_count;)
145          *         }
146          *     };
147          * \endcode
148          */
149         #define DB(x) x
150
151         #if OS_HOSTED
152                 #include <stdio.h>
153                 #include <stdarg.h>
154
155                 INLINE void kdbg_init(void) { /* nop */ }
156                 INLINE void kputchar(char c)
157                 {
158                         putc(c, stderr);
159                 }
160                 INLINE void kputs(const char *str)
161                 {
162                         fputs(str, stderr);
163                 }
164                 /* G++ can't inline functions with variable arguments... */
165                 #define kprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
166                 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
167
168                 #ifndef ASSERT
169                         #include <assert.h>
170                         #define ASSERT(x) assert(x)
171                 #endif /* ASSERT */
172
173                 /*!
174                  * Check that the given pointer is not NULL or pointing to raw memory.
175                  *
176                  * The assumption here is that valid pointers never point to low
177                  * memory regions.  This helps catching pointers taken from
178                  * struct/class memebers when the struct pointer was NULL.
179                  */
180                 #define ASSERT_VALID_PTR(p)  ASSERT((unsigned long)(p) > 0x200)
181
182                 #define ASSERT_VALID_PTR_OR_NULL(p)  ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
183         #else /* !OS_HOSTED */
184
185                 #include <config.h>  /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
186                 #include <cpu.h>  /* CPU_HARVARD */
187
188                 /* These are implemented in drv/kdebug.c */
189                 void kdbg_init(void);
190                 void kputchar(char c);
191                 int kputnum(int num);
192                 void kdump(const void *buf, size_t len);
193                 void __init_wall(long *wall, int size);
194
195                 #if CPU_HARVARD
196                         #include <mware/pgm.h>
197                         void kputs_P(const char *PROGMEM str);
198                         void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
199                         int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
200                         void __trace_P(const char *func);
201                         void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
202                         int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
203                         int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
204                         #define kputs(str)  kputs_P(PSTR(str))
205                         #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
206                         #define __assert(cond, file, line)  __assert_P(PSTR(cond), PSTR(file), (line))
207                         #define __trace(func)  __trace_P(func)
208                         #define __tracemsg(func, fmt, ...)  __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
209                         #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
210                         #define __check_wall(wall, size, name, file, line)  __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
211                 #else /* !CPU_HARVARD */
212                         void kputs(const char *str);
213                         void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
214                         int __assert(const char *cond, const char *file, int line);
215                         void __trace(const char *func);
216                         void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
217                         int __invalid_ptr(void *p, const char *name, const char *file, int line);
218                         int __check_wall(long *wall, int size, const char *name, const char *file, int line);
219                 #endif /* !CPU_HARVARD */
220
221                 #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
222                         #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
223                         #define ASSERT2(x, help)  ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
224                 #else
225                         #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
226                         #define ASSERT2(x, help)  ((void)ASSERT(x))
227                 #endif
228
229                 #define ASSERT_VALID_PTR(p)         ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
230                 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
231
232                 #if !CONFIG_KDEBUG_DISABLE_TRACE
233                         #define TRACE  __trace(__func__)
234                         #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
235                 #else
236                         #define TRACE  do {} while(0)
237                         #define TRACEMSG(...)  do {} while(0)
238                 #endif
239
240         #endif /* !OS_HOSTED */
241
242         /*!
243          * \name Walls to detect data corruption
244          * \{
245          */
246         #define WALL_SIZE                    8
247         #define WALL_VALUE                   (long)0xABADCAFEL
248         #define DECLARE_WALL(name,size)      long name[(size) / sizeof(long)];
249         #define FWD_DECLARE_WALL(name,size)  extern long name[(size) / sizeof(long)];
250         #define INIT_WALL(name)              __init_wall((name), countof(name))
251         #define CHECK_WALL(name)             __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
252         /*\}*/
253
254         /*!
255          * Check that the given pointer actually points to an object
256          * of the specified type.
257          */
258         #define ASSERT_VALID_OBJ(_t, _o) do { \
259                 ASSERT_VALID_PTR((_o)); \
260                 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
261         }
262
263         /*!
264          * \name Debug object creation and destruction.
265          *
266          * These macros help track some kinds of leaks in C++ programs.
267          * Usage is as follows:
268          *
269          * \code
270          *   class Foo
271          *   {
272          *       DECLARE_INSTANCE_TRACKING(Foo)
273          *
274          *       Foo()
275          *       {
276          *           NEW_INSTANCE(Foo);
277          *           // ...
278          *       }
279          *
280          *       ~Foo()
281          *       {
282          *           DELETE_INSTANCE(Foo);
283          *           // ...
284          *       }
285          *   };
286          *
287          *   // Put this in the implementation file of the class
288          *   IMPLEMENT_INSTANCE_TRACKING(Foo)
289          *
290          *   // Client code
291          *   int main(void)
292          *   {
293          *        Foo *foo = new Foo;
294          *        cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
295          *        delete foo;
296          *        ASSERT_ZERO_INSTANCES(Foo); // OK
297          *   }
298          * \endcode
299          * \{
300          */
301         #define NEW_INSTANCE(CLASS)                do { ++CLASS::__instances } while (0)
302         #define DELETE_INSTANCE(CLASS)             do { --CLASS::__instances } while (0)
303         #define ASSERT_ZERO_INSTANCES(CLASS)       ASSERT(CLASS::__instances == 0)
304         #define GET_INSTANCE_COUNT(CLASS)          (CLASS::__instances)
305         #define DECLARE_INSTANCE_TRACKING(CLASS)   static int __instances
306         #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
307         /*\}*/
308
309 #else /* !_DEBUG */
310
311         /*
312          * On UNIX systems the extabilished practice is to define
313          * NDEBUG for release builds and nothing for debug builds.
314          */
315         #ifndef NDEBUG
316         #define NDEBUG 1
317         #endif
318
319         #define DB(x)  /* nothing */
320         #ifndef ASSERT
321                 #define ASSERT(x)  ((void)0)
322         #endif /* ASSERT */
323         #define ASSERT2(x, help)             ((void)0)
324         #define ASSERT_VALID_PTR(p)          ((void)0)
325         #define ASSERT_VALID_PTR_OR_NULL(p)  ((void)0)
326         #define ASSERT_VALID_OBJ(_t, _o)     ((void)0)
327         #define TRACE                        do {} while (0)
328         #define TRACEMSG(x,...)              do {} while (0)
329
330         #define DECLARE_WALL(name, size)     /* nothing */
331         #define FWD_DECLARE_WALL(name, size) /* nothing */
332         #define INIT_WALL(name)              do {} while (0)
333         #define CHECK_WALL(name)             do {} while (0)
334
335         #define NEW_INSTANCE(CLASS)                do {} while (0)
336         #define DELETE_INSTANCE(CLASS)             do {} while (0)
337         #define ASSERT_ZERO_INSTANCES(CLASS)       do {} while (0)
338         #define GET_INSTANCE_COUNT(CLASS)          ERROR_ONLY_FOR_DEBUG
339         #define DECLARE_INSTANCE_TRACKING(CLASS)
340         #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
341
342         INLINE void kdbg_init(void) { /* nop */ }
343         INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
344         INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
345         INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
346         INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
347
348         #ifdef __cplusplus
349                 /* G++ can't inline functions with variable arguments... */
350                 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
351         #else
352                 /* ...but GCC can. */
353                 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
354         #endif
355
356 #endif /* _DEBUG */
357
358 #endif /* DEVLIB_DEBUG_H */