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.12 2005/02/18 11:18:33 bernie
21 *#* Fixes for Harvard processors from project_ks.
23 *#* Revision 1.11 2005/02/16 20:29:48 bernie
24 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
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.
29 *#* Revision 1.9 2005/01/25 08:36:40 bernie
30 *#* kputnum(): Export.
32 *#* Revision 1.8 2005/01/11 18:08:08 aleph
33 *#* Add empty kdump definition for debug off
35 *#* Revision 1.7 2004/12/31 17:43:09 bernie
36 *#* Use UNUSED_ARG instead of obsolete UNUSED macro.
38 *#* Revision 1.6 2004/12/08 08:52:00 bernie
39 *#* Save some more RAM on AVR.
41 #ifndef DEVLIB_DEBUG_H
42 #define DEVLIB_DEBUG_H
45 * Try to guess whether we're running in a hosted or embedded environment.
48 #if defined(__unix__) || defined(_WIN32)
53 #endif /* !OS_HOSTED */
57 * For now, avoid dependency on compiler.h
60 #define FORMAT(x,y,z) /* nothing */
63 #define INLINE static inline
67 #define UNUSED_ARG(type,name) type
69 #define UNUSED_ARG(type,name) type name
72 #else /* !OS_HOSTED */
74 #endif /* !OS_HOSTED */
77 * This preprocessor symbol is defined only in debug builds.
79 * The build infrastructure must arrange for _DEBUG to
80 * be prepredefined for all source files being compiled.
82 * This is compatible with the Microsoft convention for
83 * the default Debug and Release targets.
87 // STLport specific: enable extra checks
90 // MSVC specific: Enable memory allocation debug
96 * On UNIX systems the extabilished practice is to define
97 * NDEBUG for release builds and nothing for debug builds.
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().
109 #define THIS_FILE __FILE__
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.
122 * DB(int ref_count;) // Track number of users
137 INLINE void kdbg_init(void) { /* nop */ }
138 INLINE void kputchar(char c)
142 INLINE void kputs(const char *str)
146 INLINE void kprintf(const char * fmt, ...) FORMAT(__printf__, 1, 2)
150 vfprintf(stderr, fmt, ap);
153 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
157 #define ASSERT(x) assert(x)
161 * Check that the given pointer is not NULL or pointing to raw memory.
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.
167 #define ASSERT_VALID_PTR(p) ASSERT((unsigned long)(p) > 0x200)
169 #define ASSERT_VALID_PTR_OR_NULL(p) ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
170 #else /* !OS_HOSTED */
172 #include <config.h> /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
173 #include <cpu.h> /* CPU_HARVARD */
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);
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 */
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__)))
212 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
213 #define ASSERT2(x, help) ((void)ASSERT(x))
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__)))
219 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
220 #define TRACE __trace(__func__)
221 #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
223 #define TRACE do {} while(0)
224 #define TRACEMSG(...) do {} while(0)
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) ((void)0)
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)
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 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 */ }
336 /* G++ can't inline functions with variable arguments... */
337 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
339 /* ...but GCC can. */
340 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
345 #endif /* DEVLIB_DEBUG_H */