BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file debug.h |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | * |
||
29 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Debugging macros. |
||
32 | */ |
||
33 | |||
34 | /** |
||
35 | * @def DEBUG |
||
36 | * |
||
37 | * Macro for printing debugging text. Use the same way as printf, |
||
38 | * but without a newline. |
||
39 | * Prepends "function_name: " and appends a newline. |
||
40 | */ |
||
41 | |||
42 | /** |
||
43 | * @def ASSERT_FORCE |
||
44 | * |
||
45 | * Macro for forced assertions. |
||
46 | * Evaluates the argument and terminates the program abnormally |
||
47 | * if the result is false. |
||
48 | */ |
||
49 | |||
50 | /** |
||
51 | * @def ASSERT |
||
52 | * |
||
53 | * Macro for assertions. |
||
54 | * The argument may or may not be evaluated. |
||
55 | * If the argument is evaluated, it must not evaluate to false. |
||
56 | */ |
||
57 | |||
58 | /** |
||
59 | * @def ASSERT_EXECUTE |
||
60 | * |
||
61 | * Macro for always-evaluated assertions. |
||
62 | * The argument is evaluated. |
||
63 | * The argument must not evaluate to false. |
||
64 | */ |
||
65 | |||
66 | /** |
||
67 | * @def DEBUG_ZERO_MEMORY |
||
68 | * |
||
69 | * If debugging is enabled, zeroes the given memory region. |
||
70 | * First argument is pointer to the memory region, second is |
||
71 | * number of bytes. |
||
72 | */ |
||
73 | |||
74 | /** |
||
75 | * @def WARN_UNUSED |
||
76 | * |
||
77 | * Tells the compiler that the result of a function should not be unused. |
||
78 | * Insert at the end of the declaration of a function before the semicolon. |
||
79 | */ |
||
80 | |||
81 | /** |
||
82 | * @def B_USE |
||
83 | * |
||
84 | * This can be used to suppress warnings about unused variables. It can |
||
85 | * be applied to a variable or any expression. It does not evaluate the |
||
86 | * expression. |
||
87 | */ |
||
88 | |||
89 | #ifndef BADVPN_MISC_DEBUG_H |
||
90 | #define BADVPN_MISC_DEBUG_H |
||
91 | |||
92 | #include <stdio.h> |
||
93 | #include <stdlib.h> |
||
94 | #include <string.h> |
||
95 | #include <stdint.h> |
||
96 | #include <assert.h> |
||
97 | |||
98 | #define DEBUG(...) \ |
||
99 | { \ |
||
100 | fprintf(stderr, "%s: ", __FUNCTION__); \ |
||
101 | fprintf(stderr, __VA_ARGS__); \ |
||
102 | fprintf(stderr, "\n"); \ |
||
103 | } |
||
104 | |||
105 | #define ASSERT_FORCE(e) \ |
||
106 | { \ |
||
107 | if (!(e)) { \ |
||
108 | fprintf(stderr, "%s:%d Assertion failed\n", __FILE__, __LINE__); \ |
||
109 | abort(); \ |
||
110 | } \ |
||
111 | } |
||
112 | |||
113 | #ifdef NDEBUG |
||
114 | #define DEBUG_ZERO_MEMORY(buf, len) {} |
||
115 | #define ASSERT(e) {} |
||
116 | #define ASSERT_EXECUTE(e) { (e); } |
||
117 | #else |
||
118 | #define DEBUG_ZERO_MEMORY(buf, len) { memset((buf), 0, (len)); } |
||
119 | #ifdef BADVPN_USE_C_ASSERT |
||
120 | #define ASSERT(e) { assert(e); } |
||
121 | #define ASSERT_EXECUTE(e) \ |
||
122 | { \ |
||
123 | int _assert_res = !!(e); \ |
||
124 | assert(_assert_res); \ |
||
125 | } |
||
126 | #else |
||
127 | #define ASSERT(e) ASSERT_FORCE(e) |
||
128 | #define ASSERT_EXECUTE(e) ASSERT_FORCE(e) |
||
129 | #endif |
||
130 | #endif |
||
131 | |||
132 | #ifdef __GNUC__ |
||
133 | #define WARN_UNUSED __attribute__((warn_unused_result)) |
||
134 | #else |
||
135 | #define WARN_UNUSED |
||
136 | #endif |
||
137 | |||
138 | #define B_USE(expr) (void)(sizeof((expr))); |
||
139 | |||
140 | #define B_ASSERT_USE(expr) { ASSERT(expr) B_USE(expr) } |
||
141 | |||
142 | #endif |