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
126 INLINE void kdbg_init(void) { /* nop */ }
127 INLINE void kputchar(char c)
131 INLINE void kputs(const char *str)
135 #if COMPILER_VARIADIC_MACROS
136 /* G++ can't inline functions with variable arguments... */
137 #define kprintf(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
139 #define kvprintf(fmt, ap) vfprintf(stderr, fmt, ap)
140 INLINE int kprintf(const char *fmt, ...)
146 result = kvprintf(fmt, ap);
152 void kdump(const void *buf, size_t len); /* UNIMPLEMENTED */
156 #define ASSERT(x) assert(x)
158 #define ASSERT2(x, help) ASSERT(help && x)
161 * Check that the given pointer is either NULL or pointing to valid memory.
163 * The assumption here is that valid pointers never point to low
164 * memory regions. This helps catching pointers taken from
165 * struct/class memebers when the struct pointer was NULL.
167 #define ASSERT_VALID_PTR(p) ASSERT((unsigned long)(p) > 0x200)
170 * Check that the given pointer is not pointing to invalid memory.
172 * \see ASSERT_VALID_PTR()
174 #define ASSERT_VALID_PTR_OR_NULL(p) ASSERT((((p) == NULL) || ((unsigned long)(p) >= 0x200)))
176 #if !CONFIG_KDEBUG_DISABLE_TRACE
177 #define TRACE kprintf("%s()\n", __func__)
178 #if COMPILER_VARIADIC_MACROS
179 #define TRACEMSG(msg,...) kprintf("%s(): " msg, __func__, ## __VA_ARGS__)
181 INLINE void TRACEMSG(const char *fmt, ...)
185 kprintf("%s(): ", __func__);
191 #define TRACE do {} while(0)
192 #define TRACEMSG(...) do {} while(0)
195 #else /* !OS_HOSTED */
197 #include <appconfig.h> /* CONFIG_KDEBUG_ASSERT_NO_TEXT */
198 #include <cpu/attr.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 #if COMPILER_VARIADIC_MACROS
341 #define TRACEMSG(x, ...) do {} while (0)
343 INLINE void TRACEMSG(UNUSED_ARG(const char *, msg), ...)
349 #define DECLARE_WALL(name, size) /* nothing */
350 #define FWD_DECLARE_WALL(name, size) /* nothing */
351 #define INIT_WALL(name) do {} while (0)
352 #define CHECK_WALL(name) do {} while (0)
354 #define NEW_INSTANCE(CLASS) do {} while (0)
355 #define DELETE_INSTANCE(CLASS) do {} while (0)
356 #define ASSERT_ZERO_INSTANCES(CLASS) do {} while (0)
357 #define GET_INSTANCE_COUNT(CLASS) ERROR_ONLY_FOR_DEBUG
358 #define DECLARE_INSTANCE_TRACKING(CLASS)
359 #define IMPLEMENT_INSTANCE_TRACKING(CLASS)
361 INLINE void kdbg_init(void) { /* nop */ }
362 INLINE void kputchar(UNUSED_ARG(char, c)) { /* nop */ }
363 INLINE int kputnum(UNUSED_ARG(int, num)) { return 0; }
364 INLINE void kputs(UNUSED_ARG(const char *, str)) { /* nop */ }
365 INLINE void kdump(UNUSED_ARG(const void *, buf), UNUSED_ARG(size_t, len)) { /* nop */ }
367 #if defined(__cplusplus) && COMPILER_VARIADIC_MACROS
368 /* G++ can't inline functions with variable arguments... */
369 #define kprintf(fmt,...) do { (void)(fmt); } while(0)
371 /* ...but GCC can. */
372 INLINE void kprintf(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
377 #endif /* DEVLIB_DEBUG_H */