Silen some warning.
[bertos.git] / bertos / cfg / debug.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \defgroup debug Debugging facilities and macros
34  * \ingroup core
35  * \{
36  *
37  * \brief Simple debug facilities for hosted and embedded C/C++ applications.
38  *
39  * Debug output goes to stderr in hosted applications.
40  * Freestanding (AKA embedded) applications use \c drv/kdebug.c to output
41  * diagnostic messages to a serial terminal or a JTAG debugger.
42  *
43  * \author Bernie Innocenti <bernie@codewiz.org>
44  *
45  * $WIZ$ module_name = "debug"
46  * $WIZ$ module_configuration = "bertos/cfg/cfg_debug.h"
47  * $WIZ$ DEBUG = 1
48  * $WIZ$ module_depends = "formatwr"
49  * $WIZ$ module_supports = "not atmega103"
50  */
51
52 #ifndef BERTOS_DEBUG_H
53 #define BERTOS_DEBUG_H
54
55 #include <cfg/os.h>
56 #include <cfg/compiler.h>
57
58 #include "cfg/cfg_debug.h"   /* CONFIG_KDEBUG_* */
59
60 /*
61  * Defaults for rarely used config stuff.
62  */
63 #ifndef CONFIG_KDEBUG_TRACE
64 #define CONFIG_KDEBUG_TRACE  1
65 #endif
66
67 #ifndef CONFIG_KDEBUG_VERBOSE_ASSERT
68 #define CONFIG_KDEBUG_VERBOSE_ASSERT  1
69 #endif
70
71 #ifndef CONFIG_KDEBUG_WALLS
72 #define CONFIG_KDEBUG_WALLS  1
73 #endif
74
75 #if defined(__doxygen__)
76         /**
77          * Preprocessor symbol defined only for debug builds.
78          *
79          * The build infrastructure must arrange for _DEBUG to
80          * be predefined for all the source files being compiled.
81          *
82          * This is compatible with the MSVC convention for the
83          * default Debug and Release project targets.
84          */
85         #define _DEBUG 1
86 #endif
87
88 #ifdef _DEBUG
89         // STLport specific: enable extra checks
90         #define __STL_DEBUG 1
91
92         // MSVC specific: Enable memory allocation debug
93         #if defined(_MSC_VER)
94                 #include <crtdbg.h>
95         #endif
96
97         /*
98          * On UNIX systems the extabilished practice is to define
99          * NDEBUG for release builds and nothing for debug builds.
100          */
101         #ifdef NDEBUG
102         #undef NDEBUG
103         #endif
104
105         /**
106          * This macro duplicates the old MSVC trick of redefining
107          * THIS_FILE locally to avoid the overhead of many duplicate
108          * strings in ASSERT().
109          */
110         #ifndef THIS_FILE
111                 #define THIS_FILE  __FILE__
112         #endif
113
114         /**
115          * This macro can be used to conditionally exclude one or more
116          * statements conditioned on \c _DEBUG, avoiding the clutter
117          * of ifdef/endif pairs.
118          *
119          * \code
120          *     struct FooBar
121          *     {
122          *         int foo;
123          *         bool bar;
124          *         DB(int ref_count;) // Track number of users
125          *
126          *         void release()
127          *         {
128          *             DB(--ref_count;)
129          *         }
130          *     };
131          * \endcode
132          */
133         #define DB(x) x
134
135         #include <cpu/attr.h>        /* CPU_HARVARD */
136
137         /* These are implemented in drv/kdebug.c */
138         void kdbg_init(void);
139         void kputchar(char c);
140         int kputnum(int num);
141         void kdump(const void *buf, size_t len);
142         void __init_wall(long *wall, int size);
143
144         #if CPU_HARVARD
145                 #include <cpu/pgm.h>
146                 void kputs_P(const char *PROGMEM str);
147                 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
148                 int __bassert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
149                 void __trace_P(const char *func);
150                 void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
151                 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
152                 int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
153                 #define kputs(str)  kputs_P(PSTR(str))
154                 #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
155                 #define __bassert(cond, file, line)  __bassert_P(PSTR(cond), PSTR(file), (line))
156                 #define __trace(func)  __trace_P(func)
157                 #define __tracemsg(func, fmt, ...)  __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
158                 #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
159                 #define __check_wall(wall, size, name, file, line)  __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
160         #else /* !CPU_HARVARD */
161                 void kputs(const char *str);
162                 void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
163                 int __bassert(const char *cond, const char *file, int line);
164                 void __trace(const char *func);
165                 void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
166                 int __invalid_ptr(void *p, const char *name, const char *file, int line);
167                 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
168         #endif /* !CPU_HARVARD */
169
170         #if CONFIG_KDEBUG_VERBOSE_ASSERT
171                 /**
172                  * Assert a pre-condition on code.
173                  */
174                 #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __bassert(#x, THIS_FILE, __LINE__)))
175                 /**
176                  * Assert a pre-condition and give explanation message when assert fails
177                  */
178                 #define ASSERT2(x, help)  ((void)(LIKELY(x) ? 0 : __bassert(help " (" #x ")", THIS_FILE, __LINE__)))
179         #else
180                 #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __bassert("", THIS_FILE, __LINE__)))
181                 #define ASSERT2(x, help)  ((void)ASSERT(x))
182         #endif
183
184         #define IS_VALID_PTR(p) (LIKELY((void *)(p) >= (void *)CPU_RAM_START))
185         /**
186          * Check that the given pointer is either NULL or pointing to valid memory.
187          *
188          * The assumption here is that valid pointers never point to low
189          * memory regions.  This helps catching pointers taken from
190          * struct/class memebers when the struct pointer was NULL.
191          *
192          * \see ASSERT_VALID_PTR_OR_NULL()
193          */
194         #define ASSERT_VALID_PTR(p) (IS_VALID_PTR(p) \
195                 ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__))
196
197         /**
198          * Check that the given pointer is not pointing to invalid memory.
199          *
200          * \note The check for invalid memory is architecture specific and
201          *       conservative.  The current implementation only checks against
202          *       a lower bound.
203          *
204          * \see ASSERT_VALID_PTR()
205          */
206         #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) \
207                 || ((void *)(p) >= (void *)CPU_RAM_START)) \
208                 ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
209
210         #if CONFIG_KDEBUG_TRACE
211                 #define TRACE  __trace(__func__)
212                 #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
213         #else
214                 #define TRACE  do {} while(0)
215                 #define TRACEMSG(...)  do {} while(0)
216         #endif
217
218         /**
219          * Check that the given pointer actually points to an object
220          * of the specified type.
221          */
222         #define ASSERT_VALID_OBJ(_t, _o) do { \
223                 ASSERT_VALID_PTR((_o)); \
224                 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
225         }
226
227         /**
228          * \name Debug object creation and destruction.
229          *
230          * These macros help track some kinds of leaks in C++ programs.
231          * Usage is as follows:
232          *
233          * \code
234          *   class Foo
235          *   {
236          *       DECLARE_INSTANCE_TRACKING(Foo)
237          *
238          *       Foo()
239          *       {
240          *           NEW_INSTANCE(Foo);
241          *           // ...
242          *       }
243          *
244          *       ~Foo()
245          *       {
246          *           DELETE_INSTANCE(Foo);
247          *           // ...
248          *       }
249          *   };
250          *
251          *   // Put this in the implementation file of the class
252          *   IMPLEMENT_INSTANCE_TRACKING(Foo)
253          *
254          *   // Client code
255          *   int main(void)
256          *   {
257          *        Foo *foo = new Foo;
258          *        cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
259          *        delete foo;
260          *        ASSERT_ZERO_INSTANCES(Foo); // OK
261          *   }
262          * \endcode
263          * \{
264          */
265         #define NEW_INSTANCE(CLASS)                do { ++CLASS::__instances } while (0)
266         #define DELETE_INSTANCE(CLASS)             do { --CLASS::__instances } while (0)
267         #define ASSERT_ZERO_INSTANCES(CLASS)       ASSERT(CLASS::__instances == 0)
268         #define GET_INSTANCE_COUNT(CLASS)          (CLASS::__instances)
269         #define DECLARE_INSTANCE_TRACKING(CLASS)   static int __instances
270         #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
271         /*\}*/
272
273 #else /* !_DEBUG */
274
275         /*
276          * On UNIX systems the extabilished practice is to define
277          * NDEBUG for release builds and nothing for debug builds.
278          */
279         #ifndef NDEBUG
280         #define NDEBUG 1
281         #endif
282
283         #define DB(x)  /* nothing */
284         #ifndef ASSERT
285                 #define ASSERT(x)  ((void)0)
286         #endif /* ASSERT */
287         #define ASSERT2(x, help)             ((void)0)
288         #define IS_VALID_PTR(p)              (1)
289         #define ASSERT_VALID_PTR(p)          ((void)0)
290         #define ASSERT_VALID_PTR_OR_NULL(p)  ((void)0)
291         #define ASSERT_VALID_OBJ(_t, _o)     ((void)0)
292         #define TRACE                        do {} while (0)
293         #if COMPILER_VARIADIC_MACROS
294                 #define TRACEMSG(x, ...)         do {} while (0)
295         #else
296                 INLINE void TRACEMSG(UNUSED_ARG(const char *, msg), ...)
297                 {
298                         /* NOP */
299                 }
300         #endif
301
302         #define NEW_INSTANCE(CLASS)                do {} while (0)
303         #define DELETE_INSTANCE(CLASS)             do {} while (0)
304         #define ASSERT_ZERO_INSTANCES(CLASS)       do {} while (0)
305         #define GET_INSTANCE_COUNT(CLASS)          ERROR_ONLY_FOR_DEBUG
306         #define DECLARE_INSTANCE_TRACKING(CLASS)
307         #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
308
309         INLINE void kdbg_init(void) { /* nop */ }
310         INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
311         INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
312         INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
313         INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
314
315         #if defined(__cplusplus) && COMPILER_VARIADIC_MACROS
316                 /* G++ can't inline functions with variable arguments... */
317                 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
318         #else
319                 /* ...but GCC can. */
320                 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
321         #endif
322
323 #endif /* _DEBUG */
324
325 #if CONFIG_KDEBUG_WALLS
326         /**
327          * \name Walls to detect data corruption
328          * \{
329          */
330         #define WALL_SIZE                    8
331         #define WALL_VALUE                   (long)0xABADCAFEL
332         #define DECLARE_WALL(name,size)      long name[(size) / sizeof(long)];
333         #define FWD_DECLARE_WALL(name,size)  extern long name[(size) / sizeof(long)];
334         #define INIT_WALL(name)              __init_wall((name), countof(name))
335         #define CHECK_WALL(name)             __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
336         /*\}*/
337 #else
338         #define DECLARE_WALL(name, size)     /* nothing */
339         #define FWD_DECLARE_WALL(name, size) /* nothing */
340         #define INIT_WALL(name)              do {} while (0)
341         #define CHECK_WALL(name)             do {} while (0)
342 #endif
343
344 /** \} */ // defgroup debug
345
346 #endif /* BERTOS_DEBUG_H */