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