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