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