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