Initial commit
[amiga/BoopsiListView.git] / Debug.h
1 #ifndef DEBUG_H
2 #define DEBUG_H
3 /*
4 **      $VER: Debug.h 2.2 (19.9.98)
5 **
6 **      Copyright (C) 1995,96,97 Bernardo Innocenti. All rights reserved.
7 **
8 **      Use 4 chars wide TABs to read this file
9 **
10 **      Some handy debug macros which are automatically excluded when the
11 **      DEBUG preprocessor sysmbol is not defined. To make debug executables,
12 **      link with debug.lib or any module containing the kprintf() function.
13 **
14 **      Here is a short description of the macros defined below:
15 **
16 **      ILLEGAL
17 **              Output an inline "ILLEGAL" 68K opcode, which will
18 **              be interpreted as a breakpoint by most debuggers.
19 **
20 **      DBPRINTF
21 **              Output a formatted string to the debug console. This
22 **              macro uses the debug.lib kprintf() function by default.
23 **
24 **      ASSERT(x)
25 **              Do nothing if the expression <x> evalutates to a
26 **              non-zero value, output a debug message otherwise.
27 **
28 **      ASSERT_VALID(x)
29 **              Checks if the expression <x> points to a valid
30 **              memory location, and outputs a debug message
31 **              otherwise. A NULL pointer is considered VALID.
32 **
33 **      ASSERT_VALIDNO0(x)
34 **              Checks if the expression <x> points to a valid
35 **              memory location, and outputs a debug message
36 **              otherwise. A NULL pointer is considered INVALID.
37 **
38 **      DB(x)
39 **              Compile the expression <x> when making a debug
40 **              executable, leave it out otherwise.
41 */
42
43 #ifdef DEBUG
44
45         /* Needed for TypeOfMem() */
46         #ifndef  PROTO_EXEC_H
47         #include <proto/exec.h>
48         #endif /* PROTO_EXEC_H */
49
50         #if defined(__SASC)
51
52                 extern void __builtin_emit (int);
53                 // #define ILLEGAL __builtin_emit(0x4AFC)
54                 #define ILLEGAL 0
55                 STDARGS extern void kprintf (const char *, ...);
56
57         #elif defined(__GNUC__)
58
59                 /* Currently, there is no kprintf() in libamiga.a */
60                 #define kprintf printf
61
62                 /* GCC doesn't accept asm statemnts in the middle of an
63                  * expression such as `a ? b : asm("something")'.
64                  */
65                 #define ILLEGAL illegal()
66                 static inline int illegal(void) { asm ("illegal"); return 0; }
67                 extern void STDARGS FORMATCALL(printf,1,2) kprintf (const char *, ...);
68
69         #else
70                 #error Please add compiler specific definitions for your compiler
71         #endif
72
73         #if defined(__SASC) || defined (__GNUC__)
74
75                 /* common definitions for ASSERT and DB macros */
76
77                 #define DBPRINTF kprintf
78
79                 #define ASSERT(x) ( (x) ? 0 :                                                                   \
80                         ( DBPRINTF ("\x07%s, %ld: assertion failed: " #x "\n",          \
81                         __FILE__, __LINE__) , ILLEGAL ) );
82
83                 #define ASSERT_VALID(x) ( ((((APTR)(x)) == NULL) ||                             \
84                         (((LONG)(x) > 1024) &&  TypeOfMem ((APTR)(x)))) ? 0 :           \
85                         ( DBPRINTF ("\x07%s, %ld: bad address: " #x " = $%lx\n",        \
86                         __FILE__, __LINE__, (APTR)(x)) , ILLEGAL ) );
87
88                 #define ASSERT_VALIDNO0(x) ( (((LONG)(x) > 1024) &&                             \
89                         TypeOfMem ((APTR)(x))) ? 0 :                                                            \
90                         ( DBPRINTF ("\x07%s, %ld: bad address: " #x " = $%lx\n",        \
91                         __FILE__, __LINE__, (APTR)(x)) , ILLEGAL ) );
92
93                 #define DB(x) x
94         #endif
95
96 #else
97         #define ASSERT_VALID(x)
98         #define ASSERT_VALIDNO0(x)
99         #define ASSERT(x)
100         #define DB(x)
101 #endif /* DEBUG */
102
103 #endif /* !DEBUG_H */