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