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