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