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