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