fe67c8ed20d930d532acf66bb6d1d5b86fc66ba2
[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.4  2004/07/30 14:26:33  rasky
20  * Semplificato l'output dell'ASSERT
21  * Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
22  *
23  * Revision 1.3  2004/06/03 11:27:09  bernie
24  * Add dual-license information.
25  *
26  * Revision 1.2  2004/05/23 18:21:53  bernie
27  * Trim CVS logs and cleanup header info.
28  *
29  * Revision 1.1  2004/05/23 18:10:11  bernie
30  * Import drv/ modules.
31  *
32  */
33 #ifndef KDEBUG_H
34 #define KDEBUG_H
35
36 #include "compiler.h"
37
38 /* Avoid name clashes with Win32 headers */
39 #ifdef ASSERT
40 #undef ASSERT
41 #endif
42
43 #if defined(_DEBUG)
44         void kdbg_init(void);
45         void kdump(const void *buf, size_t len);
46
47         #ifdef __AVR__
48
49                 #include <avr/pgmspace.h>
50                 void kputs_P(const char *PROGMEM str);
51                 void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
52                 int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
53                 int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
54                 #define kputs(str)  kputs_P(PSTR(str))
55                 #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
56                 #define __assert(cond, file, line)  __assert_P(PSTR(cond), PSTR(file), (line))
57                 #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
58
59         #else
60                 void kputs(const char *str);
61                 void kprintf(const char * fmt, ...) FORMAT(__printf__, 1, 2);
62                 int __assert(const char *cond, const char *file, int line);
63                 int __invalid_ptr(void *p, const char *name, const char *file, int line);
64         #endif
65
66         void __init_wall(long *wall, int size);
67         int __check_wall(long *wall, int size, const char *name, const char *file, int line);
68
69         #define THIS_FILE                       __FILE__
70         #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
71                 #define ASSERT(x)                   ((x) ? 0 : __assert(#x, THIS_FILE, __LINE__))
72                 #define ASSERT2(x, help)            ((x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__))
73         #else
74                 #define ASSERT(x)                   ((x) ? 0 : __assert("", THIS_FILE, __LINE__))
75                 #define ASSERT2(x, help)            ASSERT(x)
76         #endif
77
78         #define ASSERT_VALID_PTR(p)             ((p >= 0x200) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__))
79         #define ASSERT_VALID_PTR_OR_NULL(p)     (((p == NULL) || (p >= 0x200)) ? 0 : __invalid_ptr(p, #p, THIS_FILE, __LINE__))
80         #define TRACE                           kprintf("%s()\n", __FUNCTION__)
81         #define TRACEMSG(msg,...)               kprintf("%s(): " msg "\n", __FUNCTION__, ## __VA_ARGS__)
82         #define DB(x)                           x
83
84         /* Walls to detect data corruption */
85         #define WALL_SIZE                       8
86         #define WALL_VALUE                      (long)0xABADCAFEL
87         #define DECLARE_WALL(name,size)         static long name[(size) / sizeof(long)];
88         #define INIT_WALL(name)                 __init_wall((name), countof(name))
89         #define CHECK_WALL(name)                __check_wall((name), countof(name), #name, THIS_FILE, __LINE__)
90
91 #else /* !_DEBUG */
92
93         #define ASSERT(x)
94         #define ASSERT2(x, help)
95         #define ASSERT_VALID_PTR(p)
96         #define ASSERT_VALID_PTR_OR_NULL(p)
97         #define TRACE                           do {} while(0)
98         #define TRACEMSG(x,...)                 do {} while(0)
99         #define DB(x)
100
101         #define DECLARE_WALL(name, size)
102         #define INIT_WALL(name)
103         #define CHECK_WALL(name)
104
105         INLINE void kdbg_init(void) {}
106         INLINE void kprintf(UNUSED(const char*, fmt), ...) {}
107
108 #endif /* !_DEBUG */
109
110 #endif /* KDEBUG_H */