4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
33 * \brief Simple debug facilities for hosted and embedded C/C++ applications.
35 * Debug output goes to stderr in hosted applications.
36 * Freestanding (AKA embedded) applications use \c drv/kdebug.c to output
37 * diagnostic messages to a serial terminal or a JTAG debugger.
40 * \author Bernardo Innocenti <bernie@develer.com>
45 *#* Revision 1.14 2006/07/19 12:56:25 bernie
46 *#* Convert to new Doxygen style.
48 *#* Revision 1.13 2006/03/22 13:34:34 bernie
51 *#* Revision 1.12 2006/03/22 09:48:23 bernie
54 *#* Revision 1.11 2006/02/23 11:17:16 bernie
55 *#* Documentation fixes.
57 *#* Revision 1.10 2006/02/23 09:10:10 bernie
58 *#* Add even more code duplication until we properly refactor debug.h.
60 *#* Revision 1.9 2006/02/23 08:40:33 bernie
61 *#* TRACEMSG() support for compilers with no variadic macros.
63 *#* Revision 1.8 2006/02/23 08:33:04 bernie
64 *#* Fix for compilers without variadic macros support.
66 *#* Revision 1.7 2006/02/20 02:01:56 bernie
67 *#* Depend on cfg/os.h.
69 *#* Revision 1.6 2006/02/17 22:28:37 bernie
70 *#* Support TRACE() and TRACEMSG() on hosted targets.
72 *#* Revision 1.5 2005/11/04 16:09:03 bernie
73 *#* Doxygen workaround.
75 *#* Revision 1.4 2005/07/03 15:18:52 bernie
78 *#* Revision 1.3 2005/06/27 21:23:55 bernie
79 *#* Rename cfg/config.h to appconfig.h.
81 *#* Revision 1.2 2005/04/11 19:10:27 bernie
82 *#* Include top-level headers from cfg/ subdir.
84 *#* Revision 1.1 2005/04/11 19:04:13 bernie
85 *#* Move top-level headers to cfg/ subdir.
87 *#* Revision 1.13 2005/03/01 23:23:58 bernie
88 *#* Provide defaults for CONFIG_KDEBUG_DISABLE_TRACE and CONFIG_KDEBUG_ASSERT_NO_TEXT.
90 *#* Revision 1.12 2005/02/18 11:18:33 bernie
91 *#* Fixes for Harvard processors from project_ks.
93 *#* Revision 1.11 2005/02/16 20:29:48 bernie
94 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
96 *#* Revision 1.10 2005/02/09 21:50:28 bernie
97 *#* Declare dummy ASSERT* macros as ((void)0) to work around a warning I can't remember any more.
99 *#* Revision 1.9 2005/01/25 08:36:40 bernie
100 *#* kputnum(): Export.
102 *#* Revision 1.8 2005/01/11 18:08:08 aleph
103 *#* Add empty kdump definition for debug off
105 *#* Revision 1.7 2004/12/31 17:43:09 bernie
106 *#* Use UNUSED_ARG instead of obsolete UNUSED macro.
108 *#* Revision 1.6 2004/12/08 08:52:00 bernie
109 *#* Save some more RAM on AVR.
111 #ifndef DEVLIB_DEBUG_H
112 #define DEVLIB_DEBUG_H
115 #include <cfg/compiler.h>
119 * Defaults for rarely used config stuff.
121 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
122 #define CONFIG_KDEBUG_DISABLE_TRACE 0
125 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
126 #define CONFIG_KDEBUG_ASSERT_NO_TEXT 0
129 #if defined(__doxygen__)
131 * Preprocessor symbol defined only for debug builds.
133 * The build infrastructure must arrange for _DEBUG to
134 * be predefined for all the source files being compiled.
136 * This is compatible with the MSVC convention for the
137 * default Debug and Release project targets.
144 // STLport specific: enable extra checks
145 #define __STL_DEBUG 1
147 // MSVC specific: Enable memory allocation debug
148 #if defined(_MSC_VER)
153 * On UNIX systems the extabilished practice is to define
154 * NDEBUG for release builds and nothing for debug builds.
161 * This macro duplicates the old MSVC trick of redefining
162 * THIS_FILE locally to avoid the overhead of many duplicate
163 * strings in ASSERT().
166 #define THIS_FILE __FILE__
170 * This macro can be used to conditionally exclude one or more
171 * statements conditioned on \c _DEBUG, avoiding the clutter
172 * of ifdef/endif pairs.
179 * DB(int ref_count;) // Track number of users
194 INLINE void kdbg_init(void) { /* nop */ }
195 INLINE void kputchar(char c)
199 INLINE void kputs(const char *str)
203 #if COMPILER_VARIADIC_MACROS
204 /* G++ can't inline functions with variable arguments... */
205 #define kprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
207 #define kvprintf(fmt, ap) vfprintf(stderr, fmt, ap)
208 INLINE int kprintf(const char *fmt, ...)
214 result = kvprintf(fmt, ap);
220 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
224 #define ASSERT(x) assert(x)
226 #define ASSERT2(x, help) ASSERT(help && x)
229 * Check that the given pointer is either NULL or pointing to valid memory.
231 * The assumption here is that valid pointers never point to low
232 * memory regions. This helps catching pointers taken from
233 * struct/class memebers when the struct pointer was NULL.
235 #define ASSERT_VALID_PTR(p) ASSERT((unsigned long)(p) > 0x200)
238 * Check that the given pointer is not pointing to invalid memory.
240 * \see ASSERT_VALID_PTR()
242 #define ASSERT_VALID_PTR_OR_NULL(p) ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
244 #if !CONFIG_KDEBUG_DISABLE_TRACE
245 #define TRACE kprintf("%s()\n", __func__)
246 #if COMPILER_VARIADIC_MACROS
247 #define TRACEMSG(msg,...) kprintf("%s(): " msg, __func__, ## __VA_ARGS__)
249 INLINE void TRACEMSG(const char *fmt, ...)
253 kprintf("%s(): ", __func__);
259 #define TRACE do {} while(0)
260 #define TRACEMSG(...) do {} while(0)
263 #else /* !OS_HOSTED */
265 #include <appconfig.h> /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
266 #include <cfg/cpu.h> /* CPU_HARVARD */
268 /* These are implemented in drv/kdebug.c */
269 void kdbg_init(void);
270 void kputchar(char c);
271 int kputnum(int num);
272 void kdump(const void *buf, size_t len);
273 void __init_wall(long *wall, int size);
276 #include <mware/pgm.h>
277 void kputs_P(const char *PROGMEM str);
278 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
279 int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
280 void __trace_P(const char *func);
281 void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
282 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
283 int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
284 #define kputs(str) kputs_P(PSTR(str))
285 #define kprintf(fmt, ...) kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
286 #define __assert(cond, file, line) __assert_P(PSTR(cond), PSTR(file), (line))
287 #define __trace(func) __trace_P(func)
288 #define __tracemsg(func, fmt, ...) __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
289 #define __invalid_ptr(p, name, file, line) __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
290 #define __check_wall(wall, size, name, file, line) __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
291 #else /* !CPU_HARVARD */
292 void kputs(const char *str);
293 void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
294 int __assert(const char *cond, const char *file, int line);
295 void __trace(const char *func);
296 void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
297 int __invalid_ptr(void *p, const char *name, const char *file, int line);
298 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
299 #endif /* !CPU_HARVARD */
301 #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
302 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
303 #define ASSERT2(x, help) ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
305 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
306 #define ASSERT2(x, help) ((void)ASSERT(x))
309 #define ASSERT_VALID_PTR(p) ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
310 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
312 #if !CONFIG_KDEBUG_DISABLE_TRACE
313 #define TRACE __trace(__func__)
314 #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
316 #define TRACE do {} while(0)
317 #define TRACEMSG(...) do {} while(0)
320 #endif /* !OS_HOSTED */
323 * \name Walls to detect data corruption
327 #define WALL_VALUE (long)0xABADCAFEL
328 #define DECLARE_WALL(name,size) long name[(size) / sizeof(long)];
329 #define FWD_DECLARE_WALL(name,size) extern long name[(size) / sizeof(long)];
330 #define INIT_WALL(name) __init_wall((name), countof(name))
331 #define CHECK_WALL(name) __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
335 * Check that the given pointer actually points to an object
336 * of the specified type.
338 #define ASSERT_VALID_OBJ(_t, _o) do { \
339 ASSERT_VALID_PTR((_o)); \
340 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
344 * \name Debug object creation and destruction.
346 * These macros help track some kinds of leaks in C++ programs.
347 * Usage is as follows:
352 * DECLARE_INSTANCE_TRACKING(Foo)
362 * DELETE_INSTANCE(Foo);
367 * // Put this in the implementation file of the class
368 * IMPLEMENT_INSTANCE_TRACKING(Foo)
373 * Foo *foo = new Foo;
374 * cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
376 * ASSERT_ZERO_INSTANCES(Foo); // OK
381 #define NEW_INSTANCE(CLASS) do { ++CLASS::__instances } while (0)
382 #define DELETE_INSTANCE(CLASS) do { --CLASS::__instances } while (0)
383 #define ASSERT_ZERO_INSTANCES(CLASS) ASSERT(CLASS::__instances == 0)
384 #define GET_INSTANCE_COUNT(CLASS) (CLASS::__instances)
385 #define DECLARE_INSTANCE_TRACKING(CLASS) static int __instances
386 #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
392 * On UNIX systems the extabilished practice is to define
393 * NDEBUG for release builds and nothing for debug builds.
399 #define DB(x) /* nothing */
401 #define ASSERT(x) ((void)0)
403 #define ASSERT2(x, help) ((void)0)
404 #define ASSERT_VALID_PTR(p) ((void)0)
405 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)0)
406 #define ASSERT_VALID_OBJ(_t, _o) ((void)0)
407 #define TRACE do {} while (0)
408 #if COMPILER_VARIADIC_MACROS
409 #define TRACEMSG(x, ...) do {} while (0)
411 INLINE void TRACEMSG(UNUSED_ARG(const char *, msg), ...)
417 #define DECLARE_WALL(name, size) /* nothing */
418 #define FWD_DECLARE_WALL(name, size) /* nothing */
419 #define INIT_WALL(name) do {} while (0)
420 #define CHECK_WALL(name) do {} while (0)
422 #define NEW_INSTANCE(CLASS) do {} while (0)
423 #define DELETE_INSTANCE(CLASS) do {} while (0)
424 #define ASSERT_ZERO_INSTANCES(CLASS) do {} while (0)
425 #define GET_INSTANCE_COUNT(CLASS) ERROR_ONLY_FOR_DEBUG
426 #define DECLARE_INSTANCE_TRACKING(CLASS)
427 #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
429 INLINE void kdbg_init(void) { /* nop */ }
430 INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
431 INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
432 INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
433 INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
435 #if defined(__cplusplus) && COMPILER_VARIADIC_MACROS
436 /* G++ can't inline functions with variable arguments... */
437 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
439 /* ...but GCC can. */
440 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
445 #endif /* DEVLIB_DEBUG_H */