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.5 2005/11/04 16:09:03 bernie
21 *#* Doxygen workaround.
23 *#* Revision 1.4 2005/07/03 15:18:52 bernie
26 *#* Revision 1.3 2005/06/27 21:23:55 bernie
27 *#* Rename cfg/config.h to appconfig.h.
29 *#* Revision 1.2 2005/04/11 19:10:27 bernie
30 *#* Include top-level headers from cfg/ subdir.
32 *#* Revision 1.1 2005/04/11 19:04:13 bernie
33 *#* Move top-level headers to cfg/ subdir.
35 *#* Revision 1.13 2005/03/01 23:23:58 bernie
36 *#* Provide defaults for CONFIG_KDEBUG_DISABLE_TRACE and CONFIG_KDEBUG_ASSERT_NO_TEXT.
38 *#* Revision 1.12 2005/02/18 11:18:33 bernie
39 *#* Fixes for Harvard processors from project_ks.
41 *#* Revision 1.11 2005/02/16 20:29:48 bernie
42 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
44 *#* Revision 1.10 2005/02/09 21:50:28 bernie
45 *#* Declare dummy ASSERT* macros as ((void)0) to work around a warning I can't remember any more.
47 *#* Revision 1.9 2005/01/25 08:36:40 bernie
48 *#* kputnum(): Export.
50 *#* Revision 1.8 2005/01/11 18:08:08 aleph
51 *#* Add empty kdump definition for debug off
53 *#* Revision 1.7 2004/12/31 17:43:09 bernie
54 *#* Use UNUSED_ARG instead of obsolete UNUSED macro.
56 *#* Revision 1.6 2004/12/08 08:52:00 bernie
57 *#* Save some more RAM on AVR.
59 #ifndef DEVLIB_DEBUG_H
60 #define DEVLIB_DEBUG_H
63 * Try to guess whether we're running in a hosted or embedded environment.
66 #if defined(__unix__) || defined(_WIN32)
71 #endif /* !OS_HOSTED */
75 * For now, avoid dependency on compiler.h
78 #define FORMAT(x,y,z) /* nothing */
81 #define INLINE static inline
85 #define UNUSED_ARG(type,name) type
87 #define UNUSED_ARG(type,name) type name
90 #else /* !OS_HOSTED */
91 #include <cfg/compiler.h>
92 #endif /* !OS_HOSTED */
96 * Defaults for rarely used config stuff.
98 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
99 #define CONFIG_KDEBUG_DISABLE_TRACE 0
102 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
103 #define CONFIG_KDEBUG_ASSERT_NO_TEXT 0
110 * This preprocessor symbol is defined only in debug builds.
112 * The build infrastructure must arrange for _DEBUG to
113 * be predefined for all source files being compiled.
115 * This is compatible with the Microsoft convention for
116 * the default Debug and Release targets.
120 // STLport specific: enable extra checks
121 #define __STL_DEBUG 1
123 // MSVC specific: Enable memory allocation debug
124 #if defined(_MSC_VER)
129 * On UNIX systems the extabilished practice is to define
130 * NDEBUG for release builds and nothing for debug builds.
137 * This macro duplicates the old MSVC trick of redefining
138 * THIS_FILE locally to avoid the overhead of many duplicate
139 * strings in ASSERT().
142 #define THIS_FILE __FILE__
146 * This macro can be used to conditionally exclude one or more
147 * statements conditioned on \c _DEBUG, avoiding the clutter
148 * of ifdef/endif pairs.
155 * DB(int ref_count;) // Track number of users
170 INLINE void kdbg_init(void) { /* nop */ }
171 INLINE void kputchar(char c)
175 INLINE void kputs(const char *str)
179 /* G++ can't inline functions with variable arguments... */
180 #define kprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
181 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
185 #define ASSERT(x) assert(x)
189 * Check that the given pointer is not NULL or pointing to raw memory.
191 * The assumption here is that valid pointers never point to low
192 * memory regions. This helps catching pointers taken from
193 * struct/class memebers when the struct pointer was NULL.
195 #define ASSERT_VALID_PTR(p) ASSERT((unsigned long)(p) > 0x200)
197 #define ASSERT_VALID_PTR_OR_NULL(p) ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
198 #else /* !OS_HOSTED */
200 #include <appconfig.h> /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
201 #include <cfg/cpu.h> /* CPU_HARVARD */
203 /* These are implemented in drv/kdebug.c */
204 void kdbg_init(void);
205 void kputchar(char c);
206 int kputnum(int num);
207 void kdump(const void *buf, size_t len);
208 void __init_wall(long *wall, int size);
211 #include <mware/pgm.h>
212 void kputs_P(const char *PROGMEM str);
213 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
214 int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
215 void __trace_P(const char *func);
216 void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
217 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
218 int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
219 #define kputs(str) kputs_P(PSTR(str))
220 #define kprintf(fmt, ...) kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
221 #define __assert(cond, file, line) __assert_P(PSTR(cond), PSTR(file), (line))
222 #define __trace(func) __trace_P(func)
223 #define __tracemsg(func, fmt, ...) __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
224 #define __invalid_ptr(p, name, file, line) __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
225 #define __check_wall(wall, size, name, file, line) __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
226 #else /* !CPU_HARVARD */
227 void kputs(const char *str);
228 void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
229 int __assert(const char *cond, const char *file, int line);
230 void __trace(const char *func);
231 void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
232 int __invalid_ptr(void *p, const char *name, const char *file, int line);
233 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
234 #endif /* !CPU_HARVARD */
236 #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
237 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
238 #define ASSERT2(x, help) ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
240 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
241 #define ASSERT2(x, help) ((void)ASSERT(x))
244 #define ASSERT_VALID_PTR(p) ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
245 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
247 #if !CONFIG_KDEBUG_DISABLE_TRACE
248 #define TRACE __trace(__func__)
249 #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
251 #define TRACE do {} while(0)
252 #define TRACEMSG(...) do {} while(0)
255 #endif /* !OS_HOSTED */
258 * \name Walls to detect data corruption
262 #define WALL_VALUE (long)0xABADCAFEL
263 #define DECLARE_WALL(name,size) long name[(size) / sizeof(long)];
264 #define FWD_DECLARE_WALL(name,size) extern long name[(size) / sizeof(long)];
265 #define INIT_WALL(name) __init_wall((name), countof(name))
266 #define CHECK_WALL(name) __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
270 * Check that the given pointer actually points to an object
271 * of the specified type.
273 #define ASSERT_VALID_OBJ(_t, _o) do { \
274 ASSERT_VALID_PTR((_o)); \
275 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
279 * \name Debug object creation and destruction.
281 * These macros help track some kinds of leaks in C++ programs.
282 * Usage is as follows:
287 * DECLARE_INSTANCE_TRACKING(Foo)
297 * DELETE_INSTANCE(Foo);
302 * // Put this in the implementation file of the class
303 * IMPLEMENT_INSTANCE_TRACKING(Foo)
308 * Foo *foo = new Foo;
309 * cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
311 * ASSERT_ZERO_INSTANCES(Foo); // OK
316 #define NEW_INSTANCE(CLASS) do { ++CLASS::__instances } while (0)
317 #define DELETE_INSTANCE(CLASS) do { --CLASS::__instances } while (0)
318 #define ASSERT_ZERO_INSTANCES(CLASS) ASSERT(CLASS::__instances == 0)
319 #define GET_INSTANCE_COUNT(CLASS) (CLASS::__instances)
320 #define DECLARE_INSTANCE_TRACKING(CLASS) static int __instances
321 #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
327 * On UNIX systems the extabilished practice is to define
328 * NDEBUG for release builds and nothing for debug builds.
334 #define DB(x) /* nothing */
336 #define ASSERT(x) ((void)0)
338 #define ASSERT2(x, help) ((void)0)
339 #define ASSERT_VALID_PTR(p) ((void)0)
340 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)0)
341 #define ASSERT_VALID_OBJ(_t, _o) ((void)0)
342 #define TRACE do {} while (0)
343 #define TRACEMSG(x,...) do {} while (0)
345 #define DECLARE_WALL(name, size) /* nothing */
346 #define FWD_DECLARE_WALL(name, size) /* nothing */
347 #define INIT_WALL(name) do {} while (0)
348 #define CHECK_WALL(name) do {} while (0)
350 #define NEW_INSTANCE(CLASS) do {} while (0)
351 #define DELETE_INSTANCE(CLASS) do {} while (0)
352 #define ASSERT_ZERO_INSTANCES(CLASS) do {} while (0)
353 #define GET_INSTANCE_COUNT(CLASS) ERROR_ONLY_FOR_DEBUG
354 #define DECLARE_INSTANCE_TRACKING(CLASS)
355 #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
357 INLINE void kdbg_init(void) { /* nop */ }
358 INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
359 INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
360 INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
361 INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
364 /* G++ can't inline functions with variable arguments... */
365 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
367 /* ...but GCC can. */
368 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
373 #endif /* DEVLIB_DEBUG_H */