947dff204f17fab746107a8def7b097d23a3db52
[pintos-anon] / src / lib / debug.h
1 #ifndef __LIB_DEBUG_H
2 #define __LIB_DEBUG_H
3
4 /* GCC lets us add "attributes" to functions, function
5    parameters, etc. to indicate their properties.
6    See the GCC manual for details. */
7 #define UNUSED __attribute__ ((unused))
8 #define NO_RETURN __attribute__ ((noreturn))
9 #define NO_INLINE __attribute__ ((noinline))
10 #define PRINTF_FORMAT(FMT, FIRST) __attribute__ ((format (printf, FMT, FIRST)))
11
12 /* Prints a debug message along with the source file name, line
13    number, and function name of where it was emitted.  CLASS is
14    used to filter out unwanted messages. */
15 #define DEBUG(CLASS, ...)                                               \
16         debug_message (__FILE__, __LINE__, __func__, #CLASS, __VA_ARGS__)
17
18 /* Halts the OS, printing the source file name, line number, and
19    function name, plus a user-specific message. */
20 #define PANIC(...) debug_panic (__FILE__, __LINE__, __func__, __VA_ARGS__)
21
22 void debug_enable (char *classes);
23 void debug_message (const char *file, int line, const char *function,
24                     const char *class, const char *message, ...)
25      PRINTF_FORMAT (5, 6);
26 void debug_panic (const char *file, int line, const char *function,
27                   const char *message, ...) PRINTF_FORMAT (4, 5) NO_RETURN;
28 void debug_backtrace (void);
29
30 #endif
31
32
33
34 /* This is outside the header guard so that debug.h may be
35    included multiple times with different settings of NDEBUG. */
36 #undef ASSERT
37 #undef NOT_REACHED
38
39 #ifndef NDEBUG
40 #define ASSERT(CONDITION)                                       \
41         if (CONDITION) { } else {                               \
42                 PANIC ("assertion `%s' failed.", #CONDITION);   \
43         }
44 #define NOT_REACHED() PANIC ("executed an unreachable statement");
45 #else
46 #define ASSERT(CONDITION) ((void) 0)
47 #define NOT_REACHED() for (;;)
48 #endif /* lib/debug.h */