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