Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "cmdline_parser.h" |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 18 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 19 | #include <numeric> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 21 | #include "gtest/gtest.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 22 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 23 | #include "base/utils.h" |
Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 24 | #include "jdwp_provider.h" |
Andreas Gampe | 2c30e4a | 2017-08-23 11:31:32 -0700 | [diff] [blame] | 25 | #include "experimental_flags.h" |
| 26 | #include "parsed_options.h" |
| 27 | #include "runtime.h" |
| 28 | #include "runtime_options.h" |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 29 | |
| 30 | #define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \ |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 31 | reinterpret_cast<void*>(nullptr)); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | bool UsuallyEquals(double expected, double actual); |
| 35 | |
| 36 | // This has a gtest dependency, which is why it's in the gtest only. |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 37 | bool operator==(const ProfileSaverOptions& lhs, const ProfileSaverOptions& rhs) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 38 | return lhs.enabled_ == rhs.enabled_ && |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 39 | lhs.min_save_period_ms_ == rhs.min_save_period_ms_ && |
| 40 | lhs.save_resolved_classes_delay_ms_ == rhs.save_resolved_classes_delay_ms_ && |
Mathieu Chartier | 7b135c8 | 2017-06-05 12:54:01 -0700 | [diff] [blame] | 41 | lhs.hot_startup_method_samples_ == rhs.hot_startup_method_samples_ && |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 42 | lhs.min_methods_to_save_ == rhs.min_methods_to_save_ && |
| 43 | lhs.min_classes_to_save_ == rhs.min_classes_to_save_ && |
| 44 | lhs.min_notification_before_wake_ == rhs.min_notification_before_wake_ && |
| 45 | lhs.max_notification_before_wake_ == rhs.max_notification_before_wake_; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | bool UsuallyEquals(double expected, double actual) { |
| 49 | using FloatingPoint = ::testing::internal::FloatingPoint<double>; |
| 50 | |
| 51 | FloatingPoint exp(expected); |
| 52 | FloatingPoint act(actual); |
| 53 | |
| 54 | // Compare with ULPs instead of comparing with == |
| 55 | return exp.AlmostEquals(act); |
| 56 | } |
| 57 | |
| 58 | template <typename T> |
| 59 | bool UsuallyEquals(const T& expected, const T& actual, |
| 60 | typename std::enable_if< |
Yi Kong | 4b22b34 | 2018-08-02 14:43:21 -0700 | [diff] [blame] | 61 | detail::SupportsEqualityOperator<T>::value>::type* = nullptr) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 62 | return expected == actual; |
| 63 | } |
| 64 | |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 65 | template <char Separator> |
| 66 | bool UsuallyEquals(const std::vector<std::string>& expected, |
| 67 | const ParseStringList<Separator>& actual) { |
| 68 | return expected == static_cast<std::vector<std::string>>(actual); |
| 69 | } |
| 70 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 71 | // Try to use memcmp to compare simple plain-old-data structs. |
| 72 | // |
| 73 | // This should *not* generate false positives, but it can generate false negatives. |
| 74 | // This will mostly work except for fields like float which can have different bit patterns |
| 75 | // that are nevertheless equal. |
| 76 | // If a test is failing because the structs aren't "equal" when they really are |
| 77 | // then it's recommended to implement operator== for it instead. |
| 78 | template <typename T, typename ... Ignore> |
| 79 | bool UsuallyEquals(const T& expected, const T& actual, |
| 80 | const Ignore& ... more ATTRIBUTE_UNUSED, |
Yi Kong | 4b22b34 | 2018-08-02 14:43:21 -0700 | [diff] [blame] | 81 | typename std::enable_if<std::is_pod<T>::value>::type* = nullptr, |
| 82 | typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type* = nullptr |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 83 | ) { |
| 84 | return memcmp(std::addressof(expected), std::addressof(actual), sizeof(T)) == 0; |
| 85 | } |
| 86 | |
| 87 | bool UsuallyEquals(const XGcOption& expected, const XGcOption& actual) { |
| 88 | return memcmp(std::addressof(expected), std::addressof(actual), sizeof(expected)) == 0; |
| 89 | } |
| 90 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 91 | bool UsuallyEquals(const char* expected, const std::string& actual) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 92 | return std::string(expected) == actual; |
| 93 | } |
| 94 | |
| 95 | template <typename TMap, typename TKey, typename T> |
| 96 | ::testing::AssertionResult IsExpectedKeyValue(const T& expected, |
| 97 | const TMap& map, |
| 98 | const TKey& key) { |
| 99 | auto* actual = map.Get(key); |
| 100 | if (actual != nullptr) { |
| 101 | if (!UsuallyEquals(expected, *actual)) { |
| 102 | return ::testing::AssertionFailure() |
| 103 | << "expected " << detail::ToStringAny(expected) << " but got " |
| 104 | << detail::ToStringAny(*actual); |
| 105 | } |
| 106 | return ::testing::AssertionSuccess(); |
| 107 | } |
| 108 | |
| 109 | return ::testing::AssertionFailure() << "key was not in the map"; |
| 110 | } |
| 111 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 112 | template <typename TMap, typename TKey, typename T> |
| 113 | ::testing::AssertionResult IsExpectedDefaultKeyValue(const T& expected, |
| 114 | const TMap& map, |
| 115 | const TKey& key) { |
| 116 | const T& actual = map.GetOrDefault(key); |
| 117 | if (!UsuallyEquals(expected, actual)) { |
| 118 | return ::testing::AssertionFailure() |
| 119 | << "expected " << detail::ToStringAny(expected) << " but got " |
| 120 | << detail::ToStringAny(actual); |
| 121 | } |
| 122 | return ::testing::AssertionSuccess(); |
| 123 | } |
| 124 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 125 | class CmdlineParserTest : public ::testing::Test { |
| 126 | public: |
| 127 | CmdlineParserTest() = default; |
| 128 | ~CmdlineParserTest() = default; |
| 129 | |
| 130 | protected: |
| 131 | using M = RuntimeArgumentMap; |
| 132 | using RuntimeParser = ParsedOptions::RuntimeParser; |
| 133 | |
| 134 | static void SetUpTestCase() { |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 135 | art::Locks::Init(); |
Andreas Gampe | 51d80cc | 2017-06-21 21:05:13 -0700 | [diff] [blame] | 136 | art::InitLogging(nullptr, art::Runtime::Abort); // argv = null |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 139 | void SetUp() override { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 140 | parser_ = ParsedOptions::MakeParser(false); // do not ignore unrecognized options |
| 141 | } |
| 142 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 143 | static ::testing::AssertionResult IsResultSuccessful(const CmdlineResult& result) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 144 | if (result.IsSuccess()) { |
| 145 | return ::testing::AssertionSuccess(); |
| 146 | } else { |
| 147 | return ::testing::AssertionFailure() |
| 148 | << result.GetStatus() << " with: " << result.GetMessage(); |
| 149 | } |
| 150 | } |
| 151 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 152 | static ::testing::AssertionResult IsResultFailure(const CmdlineResult& result, |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 153 | CmdlineResult::Status failure_status) { |
| 154 | if (result.IsSuccess()) { |
| 155 | return ::testing::AssertionFailure() << " got success but expected failure: " |
| 156 | << failure_status; |
| 157 | } else if (result.GetStatus() == failure_status) { |
| 158 | return ::testing::AssertionSuccess(); |
| 159 | } |
| 160 | |
| 161 | return ::testing::AssertionFailure() << " expected failure " << failure_status |
| 162 | << " but got " << result.GetStatus(); |
| 163 | } |
| 164 | |
| 165 | std::unique_ptr<RuntimeParser> parser_; |
| 166 | }; |
| 167 | |
| 168 | #define EXPECT_KEY_EXISTS(map, key) EXPECT_TRUE((map).Exists(key)) |
| 169 | #define EXPECT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedKeyValue(expected, map, key)) |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 170 | #define EXPECT_DEFAULT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedDefaultKeyValue(expected, map, key)) |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 171 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 172 | #define _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \ |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 173 | do { \ |
| 174 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \ |
| 175 | EXPECT_EQ(0u, parser_->GetArgumentsMap().Size()); \ |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 176 | |
| 177 | #define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \ |
| 178 | _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \ |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 179 | } while (false) |
| 180 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 181 | #define EXPECT_SINGLE_PARSE_DEFAULT_VALUE(expected, argv, key)\ |
| 182 | _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \ |
| 183 | RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \ |
| 184 | EXPECT_DEFAULT_KEY_VALUE(args, key, expected); \ |
| 185 | } while (false) // NOLINT [readability/namespace] [5] |
| 186 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 187 | #define _EXPECT_SINGLE_PARSE_EXISTS(argv, key) \ |
| 188 | do { \ |
| 189 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \ |
| 190 | RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \ |
| 191 | EXPECT_EQ(1u, args.Size()); \ |
| 192 | EXPECT_KEY_EXISTS(args, key); \ |
| 193 | |
| 194 | #define EXPECT_SINGLE_PARSE_EXISTS(argv, key) \ |
| 195 | _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \ |
| 196 | } while (false) |
| 197 | |
| 198 | #define EXPECT_SINGLE_PARSE_VALUE(expected, argv, key) \ |
| 199 | _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \ |
| 200 | EXPECT_KEY_VALUE(args, key, expected); \ |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 201 | } while (false) |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 202 | |
| 203 | #define EXPECT_SINGLE_PARSE_VALUE_STR(expected, argv, key) \ |
| 204 | EXPECT_SINGLE_PARSE_VALUE(std::string(expected), argv, key) |
| 205 | |
| 206 | #define EXPECT_SINGLE_PARSE_FAIL(argv, failure_status) \ |
| 207 | do { \ |
| 208 | EXPECT_TRUE(IsResultFailure(parser_->Parse(argv), failure_status));\ |
| 209 | RuntimeArgumentMap args = parser_->ReleaseArgumentsMap();\ |
| 210 | EXPECT_EQ(0u, args.Size()); \ |
| 211 | } while (false) |
| 212 | |
| 213 | TEST_F(CmdlineParserTest, TestSimpleSuccesses) { |
| 214 | auto& parser = *parser_; |
| 215 | |
| 216 | EXPECT_LT(0u, parser.CountDefinedArguments()); |
| 217 | |
| 218 | { |
| 219 | // Test case 1: No command line arguments |
| 220 | EXPECT_TRUE(IsResultSuccessful(parser.Parse(""))); |
| 221 | RuntimeArgumentMap args = parser.ReleaseArgumentsMap(); |
| 222 | EXPECT_EQ(0u, args.Size()); |
| 223 | } |
| 224 | |
| 225 | EXPECT_SINGLE_PARSE_EXISTS("-Xzygote", M::Zygote); |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 226 | EXPECT_SINGLE_PARSE_VALUE(std::vector<std::string>({"/hello/world"}), |
| 227 | "-Xbootclasspath:/hello/world", |
| 228 | M::BootClassPath); |
| 229 | EXPECT_SINGLE_PARSE_VALUE(std::vector<std::string>({"/hello", "/world"}), |
| 230 | "-Xbootclasspath:/hello:/world", |
| 231 | M::BootClassPath); |
| 232 | EXPECT_SINGLE_PARSE_VALUE_STR("/hello/world", "-classpath /hello/world", M::ClassPath); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 233 | EXPECT_SINGLE_PARSE_VALUE(Memory<1>(234), "-Xss234", M::StackSize); |
| 234 | EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(1234*MB), "-Xms1234m", M::MemoryInitialSize); |
| 235 | EXPECT_SINGLE_PARSE_VALUE(true, "-XX:EnableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM); |
| 236 | EXPECT_SINGLE_PARSE_VALUE(false, "-XX:DisableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM); |
| 237 | EXPECT_SINGLE_PARSE_VALUE(0.5, "-XX:HeapTargetUtilization=0.5", M::HeapTargetUtilization); |
| 238 | EXPECT_SINGLE_PARSE_VALUE(5u, "-XX:ParallelGCThreads=5", M::ParallelGCThreads); |
Jean Christophe Beyler | 24e04aa | 2014-09-12 12:03:25 -0700 | [diff] [blame] | 239 | EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 240 | } // TEST_F |
| 241 | |
| 242 | TEST_F(CmdlineParserTest, TestSimpleFailures) { |
| 243 | // Test argument is unknown to the parser |
| 244 | EXPECT_SINGLE_PARSE_FAIL("abcdefg^%@#*(@#", CmdlineResult::kUnknown); |
| 245 | // Test value map substitution fails |
| 246 | EXPECT_SINGLE_PARSE_FAIL("-Xverify:whatever", CmdlineResult::kFailure); |
| 247 | // Test value type parsing failures |
| 248 | EXPECT_SINGLE_PARSE_FAIL("-Xsswhatever", CmdlineResult::kFailure); // invalid memory value |
| 249 | EXPECT_SINGLE_PARSE_FAIL("-Xms123", CmdlineResult::kFailure); // memory value too small |
| 250 | EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=0.0", CmdlineResult::kOutOfRange); // toosmal |
| 251 | EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=2.0", CmdlineResult::kOutOfRange); // toolarg |
| 252 | EXPECT_SINGLE_PARSE_FAIL("-XX:ParallelGCThreads=-5", CmdlineResult::kOutOfRange); // too small |
| 253 | EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // not a valid suboption |
| 254 | } // TEST_F |
| 255 | |
| 256 | TEST_F(CmdlineParserTest, TestLogVerbosity) { |
| 257 | { |
| 258 | const char* log_args = "-verbose:" |
Mathieu Chartier | 765b2a0 | 2019-05-02 11:04:13 -0700 | [diff] [blame] | 259 | "class,compiler,gc,heap,interpreter,jdwp,jni,monitor,profiler,signals,simulator,startup," |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 260 | "third-party-jni,threads,verifier,verifier-debug"; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 261 | |
| 262 | LogVerbosity log_verbosity = LogVerbosity(); |
| 263 | log_verbosity.class_linker = true; |
| 264 | log_verbosity.compiler = true; |
| 265 | log_verbosity.gc = true; |
| 266 | log_verbosity.heap = true; |
Mathieu Chartier | 765b2a0 | 2019-05-02 11:04:13 -0700 | [diff] [blame] | 267 | log_verbosity.interpreter = true; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 268 | log_verbosity.jdwp = true; |
| 269 | log_verbosity.jni = true; |
| 270 | log_verbosity.monitor = true; |
| 271 | log_verbosity.profiler = true; |
| 272 | log_verbosity.signals = true; |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 273 | log_verbosity.simulator = true; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 274 | log_verbosity.startup = true; |
| 275 | log_verbosity.third_party_jni = true; |
| 276 | log_verbosity.threads = true; |
| 277 | log_verbosity.verifier = true; |
Andreas Gampe | 92d7720 | 2017-12-06 20:49:00 -0800 | [diff] [blame] | 278 | log_verbosity.verifier_debug = true; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 279 | |
| 280 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 281 | } |
| 282 | |
| 283 | { |
| 284 | const char* log_args = "-verbose:" |
| 285 | "class,compiler,gc,heap,jdwp,jni,monitor"; |
| 286 | |
| 287 | LogVerbosity log_verbosity = LogVerbosity(); |
| 288 | log_verbosity.class_linker = true; |
| 289 | log_verbosity.compiler = true; |
| 290 | log_verbosity.gc = true; |
| 291 | log_verbosity.heap = true; |
| 292 | log_verbosity.jdwp = true; |
| 293 | log_verbosity.jni = true; |
| 294 | log_verbosity.monitor = true; |
| 295 | |
| 296 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 297 | } |
| 298 | |
| 299 | EXPECT_SINGLE_PARSE_FAIL("-verbose:blablabla", CmdlineResult::kUsage); // invalid verbose opt |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 300 | |
| 301 | { |
Sebastien Hertz | bba348e | 2015-06-01 08:28:18 +0200 | [diff] [blame] | 302 | const char* log_args = "-verbose:deopt"; |
| 303 | LogVerbosity log_verbosity = LogVerbosity(); |
| 304 | log_verbosity.deopt = true; |
| 305 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 306 | } |
| 307 | |
| 308 | { |
Mathieu Chartier | 66a5539 | 2016-02-19 10:25:39 -0800 | [diff] [blame] | 309 | const char* log_args = "-verbose:collector"; |
| 310 | LogVerbosity log_verbosity = LogVerbosity(); |
| 311 | log_verbosity.collector = true; |
| 312 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 313 | } |
| 314 | |
| 315 | { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 316 | const char* log_args = "-verbose:oat"; |
| 317 | LogVerbosity log_verbosity = LogVerbosity(); |
| 318 | log_verbosity.oat = true; |
| 319 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 320 | } |
Andreas Gampe | bec07a0 | 2017-04-11 13:48:37 -0700 | [diff] [blame] | 321 | |
| 322 | { |
| 323 | const char* log_args = "-verbose:dex"; |
| 324 | LogVerbosity log_verbosity = LogVerbosity(); |
| 325 | log_verbosity.dex = true; |
| 326 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 327 | } |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 328 | } // TEST_F |
| 329 | |
Nicolas Geoffray | 8f4ee5c | 2015-02-05 10:14:10 +0000 | [diff] [blame] | 330 | // TODO: Enable this b/19274810 |
| 331 | TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 332 | /* |
| 333 | * Test success |
| 334 | */ |
| 335 | { |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 336 | XGcOption option_all_true{}; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 337 | option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS; |
| 338 | option_all_true.verify_pre_gc_heap_ = true; |
| 339 | option_all_true.verify_pre_sweeping_heap_ = true; |
| 340 | option_all_true.verify_post_gc_heap_ = true; |
| 341 | option_all_true.verify_pre_gc_rosalloc_ = true; |
| 342 | option_all_true.verify_pre_sweeping_rosalloc_ = true; |
| 343 | option_all_true.verify_post_gc_rosalloc_ = true; |
| 344 | |
| 345 | const char * xgc_args_all_true = "-Xgc:concurrent," |
| 346 | "preverify,presweepingverify,postverify," |
| 347 | "preverify_rosalloc,presweepingverify_rosalloc," |
| 348 | "postverify_rosalloc,precise," |
| 349 | "verifycardtable"; |
| 350 | |
| 351 | EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption); |
| 352 | |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 353 | XGcOption option_all_false{}; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 354 | option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS; |
| 355 | option_all_false.verify_pre_gc_heap_ = false; |
| 356 | option_all_false.verify_pre_sweeping_heap_ = false; |
| 357 | option_all_false.verify_post_gc_heap_ = false; |
| 358 | option_all_false.verify_pre_gc_rosalloc_ = false; |
| 359 | option_all_false.verify_pre_sweeping_rosalloc_ = false; |
| 360 | option_all_false.verify_post_gc_rosalloc_ = false; |
| 361 | |
| 362 | const char* xgc_args_all_false = "-Xgc:nonconcurrent," |
| 363 | "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc," |
| 364 | "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable"; |
| 365 | |
| 366 | EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption); |
| 367 | |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 368 | XGcOption option_all_default{}; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 369 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 370 | const char* xgc_args_blank = "-Xgc:"; |
| 371 | EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption); |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * Test failures |
| 376 | */ |
| 377 | EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // invalid Xgc opt |
| 378 | } // TEST_F |
| 379 | |
| 380 | /* |
Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 381 | * { "-XjdwpProvider:_" } |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 382 | */ |
Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 383 | TEST_F(CmdlineParserTest, TestJdwpProviderEmpty) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 384 | { |
Alex Light | baac7e4 | 2018-06-08 15:30:11 +0000 | [diff] [blame] | 385 | EXPECT_SINGLE_PARSE_DEFAULT_VALUE(JdwpProvider::kUnset, "", M::JdwpProvider); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 386 | } |
Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 387 | } // TEST_F |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 388 | |
Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 389 | TEST_F(CmdlineParserTest, TestJdwpProviderDefault) { |
| 390 | const char* opt_args = "-XjdwpProvider:default"; |
Alex Light | 2183d4d | 2018-01-26 14:24:54 -0800 | [diff] [blame] | 391 | EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kDefaultJdwpProvider, opt_args, M::JdwpProvider); |
Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 392 | } // TEST_F |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 393 | |
Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 394 | TEST_F(CmdlineParserTest, TestJdwpProviderNone) { |
| 395 | const char* opt_args = "-XjdwpProvider:none"; |
| 396 | EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kNone, opt_args, M::JdwpProvider); |
| 397 | } // TEST_F |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 398 | |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 399 | TEST_F(CmdlineParserTest, TestJdwpProviderAdbconnection) { |
| 400 | const char* opt_args = "-XjdwpProvider:adbconnection"; |
| 401 | EXPECT_SINGLE_PARSE_VALUE(JdwpProvider::kAdbConnection, opt_args, M::JdwpProvider); |
| 402 | } // TEST_F |
| 403 | |
Alex Light | 4032071 | 2017-12-14 11:52:04 -0800 | [diff] [blame] | 404 | TEST_F(CmdlineParserTest, TestJdwpProviderHelp) { |
| 405 | EXPECT_SINGLE_PARSE_FAIL("-XjdwpProvider:help", CmdlineResult::kUsage); |
| 406 | } // TEST_F |
| 407 | |
| 408 | TEST_F(CmdlineParserTest, TestJdwpProviderFail) { |
| 409 | EXPECT_SINGLE_PARSE_FAIL("-XjdwpProvider:blablabla", CmdlineResult::kFailure); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 410 | } // TEST_F |
| 411 | |
| 412 | /* |
| 413 | * -D_ -D_ -D_ ... |
| 414 | */ |
| 415 | TEST_F(CmdlineParserTest, TestPropertiesList) { |
| 416 | /* |
| 417 | * Test successes |
| 418 | */ |
| 419 | { |
| 420 | std::vector<std::string> opt = {"hello"}; |
| 421 | |
| 422 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList); |
| 423 | } |
| 424 | |
| 425 | { |
| 426 | std::vector<std::string> opt = {"hello", "world"}; |
| 427 | |
| 428 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList); |
| 429 | } |
| 430 | |
| 431 | { |
| 432 | std::vector<std::string> opt = {"one", "two", "three"}; |
| 433 | |
| 434 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList); |
| 435 | } |
| 436 | } // TEST_F |
| 437 | |
| 438 | /* |
| 439 | * -Xcompiler-option foo -Xcompiler-option bar ... |
| 440 | */ |
| 441 | TEST_F(CmdlineParserTest, TestCompilerOption) { |
| 442 | /* |
| 443 | * Test successes |
| 444 | */ |
| 445 | { |
| 446 | std::vector<std::string> opt = {"hello"}; |
| 447 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions); |
| 448 | } |
| 449 | |
| 450 | { |
| 451 | std::vector<std::string> opt = {"hello", "world"}; |
| 452 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 453 | "-Xcompiler-option hello -Xcompiler-option world", |
| 454 | M::CompilerOptions); |
| 455 | } |
| 456 | |
| 457 | { |
| 458 | std::vector<std::string> opt = {"one", "two", "three"}; |
| 459 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 460 | "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three", |
| 461 | M::CompilerOptions); |
| 462 | } |
| 463 | } // TEST_F |
| 464 | |
| 465 | /* |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 466 | * -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold |
| 467 | */ |
| 468 | TEST_F(CmdlineParserTest, TestJitOptions) { |
| 469 | /* |
| 470 | * Test successes |
| 471 | */ |
| 472 | { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 473 | EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJitCompilation); |
| 474 | EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJitCompilation); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 475 | } |
| 476 | { |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 477 | EXPECT_SINGLE_PARSE_VALUE( |
Nicolas Geoffray | 295a596 | 2015-11-19 18:17:41 +0000 | [diff] [blame] | 478 | MemoryKiB(16 * KB), "-Xjitinitialsize:16K", M::JITCodeCacheInitialCapacity); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 479 | EXPECT_SINGLE_PARSE_VALUE( |
Nicolas Geoffray | 295a596 | 2015-11-19 18:17:41 +0000 | [diff] [blame] | 480 | MemoryKiB(16 * MB), "-Xjitmaxsize:16M", M::JITCodeCacheMaxCapacity); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 481 | } |
| 482 | { |
| 483 | EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold); |
| 484 | } |
| 485 | } // TEST_F |
| 486 | |
| 487 | /* |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 488 | * -Xps-* |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 489 | */ |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 490 | TEST_F(CmdlineParserTest, ProfileSaverOptions) { |
Mathieu Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame] | 491 | ProfileSaverOptions opt = ProfileSaverOptions(true, 1, 2, 3, 4, 5, 6, 7, "abc", true); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 492 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 493 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 494 | "-Xjitsaveprofilinginfo " |
| 495 | "-Xps-min-save-period-ms:1 " |
| 496 | "-Xps-save-resolved-classes-delay-ms:2 " |
Mathieu Chartier | 7b135c8 | 2017-06-05 12:54:01 -0700 | [diff] [blame] | 497 | "-Xps-hot-startup-method-samples:3 " |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 498 | "-Xps-min-methods-to-save:4 " |
| 499 | "-Xps-min-classes-to-save:5 " |
| 500 | "-Xps-min-notification-before-wake:6 " |
Calin Juravle | 9545f6d | 2017-03-16 19:05:09 -0700 | [diff] [blame] | 501 | "-Xps-max-notification-before-wake:7 " |
Mathieu Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame] | 502 | "-Xps-profile-path:abc " |
| 503 | "-Xps-profile-boot-class-path", |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 504 | M::ProfileSaverOpts); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 505 | } // TEST_F |
| 506 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 507 | /* -Xexperimental:_ */ |
| 508 | TEST_F(CmdlineParserTest, TestExperimentalFlags) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 509 | // Default |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 510 | EXPECT_SINGLE_PARSE_DEFAULT_VALUE(ExperimentalFlags::kNone, |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 511 | "", |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 512 | M::Experimental); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 513 | |
| 514 | // Disabled explicitly |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 515 | EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kNone, |
| 516 | "-Xexperimental:none", |
| 517 | M::Experimental); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Igor Murashkin | 7617abd | 2015-07-10 18:27:47 -0700 | [diff] [blame] | 520 | // -Xverify:_ |
| 521 | TEST_F(CmdlineParserTest, TestVerify) { |
| 522 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone, "-Xverify:none", M::Verify); |
| 523 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:remote", M::Verify); |
| 524 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:all", M::Verify); |
| 525 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify); |
| 526 | } |
| 527 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 528 | TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) { |
| 529 | RuntimeParser::Builder parserBuilder; |
| 530 | |
| 531 | parserBuilder |
| 532 | .Define("-help") |
| 533 | .IntoKey(M::Help) |
| 534 | .IgnoreUnrecognized(true); |
| 535 | |
| 536 | parser_.reset(new RuntimeParser(parserBuilder.Build())); |
| 537 | |
| 538 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option"); |
| 539 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2"); |
| 540 | } // TEST_F |
| 541 | |
| 542 | TEST_F(CmdlineParserTest, TestIgnoredArguments) { |
| 543 | std::initializer_list<const char*> ignored_args = { |
| 544 | "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa", |
| 545 | "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef", |
| 546 | "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap", |
| 547 | "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop", |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 548 | "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck", |
| 549 | "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile", |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 550 | "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337" |
| 551 | }; |
| 552 | |
| 553 | // Check they are ignored when parsed one at a time |
| 554 | for (auto&& arg : ignored_args) { |
| 555 | SCOPED_TRACE(arg); |
| 556 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg); |
| 557 | } |
| 558 | |
| 559 | // Check they are ignored when we pass it all together at once |
| 560 | std::vector<const char*> argv = ignored_args; |
| 561 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); |
| 562 | } // TEST_F |
| 563 | |
| 564 | TEST_F(CmdlineParserTest, MultipleArguments) { |
| 565 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse( |
| 566 | "-help -XX:ForegroundHeapGrowthMultiplier=0.5 " |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 567 | "-Xmethod-trace -XX:LargeObjectSpace=map"))); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 568 | |
| 569 | auto&& map = parser_->ReleaseArgumentsMap(); |
Nicolas Geoffray | 3d8a78a | 2018-08-29 21:10:16 +0000 | [diff] [blame] | 570 | EXPECT_EQ(4u, map.Size()); |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 571 | EXPECT_KEY_VALUE(map, M::Help, Unit{}); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 572 | EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5); |
Igor Murashkin | 5573c37 | 2017-11-16 13:34:30 -0800 | [diff] [blame] | 573 | EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{}); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 574 | EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap); |
| 575 | } // TEST_F |
Orion Hodson | ffc791c | 2019-11-14 19:19:28 +0000 | [diff] [blame] | 576 | |
| 577 | TEST_F(CmdlineParserTest, TypesNotInRuntime) { |
| 578 | CmdlineType<std::vector<int32_t>> ct; |
| 579 | auto success0 = |
| 580 | CmdlineParseResult<std::vector<int32_t>>::Success(std::vector<int32_t>({1, 2, 3, 4})); |
| 581 | EXPECT_EQ(success0, ct.Parse("1,2,3,4")); |
| 582 | auto success1 = CmdlineParseResult<std::vector<int32_t>>::Success(std::vector<int32_t>({0})); |
| 583 | EXPECT_EQ(success1, ct.Parse("1")); |
| 584 | |
| 585 | EXPECT_FALSE(ct.Parse("").IsSuccess()); |
| 586 | EXPECT_FALSE(ct.Parse(",").IsSuccess()); |
| 587 | EXPECT_FALSE(ct.Parse("1,").IsSuccess()); |
| 588 | EXPECT_FALSE(ct.Parse(",1").IsSuccess()); |
| 589 | EXPECT_FALSE(ct.Parse("1a2").IsSuccess()); |
| 590 | EXPECT_EQ(CmdlineResult::kOutOfRange, ct.Parse("1,10000000000000").GetStatus()); |
| 591 | EXPECT_EQ(CmdlineResult::kOutOfRange, ct.Parse("-10000000000000,123").GetStatus()); |
| 592 | } // TEST_F |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 593 | } // namespace art |