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