blob: 9cf878535ee748030b10215ba25c3c023cfbae89 [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;
184 // Default is CMS which is Sticky + Partial + Full CMS GC.
185 collector_type_ = gc::kCollectorTypeCMS;
186 // If background_collector_type_ is kCollectorTypeNone, it defaults to the collector_type_ after
187 // parsing options.
188 background_collector_type_ = gc::kCollectorTypeNone;
189 stack_size_ = 0; // 0 means default.
190 max_spins_before_thin_lock_inflation_ = Monitor::kDefaultMaxSpinsBeforeThinLockInflation;
191 low_memory_mode_ = false;
192 use_tlab_ = false;
193 verify_pre_gc_heap_ = false;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700194 // Pre sweeping is the one that usually fails if the GC corrupted the heap.
195 verify_pre_sweeping_heap_ = kIsDebugBuild;
196 verify_post_gc_heap_ = false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800197 verify_pre_gc_rosalloc_ = kIsDebugBuild;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700198 verify_pre_sweeping_rosalloc_ = false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800199 verify_post_gc_rosalloc_ = false;
200
201 compiler_callbacks_ = nullptr;
202 is_zygote_ = false;
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -0800203 if (kPoisonHeapReferences) {
204 // kPoisonHeapReferences currently works only with the interpreter only.
205 // TODO: make it work with the compiler.
206 interpreter_only_ = true;
207 } else {
208 interpreter_only_ = false;
209 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800210 is_explicit_gc_disabled_ = false;
211
212 long_pause_log_threshold_ = gc::Heap::kDefaultLongPauseLogThreshold;
213 long_gc_log_threshold_ = gc::Heap::kDefaultLongGCLogThreshold;
214 dump_gc_performance_on_shutdown_ = false;
215 ignore_max_footprint_ = false;
216
217 lock_profiling_threshold_ = 0;
218 hook_is_sensitive_thread_ = NULL;
219
220 hook_vfprintf_ = vfprintf;
221 hook_exit_ = exit;
222 hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
223
224// gLogVerbosity.class_linker = true; // TODO: don't check this in!
225// gLogVerbosity.compiler = true; // TODO: don't check this in!
226// gLogVerbosity.verifier = true; // TODO: don't check this in!
227// gLogVerbosity.heap = true; // TODO: don't check this in!
228// gLogVerbosity.gc = true; // TODO: don't check this in!
229// gLogVerbosity.jdwp = true; // TODO: don't check this in!
230// gLogVerbosity.jni = true; // TODO: don't check this in!
231// gLogVerbosity.monitor = true; // TODO: don't check this in!
232// gLogVerbosity.startup = true; // TODO: don't check this in!
233// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
234// gLogVerbosity.threads = true; // TODO: don't check this in!
235
236 method_trace_ = false;
237 method_trace_file_ = "/data/method-trace-file.bin";
238 method_trace_file_size_ = 10 * MB;
239
240 profile_ = false;
241 profile_period_s_ = 10; // Seconds.
242 profile_duration_s_ = 20; // Seconds.
243 profile_interval_us_ = 500; // Microseconds.
244 profile_backoff_coefficient_ = 2.0;
Calin Juravle16590062014-04-07 18:07:43 +0300245 profile_start_immediately_ = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800246 profile_clock_source_ = kDefaultProfilerClockSource;
247
Jeff Hao4a200f52014-04-01 14:58:49 -0700248 verify_ = true;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100249 image_isa_ = kRuntimeISA;
Jeff Hao4a200f52014-04-01 14:58:49 -0700250
Dave Allisonb373e092014-02-20 16:06:36 -0800251 // Default to explicit checks. Switch off with -implicit-checks:.
252 // or setprop dalvik.vm.implicit_checks check1,check2,...
253#ifdef HAVE_ANDROID_OS
254 {
255 char buf[PROP_VALUE_MAX];
Dave Allisonad9697a2014-05-09 21:42:36 +0000256 property_get("dalvik.vm.implicit_checks", buf, "none");
Dave Allisonb373e092014-02-20 16:06:36 -0800257 std::string checks(buf);
258 std::vector<std::string> checkvec;
259 Split(checks, ',', checkvec);
Dave Allisondd2e8252014-03-20 14:45:17 -0700260 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
261 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800262 for (auto& str : checkvec) {
263 std::string val = Trim(str);
264 if (val == "none") {
265 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
Dave Allisondd2e8252014-03-20 14:45:17 -0700266 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800267 } else if (val == "null") {
268 explicit_checks_ &= ~kExplicitNullCheck;
269 } else if (val == "suspend") {
270 explicit_checks_ &= ~kExplicitSuspendCheck;
271 } else if (val == "stack") {
272 explicit_checks_ &= ~kExplicitStackOverflowCheck;
273 } else if (val == "all") {
274 explicit_checks_ = 0;
275 }
276 }
277 }
278#else
279 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
280 kExplicitStackOverflowCheck;
281#endif
282
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800283 for (size_t i = 0; i < options.size(); ++i) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800284 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800285 LOG(INFO) << "option[" << i << "]=" << options[i].first;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800286 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800287 }
288 for (size_t i = 0; i < options.size(); ++i) {
289 const std::string option(options[i].first);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800290 if (StartsWith(option, "-help")) {
291 Usage(nullptr);
292 return false;
293 } else if (StartsWith(option, "-showversion")) {
294 UsageMessage(stdout, "ART version %s\n", Runtime::GetVersion());
295 Exit(0);
296 } else if (StartsWith(option, "-Xbootclasspath:")) {
297 boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
298 } else if (option == "-classpath" || option == "-cp") {
299 // TODO: support -Djava.class.path
300 i++;
301 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700302 Usage("Missing required class path value for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800303 return false;
304 }
305 const StringPiece& value = options[i].first;
306 class_path_string_ = value.data();
307 } else if (option == "bootclasspath") {
308 boot_class_path_
309 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
310 } else if (StartsWith(option, "-Ximage:")) {
311 if (!ParseStringAfterChar(option, ':', &image_)) {
312 return false;
313 }
314 } else if (StartsWith(option, "-Xcheck:jni")) {
315 check_jni_ = true;
316 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
317 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
318 // TODO: move parsing logic out of Dbg
319 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
320 if (tail != "help") {
321 UsageMessage(stderr, "Failed to parse JDWP option %s\n", tail.c_str());
322 }
323 Usage("Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
324 "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n");
325 return false;
326 }
327 } else if (StartsWith(option, "-Xms")) {
328 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
329 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700330 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800331 return false;
332 }
333 heap_initial_size_ = size;
334 } else if (StartsWith(option, "-Xmx")) {
335 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
336 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700337 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800338 return false;
339 }
340 heap_maximum_size_ = size;
341 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
342 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
343 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700344 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800345 return false;
346 }
347 heap_growth_limit_ = size;
348 } else if (StartsWith(option, "-XX:HeapMinFree=")) {
349 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMinFree=")).c_str(), 1024);
350 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700351 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800352 return false;
353 }
354 heap_min_free_ = size;
355 } else if (StartsWith(option, "-XX:HeapMaxFree=")) {
356 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMaxFree=")).c_str(), 1024);
357 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700358 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800359 return false;
360 }
361 heap_max_free_ = size;
362 } else if (StartsWith(option, "-XX:HeapTargetUtilization=")) {
363 if (!ParseDouble(option, '=', 0.1, 0.9, &heap_target_utilization_)) {
364 return false;
365 }
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700366 } else if (StartsWith(option, "-XX:ForegroundHeapGrowthMultiplier=")) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700367 if (!ParseDouble(option, '=', 0.1, 10.0, &foreground_heap_growth_multiplier_)) {
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700368 return false;
369 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800370 } else if (StartsWith(option, "-XX:ParallelGCThreads=")) {
371 if (!ParseUnsignedInteger(option, '=', &parallel_gc_threads_)) {
372 return false;
373 }
374 } else if (StartsWith(option, "-XX:ConcGCThreads=")) {
375 if (!ParseUnsignedInteger(option, '=', &conc_gc_threads_)) {
376 return false;
377 }
378 } else if (StartsWith(option, "-Xss")) {
379 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
380 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700381 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800382 return false;
383 }
384 stack_size_ = size;
385 } else if (StartsWith(option, "-XX:MaxSpinsBeforeThinLockInflation=")) {
386 if (!ParseUnsignedInteger(option, '=', &max_spins_before_thin_lock_inflation_)) {
387 return false;
388 }
389 } else if (StartsWith(option, "-XX:LongPauseLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800390 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800391 if (!ParseUnsignedInteger(option, '=', &value)) {
392 return false;
393 }
394 long_pause_log_threshold_ = MsToNs(value);
395 } else if (StartsWith(option, "-XX:LongGCLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800396 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800397 if (!ParseUnsignedInteger(option, '=', &value)) {
398 return false;
399 }
400 long_gc_log_threshold_ = MsToNs(value);
401 } else if (option == "-XX:DumpGCPerformanceOnShutdown") {
402 dump_gc_performance_on_shutdown_ = true;
403 } else if (option == "-XX:IgnoreMaxFootprint") {
404 ignore_max_footprint_ = true;
405 } else if (option == "-XX:LowMemoryMode") {
406 low_memory_mode_ = true;
407 } else if (option == "-XX:UseTLAB") {
408 use_tlab_ = true;
409 } else if (StartsWith(option, "-D")) {
410 properties_.push_back(option.substr(strlen("-D")));
411 } else if (StartsWith(option, "-Xjnitrace:")) {
412 jni_trace_ = option.substr(strlen("-Xjnitrace:"));
413 } else if (option == "compilercallbacks") {
414 compiler_callbacks_ =
415 reinterpret_cast<CompilerCallbacks*>(const_cast<void*>(options[i].second));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100416 } else if (option == "imageinstructionset") {
417 image_isa_ = GetInstructionSetFromString(
418 reinterpret_cast<const char*>(options[i].second));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800419 } else if (option == "-Xzygote") {
420 is_zygote_ = true;
421 } else if (option == "-Xint") {
422 interpreter_only_ = true;
423 } else if (StartsWith(option, "-Xgc:")) {
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700424 if (!ParseXGcOption(option)) {
425 return false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800426 }
427 } else if (StartsWith(option, "-XX:BackgroundGC=")) {
428 std::string substring;
429 if (!ParseStringAfterChar(option, '=', &substring)) {
430 return false;
431 }
432 gc::CollectorType collector_type = ParseCollectorType(substring);
433 if (collector_type != gc::kCollectorTypeNone) {
434 background_collector_type_ = collector_type;
435 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700436 Usage("Unknown -XX:BackgroundGC option %s\n", substring.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800437 return false;
438 }
439 } else if (option == "-XX:+DisableExplicitGC") {
440 is_explicit_gc_disabled_ = true;
441 } else if (StartsWith(option, "-verbose:")) {
442 std::vector<std::string> verbose_options;
443 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
444 for (size_t i = 0; i < verbose_options.size(); ++i) {
445 if (verbose_options[i] == "class") {
446 gLogVerbosity.class_linker = true;
447 } else if (verbose_options[i] == "verifier") {
448 gLogVerbosity.verifier = true;
449 } else if (verbose_options[i] == "compiler") {
450 gLogVerbosity.compiler = true;
451 } else if (verbose_options[i] == "heap") {
452 gLogVerbosity.heap = true;
453 } else if (verbose_options[i] == "gc") {
454 gLogVerbosity.gc = true;
455 } else if (verbose_options[i] == "jdwp") {
456 gLogVerbosity.jdwp = true;
457 } else if (verbose_options[i] == "jni") {
458 gLogVerbosity.jni = true;
459 } else if (verbose_options[i] == "monitor") {
460 gLogVerbosity.monitor = true;
461 } else if (verbose_options[i] == "startup") {
462 gLogVerbosity.startup = true;
463 } else if (verbose_options[i] == "third-party-jni") {
464 gLogVerbosity.third_party_jni = true;
465 } else if (verbose_options[i] == "threads") {
466 gLogVerbosity.threads = true;
467 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700468 Usage("Unknown -verbose option %s\n", verbose_options[i].c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800469 return false;
470 }
471 }
Mingyao Yang42d65c52014-04-18 16:49:39 -0700472 } else if (StartsWith(option, "-verbose-methods:")) {
473 gLogVerbosity.compiler = false;
474 Split(option.substr(strlen("-verbose-methods:")), ',', gVerboseMethods);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800475 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
476 if (!ParseUnsignedInteger(option, ':', &lock_profiling_threshold_)) {
477 return false;
478 }
479 } else if (StartsWith(option, "-Xstacktracefile:")) {
480 if (!ParseStringAfterChar(option, ':', &stack_trace_file_)) {
481 return false;
482 }
483 } else if (option == "sensitiveThread") {
484 const void* hook = options[i].second;
485 hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(hook));
486 } else if (option == "vfprintf") {
487 const void* hook = options[i].second;
488 if (hook == nullptr) {
489 Usage("vfprintf argument was NULL");
490 return false;
491 }
492 hook_vfprintf_ =
493 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(hook));
494 } else if (option == "exit") {
495 const void* hook = options[i].second;
496 if (hook == nullptr) {
497 Usage("exit argument was NULL");
498 return false;
499 }
500 hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(hook));
501 } else if (option == "abort") {
502 const void* hook = options[i].second;
503 if (hook == nullptr) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700504 Usage("abort was NULL\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800505 return false;
506 }
507 hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(hook));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800508 } else if (option == "-Xmethod-trace") {
509 method_trace_ = true;
510 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
511 method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
512 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
513 if (!ParseUnsignedInteger(option, ':', &method_trace_file_size_)) {
514 return false;
515 }
516 } else if (option == "-Xprofile:threadcpuclock") {
517 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
518 } else if (option == "-Xprofile:wallclock") {
519 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
520 } else if (option == "-Xprofile:dualclock") {
521 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
522 } else if (StartsWith(option, "-Xprofile:")) {
523 if (!ParseStringAfterChar(option, ';', &profile_output_filename_)) {
524 return false;
525 }
526 profile_ = true;
527 } else if (StartsWith(option, "-Xprofile-period:")) {
528 if (!ParseUnsignedInteger(option, ':', &profile_period_s_)) {
529 return false;
530 }
531 } else if (StartsWith(option, "-Xprofile-duration:")) {
532 if (!ParseUnsignedInteger(option, ':', &profile_duration_s_)) {
533 return false;
534 }
535 } else if (StartsWith(option, "-Xprofile-interval:")) {
536 if (!ParseUnsignedInteger(option, ':', &profile_interval_us_)) {
537 return false;
538 }
539 } else if (StartsWith(option, "-Xprofile-backoff:")) {
540 if (!ParseDouble(option, ':', 1.0, 10.0, &profile_backoff_coefficient_)) {
541 return false;
542 }
Calin Juravle16590062014-04-07 18:07:43 +0300543 } else if (option == "-Xprofile-start-lazy") {
544 profile_start_immediately_ = false;
Dave Allisonb373e092014-02-20 16:06:36 -0800545 } else if (StartsWith(option, "-implicit-checks:")) {
546 std::string checks;
547 if (!ParseStringAfterChar(option, ':', &checks)) {
548 return false;
549 }
550 std::vector<std::string> checkvec;
551 Split(checks, ',', checkvec);
552 for (auto& str : checkvec) {
553 std::string val = Trim(str);
554 if (val == "none") {
555 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
556 kExplicitStackOverflowCheck;
557 } else if (val == "null") {
558 explicit_checks_ &= ~kExplicitNullCheck;
559 } else if (val == "suspend") {
560 explicit_checks_ &= ~kExplicitSuspendCheck;
561 } else if (val == "stack") {
562 explicit_checks_ &= ~kExplicitStackOverflowCheck;
563 } else if (val == "all") {
564 explicit_checks_ = 0;
565 } else {
566 return false;
567 }
568 }
569 } else if (StartsWith(option, "-explicit-checks:")) {
570 std::string checks;
571 if (!ParseStringAfterChar(option, ':', &checks)) {
572 return false;
573 }
574 std::vector<std::string> checkvec;
575 Split(checks, ',', checkvec);
576 for (auto& str : checkvec) {
577 std::string val = Trim(str);
578 if (val == "none") {
579 explicit_checks_ = 0;
580 } else if (val == "null") {
581 explicit_checks_ |= kExplicitNullCheck;
582 } else if (val == "suspend") {
583 explicit_checks_ |= kExplicitSuspendCheck;
584 } else if (val == "stack") {
585 explicit_checks_ |= kExplicitStackOverflowCheck;
586 } else if (val == "all") {
587 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
588 kExplicitStackOverflowCheck;
589 } else {
590 return false;
591 }
592 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800593 } else if (option == "-Xcompiler-option") {
594 i++;
595 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700596 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800597 return false;
598 }
599 compiler_options_.push_back(options[i].first);
600 } else if (option == "-Ximage-compiler-option") {
601 i++;
602 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700603 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800604 return false;
605 }
606 image_compiler_options_.push_back(options[i].first);
Jeff Hao4a200f52014-04-01 14:58:49 -0700607 } else if (StartsWith(option, "-Xverify:")) {
608 std::string verify_mode = option.substr(strlen("-Xverify:"));
609 if (verify_mode == "none") {
610 verify_ = false;
611 } else if (verify_mode == "remote" || verify_mode == "all") {
612 verify_ = true;
613 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700614 Usage("Unknown -Xverify option %s\n", verify_mode.c_str());
Jeff Hao4a200f52014-04-01 14:58:49 -0700615 return false;
616 }
Yevgeny Roubana6119a22014-03-24 11:31:24 +0700617 } else if (StartsWith(option, "-ea") ||
618 StartsWith(option, "-da") ||
619 StartsWith(option, "-enableassertions") ||
620 StartsWith(option, "-disableassertions") ||
Dave Allisonb373e092014-02-20 16:06:36 -0800621 (option == "--runtime-arg") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800622 (option == "-esa") ||
623 (option == "-dsa") ||
624 (option == "-enablesystemassertions") ||
625 (option == "-disablesystemassertions") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800626 (option == "-Xrs") ||
627 StartsWith(option, "-Xint:") ||
628 StartsWith(option, "-Xdexopt:") ||
629 (option == "-Xnoquithandler") ||
630 StartsWith(option, "-Xjniopts:") ||
631 StartsWith(option, "-Xjnigreflimit:") ||
632 (option == "-Xgenregmap") ||
633 (option == "-Xnogenregmap") ||
634 StartsWith(option, "-Xverifyopt:") ||
635 (option == "-Xcheckdexsum") ||
636 (option == "-Xincludeselectedop") ||
637 StartsWith(option, "-Xjitop:") ||
638 (option == "-Xincludeselectedmethod") ||
639 StartsWith(option, "-Xjitthreshold:") ||
640 StartsWith(option, "-Xjitcodecachesize:") ||
641 (option == "-Xjitblocking") ||
642 StartsWith(option, "-Xjitmethod:") ||
643 StartsWith(option, "-Xjitclass:") ||
644 StartsWith(option, "-Xjitoffset:") ||
645 StartsWith(option, "-Xjitconfig:") ||
646 (option == "-Xjitcheckcg") ||
647 (option == "-Xjitverbose") ||
648 (option == "-Xjitprofile") ||
649 (option == "-Xjitdisableopt") ||
650 (option == "-Xjitsuspendpoll") ||
651 StartsWith(option, "-XX:mainThreadStackSize=")) {
652 // Ignored for backwards compatibility.
653 } else if (!ignore_unrecognized) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700654 Usage("Unrecognized option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800655 return false;
656 }
657 }
658
659 // If a reference to the dalvik core.jar snuck in, replace it with
660 // the art specific version. This can happen with on device
661 // boot.art/boot.oat generation by GenerateImage which relies on the
662 // value of BOOTCLASSPATH.
663 std::string core_jar("/core.jar");
664 size_t core_jar_pos = boot_class_path_string_.find(core_jar);
665 if (core_jar_pos != std::string::npos) {
666 boot_class_path_string_.replace(core_jar_pos, core_jar.size(), "/core-libart.jar");
667 }
668
669 if (compiler_callbacks_ == nullptr && image_.empty()) {
670 image_ += GetAndroidRoot();
671 image_ += "/framework/boot.art";
672 }
673 if (heap_growth_limit_ == 0) {
674 heap_growth_limit_ = heap_maximum_size_;
675 }
676 if (background_collector_type_ == gc::kCollectorTypeNone) {
677 background_collector_type_ = collector_type_;
678 }
679 return true;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100680} // NOLINT(readability/fn_size)
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800681
682void ParsedOptions::Exit(int status) {
683 hook_exit_(status);
684}
685
686void ParsedOptions::Abort() {
687 hook_abort_();
688}
689
690void ParsedOptions::UsageMessageV(FILE* stream, const char* fmt, va_list ap) {
691 hook_vfprintf_(stderr, fmt, ap);
692}
693
694void ParsedOptions::UsageMessage(FILE* stream, const char* fmt, ...) {
695 va_list ap;
696 va_start(ap, fmt);
697 UsageMessageV(stream, fmt, ap);
698 va_end(ap);
699}
700
701void ParsedOptions::Usage(const char* fmt, ...) {
702 bool error = (fmt != nullptr);
703 FILE* stream = error ? stderr : stdout;
704
705 if (fmt != nullptr) {
706 va_list ap;
707 va_start(ap, fmt);
708 UsageMessageV(stream, fmt, ap);
709 va_end(ap);
710 }
711
712 const char* program = "dalvikvm";
713 UsageMessage(stream, "%s: [options] class [argument ...]\n", program);
714 UsageMessage(stream, "\n");
715 UsageMessage(stream, "The following standard options are supported:\n");
716 UsageMessage(stream, " -classpath classpath (-cp classpath)\n");
717 UsageMessage(stream, " -Dproperty=value\n");
718 UsageMessage(stream, " -verbose:tag ('gc', 'jni', or 'class')\n");
719 UsageMessage(stream, " -showversion\n");
720 UsageMessage(stream, " -help\n");
721 UsageMessage(stream, " -agentlib:jdwp=options\n");
722 UsageMessage(stream, "\n");
723
724 UsageMessage(stream, "The following extended options are supported:\n");
725 UsageMessage(stream, " -Xrunjdwp:<options>\n");
726 UsageMessage(stream, " -Xbootclasspath:bootclasspath\n");
727 UsageMessage(stream, " -Xcheck:tag (e.g. 'jni')\n");
728 UsageMessage(stream, " -XmsN (min heap, must be multiple of 1K, >= 1MB)\n");
729 UsageMessage(stream, " -XmxN (max heap, must be multiple of 1K, >= 2MB)\n");
730 UsageMessage(stream, " -XssN (stack size)\n");
731 UsageMessage(stream, " -Xint\n");
732 UsageMessage(stream, "\n");
733
734 UsageMessage(stream, "The following Dalvik options are supported:\n");
735 UsageMessage(stream, " -Xzygote\n");
736 UsageMessage(stream, " -Xjnitrace:substring (eg NativeClass or nativeMethod)\n");
737 UsageMessage(stream, " -Xstacktracefile:<filename>\n");
738 UsageMessage(stream, " -Xgc:[no]preverify\n");
739 UsageMessage(stream, " -Xgc:[no]postverify\n");
740 UsageMessage(stream, " -XX:+DisableExplicitGC\n");
741 UsageMessage(stream, " -XX:HeapGrowthLimit=N\n");
742 UsageMessage(stream, " -XX:HeapMinFree=N\n");
743 UsageMessage(stream, " -XX:HeapMaxFree=N\n");
744 UsageMessage(stream, " -XX:HeapTargetUtilization=doublevalue\n");
Mathieu Chartier455820e2014-04-18 12:02:39 -0700745 UsageMessage(stream, " -XX:ForegroundHeapGrowthMultiplier=doublevalue\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800746 UsageMessage(stream, " -XX:LowMemoryMode\n");
747 UsageMessage(stream, " -Xprofile:{threadcpuclock,wallclock,dualclock}\n");
748 UsageMessage(stream, "\n");
749
750 UsageMessage(stream, "The following unique to ART options are supported:\n");
751 UsageMessage(stream, " -Xgc:[no]preverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700752 UsageMessage(stream, " -Xgc:[no]postsweepingverify_rosalloc\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800753 UsageMessage(stream, " -Xgc:[no]postverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700754 UsageMessage(stream, " -Xgc:[no]presweepingverify\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800755 UsageMessage(stream, " -Ximage:filename\n");
756 UsageMessage(stream, " -XX:ParallelGCThreads=integervalue\n");
757 UsageMessage(stream, " -XX:ConcGCThreads=integervalue\n");
758 UsageMessage(stream, " -XX:MaxSpinsBeforeThinLockInflation=integervalue\n");
759 UsageMessage(stream, " -XX:LongPauseLogThreshold=integervalue\n");
760 UsageMessage(stream, " -XX:LongGCLogThreshold=integervalue\n");
761 UsageMessage(stream, " -XX:DumpGCPerformanceOnShutdown\n");
762 UsageMessage(stream, " -XX:IgnoreMaxFootprint\n");
763 UsageMessage(stream, " -XX:UseTLAB\n");
764 UsageMessage(stream, " -XX:BackgroundGC=none\n");
765 UsageMessage(stream, " -Xmethod-trace\n");
766 UsageMessage(stream, " -Xmethod-trace-file:filename");
767 UsageMessage(stream, " -Xmethod-trace-file-size:integervalue\n");
768 UsageMessage(stream, " -Xprofile=filename\n");
769 UsageMessage(stream, " -Xprofile-period:integervalue\n");
770 UsageMessage(stream, " -Xprofile-duration:integervalue\n");
771 UsageMessage(stream, " -Xprofile-interval:integervalue\n");
772 UsageMessage(stream, " -Xprofile-backoff:integervalue\n");
773 UsageMessage(stream, " -Xcompiler-option dex2oat-option\n");
774 UsageMessage(stream, " -Ximage-compiler-option dex2oat-option\n");
775 UsageMessage(stream, "\n");
776
777 UsageMessage(stream, "The following previously supported Dalvik options are ignored:\n");
778 UsageMessage(stream, " -ea[:<package name>... |:<class name>]\n");
779 UsageMessage(stream, " -da[:<package name>... |:<class name>]\n");
780 UsageMessage(stream, " (-enableassertions, -disableassertions)\n");
781 UsageMessage(stream, " -esa\n");
782 UsageMessage(stream, " -dsa\n");
783 UsageMessage(stream, " (-enablesystemassertions, -disablesystemassertions)\n");
784 UsageMessage(stream, " -Xverify:{none,remote,all}\n");
785 UsageMessage(stream, " -Xrs\n");
786 UsageMessage(stream, " -Xint:portable, -Xint:fast, -Xint:jit\n");
787 UsageMessage(stream, " -Xdexopt:{none,verified,all,full}\n");
788 UsageMessage(stream, " -Xnoquithandler\n");
789 UsageMessage(stream, " -Xjniopts:{warnonly,forcecopy}\n");
790 UsageMessage(stream, " -Xjnigreflimit:integervalue\n");
791 UsageMessage(stream, " -Xgc:[no]precise\n");
792 UsageMessage(stream, " -Xgc:[no]verifycardtable\n");
793 UsageMessage(stream, " -X[no]genregmap\n");
794 UsageMessage(stream, " -Xverifyopt:[no]checkmon\n");
795 UsageMessage(stream, " -Xcheckdexsum\n");
796 UsageMessage(stream, " -Xincludeselectedop\n");
797 UsageMessage(stream, " -Xjitop:hexopvalue[-endvalue][,hexopvalue[-endvalue]]*\n");
798 UsageMessage(stream, " -Xincludeselectedmethod\n");
799 UsageMessage(stream, " -Xjitthreshold:integervalue\n");
800 UsageMessage(stream, " -Xjitcodecachesize:decimalvalueofkbytes\n");
801 UsageMessage(stream, " -Xjitblocking\n");
802 UsageMessage(stream, " -Xjitmethod:signature[,signature]* (eg Ljava/lang/String\\;replace)\n");
803 UsageMessage(stream, " -Xjitclass:classname[,classname]*\n");
804 UsageMessage(stream, " -Xjitoffset:offset[,offset]\n");
805 UsageMessage(stream, " -Xjitconfig:filename\n");
806 UsageMessage(stream, " -Xjitcheckcg\n");
807 UsageMessage(stream, " -Xjitverbose\n");
808 UsageMessage(stream, " -Xjitprofile\n");
809 UsageMessage(stream, " -Xjitdisableopt\n");
810 UsageMessage(stream, " -Xjitsuspendpoll\n");
811 UsageMessage(stream, " -XX:mainThreadStackSize=N\n");
812 UsageMessage(stream, "\n");
813
814 Exit((error) ? 1 : 0);
815}
816
817bool ParsedOptions::ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) {
818 std::string::size_type colon = s.find(c);
819 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700820 Usage("Missing char %c in option %s\n", c, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800821 return false;
822 }
823 // Add one to remove the char we were trimming until.
824 *parsed_value = s.substr(colon + 1);
825 return true;
826}
827
828bool ParsedOptions::ParseInteger(const std::string& s, char after_char, int* parsed_value) {
829 std::string::size_type colon = s.find(after_char);
830 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700831 Usage("Missing char %c in option %s\n", after_char, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800832 return false;
833 }
834 const char* begin = &s[colon + 1];
835 char* end;
836 size_t result = strtoul(begin, &end, 10);
837 if (begin == end || *end != '\0') {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700838 Usage("Failed to parse integer from %s\n", s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800839 return false;
840 }
841 *parsed_value = result;
842 return true;
843}
844
845bool ParsedOptions::ParseUnsignedInteger(const std::string& s, char after_char,
846 unsigned int* parsed_value) {
847 int i;
848 if (!ParseInteger(s, after_char, &i)) {
849 return false;
850 }
851 if (i < 0) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700852 Usage("Negative value %d passed for unsigned option %s\n", i, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800853 return false;
854 }
855 *parsed_value = i;
856 return true;
857}
858
859bool ParsedOptions::ParseDouble(const std::string& option, char after_char,
860 double min, double max, double* parsed_value) {
861 std::string substring;
862 if (!ParseStringAfterChar(option, after_char, &substring)) {
863 return false;
864 }
865 std::istringstream iss(substring);
866 double value;
867 iss >> value;
868 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
869 const bool sane_val = iss.eof() && (value >= min) && (value <= max);
870 if (!sane_val) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700871 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800872 return false;
873 }
874 *parsed_value = value;
875 return true;
876}
877
878} // namespace art