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