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>
43 #ifndef DEVLIB_DEBUG_H
44 #define DEVLIB_DEBUG_H
47 #include <cfg/compiler.h>
51 * Defaults for rarely used config stuff.
53 #ifndef CONFIG_KDEBUG_DISABLE_TRACE
54 #define CONFIG_KDEBUG_DISABLE_TRACE 0
57 #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
58 #define CONFIG_KDEBUG_ASSERT_NO_TEXT 0
61 #if defined(__doxygen__)
63 * Preprocessor symbol defined only for debug builds.
65 * The build infrastructure must arrange for _DEBUG to
66 * be predefined for all the source files being compiled.
68 * This is compatible with the MSVC convention for the
69 * default Debug and Release project targets.
76 // STLport specific: enable extra checks
79 // MSVC specific: Enable memory allocation debug
85 * On UNIX systems the extabilished practice is to define
86 * NDEBUG for release builds and nothing for debug builds.
93 * This macro duplicates the old MSVC trick of redefining
94 * THIS_FILE locally to avoid the overhead of many duplicate
95 * strings in ASSERT().
98 #define THIS_FILE __FILE__
102 * This macro can be used to conditionally exclude one or more
103 * statements conditioned on \c _DEBUG, avoiding the clutter
104 * of ifdef/endif pairs.
111 * DB(int ref_count;) // Track number of users
122 #include <appconfig.h> /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
123 #include <cpu/attr.h> /* CPU_HARVARD */
125 /* These are implemented in drv/kdebug.c */
126 void kdbg_init(void);
127 void kputchar(char c);
128 int kputnum(int num);
129 void kdump(const void *buf, size_t len);
130 void __init_wall(long *wall, int size);
133 #include <mware/pgm.h>
134 void kputs_P(const char *PROGMEM str);
135 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
136 int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
137 void __trace_P(const char *func);
138 void __tracemsg_P(const char *func, const char *PROGMEM fmt, ...);
139 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
140 int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
141 #define kputs(str) kputs_P(PSTR(str))
142 #define kprintf(fmt, ...) kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
143 #define __assert(cond, file, line) __assert_P(PSTR(cond), PSTR(file), (line))
144 #define __trace(func) __trace_P(func)
145 #define __tracemsg(func, fmt, ...) __tracemsg_P(func, PSTR(fmt), ## __VA_ARGS__)
146 #define __invalid_ptr(p, name, file, line) __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
147 #define __check_wall(wall, size, name, file, line) __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
148 #else /* !CPU_HARVARD */
149 void kputs(const char *str);
150 void kprintf(const char *fmt, ...) FORMAT(__printf__, 1, 2);
151 int __assert(const char *cond, const char *file, int line);
152 void __trace(const char *func);
153 void __tracemsg(const char *func, const char *fmt, ...) FORMAT(__printf__, 2, 3);
154 int __invalid_ptr(void *p, const char *name, const char *file, int line);
155 int __check_wall(long *wall, int size, const char *name, const char *file, int line);
156 #endif /* !CPU_HARVARD */
158 #if !CONFIG_KDEBUG_ASSERT_NO_TEXT
159 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
160 #define ASSERT2(x, help) ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
162 #define ASSERT(x) ((void)(LIKELY(x) ? 0 : __assert("", THIS_FILE, __LINE__)))
163 #define ASSERT2(x, help) ((void)ASSERT(x))
167 * Check that the given pointer is either NULL or pointing to valid memory.
169 * The assumption here is that valid pointers never point to low
170 * memory regions. This helps catching pointers taken from
171 * struct/class memebers when the struct pointer was NULL.
173 #define ASSERT_VALID_PTR(p) ((void)(LIKELY((p) >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__)))
176 * Check that the given pointer is not pointing to invalid memory.
178 * \see ASSERT_VALID_PTR()
180 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)(LIKELY((p == NULL) || ((p) >= 0x200)) ? 0 : __invalid_ptr((p), #p, THIS_FILE, __LINE__)))
182 #if !CONFIG_KDEBUG_DISABLE_TRACE
183 #define TRACE __trace(__func__)
184 #define TRACEMSG(msg,...) __tracemsg(__func__, msg, ## __VA_ARGS__)
186 #define TRACE do {} while(0)
187 #define TRACEMSG(...) do {} while(0)
191 * \name Walls to detect data corruption
195 #define WALL_VALUE (long)0xABADCAFEL
196 #define DECLARE_WALL(name,size) long name[(size) / sizeof(long)];
197 #define FWD_DECLARE_WALL(name,size) extern long name[(size) / sizeof(long)];
198 #define INIT_WALL(name) __init_wall((name), countof(name))
199 #define CHECK_WALL(name) __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
203 * Check that the given pointer actually points to an object
204 * of the specified type.
206 #define ASSERT_VALID_OBJ(_t, _o) do { \
207 ASSERT_VALID_PTR((_o)); \
208 ASSERT(dynamic_cast<_t>((_o)) != NULL); \
212 * \name Debug object creation and destruction.
214 * These macros help track some kinds of leaks in C++ programs.
215 * Usage is as follows:
220 * DECLARE_INSTANCE_TRACKING(Foo)
230 * DELETE_INSTANCE(Foo);
235 * // Put this in the implementation file of the class
236 * IMPLEMENT_INSTANCE_TRACKING(Foo)
241 * Foo *foo = new Foo;
242 * cout << GET_INSTANCE_COUNT(Foo) << endl; // prints "1"
244 * ASSERT_ZERO_INSTANCES(Foo); // OK
249 #define NEW_INSTANCE(CLASS) do { ++CLASS::__instances } while (0)
250 #define DELETE_INSTANCE(CLASS) do { --CLASS::__instances } while (0)
251 #define ASSERT_ZERO_INSTANCES(CLASS) ASSERT(CLASS::__instances == 0)
252 #define GET_INSTANCE_COUNT(CLASS) (CLASS::__instances)
253 #define DECLARE_INSTANCE_TRACKING(CLASS) static int __instances
254 #define IMPLEMENT_INSTANCE_TRACKING(CLASS) int CLASS::__instances = 0
260 * On UNIX systems the extabilished practice is to define
261 * NDEBUG for release builds and nothing for debug builds.
267 #define DB(x) /* nothing */
269 #define ASSERT(x) ((void)0)
271 #define ASSERT2(x, help) ((void)0)
272 #define ASSERT_VALID_PTR(p) ((void)0)
273 #define ASSERT_VALID_PTR_OR_NULL(p) ((void)0)
274 #define ASSERT_VALID_OBJ(_t, _o) ((void)0)
275 #define TRACE do {} while (0)
276 #if COMPILER_VARIADIC_MACROS
277 #define TRACEMSG(x, ...) do {} while (0)
279 INLINE void TRACEMSG(UNUSED_ARG(const char *, msg), ...)
285 #define DECLARE_WALL(name, size) /* nothing */
286 #define FWD_DECLARE_WALL(name, size) /* nothing */
287 #define INIT_WALL(name) do {} while (0)
288 #define CHECK_WALL(name) do {} while (0)
290 #define NEW_INSTANCE(CLASS) do {} while (0)
291 #define DELETE_INSTANCE(CLASS) do {} while (0)
292 #define ASSERT_ZERO_INSTANCES(CLASS) do {} while (0)
293 #define GET_INSTANCE_COUNT(CLASS) ERROR_ONLY_FOR_DEBUG
294 #define DECLARE_INSTANCE_TRACKING(CLASS)
295 #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
297 INLINE void kdbg_init(void) { /* nop */ }
298 INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
299 INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
300 INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
301 INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
303 #if defined(__cplusplus) && COMPILER_VARIADIC_MACROS
304 /* G++ can't inline functions with variable arguments... */
305 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
307 /* ...but GCC can. */
308 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
313 #endif /* DEVLIB_DEBUG_H */