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