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