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