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