1 /*
2 * Copyright (C) 2021 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 "flags.h"
18
19 #include <algorithm>
20
21 #include "android-base/parsebool.h"
22 #include "android-base/parseint.h"
23 #include "android-base/properties.h"
24
25 #include "base/utils.h"
26
27 #pragma clang diagnostic push
28 #pragma clang diagnostic error "-Wconversion"
29
30 namespace {
31 constexpr const char* kPhenotypeFlagPrefix = "persist.device_config.runtime_native.";
32 constexpr const char* kSysPropertyFlagPrefix = "dalvik.vm.";
33 constexpr const char* kUndefinedValue = "";
34
35 // The various ParseValue functions store the parsed value into *destination. If parsing fails for
36 // some reason, ParseValue makes no changes to *destination.
37
ParseValue(const std::string_view value,std::optional<bool> * destination)38 bool ParseValue(const std::string_view value, std::optional<bool>* destination) {
39 switch (::android::base::ParseBool(value)) {
40 case ::android::base::ParseBoolResult::kError:
41 return false;
42 case ::android::base::ParseBoolResult::kTrue:
43 *destination = true;
44 return true;
45 case ::android::base::ParseBoolResult::kFalse:
46 *destination = false;
47 return true;
48 }
49 }
50
ParseValue(const std::string_view value,std::optional<int32_t> * destination)51 bool ParseValue(const std::string_view value, std::optional<int32_t>* destination) {
52 int32_t parsed_value = 0;
53 if (::android::base::ParseInt(std::string{value}, &parsed_value)) {
54 *destination = parsed_value;
55 return true;
56 }
57 return false;
58 }
59
ParseValue(const std::string_view value,std::optional<uint32_t> * destination)60 bool ParseValue(const std::string_view value,
61 std::optional<uint32_t>* destination) {
62 uint32_t parsed_value = 0;
63 if (::android::base::ParseUint(std::string{value}, &parsed_value)) {
64 *destination = parsed_value;
65 return true;
66 }
67 return false;
68 }
69
ParseValue(const std::string_view value,std::optional<std::string> * destination)70 bool ParseValue(const std::string_view value, std::optional<std::string>* destination) {
71 *destination = value;
72 return true;
73 }
74
75 } // namespace
76
77 namespace art {
78
79 template <>
80 std::forward_list<FlagBase*> FlagBase::ALL_FLAGS{};
81
82 // gFlags must be defined after FlagBase::ALL_FLAGS so the constructors run in the right order.
83 Flags gFlags;
84
GenerateCmdLineArgName(const std::string & name)85 static std::string GenerateCmdLineArgName(const std::string& name) {
86 std::string result = "-X" + name + ":_";
87 std::replace(result.begin(), result.end(), '.', '-');
88 return result;
89 }
90
GenerateSysPropName(const std::string & name)91 static std::string GenerateSysPropName(const std::string& name) {
92 return kSysPropertyFlagPrefix + name;
93 }
94
GeneratePhenotypeName(const std::string & name)95 static std::string GeneratePhenotypeName(const std::string& name) {
96 return kPhenotypeFlagPrefix + name;
97 }
98
99 template <typename Value>
Flag(const std::string & name,Value default_value,FlagType type)100 Flag<Value>::Flag(const std::string& name, Value default_value, FlagType type) :
101 FlagBase(GenerateCmdLineArgName(name),
102 GenerateSysPropName(name),
103 GeneratePhenotypeName(name),
104 type),
105 initialized_{false},
106 default_{default_value} {
107 ALL_FLAGS.push_front(this);
108 }
109
110 template <typename Value>
~Flag()111 Flag<Value>::~Flag() {
112 ALL_FLAGS.remove(this);
113 }
114
115 template <typename Value>
Reload()116 void Flag<Value>::Reload() {
117 initialized_ = true;
118
119 // The cmdline flags are loaded by the parsed_options infra. No action needed here.
120 if (type_ == FlagType::kCmdlineOnly) {
121 return;
122 }
123
124 // Load system properties.
125 from_system_property_ = std::nullopt;
126 const std::string sysprop = ::android::base::GetProperty(system_property_name_, kUndefinedValue);
127 if (sysprop != kUndefinedValue) {
128 if (!ParseValue(sysprop, &from_system_property_)) {
129 LOG(ERROR) << "Failed to parse " << system_property_name_ << "=" << sysprop;
130 }
131 }
132
133 // Load the server-side configuration.
134 from_server_setting_ = std::nullopt;
135 const std::string server_config =
136 ::android::base::GetProperty(server_setting_name_, kUndefinedValue);
137 if (server_config != kUndefinedValue) {
138 if (!ParseValue(server_config, &from_server_setting_)) {
139 LOG(ERROR) << "Failed to parse " << server_setting_name_ << "=" << server_config;
140 }
141 }
142 }
143
144 template <typename Value>
DumpValue(std::ostream & oss,const std::optional<Value> & val)145 void DumpValue(std::ostream& oss, const std::optional<Value>& val) {
146 if (val.has_value()) {
147 oss << val.value();
148 } else {
149 oss << kUndefinedValue;
150 }
151 }
152
153 template <typename Value>
Dump(std::ostream & oss) const154 void Flag<Value>::Dump(std::ostream& oss) const {
155 std::pair<Value, FlagOrigin> valueOrigin = GetValueAndOrigin();
156 std::string origin;
157 switch (std::get<1>(valueOrigin)) {
158 case FlagOrigin::kDefaultValue: origin = "default_value"; break;
159 case FlagOrigin::kCmdlineArg: origin = "cmdline_arg"; break;
160 case FlagOrigin::kSystemProperty: origin = "system_property"; break;
161 case FlagOrigin::kServerSetting: origin = "server_setting"; break;
162 }
163
164 oss << "value: " << std::get<0>(valueOrigin) << " (from " << origin << ")";
165
166 oss << "\n default: " << default_;
167 oss << "\n " << command_line_argument_name_ << ": ";
168 DumpValue(oss, from_command_line_);
169 oss << "\n " << system_property_name_ << ": ";
170 DumpValue(oss, from_system_property_);
171 oss << "\n " << server_setting_name_ << ": ";
172 DumpValue(oss, from_server_setting_);
173 }
174
175 template class Flag<bool>;
176 template class Flag<int>;
177 template class Flag<std::string>;
178
179 } // namespace art
180
181 #pragma clang diagnostic pop // -Wconversion
182