Add some modules.
[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  * \brief Simple debug facilities for hosted and embedded C/C++ applications.
34  *
35  * Debug output goes to stderr in hosted applications.
36  * Freestanding (AKA embedded) applications use \c drv/kdebug.c to output
37  * diagnostic messages to a serial terminal or a JTAG debugger.
38  *
39  * \version $Id$
40  * \author Bernie Innocenti <bernie@codewiz.org>
41  *
42  * $WIZ$ module_name = "debug"
43  * $WIZ$ module_configuration = "bertos/cfg/cfg_debug.h"
44  * $WIZ$ DEBUG = 1
45  * $WIZ$ module_depends = "formatwr"
46  */
47
48 #ifndef BERTOS_DEBUG_H
49 #define BERTOS_DEBUG_H
50
51 #include <cfg/os.h>
52 #include <cfg/compiler.h>
53
54
55 /*
56  * Defaults for rarely used config stuff.
57  */
58 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
59 #define CONFIG_KDEBUG_DISABLE_TRACE  0
60 #endif
61
62 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
63 #define CONFIG_KDEBUG_ASSERT_NO_TEXT  0
64 #endif
65
66 #if defined(__doxygen__)
67         /**
68          * Preprocessor symbol defined only for debug builds.
69          *
70          * The build infrastructure must arrange for _DEBUG to
71          * be predefined for all the source files being compiled.
72          *
73          * This is compatible with the MSVC convention for the
74          * default Debug and Release project targets.
75          */
76         #define _DEBUG 1
77 #endif
78
79 #ifdef _DEBUG
80         // STLport specific: enable extra checks
81         #define __STL_DEBUG 1
82
83         // MSVC specific: Enable memory allocation debug
84         #if defined(_MSC_VER)
85                 #include <crtdbg.h>
86         #endif
87
88         /*
89          * On UNIX systems the extabilished practice is to define
90          * NDEBUG for release builds and nothing for debug builds.
91          */
92         #ifdef NDEBUG
93         #undef NDEBUG
94         #endif
95
96         /**
97          * This macro duplicates the old MSVC trick of redefining
98          * THIS_FILE locally to avoid the overhead of many duplicate
99          * strings in ASSERT().
100          */
101         #ifndef THIS_FILE
102                 #define THIS_FILE  __FILE__
103         #endif
104
105         /**
106          * This macro can be used to conditionally exclude one or more
107          * statements conditioned on \c _DEBUG, avoiding the clutter
108          * of ifdef/endif pairs.
109          *
110          * \code
111          *     struct FooBar
112          *     {
113          *         int foo;
114          *         bool bar;
115          *         DB(int ref_count;) // Track number of users
116          *
117          *         void release()
118          *         {
119          *             DB(--ref_count;)
120          *         }
121          *     };
122          * \endcode
123          */
124         #define DB(x) x
125
126         #include "cfg/cfg_debug.h"   /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
127         #include <cpu/attr.h>        /* CPU_HARVARD */
128
129         /* These are implemented in drv/kdebug.c */
130         void kdbg_init(void);
131         void kputchar(char c);
132         int kputnum(int num);
133         void kdump(const void *buf, size_t len);
134         void __init_wall(long *wall, int size);
135
136         #if CPU_HARVARD
137                 #include <cpu/pgm.h>
138                 void kputs_P(const char *PROGMEM str);
139                 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
140                 int __bassert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
141                 void __trace_P(const char *func);
142                 void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
143                 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
144                 int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
145                 #define kputs(str)  kputs_P(PSTR(str))
146                 #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
147                 #define __bassert(cond, file, line)  __bassert_P(PSTR(cond), PSTR(file), (line))
148                 #define __trace(func)  __trace_P(func)
149                 #define __tracemsg(func, fmt, ...)  __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
150                 #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
151                 #define __check_wall(wall, size, name, file, line)  __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
152         #else /* !CPU_HARVARD */
153                 void kputs(const char *str);
154                 void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
155                 int __bassert(const char *cond, const char *file, int line);
156                 void __trace(const char *func);
157                 void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
158                 int __invalid_ptr(void *p, const char *name, const char *file, int line);
159                 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
160         #endif /* !CPU_HARVARD */
161
162         #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
163                 #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __bassert(#x, THIS_FILE, __LINE__)))
164                 #define ASSERT2(x, help)  ((void)(LIKELY(x) ? 0 : __bassert(help " (" #x ")", THIS_FILE, __LINE__)))
165         #else
166                 #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __bassert("", THIS_FILE, __LINE__)))
167                 #define ASSERT2(x, help)  ((void)ASSERT(x))
168         #endif
169
170         /**
171          * Check that the given pointer is either NULL or pointing to valid memory.
172          *
173          * The assumption here is that valid pointers never point to low
174          * memory regions.  This helps catching pointers taken from
175          * struct/class memebers when the struct pointer was NULL.
176          *
177          * \see ASSERT_VALID_PTR_OR_NULL()
178          */
179         #define ASSERT_VALID_PTR(p) ((void)(LIKELY((void *)(p) >= (void *)CPU_RAM_START) \
180                 ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
181
182         /**
183          * Check that the given pointer is not pointing to invalid memory.
184          *
185          * \note The check for invalid memory is architecture specific and
186          *       conservative.  The current implementation only checks against
187          *       a lower bound.
188          *
189          * \see ASSERT_VALID_PTR()
190          */
191         #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) \
192                 || ((void *)(p) >= (void *)CPU_RAM_START)) \
193                 ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
194
195         #if !CONFIG_KDEBUG_DISABLE_TRACE
196                 #define TRACE  __trace(__func__)
197                 #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
198         #else
199                 #define TRACE  do {} while(0)
200                 #define TRACEMSG(...)  do {} while(0)
201         #endif
202
203
204         /**
205          * \name Walls to detect data corruption
206          * \{
207          */
208         #define WALL_SIZE                    8
209         #define WALL_VALUE                   (long)0xABADCAFEL
210         #define DECLARE_WALL(name,size)      long name[(size) / sizeof(long)];
211         #define FWD_DECLARE_WALL(name,size)  extern long name[(size) / sizeof(long)];
212         #define INIT_WALL(name)              __init_wall((name), countof(name))
213         #define CHECK_WALL(name)             __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
214         /*\}*/
215
216         /**
217          * Check that the given pointer actually points to an object
218          * of the specified type.
219          */
220         #define ASSERT_VALID_OBJ(_t, _o) do { \
221                 ASSERT_VALID_PTR((_o)); \
222                 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
223         }
224
225         /**
226          * \name Debug object creation and destruction.
227          *
228          * These macros help track some kinds of leaks in C++ programs.
229          * Usage is as follows:
230          *
231          * \code
232          *   class Foo
233          *   {
234          *       DECLARE_INSTANCE_TRACKING(Foo)
235          *
236          *       Foo()
237          *       {
238          *           NEW_INSTANCE(Foo);
239          *           // ...
240          *       }
241          *
242          *       ~Foo()
243          *       {
244          *           DELETE_INSTANCE(Foo);
245          *           // ...
246          *       }
247          *   };
248          *
249          *   // Put this in the implementation file of the class
250          *   IMPLEMENT_INSTANCE_TRACKING(Foo)
251          *
252          *   // Client code
253          *   int main(void)
254          *   {
255          *        Foo *foo = new Foo;
256          *        cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
257          *        delete foo;
258          *        ASSERT_ZERO_INSTANCES(Foo); // OK
259          *   }
260          * \endcode
261          * \{
262          */
263         #define NEW_INSTANCE(CLASS)                do { ++CLASS::__instances } while (0)
264         #define DELETE_INSTANCE(CLASS)             do { --CLASS::__instances } while (0)
265         #define ASSERT_ZERO_INSTANCES(CLASS)       ASSERT(CLASS::__instances == 0)
266         #define GET_INSTANCE_COUNT(CLASS)          (CLASS::__instances)
267         #define DECLARE_INSTANCE_TRACKING(CLASS)   static int __instances
268         #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
269         /*\}*/
270
271 #else /* !_DEBUG */
272
273         /*
274          * On UNIX systems the extabilished practice is to define
275          * NDEBUG for release builds and nothing for debug builds.
276          */
277         #ifndef NDEBUG
278         #define NDEBUG 1
279         #endif
280
281         #define DB(x)  /* nothing */
282         #ifndef ASSERT
283                 #define ASSERT(x)  ((void)0)
284         #endif /* ASSERT */
285         #define ASSERT2(x, help)             ((void)0)
286         #define ASSERT_VALID_PTR(p)          ((void)0)
287         #define ASSERT_VALID_PTR_OR_NULL(p)  ((void)0)
288         #define ASSERT_VALID_OBJ(_t, _o)     ((void)0)
289         #define TRACE                        do {} while (0)
290         #if COMPILER_VARIADIC_MACROS
291                 #define TRACEMSG(x, ...)         do {} while (0)
292         #else
293                 INLINE void TRACEMSG(UNUSED_ARG(const char *, msg), ...)
294                 {
295                         /* NOP */
296                 }
297         #endif
298
299         #define DECLARE_WALL(name, size)     /* nothing */
300         #define FWD_DECLARE_WALL(name, size) /* nothing */
301         #define INIT_WALL(name)              do {} while (0)
302         #define CHECK_WALL(name)             do {} while (0)
303
304         #define NEW_INSTANCE(CLASS)                do {} while (0)
305         #define DELETE_INSTANCE(CLASS)             do {} while (0)
306         #define ASSERT_ZERO_INSTANCES(CLASS)       do {} while (0)
307         #define GET_INSTANCE_COUNT(CLASS)          ERROR_ONLY_FOR_DEBUG
308         #define DECLARE_INSTANCE_TRACKING(CLASS)
309         #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
310
311         INLINE void kdbg_init(void) { /* nop */ }
312         INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
313         INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
314         INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
315         INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
316
317         #if defined(__cplusplus) && COMPILER_VARIADIC_MACROS
318                 /* G++ can't inline functions with variable arguments... */
319                 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
320         #else
321                 /* ...but GCC can. */
322                 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
323         #endif
324
325 #endif /* _DEBUG */
326
327 #endif /* BERTOS_DEBUG_H */