1 /* 2 * Copyright 2007, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _DEBUG_H 6 #define _DEBUG_H 7 8 9 #include <BeBuild.h> 10 #include <OS.h> 11 12 #include <stdarg.h> 13 #include <stdio.h> 14 #include <stdlib.h> 15 16 17 /* Private */ 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 extern bool _rtDebugFlag; 22 23 bool _debugFlag(void); 24 bool _setDebugFlag(bool); 25 26 #if __GNUC__ 27 int _debugPrintf(const char *, ...) _PRINTFLIKE(1, 2); 28 int _sPrintf(const char *, ...) _PRINTFLIKE(1, 2); 29 #else 30 int _debugPrintf(const char *, ...); 31 int _sPrintf(const char *, ...); 32 #endif 33 int _xdebugPrintf(const char *, ...); 34 int _debuggerAssert(const char *, int, char *); 35 #ifdef __cplusplus 36 } 37 #endif 38 39 /* Debug macros */ 40 #if DEBUG 41 #define SET_DEBUG_ENABLED(FLAG) _setDebugFlag(FLAG) 42 #define IS_DEBUG_ENABLED() _debugFlag() 43 44 #define SERIAL_PRINT(ARGS) _sPrintf ARGS 45 #define PRINT(ARGS) _debugPrintf ARGS 46 #define PRINT_OBJECT(OBJ) if (_rtDebugFlag) { \ 47 PRINT(("%s\t", #OBJ)); \ 48 (OBJ).PrintToStream(); \ 49 } ((void)0) 50 #define TRACE() _debugPrintf("File: %s, Line: %d, Thread: %d\n", \ 51 __FILE__, __LINE__, find_thread(NULL)) 52 53 #define SERIAL_TRACE() _sPrintf("File: %s, Line: %d, Thread: %d\n", \ 54 __FILE__, __LINE__, find_thread(NULL)) 55 56 #define DEBUGGER(MSG) if (_rtDebugFlag) debugger(MSG) 57 #if !defined(ASSERT) 58 #define ASSERT(E) (!(E) ? _debuggerAssert(__FILE__,__LINE__, #E) \ 59 : (int)0) 60 #endif 61 62 #define ASSERT_WITH_MESSAGE(expr, msg) \ 63 (!(expr) ? _debuggerAssert( __FILE__,__LINE__, msg) \ 64 : (int)0) 65 66 #define TRESPASS() DEBUGGER("Should not be here"); 67 68 #define DEBUG_ONLY(arg) arg 69 70 #else /* DEBUG == 0 */ 71 #define SET_DEBUG_ENABLED(FLAG) (void)0 72 #define IS_DEBUG_ENABLED() (void)0 73 74 #define SERIAL_PRINT(ARGS) (void)0 75 #define PRINT(ARGS) (void)0 76 #define PRINT_OBJECT(OBJ) (void)0 77 #define TRACE() (void)0 78 #define SERIAL_TRACE() (void)0 79 80 #define DEBUGGER(MSG) (void)0 81 #if !defined(ASSERT) 82 #define ASSERT(E) (void)0 83 #endif 84 #define ASSERT_WITH_MESSAGE(expr, msg) (void)0 85 #define TRESPASS() (void)0 86 #define DEBUG_ONLY(x) 87 #endif 88 89 /* STATIC_ASSERT is a compile-time check that can be used to */ 90 /* verify static expressions such as: STATIC_ASSERT(sizeof(int64) == 8); */ 91 #define STATIC_ASSERT(x) \ 92 do { \ 93 struct __staticAssertStruct__ { \ 94 char __static_assert_failed__[2*(x) - 1]; \ 95 }; \ 96 } while (false) 97 98 99 #endif /* _DEBUG_H */ 100