Depend on cfg/os.h.
[bertos.git] / cfg / debug.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib 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.
11  * Freestanding (AKA embedded) applications use \c drv/kdebug.c to output
12  * diagnostic messages to a serial terminal or a JTAG debugger.
13  *
14  * \version $Id$
15  * \author Bernardo Innocenti <bernie@develer.com>
16  */
17
18 /*#*
19  *#* $Log$
20  *#* Revision 1.7  2006/02/20 02:01:56  bernie
21  *#* Depend on cfg/os.h.
22  *#*
23  *#* Revision 1.6  2006/02/17 22:28:37  bernie
24  *#* Support TRACE() and TRACEMSG() on hosted targets.
25  *#*
26  *#* Revision 1.5  2005/11/04 16:09:03  bernie
27  *#* Doxygen workaround.
28  *#*
29  *#* Revision 1.4  2005/07/03 15:18:52  bernie
30  *#* Typo.
31  *#*
32  *#* Revision 1.3  2005/06/27 21:23:55  bernie
33  *#* Rename cfg/config.h to appconfig.h.
34  *#*
35  *#* Revision 1.2  2005/04/11 19:10:27  bernie
36  *#* Include top-level headers from cfg/ subdir.
37  *#*
38  *#* Revision 1.1  2005/04/11 19:04:13  bernie
39  *#* Move top-level headers to cfg/ subdir.
40  *#*
41  *#* Revision 1.13  2005/03/01 23:23:58  bernie
42  *#* Provide defaults for CONFIG_KDEBUG_DISABLE_TRACE and CONFIG_KDEBUG_ASSERT_NO_TEXT.
43  *#*
44  *#* Revision 1.12  2005/02/18 11:18:33  bernie
45  *#* Fixes for Harvard processors from project_ks.
46  *#*
47  *#* Revision 1.11  2005/02/16 20:29:48  bernie
48  *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
49  *#*
50  *#* Revision 1.10  2005/02/09 21:50:28  bernie
51  *#* Declare dummy ASSERT* macros as ((void)0) to work around a warning I can't remember any more.
52  *#*
53  *#* Revision 1.9  2005/01/25 08:36:40  bernie
54  *#* kputnum(): Export.
55  *#*
56  *#* Revision 1.8  2005/01/11 18:08:08  aleph
57  *#* Add empty kdump definition for debug off
58  *#*
59  *#* Revision 1.7  2004/12/31 17:43:09  bernie
60  *#* Use UNUSED_ARG instead of obsolete UNUSED macro.
61  *#*
62  *#* Revision 1.6  2004/12/08 08:52:00  bernie
63  *#* Save some more RAM on AVR.
64  *#*/
65 #ifndef DEVLIB_DEBUG_H
66 #define DEVLIB_DEBUG_H
67
68 #include <cfg/os.h>
69
70 #if OS_HOSTED
71         /*
72          * For now, avoid dependency on compiler.h
73          */
74         #ifndef FORMAT
75         #define FORMAT(x,y,z) /* nothing */
76         #endif
77         #ifndef INLINE
78         #define INLINE static inline
79         #endif
80         #ifndef UNUSED_ARG
81         #ifdef __cplusplus
82                 #define UNUSED_ARG(type,name) type
83         #else
84                 #define UNUSED_ARG(type,name) type name
85         #endif
86         #endif
87 #else /* !OS_HOSTED */
88         #include <cfg/compiler.h>
89 #endif /* !OS_HOSTED */
90
91
92 /*
93  * Defaults for rarely used config stuff.
94  */
95 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
96 #define CONFIG_KDEBUG_DISABLE_TRACE  0
97 #endif
98
99 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
100 #define CONFIG_KDEBUG_ASSERT_NO_TEXT  0
101 #endif
102
103
104 /*!
105  * \def _DEBUG
106  *
107  * This preprocessor symbol is defined only in debug builds.
108  *
109  * The build infrastructure must arrange for _DEBUG to
110  * be predefined for all source files being compiled.
111  *
112  * This is compatible with the Microsoft convention for
113  * the default Debug and Release targets.
114  */
115 #ifdef _DEBUG
116
117         // STLport specific: enable extra checks
118         #define __STL_DEBUG 1
119
120         // MSVC specific: Enable memory allocation debug
121         #if defined(_MSC_VER)
122                 #include <crtdbg.h>
123         #endif
124
125         /*
126          * On UNIX systems the extabilished practice is to define
127          * NDEBUG for release builds and nothing for debug builds.
128          */
129         #ifdef NDEBUG
130         #undef NDEBUG
131         #endif
132
133         /*!
134          * This macro duplicates the old MSVC trick of redefining
135          * THIS_FILE locally to avoid the overhead of many duplicate
136          * strings in ASSERT().
137          */
138         #ifndef THIS_FILE
139                 #define THIS_FILE  __FILE__
140         #endif
141
142         /*!
143          * This macro can be used to conditionally exclude one or more
144          * statements conditioned on \c _DEBUG, avoiding the clutter
145          * of ifdef/endif pairs.
146          *
147          * \code
148          *     struct FooBar
149          *     {
150          *         int foo;
151          *         bool bar;
152          *         DB(int ref_count;) // Track number of users
153          *
154          *         void release()
155          *         {
156          *             DB(--ref_count;)
157          *         }
158          *     };
159          * \endcode
160          */
161         #define DB(x) x
162
163         #if OS_HOSTED
164                 #include <stdio.h>
165                 #include <stdarg.h>
166
167                 INLINE void kdbg_init(void) { /* nop */ }
168                 INLINE void kputchar(char c)
169                 {
170                         putc(c, stderr);
171                 }
172                 INLINE void kputs(const char *str)
173                 {
174                         fputs(str, stderr);
175                 }
176                 /* G++ can't inline functions with variable arguments... */
177                 #define kprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
178                 #define kvprintf(fmt, ap) vfprintf(stderr, fmt, ap)
179                 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
180
181                 #ifndef ASSERT
182                         #include <assert.h>
183                         #define ASSERT(x) assert(x)
184                 #endif /* ASSERT */
185                 #define ASSERT2(x, help)  ASSERT(help && x)
186
187                 /*!
188                  * Check that the given pointer is not NULL or pointing to raw memory.
189                  *
190                  * The assumption here is that valid pointers never point to low
191                  * memory regions.  This helps catching pointers taken from
192                  * struct/class memebers when the struct pointer was NULL.
193                  */
194                 #define ASSERT_VALID_PTR(p)  ASSERT((unsigned long)(p) > 0x200)
195
196                 #define ASSERT_VALID_PTR_OR_NULL(p)  ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
197
198                 #if !CONFIG_KDEBUG_DISABLE_TRACE
199                         #define TRACE  kporintf("%s()\n", __func__)
200                         #define TRACEMSG(msg,...) kprintf("%s(): " msg, __func__, ## __VA_ARGS__)
201                 #else
202                         #define TRACE  do {} while(0)
203                         #define TRACEMSG(...)  do {} while(0)
204                 #endif
205
206         #else /* !OS_HOSTED */
207
208                 #include <appconfig.h>  /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
209                 #include <cfg/cpu.h>  /* CPU_HARVARD */
210
211                 /* These are implemented in drv/kdebug.c */
212                 void kdbg_init(void);
213                 void kputchar(char c);
214                 int kputnum(int num);
215                 void kdump(const void *buf, size_t len);
216                 void __init_wall(long *wall, int size);
217
218                 #if CPU_HARVARD
219                         #include <mware/pgm.h>
220                         void kputs_P(const char *PROGMEM str);
221                         void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
222                         int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
223                         void __trace_P(const char *func);
224                         void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
225                         int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
226                         int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
227                         #define kputs(str)  kputs_P(PSTR(str))
228                         #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
229                         #define __assert(cond, file, line)  __assert_P(PSTR(cond), PSTR(file), (line))
230                         #define __trace(func)  __trace_P(func)
231                         #define __tracemsg(func, fmt, ...)  __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
232                         #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
233                         #define __check_wall(wall, size, name, file, line)  __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
234                 #else /* !CPU_HARVARD */
235                         void kputs(const char *str);
236                         void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
237                         int __assert(const char *cond, const char *file, int line);
238                         void __trace(const char *func);
239                         void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
240                         int __invalid_ptr(void *p, const char *name, const char *file, int line);
241                         int __check_wall(long *wall, int size, const char *name, const char *file, int line);
242                 #endif /* !CPU_HARVARD */
243
244                 #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
245                         #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
246                         #define ASSERT2(x, help)  ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
247                 #else
248                         #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
249                         #define ASSERT2(x, help)  ((void)ASSERT(x))
250                 #endif
251
252                 #define ASSERT_VALID_PTR(p)         ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
253                 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
254
255                 #if !CONFIG_KDEBUG_DISABLE_TRACE
256                         #define TRACE  __trace(__func__)
257                         #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
258                 #else
259                         #define TRACE  do {} while(0)
260                         #define TRACEMSG(...)  do {} while(0)
261                 #endif
262
263         #endif /* !OS_HOSTED */
264
265         /*!
266          * \name Walls to detect data corruption
267          * \{
268          */
269         #define WALL_SIZE                    8
270         #define WALL_VALUE                   (long)0xABADCAFEL
271         #define DECLARE_WALL(name,size)      long name[(size) / sizeof(long)];
272         #define FWD_DECLARE_WALL(name,size)  extern long name[(size) / sizeof(long)];
273         #define INIT_WALL(name)              __init_wall((name), countof(name))
274         #define CHECK_WALL(name)             __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
275         /*\}*/
276
277         /*!
278          * Check that the given pointer actually points to an object
279          * of the specified type.
280          */
281         #define ASSERT_VALID_OBJ(_t, _o) do { \
282                 ASSERT_VALID_PTR((_o)); \
283                 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
284         }
285
286         /*!
287          * \name Debug object creation and destruction.
288          *
289          * These macros help track some kinds of leaks in C++ programs.
290          * Usage is as follows:
291          *
292          * \code
293          *   class Foo
294          *   {
295          *       DECLARE_INSTANCE_TRACKING(Foo)
296          *
297          *       Foo()
298          *       {
299          *           NEW_INSTANCE(Foo);
300          *           // ...
301          *       }
302          *
303          *       ~Foo()
304          *       {
305          *           DELETE_INSTANCE(Foo);
306          *           // ...
307          *       }
308          *   };
309          *
310          *   // Put this in the implementation file of the class
311          *   IMPLEMENT_INSTANCE_TRACKING(Foo)
312          *
313          *   // Client code
314          *   int main(void)
315          *   {
316          *        Foo *foo = new Foo;
317          *        cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
318          *        delete foo;
319          *        ASSERT_ZERO_INSTANCES(Foo); // OK
320          *   }
321          * \endcode
322          * \{
323          */
324         #define NEW_INSTANCE(CLASS)                do { ++CLASS::__instances } while (0)
325         #define DELETE_INSTANCE(CLASS)             do { --CLASS::__instances } while (0)
326         #define ASSERT_ZERO_INSTANCES(CLASS)       ASSERT(CLASS::__instances == 0)
327         #define GET_INSTANCE_COUNT(CLASS)          (CLASS::__instances)
328         #define DECLARE_INSTANCE_TRACKING(CLASS)   static int __instances
329         #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
330         /*\}*/
331
332 #else /* !_DEBUG */
333
334         /*
335          * On UNIX systems the extabilished practice is to define
336          * NDEBUG for release builds and nothing for debug builds.
337          */
338         #ifndef NDEBUG
339         #define NDEBUG 1
340         #endif
341
342         #define DB(x)  /* nothing */
343         #ifndef ASSERT
344                 #define ASSERT(x)  ((void)0)
345         #endif /* ASSERT */
346         #define ASSERT2(x, help)             ((void)0)
347         #define ASSERT_VALID_PTR(p)          ((void)0)
348         #define ASSERT_VALID_PTR_OR_NULL(p)  ((void)0)
349         #define ASSERT_VALID_OBJ(_t, _o)     ((void)0)
350         #define TRACE                        do {} while (0)
351         #define TRACEMSG(x,...)              do {} while (0)
352
353         #define DECLARE_WALL(name, size)     /* nothing */
354         #define FWD_DECLARE_WALL(name, size) /* nothing */
355         #define INIT_WALL(name)              do {} while (0)
356         #define CHECK_WALL(name)             do {} while (0)
357
358         #define NEW_INSTANCE(CLASS)                do {} while (0)
359         #define DELETE_INSTANCE(CLASS)             do {} while (0)
360         #define ASSERT_ZERO_INSTANCES(CLASS)       do {} while (0)
361         #define GET_INSTANCE_COUNT(CLASS)          ERROR_ONLY_FOR_DEBUG
362         #define DECLARE_INSTANCE_TRACKING(CLASS)
363         #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
364
365         INLINE void kdbg_init(void) { /* nop */ }
366         INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
367         INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
368         INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
369         INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
370
371         #ifdef __cplusplus
372                 /* G++ can't inline functions with variable arguments... */
373                 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
374         #else
375                 /* ...but GCC can. */
376                 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
377         #endif
378
379 #endif /* _DEBUG */
380
381 #endif /* DEVLIB_DEBUG_H */