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