kputchar(): New public function; Add missing dummy inlines for \!_DEBUG.
[bertos.git] / drv / kdebug.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2000,2001,2002 Bernardo Innocenti <bernie@codewiz.org>
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief Definition of handy debug macros.  These macros are no-ops
10  *        when the preprocessor symbol _DEBUG isn't defined.
11  *
12  * \version $Id$
13  *
14  * \author Bernardo Innocenti <bernie@develer.com>
15  */
16
17 /*
18  * $Log$
19  * Revision 1.5  2004/08/24 16:19:08  bernie
20  * kputchar(): New public function; Add missing dummy inlines for \!_DEBUG.
21  *
22  * Revision 1.4  2004/07/30 14:26:33  rasky
23  * Semplificato l'output dell'ASSERT
24  * Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
25  *
26  * Revision 1.3  2004/06/03 11:27:09  bernie
27  * Add dual-license information.
28  *
29  * Revision 1.2  2004/05/23 18:21:53  bernie
30  * Trim CVS logs and cleanup header info.
31  *
32  * Revision 1.1  2004/05/23 18:10:11  bernie
33  * Import drv/ modules.
34  *
35  */
36 #ifndef KDEBUG_H
37 #define KDEBUG_H
38
39 #include "compiler.h"
40
41 /* Avoid name clashes with Win32 headers */
42 #ifdef ASSERT
43 #undef ASSERT
44 #endif
45
46 #if defined(_DEBUG)
47         void kdbg_init(void);
48         void kputchar(char c);
49         void kdump(const void *buf, size_t len);
50
51         #ifdef __AVR__
52
53                 #include <avr/pgmspace.h>
54                 void kputs_P(const char *PROGMEM str);
55                 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
56                 int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
57                 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
58                 #define kputs(str)  kputs_P(PSTR(str))
59                 #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
60                 #define __assert(cond, file, line)  __assert_P(PSTR(cond), PSTR(file), (line))
61                 #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
62
63         #else
64                 void kputs(const char *str);
65                 void kprintf(const char * fmt, ...) FORMAT(__printf__, 1, 2);
66                 int __assert(const char *cond, const char *file, int line);
67                 int __invalid_ptr(void *p, const char *name, const char *file, int line);
68         #endif
69
70         void __init_wall(long *wall, int size);
71         int __check_wall(long *wall, int size, const char *name, const char *file, int line);
72
73         #define THIS_FILE  __FILE__
74         #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
75                 #define ASSERT(x)                   ((x) ? 0 : __assert(#x, THIS_FILE, __LINE__))
76                 #define ASSERT2(x, help)            ((x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__))
77         #else
78                 #define ASSERT(x)                   ((x) ? 0 : __assert("", THIS_FILE, __LINE__))
79                 #define ASSERT2(x, help)            ASSERT(x)
80         #endif
81
82         #define ASSERT_VALID_PTR(p)             ((p >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__))
83         #define ASSERT_VALID_PTR_OR_NULL(p)     (((p == NULL) || (p >= 0x200)) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__))
84         #define TRACE                           kprintf("%s()\n", __FUNCTION__)
85         #define TRACEMSG(msg,...)               kprintf("%s(): " msg "\n", __FUNCTION__, ## __VA_ARGS__)
86         #define DB(x)                           x
87
88         /* Walls to detect data corruption */
89         #define WALL_SIZE                       8
90         #define WALL_VALUE                      (long)0xABADCAFEL
91         #define DECLARE_WALL(name,size)         static long name[(size) / sizeof(long)];
92         #define INIT_WALL(name)                 __init_wall((name), countof(name))
93         #define CHECK_WALL(name)                __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
94
95 #else /* !_DEBUG */
96
97         #define ASSERT(x)                    do {} while(0)
98         #define ASSERT2(x, help)             do {} while(0)
99         #define ASSERT_VALID_PTR(p)          do {} while(0)
100         #define ASSERT_VALID_PTR_OR_NULL(p)  do {} while(0)
101         #define TRACE                        do {} while(0)
102         #define TRACEMSG(x,...)              do {} while(0)
103         #define DB(x)                        /* nothing */
104
105         #define DECLARE_WALL(name, size)
106         #define INIT_WALL(name)
107         #define CHECK_WALL(name)
108
109         INLINE void kdbg_init(void) { /*nop*/ }
110         INLINE void kputchar(UNUSED(char, c)) { /*nop*/ }
111         INLINE void kputs(UNUSED(const char*, str)) { /*nop*/ }
112         INLINE void kprintf(UNUSED(const char*, fmt), ...) { /*nop*/ }
113
114 #endif /* !_DEBUG */
115
116 #endif /* KDEBUG_H */