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