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