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 #include "runtime_debug.h"
18 
19 #include <vector>
20 
21 #include "globals.h"
22 
23 namespace art {
24 
25 // We test here that the runtime-debug-checks are actually a no-op constexpr false in release
26 // builds, as we can't check that in gtests (which are always debug).
27 
28 #ifdef NDEBUG
29 namespace {
30 DECLARE_RUNTIME_DEBUG_FLAG(kTestForConstexpr);
31 static_assert(!kTestForConstexpr, "Issue with DECLARE_RUNTIME_DEBUG_FLAG in NDEBUG.");
32 }
33 #endif
34 
35 // Implementation of runtime debug flags. This should be compile-time optimized away in release
36 // builds.
37 namespace {
38 bool gSlowEnabled = false;  // Default for slow flags is "off."
39 
40 // Use a function with a static to ensure our vector storage doesn't have initialization order
41 // issues.
GetFlagPtrs()42 std::vector<bool*>& GetFlagPtrs() {
43   static std::vector<bool*> g_flag_ptrs;
44   return g_flag_ptrs;
45 }
46 
RegisterRuntimeDebugFlagImpl(bool * flag_ptr)47 bool RegisterRuntimeDebugFlagImpl(bool* flag_ptr) {
48   GetFlagPtrs().push_back(flag_ptr);
49   return gSlowEnabled;
50 }
51 
SetRuntimeDebugFlagsEnabledImpl(bool enabled)52 void SetRuntimeDebugFlagsEnabledImpl(bool enabled) {
53   gSlowEnabled = enabled;
54   for (bool* flag_ptr : GetFlagPtrs()) {
55     *flag_ptr = enabled;
56   }
57 }
58 
59 }  // namespace
60 
RegisterRuntimeDebugFlag(bool * flag_ptr)61 bool RegisterRuntimeDebugFlag(bool* flag_ptr) {
62   if (kIsDebugBuild) {
63     return RegisterRuntimeDebugFlagImpl(flag_ptr);
64   }
65   return false;
66 }
67 
SetRuntimeDebugFlagsEnabled(bool enabled)68 void SetRuntimeDebugFlagsEnabled(bool enabled) {
69   if (kIsDebugBuild) {
70     SetRuntimeDebugFlagsEnabledImpl(enabled);
71   }
72 }
73 
74 }  // namespace art
75