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