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