1 // 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 #ifndef DebugAndroid_hpp
16 #define DebugAndroid_hpp
17 
18 #if ANDROID_PLATFORM_SDK_VERSION < 27
19 #include <cutils/log.h>
20 #elif ANDROID_PLATFORM_SDK_VERSION >= 27
21 #include <log/log.h>
22 #else
23 #error "ANDROID_PLATFORM_SDK_VERSION is not defined"
24 #endif
25 
26 #include <cassert>
27 
28 // On Android Virtual Devices we heavily depend on logging, even in
29 // production builds. We do this because AVDs are components of larger
30 // systems, and may be configured in ways that are difficult to
31 // reproduce locally. For example some system run tests against
32 // third-party code that we cannot access.  Aborting (cf. assert) on
33 // unimplemented functionality creates two problems. First, it produces
34 // a service failure where none is needed. Second, it puts the
35 // customer on the critical path for notifying us of a problem.
36 // The alternative, skipping unimplemented functionality silently, is
37 // arguably worse: neither the service provider nor the customer will
38 // learn that unimplemented functionality may have compromised the test
39 // results.
40 // Logging invocations of unimplemented functionality is useful to both
41 // service provider and the customer. The service provider can learn
42 // that the functionality is needed. The customer learns that the test
43 // results may be compromised.
44 
45 /**
46  * Enter the debugger with a memory fault iff debuggerd is set to capture this
47  * process. Otherwise return.
48  */
49 void AndroidEnterDebugger();
50 
51 #define ASSERT(E) do { \
52 		if (!(E)) { \
53 			ALOGE("badness: assertion_failed %s in %s at %s:%d", #E,	\
54 				  __FUNCTION__, __FILE__, __LINE__);					\
55 			AndroidEnterDebugger();										\
56 		}																\
57 	} while(0)
58 
59 #undef assert
60 #define assert(E) ASSERT(E)
61 
62 #define ERR(format, ...)												\
63 	do {																\
64 		ALOGE("badness: err %s %s:%d (" format ")", __FUNCTION__, __FILE__, \
65 			  __LINE__, ##__VA_ARGS__);									\
66 		AndroidEnterDebugger();											\
67 	} while(0)
68 
69 #define FIXME(format, ...)												\
70 	do {																\
71 		ALOGE("badness: fixme %s %s:%d (" format ")", __FUNCTION__, __FILE__, \
72 			  __LINE__, ##__VA_ARGS__);									\
73 		AndroidEnterDebugger();											\
74 	} while(0)
75 
76 // TODO: Handle __VA_ARGS__ (can be empty)
77 #define UNIMPLEMENTED(...) do {						\
78 		ALOGE("badness: unimplemented: %s %s:%d",	\
79 			  __FUNCTION__, __FILE__, __LINE__);	\
80 		AndroidEnterDebugger();						\
81 	} while(0)
82 
83 #define UNREACHABLE(value) do {                                         \
84 		ALOGE("badness: unreachable case reached: %s %s:%d. %s: %d", \
85 			  __FUNCTION__, __FILE__, __LINE__, #value, value);			\
86 		AndroidEnterDebugger();                                         \
87 	} while(0)
88 
89 #ifndef NDEBUG
90 	#define TRACE(format, ...)								   \
91 		ALOGV("%s %s:%d (" format ")", __FUNCTION__, __FILE__, \
92 			  __LINE__, ##__VA_ARGS__)
93 #else
94 	#define TRACE(...) ((void)0)
95 #endif
96 
97 void trace(const char *format, ...);
98 
99 #endif   // DebugAndroid_hpp
100