blob: 304a0d7d806d6ba7f80f44befa33d0947ea06dc2 [file] [log] [blame]
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08001/*
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 "parsed_options.h"
Dave Allisonb373e092014-02-20 16:06:36 -080018#ifdef HAVE_ANDROID_OS
19#include "cutils/properties.h"
20#endif
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080021
22#include "debugger.h"
23#include "monitor.h"
24
25namespace art {
26
27ParsedOptions* ParsedOptions::Create(const Runtime::Options& options, bool ignore_unrecognized) {
28 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
29 if (parsed->Parse(options, ignore_unrecognized)) {
30 return parsed.release();
31 }
32 return nullptr;
33}
34
35// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
36// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
37// [gG] gigabytes.
38//
39// "s" should point just past the "-Xm?" part of the string.
40// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
41// of 1024.
42//
43// The spec says the -Xmx and -Xms options must be multiples of 1024. It
44// doesn't say anything about -Xss.
45//
46// Returns 0 (a useless size) if "s" is malformed or specifies a low or
47// non-evenly-divisible value.
48//
49size_t ParseMemoryOption(const char* s, size_t div) {
50 // strtoul accepts a leading [+-], which we don't want,
51 // so make sure our string starts with a decimal digit.
52 if (isdigit(*s)) {
53 char* s2;
54 size_t val = strtoul(s, &s2, 10);
55 if (s2 != s) {
56 // s2 should be pointing just after the number.
57 // If this is the end of the string, the user
58 // has specified a number of bytes. Otherwise,
59 // there should be exactly one more character
60 // that specifies a multiplier.
61 if (*s2 != '\0') {
62 // The remainder of the string is either a single multiplier
63 // character, or nothing to indicate that the value is in
64 // bytes.
65 char c = *s2++;
66 if (*s2 == '\0') {
67 size_t mul;
68 if (c == '\0') {
69 mul = 1;
70 } else if (c == 'k' || c == 'K') {
71 mul = KB;
72 } else if (c == 'm' || c == 'M') {
73 mul = MB;
74 } else if (c == 'g' || c == 'G') {
75 mul = GB;
76 } else {
77 // Unknown multiplier character.
78 return 0;
79 }
80
81 if (val <= std::numeric_limits<size_t>::max() / mul) {
82 val *= mul;
83 } else {
84 // Clamp to a multiple of 1024.
85 val = std::numeric_limits<size_t>::max() & ~(1024-1);
86 }
87 } else {
88 // There's more than one character after the numeric part.
89 return 0;
90 }
91 }
92 // The man page says that a -Xm value must be a multiple of 1024.
93 if (val % div == 0) {
94 return val;
95 }
96 }
97 }
98 return 0;
99}
100
101static gc::CollectorType ParseCollectorType(const std::string& option) {
102 if (option == "MS" || option == "nonconcurrent") {
103 return gc::kCollectorTypeMS;
104 } else if (option == "CMS" || option == "concurrent") {
105 return gc::kCollectorTypeCMS;
106 } else if (option == "SS") {
107 return gc::kCollectorTypeSS;
108 } else if (option == "GSS") {
109 return gc::kCollectorTypeGSS;
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700110 } else if (option == "CC") {
111 return gc::kCollectorTypeCC;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800112 } else {
113 return gc::kCollectorTypeNone;
114 }
115}
116
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700117bool ParsedOptions::ParseXGcOption(const std::string& option) {
118 std::vector<std::string> gc_options;
119 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
120 for (const std::string& gc_option : gc_options) {
121 gc::CollectorType collector_type = ParseCollectorType(gc_option);
122 if (collector_type != gc::kCollectorTypeNone) {
123 collector_type_ = collector_type;
124 } else if (gc_option == "preverify") {
125 verify_pre_gc_heap_ = true;
126 } else if (gc_option == "nopreverify") {
127 verify_pre_gc_heap_ = false;
128 } else if (gc_option == "presweepingverify") {
129 verify_pre_sweeping_heap_ = true;
130 } else if (gc_option == "nopresweepingverify") {
131 verify_pre_sweeping_heap_ = false;
132 } else if (gc_option == "postverify") {
133 verify_post_gc_heap_ = true;
134 } else if (gc_option == "nopostverify") {
135 verify_post_gc_heap_ = false;
136 } else if (gc_option == "preverify_rosalloc") {
137 verify_pre_gc_rosalloc_ = true;
138 } else if (gc_option == "nopreverify_rosalloc") {
139 verify_pre_gc_rosalloc_ = false;
140 } else if (gc_option == "presweepingverify_rosalloc") {
141 verify_pre_sweeping_rosalloc_ = true;
142 } else if (gc_option == "nopresweepingverify_rosalloc") {
143 verify_pre_sweeping_rosalloc_ = false;
144 } else if (gc_option == "postverify_rosalloc") {
145 verify_post_gc_rosalloc_ = true;
146 } else if (gc_option == "nopostverify_rosalloc") {
147 verify_post_gc_rosalloc_ = false;
148 } else if ((gc_option == "precise") ||
149 (gc_option == "noprecise") ||
150 (gc_option == "verifycardtable") ||
151 (gc_option == "noverifycardtable")) {
152 // Ignored for backwards compatibility.
153 } else {
154 Usage("Unknown -Xgc option %s\n", gc_option.c_str());
155 return false;
156 }
157 }
158 return true;
159}
160
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800161bool ParsedOptions::Parse(const Runtime::Options& options, bool ignore_unrecognized) {
162 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
163 if (boot_class_path_string != NULL) {
164 boot_class_path_string_ = boot_class_path_string;
165 }
166 const char* class_path_string = getenv("CLASSPATH");
167 if (class_path_string != NULL) {
168 class_path_string_ = class_path_string;
169 }
170 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
171 check_jni_ = kIsDebugBuild;
172
173 heap_initial_size_ = gc::Heap::kDefaultInitialSize;
174 heap_maximum_size_ = gc::Heap::kDefaultMaximumSize;
175 heap_min_free_ = gc::Heap::kDefaultMinFree;
176 heap_max_free_ = gc::Heap::kDefaultMaxFree;
177 heap_target_utilization_ = gc::Heap::kDefaultTargetUtilization;
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700178 foreground_heap_growth_multiplier_ = gc::Heap::kDefaultHeapGrowthMultiplier;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800179 heap_growth_limit_ = 0; // 0 means no growth limit .
180 // Default to number of processors minus one since the main GC thread also does work.
181 parallel_gc_threads_ = sysconf(_SC_NPROCESSORS_CONF) - 1;
182 // Only the main GC thread, no workers.
183 conc_gc_threads_ = 0;
Hiroshi Yamauchi1dda0602014-05-12 12:32:32 -0700184 // The default GC type is set in makefiles.
185#if ART_DEFAULT_GC_TYPE_IS_CMS
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800186 collector_type_ = gc::kCollectorTypeCMS;
Hiroshi Yamauchi1dda0602014-05-12 12:32:32 -0700187#elif ART_DEFAULT_GC_TYPE_IS_SS
188 collector_type_ = gc::kCollectorTypeSS;
189#elif ART_DEFAULT_GC_TYPE_IS_GSS
190 collector_type_ = gc::kCollectorTypeGSS;
191#else
192#error "ART default GC type must be set"
193#endif
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800194 // If background_collector_type_ is kCollectorTypeNone, it defaults to the collector_type_ after
195 // parsing options.
196 background_collector_type_ = gc::kCollectorTypeNone;
197 stack_size_ = 0; // 0 means default.
198 max_spins_before_thin_lock_inflation_ = Monitor::kDefaultMaxSpinsBeforeThinLockInflation;
199 low_memory_mode_ = false;
200 use_tlab_ = false;
201 verify_pre_gc_heap_ = false;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700202 // Pre sweeping is the one that usually fails if the GC corrupted the heap.
203 verify_pre_sweeping_heap_ = kIsDebugBuild;
204 verify_post_gc_heap_ = false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800205 verify_pre_gc_rosalloc_ = kIsDebugBuild;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700206 verify_pre_sweeping_rosalloc_ = false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800207 verify_post_gc_rosalloc_ = false;
208
209 compiler_callbacks_ = nullptr;
210 is_zygote_ = false;
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -0800211 if (kPoisonHeapReferences) {
212 // kPoisonHeapReferences currently works only with the interpreter only.
213 // TODO: make it work with the compiler.
214 interpreter_only_ = true;
215 } else {
216 interpreter_only_ = false;
217 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800218 is_explicit_gc_disabled_ = false;
219
220 long_pause_log_threshold_ = gc::Heap::kDefaultLongPauseLogThreshold;
221 long_gc_log_threshold_ = gc::Heap::kDefaultLongGCLogThreshold;
222 dump_gc_performance_on_shutdown_ = false;
223 ignore_max_footprint_ = false;
224
225 lock_profiling_threshold_ = 0;
226 hook_is_sensitive_thread_ = NULL;
227
228 hook_vfprintf_ = vfprintf;
229 hook_exit_ = exit;
230 hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
231
232// gLogVerbosity.class_linker = true; // TODO: don't check this in!
233// gLogVerbosity.compiler = true; // TODO: don't check this in!
234// gLogVerbosity.verifier = true; // TODO: don't check this in!
235// gLogVerbosity.heap = true; // TODO: don't check this in!
236// gLogVerbosity.gc = true; // TODO: don't check this in!
237// gLogVerbosity.jdwp = true; // TODO: don't check this in!
238// gLogVerbosity.jni = true; // TODO: don't check this in!
239// gLogVerbosity.monitor = true; // TODO: don't check this in!
240// gLogVerbosity.startup = true; // TODO: don't check this in!
241// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
242// gLogVerbosity.threads = true; // TODO: don't check this in!
Dave Allison5cd33752014-04-15 15:57:58 -0700243// gLogVerbosity.signals = true; // TODO: don't check this in!
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800244
245 method_trace_ = false;
246 method_trace_file_ = "/data/method-trace-file.bin";
247 method_trace_file_size_ = 10 * MB;
248
249 profile_ = false;
250 profile_period_s_ = 10; // Seconds.
251 profile_duration_s_ = 20; // Seconds.
252 profile_interval_us_ = 500; // Microseconds.
253 profile_backoff_coefficient_ = 2.0;
Calin Juravle16590062014-04-07 18:07:43 +0300254 profile_start_immediately_ = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800255 profile_clock_source_ = kDefaultProfilerClockSource;
256
Jeff Hao4a200f52014-04-01 14:58:49 -0700257 verify_ = true;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100258 image_isa_ = kRuntimeISA;
Jeff Hao4a200f52014-04-01 14:58:49 -0700259
Dave Allisonb373e092014-02-20 16:06:36 -0800260 // Default to explicit checks. Switch off with -implicit-checks:.
261 // or setprop dalvik.vm.implicit_checks check1,check2,...
262#ifdef HAVE_ANDROID_OS
263 {
264 char buf[PROP_VALUE_MAX];
Dave Allison05266432014-05-05 13:17:37 -0700265 property_get("dalvik.vm.implicit_checks", buf, "null,stack");
Dave Allisonb373e092014-02-20 16:06:36 -0800266 std::string checks(buf);
267 std::vector<std::string> checkvec;
268 Split(checks, ',', checkvec);
Dave Allisondd2e8252014-03-20 14:45:17 -0700269 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
270 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800271 for (auto& str : checkvec) {
272 std::string val = Trim(str);
273 if (val == "none") {
274 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
Dave Allisondd2e8252014-03-20 14:45:17 -0700275 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800276 } else if (val == "null") {
277 explicit_checks_ &= ~kExplicitNullCheck;
278 } else if (val == "suspend") {
279 explicit_checks_ &= ~kExplicitSuspendCheck;
280 } else if (val == "stack") {
281 explicit_checks_ &= ~kExplicitStackOverflowCheck;
282 } else if (val == "all") {
283 explicit_checks_ = 0;
284 }
285 }
286 }
287#else
288 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
289 kExplicitStackOverflowCheck;
290#endif
291
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800292 for (size_t i = 0; i < options.size(); ++i) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800293 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800294 LOG(INFO) << "option[" << i << "]=" << options[i].first;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800295 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800296 }
297 for (size_t i = 0; i < options.size(); ++i) {
298 const std::string option(options[i].first);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800299 if (StartsWith(option, "-help")) {
300 Usage(nullptr);
301 return false;
302 } else if (StartsWith(option, "-showversion")) {
303 UsageMessage(stdout, "ART version %s\n", Runtime::GetVersion());
304 Exit(0);
305 } else if (StartsWith(option, "-Xbootclasspath:")) {
306 boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
307 } else if (option == "-classpath" || option == "-cp") {
308 // TODO: support -Djava.class.path
309 i++;
310 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700311 Usage("Missing required class path value for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800312 return false;
313 }
314 const StringPiece& value = options[i].first;
315 class_path_string_ = value.data();
316 } else if (option == "bootclasspath") {
317 boot_class_path_
318 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
319 } else if (StartsWith(option, "-Ximage:")) {
320 if (!ParseStringAfterChar(option, ':', &image_)) {
321 return false;
322 }
323 } else if (StartsWith(option, "-Xcheck:jni")) {
324 check_jni_ = true;
325 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
326 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
327 // TODO: move parsing logic out of Dbg
328 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
329 if (tail != "help") {
330 UsageMessage(stderr, "Failed to parse JDWP option %s\n", tail.c_str());
331 }
332 Usage("Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
333 "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n");
334 return false;
335 }
336 } else if (StartsWith(option, "-Xms")) {
337 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
338 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700339 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800340 return false;
341 }
342 heap_initial_size_ = size;
343 } else if (StartsWith(option, "-Xmx")) {
344 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
345 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700346 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800347 return false;
348 }
349 heap_maximum_size_ = size;
350 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
351 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
352 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700353 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800354 return false;
355 }
356 heap_growth_limit_ = size;
357 } else if (StartsWith(option, "-XX:HeapMinFree=")) {
358 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMinFree=")).c_str(), 1024);
359 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700360 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800361 return false;
362 }
363 heap_min_free_ = size;
364 } else if (StartsWith(option, "-XX:HeapMaxFree=")) {
365 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMaxFree=")).c_str(), 1024);
366 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700367 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800368 return false;
369 }
370 heap_max_free_ = size;
371 } else if (StartsWith(option, "-XX:HeapTargetUtilization=")) {
372 if (!ParseDouble(option, '=', 0.1, 0.9, &heap_target_utilization_)) {
373 return false;
374 }
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700375 } else if (StartsWith(option, "-XX:ForegroundHeapGrowthMultiplier=")) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700376 if (!ParseDouble(option, '=', 0.1, 10.0, &foreground_heap_growth_multiplier_)) {
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700377 return false;
378 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800379 } else if (StartsWith(option, "-XX:ParallelGCThreads=")) {
380 if (!ParseUnsignedInteger(option, '=', &parallel_gc_threads_)) {
381 return false;
382 }
383 } else if (StartsWith(option, "-XX:ConcGCThreads=")) {
384 if (!ParseUnsignedInteger(option, '=', &conc_gc_threads_)) {
385 return false;
386 }
387 } else if (StartsWith(option, "-Xss")) {
388 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
389 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700390 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800391 return false;
392 }
393 stack_size_ = size;
394 } else if (StartsWith(option, "-XX:MaxSpinsBeforeThinLockInflation=")) {
395 if (!ParseUnsignedInteger(option, '=', &max_spins_before_thin_lock_inflation_)) {
396 return false;
397 }
398 } else if (StartsWith(option, "-XX:LongPauseLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800399 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800400 if (!ParseUnsignedInteger(option, '=', &value)) {
401 return false;
402 }
403 long_pause_log_threshold_ = MsToNs(value);
404 } else if (StartsWith(option, "-XX:LongGCLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800405 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800406 if (!ParseUnsignedInteger(option, '=', &value)) {
407 return false;
408 }
409 long_gc_log_threshold_ = MsToNs(value);
410 } else if (option == "-XX:DumpGCPerformanceOnShutdown") {
411 dump_gc_performance_on_shutdown_ = true;
412 } else if (option == "-XX:IgnoreMaxFootprint") {
413 ignore_max_footprint_ = true;
414 } else if (option == "-XX:LowMemoryMode") {
415 low_memory_mode_ = true;
416 } else if (option == "-XX:UseTLAB") {
417 use_tlab_ = true;
418 } else if (StartsWith(option, "-D")) {
419 properties_.push_back(option.substr(strlen("-D")));
420 } else if (StartsWith(option, "-Xjnitrace:")) {
421 jni_trace_ = option.substr(strlen("-Xjnitrace:"));
422 } else if (option == "compilercallbacks") {
423 compiler_callbacks_ =
424 reinterpret_cast<CompilerCallbacks*>(const_cast<void*>(options[i].second));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100425 } else if (option == "imageinstructionset") {
426 image_isa_ = GetInstructionSetFromString(
427 reinterpret_cast<const char*>(options[i].second));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800428 } else if (option == "-Xzygote") {
429 is_zygote_ = true;
430 } else if (option == "-Xint") {
431 interpreter_only_ = true;
432 } else if (StartsWith(option, "-Xgc:")) {
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700433 if (!ParseXGcOption(option)) {
434 return false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800435 }
436 } else if (StartsWith(option, "-XX:BackgroundGC=")) {
437 std::string substring;
438 if (!ParseStringAfterChar(option, '=', &substring)) {
439 return false;
440 }
441 gc::CollectorType collector_type = ParseCollectorType(substring);
442 if (collector_type != gc::kCollectorTypeNone) {
443 background_collector_type_ = collector_type;
444 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700445 Usage("Unknown -XX:BackgroundGC option %s\n", substring.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800446 return false;
447 }
448 } else if (option == "-XX:+DisableExplicitGC") {
449 is_explicit_gc_disabled_ = true;
450 } else if (StartsWith(option, "-verbose:")) {
451 std::vector<std::string> verbose_options;
452 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
453 for (size_t i = 0; i < verbose_options.size(); ++i) {
454 if (verbose_options[i] == "class") {
455 gLogVerbosity.class_linker = true;
456 } else if (verbose_options[i] == "verifier") {
457 gLogVerbosity.verifier = true;
458 } else if (verbose_options[i] == "compiler") {
459 gLogVerbosity.compiler = true;
460 } else if (verbose_options[i] == "heap") {
461 gLogVerbosity.heap = true;
462 } else if (verbose_options[i] == "gc") {
463 gLogVerbosity.gc = true;
464 } else if (verbose_options[i] == "jdwp") {
465 gLogVerbosity.jdwp = true;
466 } else if (verbose_options[i] == "jni") {
467 gLogVerbosity.jni = true;
468 } else if (verbose_options[i] == "monitor") {
469 gLogVerbosity.monitor = true;
470 } else if (verbose_options[i] == "startup") {
471 gLogVerbosity.startup = true;
472 } else if (verbose_options[i] == "third-party-jni") {
473 gLogVerbosity.third_party_jni = true;
474 } else if (verbose_options[i] == "threads") {
475 gLogVerbosity.threads = true;
Dave Allison5cd33752014-04-15 15:57:58 -0700476 } else if (verbose_options[i] == "signals") {
477 gLogVerbosity.signals = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800478 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700479 Usage("Unknown -verbose option %s\n", verbose_options[i].c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800480 return false;
481 }
482 }
Mingyao Yang42d65c52014-04-18 16:49:39 -0700483 } else if (StartsWith(option, "-verbose-methods:")) {
484 gLogVerbosity.compiler = false;
485 Split(option.substr(strlen("-verbose-methods:")), ',', gVerboseMethods);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800486 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
487 if (!ParseUnsignedInteger(option, ':', &lock_profiling_threshold_)) {
488 return false;
489 }
490 } else if (StartsWith(option, "-Xstacktracefile:")) {
491 if (!ParseStringAfterChar(option, ':', &stack_trace_file_)) {
492 return false;
493 }
494 } else if (option == "sensitiveThread") {
495 const void* hook = options[i].second;
496 hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(hook));
497 } else if (option == "vfprintf") {
498 const void* hook = options[i].second;
499 if (hook == nullptr) {
500 Usage("vfprintf argument was NULL");
501 return false;
502 }
503 hook_vfprintf_ =
504 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(hook));
505 } else if (option == "exit") {
506 const void* hook = options[i].second;
507 if (hook == nullptr) {
508 Usage("exit argument was NULL");
509 return false;
510 }
511 hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(hook));
512 } else if (option == "abort") {
513 const void* hook = options[i].second;
514 if (hook == nullptr) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700515 Usage("abort was NULL\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800516 return false;
517 }
518 hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(hook));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800519 } else if (option == "-Xmethod-trace") {
520 method_trace_ = true;
521 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
522 method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
523 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
524 if (!ParseUnsignedInteger(option, ':', &method_trace_file_size_)) {
525 return false;
526 }
527 } else if (option == "-Xprofile:threadcpuclock") {
528 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
529 } else if (option == "-Xprofile:wallclock") {
530 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
531 } else if (option == "-Xprofile:dualclock") {
532 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
533 } else if (StartsWith(option, "-Xprofile:")) {
534 if (!ParseStringAfterChar(option, ';', &profile_output_filename_)) {
535 return false;
536 }
537 profile_ = true;
538 } else if (StartsWith(option, "-Xprofile-period:")) {
539 if (!ParseUnsignedInteger(option, ':', &profile_period_s_)) {
540 return false;
541 }
542 } else if (StartsWith(option, "-Xprofile-duration:")) {
543 if (!ParseUnsignedInteger(option, ':', &profile_duration_s_)) {
544 return false;
545 }
546 } else if (StartsWith(option, "-Xprofile-interval:")) {
547 if (!ParseUnsignedInteger(option, ':', &profile_interval_us_)) {
548 return false;
549 }
550 } else if (StartsWith(option, "-Xprofile-backoff:")) {
551 if (!ParseDouble(option, ':', 1.0, 10.0, &profile_backoff_coefficient_)) {
552 return false;
553 }
Calin Juravle16590062014-04-07 18:07:43 +0300554 } else if (option == "-Xprofile-start-lazy") {
555 profile_start_immediately_ = false;
Dave Allisonb373e092014-02-20 16:06:36 -0800556 } else if (StartsWith(option, "-implicit-checks:")) {
557 std::string checks;
558 if (!ParseStringAfterChar(option, ':', &checks)) {
559 return false;
560 }
561 std::vector<std::string> checkvec;
562 Split(checks, ',', checkvec);
563 for (auto& str : checkvec) {
564 std::string val = Trim(str);
565 if (val == "none") {
566 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
567 kExplicitStackOverflowCheck;
568 } else if (val == "null") {
569 explicit_checks_ &= ~kExplicitNullCheck;
570 } else if (val == "suspend") {
571 explicit_checks_ &= ~kExplicitSuspendCheck;
572 } else if (val == "stack") {
573 explicit_checks_ &= ~kExplicitStackOverflowCheck;
574 } else if (val == "all") {
575 explicit_checks_ = 0;
576 } else {
577 return false;
578 }
579 }
580 } else if (StartsWith(option, "-explicit-checks:")) {
581 std::string checks;
582 if (!ParseStringAfterChar(option, ':', &checks)) {
583 return false;
584 }
585 std::vector<std::string> checkvec;
586 Split(checks, ',', checkvec);
587 for (auto& str : checkvec) {
588 std::string val = Trim(str);
589 if (val == "none") {
590 explicit_checks_ = 0;
591 } else if (val == "null") {
592 explicit_checks_ |= kExplicitNullCheck;
593 } else if (val == "suspend") {
594 explicit_checks_ |= kExplicitSuspendCheck;
595 } else if (val == "stack") {
596 explicit_checks_ |= kExplicitStackOverflowCheck;
597 } else if (val == "all") {
598 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
599 kExplicitStackOverflowCheck;
600 } else {
601 return false;
602 }
603 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800604 } else if (option == "-Xcompiler-option") {
605 i++;
606 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700607 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800608 return false;
609 }
610 compiler_options_.push_back(options[i].first);
611 } else if (option == "-Ximage-compiler-option") {
612 i++;
613 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700614 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800615 return false;
616 }
617 image_compiler_options_.push_back(options[i].first);
Jeff Hao4a200f52014-04-01 14:58:49 -0700618 } else if (StartsWith(option, "-Xverify:")) {
619 std::string verify_mode = option.substr(strlen("-Xverify:"));
620 if (verify_mode == "none") {
621 verify_ = false;
622 } else if (verify_mode == "remote" || verify_mode == "all") {
623 verify_ = true;
624 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700625 Usage("Unknown -Xverify option %s\n", verify_mode.c_str());
Jeff Hao4a200f52014-04-01 14:58:49 -0700626 return false;
627 }
Yevgeny Roubana6119a22014-03-24 11:31:24 +0700628 } else if (StartsWith(option, "-ea") ||
629 StartsWith(option, "-da") ||
630 StartsWith(option, "-enableassertions") ||
631 StartsWith(option, "-disableassertions") ||
Dave Allisonb373e092014-02-20 16:06:36 -0800632 (option == "--runtime-arg") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800633 (option == "-esa") ||
634 (option == "-dsa") ||
635 (option == "-enablesystemassertions") ||
636 (option == "-disablesystemassertions") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800637 (option == "-Xrs") ||
638 StartsWith(option, "-Xint:") ||
639 StartsWith(option, "-Xdexopt:") ||
640 (option == "-Xnoquithandler") ||
641 StartsWith(option, "-Xjniopts:") ||
642 StartsWith(option, "-Xjnigreflimit:") ||
643 (option == "-Xgenregmap") ||
644 (option == "-Xnogenregmap") ||
645 StartsWith(option, "-Xverifyopt:") ||
646 (option == "-Xcheckdexsum") ||
647 (option == "-Xincludeselectedop") ||
648 StartsWith(option, "-Xjitop:") ||
649 (option == "-Xincludeselectedmethod") ||
650 StartsWith(option, "-Xjitthreshold:") ||
651 StartsWith(option, "-Xjitcodecachesize:") ||
652 (option == "-Xjitblocking") ||
653 StartsWith(option, "-Xjitmethod:") ||
654 StartsWith(option, "-Xjitclass:") ||
655 StartsWith(option, "-Xjitoffset:") ||
656 StartsWith(option, "-Xjitconfig:") ||
657 (option == "-Xjitcheckcg") ||
658 (option == "-Xjitverbose") ||
659 (option == "-Xjitprofile") ||
660 (option == "-Xjitdisableopt") ||
661 (option == "-Xjitsuspendpoll") ||
662 StartsWith(option, "-XX:mainThreadStackSize=")) {
663 // Ignored for backwards compatibility.
664 } else if (!ignore_unrecognized) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700665 Usage("Unrecognized option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800666 return false;
667 }
668 }
669
670 // If a reference to the dalvik core.jar snuck in, replace it with
671 // the art specific version. This can happen with on device
672 // boot.art/boot.oat generation by GenerateImage which relies on the
673 // value of BOOTCLASSPATH.
674 std::string core_jar("/core.jar");
675 size_t core_jar_pos = boot_class_path_string_.find(core_jar);
676 if (core_jar_pos != std::string::npos) {
677 boot_class_path_string_.replace(core_jar_pos, core_jar.size(), "/core-libart.jar");
678 }
679
680 if (compiler_callbacks_ == nullptr && image_.empty()) {
681 image_ += GetAndroidRoot();
682 image_ += "/framework/boot.art";
683 }
684 if (heap_growth_limit_ == 0) {
685 heap_growth_limit_ = heap_maximum_size_;
686 }
687 if (background_collector_type_ == gc::kCollectorTypeNone) {
688 background_collector_type_ = collector_type_;
689 }
690 return true;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100691} // NOLINT(readability/fn_size)
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800692
693void ParsedOptions::Exit(int status) {
694 hook_exit_(status);
695}
696
697void ParsedOptions::Abort() {
698 hook_abort_();
699}
700
701void ParsedOptions::UsageMessageV(FILE* stream, const char* fmt, va_list ap) {
702 hook_vfprintf_(stderr, fmt, ap);
703}
704
705void ParsedOptions::UsageMessage(FILE* stream, const char* fmt, ...) {
706 va_list ap;
707 va_start(ap, fmt);
708 UsageMessageV(stream, fmt, ap);
709 va_end(ap);
710}
711
712void ParsedOptions::Usage(const char* fmt, ...) {
713 bool error = (fmt != nullptr);
714 FILE* stream = error ? stderr : stdout;
715
716 if (fmt != nullptr) {
717 va_list ap;
718 va_start(ap, fmt);
719 UsageMessageV(stream, fmt, ap);
720 va_end(ap);
721 }
722
723 const char* program = "dalvikvm";
724 UsageMessage(stream, "%s: [options] class [argument ...]\n", program);
725 UsageMessage(stream, "\n");
726 UsageMessage(stream, "The following standard options are supported:\n");
727 UsageMessage(stream, " -classpath classpath (-cp classpath)\n");
728 UsageMessage(stream, " -Dproperty=value\n");
729 UsageMessage(stream, " -verbose:tag ('gc', 'jni', or 'class')\n");
730 UsageMessage(stream, " -showversion\n");
731 UsageMessage(stream, " -help\n");
732 UsageMessage(stream, " -agentlib:jdwp=options\n");
733 UsageMessage(stream, "\n");
734
735 UsageMessage(stream, "The following extended options are supported:\n");
736 UsageMessage(stream, " -Xrunjdwp:<options>\n");
737 UsageMessage(stream, " -Xbootclasspath:bootclasspath\n");
738 UsageMessage(stream, " -Xcheck:tag (e.g. 'jni')\n");
739 UsageMessage(stream, " -XmsN (min heap, must be multiple of 1K, >= 1MB)\n");
740 UsageMessage(stream, " -XmxN (max heap, must be multiple of 1K, >= 2MB)\n");
741 UsageMessage(stream, " -XssN (stack size)\n");
742 UsageMessage(stream, " -Xint\n");
743 UsageMessage(stream, "\n");
744
745 UsageMessage(stream, "The following Dalvik options are supported:\n");
746 UsageMessage(stream, " -Xzygote\n");
747 UsageMessage(stream, " -Xjnitrace:substring (eg NativeClass or nativeMethod)\n");
748 UsageMessage(stream, " -Xstacktracefile:<filename>\n");
749 UsageMessage(stream, " -Xgc:[no]preverify\n");
750 UsageMessage(stream, " -Xgc:[no]postverify\n");
751 UsageMessage(stream, " -XX:+DisableExplicitGC\n");
752 UsageMessage(stream, " -XX:HeapGrowthLimit=N\n");
753 UsageMessage(stream, " -XX:HeapMinFree=N\n");
754 UsageMessage(stream, " -XX:HeapMaxFree=N\n");
755 UsageMessage(stream, " -XX:HeapTargetUtilization=doublevalue\n");
Mathieu Chartier455820e2014-04-18 12:02:39 -0700756 UsageMessage(stream, " -XX:ForegroundHeapGrowthMultiplier=doublevalue\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800757 UsageMessage(stream, " -XX:LowMemoryMode\n");
758 UsageMessage(stream, " -Xprofile:{threadcpuclock,wallclock,dualclock}\n");
759 UsageMessage(stream, "\n");
760
761 UsageMessage(stream, "The following unique to ART options are supported:\n");
762 UsageMessage(stream, " -Xgc:[no]preverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700763 UsageMessage(stream, " -Xgc:[no]postsweepingverify_rosalloc\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800764 UsageMessage(stream, " -Xgc:[no]postverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700765 UsageMessage(stream, " -Xgc:[no]presweepingverify\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800766 UsageMessage(stream, " -Ximage:filename\n");
767 UsageMessage(stream, " -XX:ParallelGCThreads=integervalue\n");
768 UsageMessage(stream, " -XX:ConcGCThreads=integervalue\n");
769 UsageMessage(stream, " -XX:MaxSpinsBeforeThinLockInflation=integervalue\n");
770 UsageMessage(stream, " -XX:LongPauseLogThreshold=integervalue\n");
771 UsageMessage(stream, " -XX:LongGCLogThreshold=integervalue\n");
772 UsageMessage(stream, " -XX:DumpGCPerformanceOnShutdown\n");
773 UsageMessage(stream, " -XX:IgnoreMaxFootprint\n");
774 UsageMessage(stream, " -XX:UseTLAB\n");
775 UsageMessage(stream, " -XX:BackgroundGC=none\n");
776 UsageMessage(stream, " -Xmethod-trace\n");
777 UsageMessage(stream, " -Xmethod-trace-file:filename");
778 UsageMessage(stream, " -Xmethod-trace-file-size:integervalue\n");
779 UsageMessage(stream, " -Xprofile=filename\n");
780 UsageMessage(stream, " -Xprofile-period:integervalue\n");
781 UsageMessage(stream, " -Xprofile-duration:integervalue\n");
782 UsageMessage(stream, " -Xprofile-interval:integervalue\n");
783 UsageMessage(stream, " -Xprofile-backoff:integervalue\n");
784 UsageMessage(stream, " -Xcompiler-option dex2oat-option\n");
785 UsageMessage(stream, " -Ximage-compiler-option dex2oat-option\n");
786 UsageMessage(stream, "\n");
787
788 UsageMessage(stream, "The following previously supported Dalvik options are ignored:\n");
789 UsageMessage(stream, " -ea[:<package name>... |:<class name>]\n");
790 UsageMessage(stream, " -da[:<package name>... |:<class name>]\n");
791 UsageMessage(stream, " (-enableassertions, -disableassertions)\n");
792 UsageMessage(stream, " -esa\n");
793 UsageMessage(stream, " -dsa\n");
794 UsageMessage(stream, " (-enablesystemassertions, -disablesystemassertions)\n");
795 UsageMessage(stream, " -Xverify:{none,remote,all}\n");
796 UsageMessage(stream, " -Xrs\n");
797 UsageMessage(stream, " -Xint:portable, -Xint:fast, -Xint:jit\n");
798 UsageMessage(stream, " -Xdexopt:{none,verified,all,full}\n");
799 UsageMessage(stream, " -Xnoquithandler\n");
800 UsageMessage(stream, " -Xjniopts:{warnonly,forcecopy}\n");
801 UsageMessage(stream, " -Xjnigreflimit:integervalue\n");
802 UsageMessage(stream, " -Xgc:[no]precise\n");
803 UsageMessage(stream, " -Xgc:[no]verifycardtable\n");
804 UsageMessage(stream, " -X[no]genregmap\n");
805 UsageMessage(stream, " -Xverifyopt:[no]checkmon\n");
806 UsageMessage(stream, " -Xcheckdexsum\n");
807 UsageMessage(stream, " -Xincludeselectedop\n");
808 UsageMessage(stream, " -Xjitop:hexopvalue[-endvalue][,hexopvalue[-endvalue]]*\n");
809 UsageMessage(stream, " -Xincludeselectedmethod\n");
810 UsageMessage(stream, " -Xjitthreshold:integervalue\n");
811 UsageMessage(stream, " -Xjitcodecachesize:decimalvalueofkbytes\n");
812 UsageMessage(stream, " -Xjitblocking\n");
813 UsageMessage(stream, " -Xjitmethod:signature[,signature]* (eg Ljava/lang/String\\;replace)\n");
814 UsageMessage(stream, " -Xjitclass:classname[,classname]*\n");
815 UsageMessage(stream, " -Xjitoffset:offset[,offset]\n");
816 UsageMessage(stream, " -Xjitconfig:filename\n");
817 UsageMessage(stream, " -Xjitcheckcg\n");
818 UsageMessage(stream, " -Xjitverbose\n");
819 UsageMessage(stream, " -Xjitprofile\n");
820 UsageMessage(stream, " -Xjitdisableopt\n");
821 UsageMessage(stream, " -Xjitsuspendpoll\n");
822 UsageMessage(stream, " -XX:mainThreadStackSize=N\n");
823 UsageMessage(stream, "\n");
824
825 Exit((error) ? 1 : 0);
826}
827
828bool ParsedOptions::ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) {
829 std::string::size_type colon = s.find(c);
830 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700831 Usage("Missing char %c in option %s\n", c, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800832 return false;
833 }
834 // Add one to remove the char we were trimming until.
835 *parsed_value = s.substr(colon + 1);
836 return true;
837}
838
839bool ParsedOptions::ParseInteger(const std::string& s, char after_char, int* parsed_value) {
840 std::string::size_type colon = s.find(after_char);
841 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700842 Usage("Missing char %c in option %s\n", after_char, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800843 return false;
844 }
845 const char* begin = &s[colon + 1];
846 char* end;
847 size_t result = strtoul(begin, &end, 10);
848 if (begin == end || *end != '\0') {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700849 Usage("Failed to parse integer from %s\n", s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800850 return false;
851 }
852 *parsed_value = result;
853 return true;
854}
855
856bool ParsedOptions::ParseUnsignedInteger(const std::string& s, char after_char,
857 unsigned int* parsed_value) {
858 int i;
859 if (!ParseInteger(s, after_char, &i)) {
860 return false;
861 }
862 if (i < 0) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700863 Usage("Negative value %d passed for unsigned option %s\n", i, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800864 return false;
865 }
866 *parsed_value = i;
867 return true;
868}
869
870bool ParsedOptions::ParseDouble(const std::string& option, char after_char,
871 double min, double max, double* parsed_value) {
872 std::string substring;
873 if (!ParseStringAfterChar(option, after_char, &substring)) {
874 return false;
875 }
876 std::istringstream iss(substring);
877 double value;
878 iss >> value;
879 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
880 const bool sane_val = iss.eof() && (value >= min) && (value <= max);
881 if (!sane_val) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700882 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800883 return false;
884 }
885 *parsed_value = value;
886 return true;
887}
888
889} // namespace art