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