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