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