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.2 2005/04/11 19:10:27 bernie
21 *#* Include top-level headers from cfg/ subdir.
23 *#* Revision 1.1 2005/04/11 19:04:13 bernie
24 *#* Move top-level headers to cfg/ subdir.
26 *#* Revision 1.13 2005/03/01 23:23:58 bernie
27 *#* Provide defaults for CONFIG_KDEBUG_DISABLE_TRACE and CONFIG_KDEBUG_ASSERT_NO_TEXT.
29 *#* Revision 1.12 2005/02/18 11:18:33 bernie
30 *#* Fixes for Harvard processors from project_ks.
32 *#* Revision 1.11 2005/02/16 20:29:48 bernie
33 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
35 *#* Revision 1.10 2005/02/09 21:50:28 bernie
36 *#* Declare dummy ASSERT* macros as ((void)0) to work around a warning I can't remember any more.
38 *#* Revision 1.9 2005/01/25 08:36:40 bernie
39 *#* kputnum(): Export.
41 *#* Revision 1.8 2005/01/11 18:08:08 aleph
42 *#* Add empty kdump definition for debug off
44 *#* Revision 1.7 2004/12/31 17:43:09 bernie
45 *#* Use UNUSED_ARG instead of obsolete UNUSED macro.
47 *#* Revision 1.6 2004/12/08 08:52:00 bernie
48 *#* Save some more RAM on AVR.
50 #ifndef DEVLIB_DEBUG_H
51 #define DEVLIB_DEBUG_H
54 * Try to guess whether we're running in a hosted or embedded environment.
57 #if defined(__unix__) || defined(_WIN32)
62 #endif /* !OS_HOSTED */
66 * For now, avoid dependency on compiler.h
69 #define FORMAT(x,y,z) /* nothing */
72 #define INLINE static inline
76 #define UNUSED_ARG(type,name) type
78 #define UNUSED_ARG(type,name) type name
81 #else /* !OS_HOSTED */
82 #include <cfg/compiler.h>
83 #endif /* !OS_HOSTED */
87 * Defaults for rarely used config stuff.
89 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
90 #define CONFIG_KDEBUG_DISABLE_TRACE 0
93 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
94 #define CONFIG_KDEBUG_ASSERT_NO_TEXT 0
101 * This preprocessor symbol is defined only in debug builds.
103 * The build infrastructure must arrange for _DEBUG to
104 * be prepredefined for all source files being compiled.
106 * This is compatible with the Microsoft convention for
107 * the default Debug and Release targets.
111 // STLport specific: enable extra checks
112 #define __STL_DEBUG 1
114 // MSVC specific: Enable memory allocation debug
115 #if defined(_MSC_VER)
120 * On UNIX systems the extabilished practice is to define
121 * NDEBUG for release builds and nothing for debug builds.
128 * This macro duplicates the old MSVC trick of redefining
129 * THIS_FILE locally to avoid the overhead of many duplicate
130 * strings in ASSERT().
133 #define THIS_FILE __FILE__
137 * This macro can be used to conditionally exclude one or more
138 * statements conditioned on \c _DEBUG, avoiding the clutter
139 * of #ifdef/#endif pairs.
146 * DB(int ref_count;) // Track number of users
161 INLINE void kdbg_init(void) { /* nop */ }
162 INLINE void kputchar(char c)
166 INLINE void kputs(const char *str)
170 /* G++ can't inline functions with variable arguments... */
171 #define kprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
172 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
176 #define ASSERT(x) assert(x)
180 * Check that the given pointer is not NULL or pointing to raw memory.
182 * The assumption here is that valid pointers never point to low
183 * memory regions. This helps catching pointers taken from
184 * struct/class memebers when the struct pointer was NULL.
186 #define ASSERT_VALID_PTR(p) ASSERT((unsigned long)(p) > 0x200)
188 #define ASSERT_VALID_PTR_OR_NULL(p) ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
189 #else /* !OS_HOSTED */
191 #include <cfg/config.h> /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
192 #include <cfg/cpu.h> /* CPU_HARVARD */
194 /* These are implemented in drv/kdebug.c */
195 void kdbg_init(void);
196 void kputchar(char c);
197 int kputnum(int num);
198 void kdump(const void *buf, size_t len);
199 void __init_wall(long *wall, int size);
202 #include <mware/pgm.h>
203 void kputs_P(const char *PROGMEM str);
204 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
205 int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
206 void __trace_P(const char *func);
207 void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
208 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
209 int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
210 #define kputs(str) kputs_P(PSTR(str))
211 #define kprintf(fmt, ...) kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
212 #define __assert(cond, file, line) __assert_P(PSTR(cond), PSTR(file), (line))
213 #define __trace(func) __trace_P(func)
214 #define __tracemsg(func, fmt, ...) __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
215 #define __invalid_ptr(p, name, file, line) __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
216 #define __check_wall(wall, size, name, file, line) __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
217 #else /* !CPU_HARVARD */
218 void kputs(const char *str);
219 void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
220 int __assert(const char *cond, const char *file, int line);
221 void __trace(const char *func);
222 void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
223 int __invalid_ptr(void *p, const char *name, const char *file, int line);
224 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
225 #endif /* !CPU_HARVARD */
227 #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
228 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
229 #define ASSERT2(x, help) ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
231 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
232 #define ASSERT2(x, help) ((void)ASSERT(x))
235 #define ASSERT_VALID_PTR(p) ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
236 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
238 #if !CONFIG_KDEBUG_DISABLE_TRACE
239 #define TRACE __trace(__func__)
240 #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
242 #define TRACE do {} while(0)
243 #define TRACEMSG(...) do {} while(0)
246 #endif /* !OS_HOSTED */
249 * \name Walls to detect data corruption
253 #define WALL_VALUE (long)0xABADCAFEL
254 #define DECLARE_WALL(name,size) long name[(size) / sizeof(long)];
255 #define FWD_DECLARE_WALL(name,size) extern long name[(size) / sizeof(long)];
256 #define INIT_WALL(name) __init_wall((name), countof(name))
257 #define CHECK_WALL(name) __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
261 * Check that the given pointer actually points to an object
262 * of the specified type.
264 #define ASSERT_VALID_OBJ(_t, _o) do { \
265 ASSERT_VALID_PTR((_o)); \
266 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
270 * \name Debug object creation and destruction.
272 * These macros help track some kinds of leaks in C++ programs.
273 * Usage is as follows:
278 * DECLARE_INSTANCE_TRACKING(Foo)
288 * DELETE_INSTANCE(Foo);
293 * // Put this in the implementation file of the class
294 * IMPLEMENT_INSTANCE_TRACKING(Foo)
299 * Foo *foo = new Foo;
300 * cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
302 * ASSERT_ZERO_INSTANCES(Foo); // OK
307 #define NEW_INSTANCE(CLASS) do { ++CLASS::__instances } while (0)
308 #define DELETE_INSTANCE(CLASS) do { --CLASS::__instances } while (0)
309 #define ASSERT_ZERO_INSTANCES(CLASS) ASSERT(CLASS::__instances == 0)
310 #define GET_INSTANCE_COUNT(CLASS) (CLASS::__instances)
311 #define DECLARE_INSTANCE_TRACKING(CLASS) static int __instances
312 #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
318 * On UNIX systems the extabilished practice is to define
319 * NDEBUG for release builds and nothing for debug builds.
325 #define DB(x) /* nothing */
327 #define ASSERT(x) ((void)0)
329 #define ASSERT2(x, help) ((void)0)
330 #define ASSERT_VALID_PTR(p) ((void)0)
331 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)0)
332 #define ASSERT_VALID_OBJ(_t, _o) ((void)0)
333 #define TRACE do {} while (0)
334 #define TRACEMSG(x,...) do {} while (0)
336 #define DECLARE_WALL(name, size) /* nothing */
337 #define FWD_DECLARE_WALL(name, size) /* nothing */
338 #define INIT_WALL(name) do {} while (0)
339 #define CHECK_WALL(name) do {} while (0)
341 #define NEW_INSTANCE(CLASS) do {} while (0)
342 #define DELETE_INSTANCE(CLASS) do {} while (0)
343 #define ASSERT_ZERO_INSTANCES(CLASS) do {} while (0)
344 #define GET_INSTANCE_COUNT(CLASS) ERROR_ONLY_FOR_DEBUG
345 #define DECLARE_INSTANCE_TRACKING(CLASS)
346 #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
348 INLINE void kdbg_init(void) { /* nop */ }
349 INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
350 INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
351 INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
352 INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
355 /* G++ can't inline functions with variable arguments... */
356 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
358 /* ...but GCC can. */
359 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
364 #endif /* DEVLIB_DEBUG_H */