4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See devlib/README for information.
8 * \brief Simple debug facilities for hosted and embedded C/C++ applications.
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.
15 * \author Bernardo Innocenti <bernie@develer.com>
20 *#* Revision 1.8 2005/01/11 18:08:08 aleph
21 *#* Add empty kdump definition for debug off
23 *#* Revision 1.7 2004/12/31 17:43:09 bernie
24 *#* Use UNUSED_ARG instead of obsolete UNUSED macro.
26 *#* Revision 1.6 2004/12/08 08:52:00 bernie
27 *#* Save some more RAM on AVR.
29 *#* Revision 1.5 2004/12/08 08:04:13 bernie
32 *#* Revision 1.4 2004/12/08 07:29:27 bernie
35 *#* Revision 1.3 2004/09/20 03:31:27 bernie
38 *#* Revision 1.2 2004/09/14 21:01:46 bernie
39 *#* Mark assertions as LIKELY().
41 *#* Revision 1.1 2004/09/14 20:19:47 bernie
42 *#* Unified debug macros.
44 *#* Revision 1.3 2004/09/06 12:11:29 bernie
45 *#* Merge debug.h with DevLib.
47 *#* Revision 1.2 2004/08/13 03:22:07 bernie
48 *#* Resurrect some debug macros from old projects.
50 *#* Revision 1.1 2004/08/12 06:56:35 bernie
51 *#* Move debug.h from device/ to util/.
53 *#* Revision 1.1 2004/08/12 06:52:26 bernie
54 *#* Factor out a few simple debug macros.
57 #ifndef DEVLIB_DEBUG_H
58 #define DEVLIB_DEBUG_H
61 * Try to guess whether we're running in a hosted or embedded environment.
64 #if defined(__unix__) || defined(_WIN32)
69 #endif /* !OS_HOSTED */
73 * For now, avoid dependency on compiler.h
76 #define FORMAT(x,y,z) /* nothing */
79 #define INLINE static inline
83 #define UNUSED_ARG(type,name) type
85 #define UNUSED_ARG(type,name) type name
88 #else /* !OS_HOSTED */
91 #endif /* !OS_HOSTED */
94 * This preprocessor symbol is defined only in debug builds.
96 * The build infrastructure must arrange for _DEBUG to
97 * be prepredefined for all source files being compiled.
99 * This is compatible with the Microsoft convention for
100 * the default Debug and Release targets.
104 // STLport specific: enable extra checks
105 #define __STL_DEBUG 1
107 // MSVC specific: Enable memory allocation debug
108 #if defined(_MSC_VER)
113 * On UNIX systems the extabilished practice is to define
114 * NDEBUG for release builds and nothing for debug builds.
121 * This macro duplicates the old MSVC trick of redefining
122 * THIS_FILE locally to avoid the overhead of many duplicate
123 * strings in ASSERT().
126 #define THIS_FILE __FILE__
130 * This macro can be used to conditionally exclude one or more
131 * statements conditioned on \c _DEBUG, avoiding the clutter
132 * of #ifdef/#endif pairs.
139 * DB(int ref_count;) // Track number of users
153 INLINE void kdbg_init(void) { /* nop */ }
154 INLINE void kputchar(char c)
158 INLINE void kputs(const char *str)
162 INLINE void kprintf(const char * fmt, ...) FORMAT(__printf__, 1, 2)
166 vfprintf(stderr, fmt, ap);
169 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
173 #define ASSERT(x) assert(x)
177 * Check that the given pointer is not NULL or pointing to raw memory.
179 * The assumption here is that valid pointers never point to low
180 * memory regions. This helps catching pointers taken from
181 * struct/class memebers when the struct pointer was NULL.
183 #define ASSERT_VALID_PTR(p) ASSERT((unsigned long)(p) > 0x200)
185 #define ASSERT_VALID_PTR_OR_NULL(p) ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
186 #else /* !OS_HOSTED */
188 /* These are implemented in drv/kdebug.c */
189 void kdbg_init(void);
190 void kputchar(char c);
191 void kdump(const void *buf, size_t len);
192 void __init_wall(long *wall, int size);
195 #include <avr/pgmspace.h>
196 void kputs_P(const char *PROGMEM str);
197 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
198 int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
199 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
200 int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
201 #define kputs(str) kputs_P(PSTR(str))
202 #define kprintf(fmt, ...) kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
203 #define __assert(cond, file, line) __assert_P(PSTR(cond), PSTR(file), (line))
204 #define __invalid_ptr(p, name, file, line) __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
205 #define __check_wall(wall, size, name, file, line) __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
207 void kputs(const char *str);
208 void kprintf(const char * fmt, ...) FORMAT(__printf__, 1, 2);
209 int __assert(const char *cond, const char *file, int line);
210 int __invalid_ptr(void *p, const char *name, const char *file, int line);
211 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
212 #endif /* !__AVR__ */
214 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
215 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
216 #define ASSERT2(x, help) ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
218 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
219 #define ASSERT2(x, help) ((void)ASSERT(x))
222 #define ASSERT_VALID_PTR(p) ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
223 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
224 #define TRACE kprintf("%s()\n", __FUNCTION__)
225 #define TRACEMSG(msg,...) kprintf("%s(): " msg "\n", __FUNCTION__, ## __VA_ARGS__)
227 #endif /* !OS_HOSTED */
230 * \name Walls to detect data corruption
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__)
242 * Check that the given pointer actually points to an object
243 * of the specified type.
245 #define ASSERT_VALID_OBJ(_t, _o) do { \
246 ASSERT_VALID_PTR((_o)); \
247 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
251 * \name Debug object creation and destruction.
253 * These macros help track some kinds of leaks in C++ programs.
254 * Usage is as follows:
259 * DECLARE_INSTANCE_TRACKING(Foo)
269 * DELETE_INSTANCE(Foo);
274 * // Put this in the implementation file of the class
275 * IMPLEMENT_INSTANCE_TRACKING(Foo)
280 * Foo *foo = new Foo;
281 * cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
283 * ASSERT_ZERO_INSTANCES(Foo); // OK
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
299 * On UNIX systems the extabilished practice is to define
300 * NDEBUG for release builds and nothing for debug builds.
306 #define DB(x) /* nothing */
308 #define ASSERT(x) do {} while (0)
310 #define ASSERT2(x, help) do {} while (0)
311 #define ASSERT_VALID_PTR(p) do {} while (0)
312 #define ASSERT_VALID_PTR_OR_NULL(p) do {} while (0)
313 #define ASSERT_VALID_OBJ(_t, _o) do {} while (0)
314 #define TRACE do {} while (0)
315 #define TRACEMSG(x,...) do {} while (0)
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)
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)
329 INLINE void kdbg_init(void) { /* nop */ }
330 INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
331 INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
332 INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
335 /* G++ can't inline functions with variable arguments... */
336 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
338 /* ...but GCC can. */
339 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
344 #endif /* DEVLIB_DEBUG_H */