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.4 2005/07/03 15:18:52 bernie
23 *#* Revision 1.3 2005/06/27 21:23:55 bernie
24 *#* Rename cfg/config.h to appconfig.h.
26 *#* Revision 1.2 2005/04/11 19:10:27 bernie
27 *#* Include top-level headers from cfg/ subdir.
29 *#* Revision 1.1 2005/04/11 19:04:13 bernie
30 *#* Move top-level headers to cfg/ subdir.
32 *#* Revision 1.13 2005/03/01 23:23:58 bernie
33 *#* Provide defaults for CONFIG_KDEBUG_DISABLE_TRACE and CONFIG_KDEBUG_ASSERT_NO_TEXT.
35 *#* Revision 1.12 2005/02/18 11:18:33 bernie
36 *#* Fixes for Harvard processors from project_ks.
38 *#* Revision 1.11 2005/02/16 20:29:48 bernie
39 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
41 *#* Revision 1.10 2005/02/09 21:50:28 bernie
42 *#* Declare dummy ASSERT* macros as ((void)0) to work around a warning I can't remember any more.
44 *#* Revision 1.9 2005/01/25 08:36:40 bernie
45 *#* kputnum(): Export.
47 *#* Revision 1.8 2005/01/11 18:08:08 aleph
48 *#* Add empty kdump definition for debug off
50 *#* Revision 1.7 2004/12/31 17:43:09 bernie
51 *#* Use UNUSED_ARG instead of obsolete UNUSED macro.
53 *#* Revision 1.6 2004/12/08 08:52:00 bernie
54 *#* Save some more RAM on AVR.
56 #ifndef DEVLIB_DEBUG_H
57 #define DEVLIB_DEBUG_H
60 * Try to guess whether we're running in a hosted or embedded environment.
63 #if defined(__unix__) || defined(_WIN32)
68 #endif /* !OS_HOSTED */
72 * For now, avoid dependency on compiler.h
75 #define FORMAT(x,y,z) /* nothing */
78 #define INLINE static inline
82 #define UNUSED_ARG(type,name) type
84 #define UNUSED_ARG(type,name) type name
87 #else /* !OS_HOSTED */
88 #include <cfg/compiler.h>
89 #endif /* !OS_HOSTED */
93 * Defaults for rarely used config stuff.
95 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
96 #define CONFIG_KDEBUG_DISABLE_TRACE 0
99 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
100 #define CONFIG_KDEBUG_ASSERT_NO_TEXT 0
107 * This preprocessor symbol is defined only in debug builds.
109 * The build infrastructure must arrange for _DEBUG to
110 * be predefined for all source files being compiled.
112 * This is compatible with the Microsoft convention for
113 * the default Debug and Release targets.
117 // STLport specific: enable extra checks
118 #define __STL_DEBUG 1
120 // MSVC specific: Enable memory allocation debug
121 #if defined(_MSC_VER)
126 * On UNIX systems the extabilished practice is to define
127 * NDEBUG for release builds and nothing for debug builds.
134 * This macro duplicates the old MSVC trick of redefining
135 * THIS_FILE locally to avoid the overhead of many duplicate
136 * strings in ASSERT().
139 #define THIS_FILE __FILE__
143 * This macro can be used to conditionally exclude one or more
144 * statements conditioned on \c _DEBUG, avoiding the clutter
145 * of #ifdef/#endif pairs.
152 * DB(int ref_count;) // Track number of users
167 INLINE void kdbg_init(void) { /* nop */ }
168 INLINE void kputchar(char c)
172 INLINE void kputs(const char *str)
176 /* G++ can't inline functions with variable arguments... */
177 #define kprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
178 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
182 #define ASSERT(x) assert(x)
186 * Check that the given pointer is not NULL or pointing to raw memory.
188 * The assumption here is that valid pointers never point to low
189 * memory regions. This helps catching pointers taken from
190 * struct/class memebers when the struct pointer was NULL.
192 #define ASSERT_VALID_PTR(p) ASSERT((unsigned long)(p) > 0x200)
194 #define ASSERT_VALID_PTR_OR_NULL(p) ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
195 #else /* !OS_HOSTED */
197 #include <appconfig.h> /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
198 #include <cfg/cpu.h> /* CPU_HARVARD */
200 /* These are implemented in drv/kdebug.c */
201 void kdbg_init(void);
202 void kputchar(char c);
203 int kputnum(int num);
204 void kdump(const void *buf, size_t len);
205 void __init_wall(long *wall, int size);
208 #include <mware/pgm.h>
209 void kputs_P(const char *PROGMEM str);
210 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
211 int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
212 void __trace_P(const char *func);
213 void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
214 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
215 int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
216 #define kputs(str) kputs_P(PSTR(str))
217 #define kprintf(fmt, ...) kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
218 #define __assert(cond, file, line) __assert_P(PSTR(cond), PSTR(file), (line))
219 #define __trace(func) __trace_P(func)
220 #define __tracemsg(func, fmt, ...) __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
221 #define __invalid_ptr(p, name, file, line) __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
222 #define __check_wall(wall, size, name, file, line) __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
223 #else /* !CPU_HARVARD */
224 void kputs(const char *str);
225 void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
226 int __assert(const char *cond, const char *file, int line);
227 void __trace(const char *func);
228 void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
229 int __invalid_ptr(void *p, const char *name, const char *file, int line);
230 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
231 #endif /* !CPU_HARVARD */
233 #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
234 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
235 #define ASSERT2(x, help) ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
237 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
238 #define ASSERT2(x, help) ((void)ASSERT(x))
241 #define ASSERT_VALID_PTR(p) ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
242 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
244 #if !CONFIG_KDEBUG_DISABLE_TRACE
245 #define TRACE __trace(__func__)
246 #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
248 #define TRACE do {} while(0)
249 #define TRACEMSG(...) do {} while(0)
252 #endif /* !OS_HOSTED */
255 * \name Walls to detect data corruption
259 #define WALL_VALUE (long)0xABADCAFEL
260 #define DECLARE_WALL(name,size) long name[(size) / sizeof(long)];
261 #define FWD_DECLARE_WALL(name,size) extern long name[(size) / sizeof(long)];
262 #define INIT_WALL(name) __init_wall((name), countof(name))
263 #define CHECK_WALL(name) __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
267 * Check that the given pointer actually points to an object
268 * of the specified type.
270 #define ASSERT_VALID_OBJ(_t, _o) do { \
271 ASSERT_VALID_PTR((_o)); \
272 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
276 * \name Debug object creation and destruction.
278 * These macros help track some kinds of leaks in C++ programs.
279 * Usage is as follows:
284 * DECLARE_INSTANCE_TRACKING(Foo)
294 * DELETE_INSTANCE(Foo);
299 * // Put this in the implementation file of the class
300 * IMPLEMENT_INSTANCE_TRACKING(Foo)
305 * Foo *foo = new Foo;
306 * cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
308 * ASSERT_ZERO_INSTANCES(Foo); // OK
313 #define NEW_INSTANCE(CLASS) do { ++CLASS::__instances } while (0)
314 #define DELETE_INSTANCE(CLASS) do { --CLASS::__instances } while (0)
315 #define ASSERT_ZERO_INSTANCES(CLASS) ASSERT(CLASS::__instances == 0)
316 #define GET_INSTANCE_COUNT(CLASS) (CLASS::__instances)
317 #define DECLARE_INSTANCE_TRACKING(CLASS) static int __instances
318 #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
324 * On UNIX systems the extabilished practice is to define
325 * NDEBUG for release builds and nothing for debug builds.
331 #define DB(x) /* nothing */
333 #define ASSERT(x) ((void)0)
335 #define ASSERT2(x, help) ((void)0)
336 #define ASSERT_VALID_PTR(p) ((void)0)
337 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)0)
338 #define ASSERT_VALID_OBJ(_t, _o) ((void)0)
339 #define TRACE do {} while (0)
340 #define TRACEMSG(x,...) do {} while (0)
342 #define DECLARE_WALL(name, size) /* nothing */
343 #define FWD_DECLARE_WALL(name, size) /* nothing */
344 #define INIT_WALL(name) do {} while (0)
345 #define CHECK_WALL(name) do {} while (0)
347 #define NEW_INSTANCE(CLASS) do {} while (0)
348 #define DELETE_INSTANCE(CLASS) do {} while (0)
349 #define ASSERT_ZERO_INSTANCES(CLASS) do {} while (0)
350 #define GET_INSTANCE_COUNT(CLASS) ERROR_ONLY_FOR_DEBUG
351 #define DECLARE_INSTANCE_TRACKING(CLASS)
352 #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
354 INLINE void kdbg_init(void) { /* nop */ }
355 INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
356 INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
357 INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
358 INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
361 /* G++ can't inline functions with variable arguments... */
362 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
364 /* ...but GCC can. */
365 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
370 #endif /* DEVLIB_DEBUG_H */