1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_LIBARTBASE_BASE_RUNTIME_DEBUG_H_
18 #define ART_LIBARTBASE_BASE_RUNTIME_DEBUG_H_
19 
20 namespace art {
21 
22 // Runtime debug flags are flags that have a runtime component, that is, their value can be changed.
23 // This is meant to implement fast vs slow debug builds, in that certain debug flags can be turned
24 // on and off. To that effect, expose two macros to help implement and globally drive these flags:
25 //
26 // In the header, declare a (class) flag like this:
27 //
28 //   class C {
29 //     DECLARE_RUNTIME_DEBUG_FLAG(kFlag);
30 //   };
31 //
32 // This will declare a flag kFlag that is a constexpr false in release builds, and a static field
33 // in debug builds. Usage is than uniform as C::kFlag.
34 //
35 // In the cc file, define the flag like this:
36 //
37 //   DEFINE_RUNTIME_DEBUG_FLAG(C, kFlag);
38 //
39 // This will define the static storage, as necessary, and register the flag with the runtime
40 // infrastructure to toggle the value.
41 
42 #ifdef NDEBUG
43 #define DECLARE_RUNTIME_DEBUG_FLAG(x) \
44   static constexpr bool x = false;
45 // Note: the static_assert in the following only works for public flags. Fix this when we cross
46 //       the line at some point.
47 #define DEFINE_RUNTIME_DEBUG_FLAG(C, x) \
48   static_assert(!C::x, "Unexpected enabled flag in release build");
49 #else
50 #define DECLARE_RUNTIME_DEBUG_FLAG(x) \
51   static bool x;
52 #define DEFINE_RUNTIME_DEBUG_FLAG(C, x) \
53   bool C::x = RegisterRuntimeDebugFlag(&C::x);
54 #endif  // NDEBUG
55 
56 bool RegisterRuntimeDebugFlag(bool* runtime_debug_flag);
57 void SetRuntimeDebugFlagsEnabled(bool enabled);
58 
59 }  // namespace art
60 
61 #endif  // ART_LIBARTBASE_BASE_RUNTIME_DEBUG_H_
62