blob: ce488a21a9acd0af9b23afb5b64a742f928f6bec [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// debug.h: Debugging utilities.
16
17#ifndef COMMON_DEBUG_H_
18#define COMMON_DEBUG_H_
19
Stephen Whitee6ab01f2019-04-04 14:31:25 -040020#if defined(__ANDROID__) && !defined(ANDROID_HOST_BUILD) && !defined(ANDROID_NDK_BUILD)
Nicolas Capens0bac2852016-05-07 06:09:58 -040021#include "../../Common/DebugAndroid.hpp"
22#else
23#include <stdio.h>
24#include <assert.h>
25
26#if !defined(TRACE_OUTPUT_FILE)
27#define TRACE_OUTPUT_FILE "debug.txt"
28#endif
29
30namespace es
31{
Nicolas Capensa5dfd972018-09-28 15:27:08 -040032// Outputs text to the debugging log
33void trace(const char *format, ...);
34inline void trace() {}
Nicolas Capens0bac2852016-05-07 06:09:58 -040035}
36
37// A macro to output a trace of a function call and its arguments to the debugging log
38#if defined(ANGLE_DISABLE_TRACE)
39#define TRACE(message, ...) (void(0))
40#else
41#define TRACE(message, ...) es::trace("trace: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__)
42#endif
43
44// A macro to output a function call and its arguments to the debugging log, to denote an item in need of fixing.
45#if defined(ANGLE_DISABLE_TRACE)
46#define FIXME(message, ...) (void(0))
47#else
48#define FIXME(message, ...) do {es::trace("fixme: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); assert(false);} while(false)
49#endif
50
51// A macro to output a function call and its arguments to the debugging log, in case of error.
52#if defined(ANGLE_DISABLE_TRACE)
53#define ERR(message, ...) (void(0))
54#else
55#define ERR(message, ...) do {es::trace("err: %s(%d): " message "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); assert(false);} while(false)
56#endif
57
58// A macro asserting a condition and outputting failures to the debug log
59#undef ASSERT
Alexis Hetu0c5035b2017-09-20 15:55:08 -040060#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
Nicolas Capens0bac2852016-05-07 06:09:58 -040061#define ASSERT(expression) do { \
Nicolas Capens9e8bfca2018-02-22 16:39:20 -050062 if(!(expression)) { \
Nicolas Capens0bac2852016-05-07 06:09:58 -040063 ERR("\t! Assert failed in %s(%d): "#expression"\n", __FUNCTION__, __LINE__); \
64 assert(expression); \
Nicolas Capens9e8bfca2018-02-22 16:39:20 -050065 } } while(0)
Nicolas Capens0bac2852016-05-07 06:09:58 -040066#else
67#define ASSERT(expression) (void(0))
68#endif
69
70// A macro to indicate unimplemented functionality
71#undef UNIMPLEMENTED
Alexis Hetu0c5035b2017-09-20 15:55:08 -040072#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
Nicolas Capensa5dfd972018-09-28 15:27:08 -040073#define UNIMPLEMENTED(...) do { \
74 es::trace("\t! Unimplemented: %s(%d): ", __FUNCTION__, __LINE__); \
Nicolas Capens60be5c42018-10-02 10:49:22 -040075 es::trace(__VA_ARGS__); \
Nicolas Capensa5dfd972018-09-28 15:27:08 -040076 es::trace("\n"); \
Nicolas Capens0bac2852016-05-07 06:09:58 -040077 assert(false); \
78 } while(0)
79#else
Nicolas Capensa5dfd972018-09-28 15:27:08 -040080 #define UNIMPLEMENTED(...) FIXME("\t! Unimplemented: %s(%d)\n", __FUNCTION__, __LINE__)
Nicolas Capens0bac2852016-05-07 06:09:58 -040081#endif
82
83// A macro for code which is not expected to be reached under valid assumptions
84#undef UNREACHABLE
Alexis Hetu0c5035b2017-09-20 15:55:08 -040085#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
Nicolas Capens0bac2852016-05-07 06:09:58 -040086#define UNREACHABLE(value) do { \
87 ERR("\t! Unreachable case reached: %s(%d). %s: %d\n", __FUNCTION__, __LINE__, #value, value); \
88 assert(false); \
89 } while(0)
90#else
91 #define UNREACHABLE(value) ERR("\t! Unreachable reached: %s(%d). %s: %d\n", __FUNCTION__, __LINE__, #value, value)
92#endif
93
Nicolas Capens9e8bfca2018-02-22 16:39:20 -050094#endif // !__ANDROID__
95
96// A macro asserting a condition and outputting failures to the debug log, or return when in release mode.
97#undef ASSERT_OR_RETURN
98#define ASSERT_OR_RETURN(expression) do { \
99 if(!(expression)) { \
100 ASSERT(expression); \
101 return; \
102 } } while(0)
Nicolas Capens0bac2852016-05-07 06:09:58 -0400103
Nicolas Capens0bac2852016-05-07 06:09:58 -0400104#endif // COMMON_DEBUG_H_