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