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.
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.13 2005/03/01 23:23:58 bernie
21 *#* Provide defaults for CONFIG_KDEBUG_DISABLE_TRACE and CONFIG_KDEBUG_ASSERT_NO_TEXT.
23 *#* Revision 1.12 2005/02/18 11:18:33 bernie
24 *#* Fixes for Harvard processors from project_ks.
26 *#* Revision 1.11 2005/02/16 20:29:48 bernie
27 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
29 *#* Revision 1.10 2005/02/09 21:50:28 bernie
30 *#* Declare dummy ASSERT* macros as ((void)0) to work around a warning I can't remember any more.
32 *#* Revision 1.9 2005/01/25 08:36:40 bernie
33 *#* kputnum(): Export.
35 *#* Revision 1.8 2005/01/11 18:08:08 aleph
36 *#* Add empty kdump definition for debug off
38 *#* Revision 1.7 2004/12/31 17:43:09 bernie
39 *#* Use UNUSED_ARG instead of obsolete UNUSED macro.
41 *#* Revision 1.6 2004/12/08 08:52:00 bernie
42 *#* Save some more RAM on AVR.
44 #ifndef DEVLIB_DEBUG_H
45 #define DEVLIB_DEBUG_H
48 * Try to guess whether we're running in a hosted or embedded environment.
51 #if defined(__unix__) || defined(_WIN32)
56 #endif /* !OS_HOSTED */
60 * For now, avoid dependency on compiler.h
63 #define FORMAT(x,y,z) /* nothing */
66 #define INLINE static inline
70 #define UNUSED_ARG(type,name) type
72 #define UNUSED_ARG(type,name) type name
75 #else /* !OS_HOSTED */
77 #endif /* !OS_HOSTED */
81 * Defaults for rarely used config stuff.
83 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
84 #define CONFIG_KDEBUG_DISABLE_TRACE 0
87 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
88 #define CONFIG_KDEBUG_ASSERT_NO_TEXT 0
95 * This preprocessor symbol is defined only in debug builds.
97 * The build infrastructure must arrange for _DEBUG to
98 * be prepredefined for all source files being compiled.
100 * This is compatible with the Microsoft convention for
101 * the default Debug and Release targets.
105 // STLport specific: enable extra checks
106 #define __STL_DEBUG 1
108 // MSVC specific: Enable memory allocation debug
109 #if defined(_MSC_VER)
114 * On UNIX systems the extabilished practice is to define
115 * NDEBUG for release builds and nothing for debug builds.
122 * This macro duplicates the old MSVC trick of redefining
123 * THIS_FILE locally to avoid the overhead of many duplicate
124 * strings in ASSERT().
127 #define THIS_FILE __FILE__
131 * This macro can be used to conditionally exclude one or more
132 * statements conditioned on \c _DEBUG, avoiding the clutter
133 * of #ifdef/#endif pairs.
140 * DB(int ref_count;) // Track number of users
155 INLINE void kdbg_init(void) { /* nop */ }
156 INLINE void kputchar(char c)
160 INLINE void kputs(const char *str)
164 /* G++ can't inline functions with variable arguments... */
165 #define kprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
166 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
170 #define ASSERT(x) assert(x)
174 * Check that the given pointer is not NULL or pointing to raw memory.
176 * The assumption here is that valid pointers never point to low
177 * memory regions. This helps catching pointers taken from
178 * struct/class memebers when the struct pointer was NULL.
180 #define ASSERT_VALID_PTR(p) ASSERT((unsigned long)(p) > 0x200)
182 #define ASSERT_VALID_PTR_OR_NULL(p) ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
183 #else /* !OS_HOSTED */
185 #include <config.h> /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
186 #include <cpu.h> /* CPU_HARVARD */
188 /* These are implemented in drv/kdebug.c */
189 void kdbg_init(void);
190 void kputchar(char c);
191 int kputnum(int num);
192 void kdump(const void *buf, size_t len);
193 void __init_wall(long *wall, int size);
196 #include <mware/pgm.h>
197 void kputs_P(const char *PROGMEM str);
198 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
199 int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
200 void __trace_P(const char *func);
201 void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
202 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
203 int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
204 #define kputs(str) kputs_P(PSTR(str))
205 #define kprintf(fmt, ...) kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
206 #define __assert(cond, file, line) __assert_P(PSTR(cond), PSTR(file), (line))
207 #define __trace(func) __trace_P(func)
208 #define __tracemsg(func, fmt, ...) __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
209 #define __invalid_ptr(p, name, file, line) __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
210 #define __check_wall(wall, size, name, file, line) __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
211 #else /* !CPU_HARVARD */
212 void kputs(const char *str);
213 void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
214 int __assert(const char *cond, const char *file, int line);
215 void __trace(const char *func);
216 void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
217 int __invalid_ptr(void *p, const char *name, const char *file, int line);
218 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
219 #endif /* !CPU_HARVARD */
221 #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
222 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
223 #define ASSERT2(x, help) ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
225 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
226 #define ASSERT2(x, help) ((void)ASSERT(x))
229 #define ASSERT_VALID_PTR(p) ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
230 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
232 #if !CONFIG_KDEBUG_DISABLE_TRACE
233 #define TRACE __trace(__func__)
234 #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
236 #define TRACE do {} while(0)
237 #define TRACEMSG(...) do {} while(0)
240 #endif /* !OS_HOSTED */
243 * \name Walls to detect data corruption
247 #define WALL_VALUE (long)0xABADCAFEL
248 #define DECLARE_WALL(name,size) long name[(size) / sizeof(long)];
249 #define FWD_DECLARE_WALL(name,size) extern long name[(size) / sizeof(long)];
250 #define INIT_WALL(name) __init_wall((name), countof(name))
251 #define CHECK_WALL(name) __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
255 * Check that the given pointer actually points to an object
256 * of the specified type.
258 #define ASSERT_VALID_OBJ(_t, _o) do { \
259 ASSERT_VALID_PTR((_o)); \
260 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
264 * \name Debug object creation and destruction.
266 * These macros help track some kinds of leaks in C++ programs.
267 * Usage is as follows:
272 * DECLARE_INSTANCE_TRACKING(Foo)
282 * DELETE_INSTANCE(Foo);
287 * // Put this in the implementation file of the class
288 * IMPLEMENT_INSTANCE_TRACKING(Foo)
293 * Foo *foo = new Foo;
294 * cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
296 * ASSERT_ZERO_INSTANCES(Foo); // OK
301 #define NEW_INSTANCE(CLASS) do { ++CLASS::__instances } while (0)
302 #define DELETE_INSTANCE(CLASS) do { --CLASS::__instances } while (0)
303 #define ASSERT_ZERO_INSTANCES(CLASS) ASSERT(CLASS::__instances == 0)
304 #define GET_INSTANCE_COUNT(CLASS) (CLASS::__instances)
305 #define DECLARE_INSTANCE_TRACKING(CLASS) static int __instances
306 #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
312 * On UNIX systems the extabilished practice is to define
313 * NDEBUG for release builds and nothing for debug builds.
319 #define DB(x) /* nothing */
321 #define ASSERT(x) ((void)0)
323 #define ASSERT2(x, help) ((void)0)
324 #define ASSERT_VALID_PTR(p) ((void)0)
325 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)0)
326 #define ASSERT_VALID_OBJ(_t, _o) ((void)0)
327 #define TRACE do {} while (0)
328 #define TRACEMSG(x,...) do {} while (0)
330 #define DECLARE_WALL(name, size) /* nothing */
331 #define FWD_DECLARE_WALL(name, size) /* nothing */
332 #define INIT_WALL(name) do {} while (0)
333 #define CHECK_WALL(name) do {} while (0)
335 #define NEW_INSTANCE(CLASS) do {} while (0)
336 #define DELETE_INSTANCE(CLASS) do {} while (0)
337 #define ASSERT_ZERO_INSTANCES(CLASS) do {} while (0)
338 #define GET_INSTANCE_COUNT(CLASS) ERROR_ONLY_FOR_DEBUG
339 #define DECLARE_INSTANCE_TRACKING(CLASS)
340 #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
342 INLINE void kdbg_init(void) { /* nop */ }
343 INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
344 INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
345 INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
346 INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
349 /* G++ can't inline functions with variable arguments... */
350 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
352 /* ...but GCC can. */
353 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
358 #endif /* DEVLIB_DEBUG_H */