Use debug.h instead of kdebug.h.
[bertos.git] / debug.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See devlib/README for information.
6  * -->
7  *
8  * \brief Simple debug facilities for hosted and embedded C/C++ applications
9  *
10  * Debug output goes to stderr in hosted applications.  Freestanding (AKA embedded)
11  * applications use drv/kdebug.c to output diagnostic messages to a serial terminal
12  * or a JTAG debugger.
13  *
14  * \version $Id$
15  * \author Bernardo Innocenti <bernie@develer.com>
16  */
17
18 /*#*
19  *#* $Log$
20  *#* Revision 1.1  2004/09/14 20:19:47  bernie
21  *#* Unified debug macros.
22  *#*
23  *#* Revision 1.3  2004/09/06 12:11:29  bernie
24  *#* Merge debug.h with DevLib.
25  *#*
26  *#* Revision 1.2  2004/08/13 03:22:07  bernie
27  *#* Resurrect some debug macros from old projects.
28  *#*
29  *#* Revision 1.1  2004/08/12 06:56:35  bernie
30  *#* Move debug.h from device/ to util/.
31  *#*
32  *#* Revision 1.1  2004/08/12 06:52:26  bernie
33  *#* Factor out a few simple debug macros.
34  *#*
35  *#*/
36 #ifndef DEVLIB_DEBUG_H
37 #define DEVLIB_DEBUG_H
38
39 /*
40  * Try to guess whether we're running in a hosted or embedded environment.
41  */
42 #ifndef OS_HOSTED
43         #if defined(__unix__) || defined(_WIN32)
44                 #define OS_HOSTED  1
45         #else
46                 #define OS_HOSTED  0
47         #endif
48 #endif /* !OS_HOSTED */
49
50 #if OS_HOSTED
51         /*
52          * For now, avoid dependency on compiler.h
53          */
54         #ifndef FORMAT
55         #define FORMAT(x,y,z) /* nothing */
56         #endif
57         #ifndef INLINE
58         #define INLINE static inline
59         #endif
60         #ifndef UNUSED
61         #define UNUSED(type,name) type
62         #endif
63 #else /* !OS_HOSTED */
64         #include <compiler.h>
65 #endif /* !OS_HOSTED */
66
67 /*!
68  * \def _DEBUG
69  *
70  * This preprocessor symbol is defined only in debug builds.
71  *
72  * The build infrastructure must arrange for _DEBUG to
73  * be prepredefined for all source files being compiled.
74  *
75  * This is compatible with the Microsoft convention for
76  * the default Debug and Release targets.
77  */
78 #ifdef _DEBUG
79
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          * \example
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          *
123          * \endexample
124          */
125         #define DB(x) x
126
127         #if OS_HOSTED
128                 #include <stdio.h>
129                 #include <stdarg.h>
130                 INLINE void kdbg_init(void) { /* nop */ }
131                 INLINE void kputchar(char c)
132                 {
133                         putc(c, stderr);
134                 }
135                 INLINE void kputs(const char *str)
136                 {
137                         fputs(str, stderr);
138                 }
139                 INLINE void kprintf(const char * fmt, ...) FORMAT(__printf__, 1, 2)
140                 {
141                         va_list ap;
142                         va_start(ap, fmt);
143                         vfprintf(stderr, fmt, ap);
144                         va_end(ap);
145                 }
146                 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
147
148                 #ifndef ASSERT
149                         #include <assert.h>
150                         #define ASSERT(x) assert(x)
151                 #endif /* ASSERT */
152
153                 /*!
154                  * Check that the given pointer is not NULL or pointing to raw memory.
155                  *
156                  * The assumption here is that valid pointers never point to low
157                  * memory regions.  This helps catching pointers taken from
158                  * struct/class memebers when the struct pointer was NULL.
159                  */
160                 #define ASSERT_VALID_PTR(p)  ASSERT((unsigned long)(p) > 0x200)
161
162                 #define ASSERT_VALID_PTR_OR_NULL(p)  ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
163         #else /* !OS_HOSTED */
164
165                 /* Implemented in drv/kdebug.h */
166                 void kdbg_init(void);
167                 void kputchar(char c);
168                 void kdump(const void *buf, size_t len);
169
170                 #ifdef __AVR__
171                         #include <avr/pgmspace.h>
172                         void kputs_P(const char *PROGMEM str);
173                         void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
174                         int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
175                         int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
176                         #define kputs(str)  kputs_P(PSTR(str))
177                         #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
178                         #define __assert(cond, file, line)  __assert_P(PSTR(cond), PSTR(file), (line))
179                         #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
180                 #else /* !__AVR__ */
181                         void kputs(const char *str);
182                         void kprintf(const char * fmt, ...) FORMAT(__printf__, 1, 2);
183                         int __assert(const char *cond, const char *file, int line);
184                         int __invalid_ptr(void *p, const char *name, const char *file, int line);
185                 #endif /* !__AVR__ */
186
187                 void __init_wall(long *wall, int size);
188                 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
189
190                 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
191                         #define ASSERT(x)                   ((x) ? 0 : __assert(#x, THIS_FILE, __LINE__))
192                         #define ASSERT2(x, help)            ((x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__))
193                 #else
194                         #define ASSERT(x)                   ((x) ? 0 : __assert("", THIS_FILE, __LINE__))
195                         #define ASSERT2(x, help)            ASSERT(x)
196                 #endif
197
198                 #define ASSERT_VALID_PTR(p)             (((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__))
199                 #define ASSERT_VALID_PTR_OR_NULL(p)     (((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__))
200                 #define TRACE                           kprintf("%s()\n", __FUNCTION__)
201                 #define TRACEMSG(msg,...)               kprintf("%s(): " msg "\n", __FUNCTION__, ## __VA_ARGS__)
202
203
204         #endif /* !OS_HOSTED */
205
206         /*!
207          * \name Walls to detect data corruption
208          * \{
209          */
210         #define WALL_SIZE                    8
211         #define WALL_VALUE                   (long)0xABADCAFEL
212         #define DECLARE_WALL(name,size)      long name[(size) / sizeof(long)];
213         #define FWD_DECLARE_WALL(name,size)  extern long name[(size) / sizeof(long)];
214         #define INIT_WALL(name)              __init_wall((name), countof(name))
215         #define CHECK_WALL(name)             __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
216         /*\}*/
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          * \example
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          *
263          * \end example
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 #else /* !_DEBUG */
273
274         /*
275          * On UNIX systems the extabilished practice is to define
276          * NDEBUG for release builds and nothing for debug builds.
277          */
278         #ifndef NDEBUG
279         #define NDEBUG 1
280         #endif
281
282         #define DB(x)  /* nothing */
283         #ifndef ASSERT
284                 #define ASSERT(x)  do {} while (0)
285         #endif /* ASSERT */
286         #define ASSERT2(x, help)             do {} while (0)
287         #define ASSERT_VALID_PTR(p)          do {} while (0)
288         #define ASSERT_VALID_PTR_OR_NULL(p)  do {} while (0)
289         #define ASSERT_VALID_OBJ(_t, _o)     do {} while (0)
290         #define TRACE                        do {} while (0)
291         #define TRACEMSG(x,...)              do {} while (0)
292
293         #define DECLARE_WALL(name, size)     /* nothing */
294         #define FWD_DECLARE_WALL(name, size) /* nothing */
295         #define INIT_WALL(name)              do {} while (0)
296         #define CHECK_WALL(name)             do {} while (0)
297
298         #define NEW_INSTANCE(CLASS)                do {} while (0)
299         #define DELETE_INSTANCE(CLASS)             do {} while (0)
300         #define ASSERT_ZERO_INSTANCES(CLASS)       do {} while (0)
301         #define GET_INSTANCE_COUNT(CLASS)          ERROR_ONLY_FOR_DEBUG
302         #define DECLARE_INSTANCE_TRACKING(CLASS)
303         #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
304
305         INLINE void kdbg_init(void) { /* nop */ }
306         INLINE void kputchar(UNUSED(char, c)) { /* nop */ }
307         INLINE void kputs(UNUSED(const char*, str)) { /* nop */ }
308         INLINE void kprintf(UNUSED(const char*, fmt), ...) { /* nop */ }
309
310 #endif /* _DEBUG */
311
312 #endif /* DEVLIB_DEBUG_H */