blob: 15625274297b6c3628a4741729f1fb2b968d7e29 [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!
Dave Allison5cd33752014-04-15 15:57:58 -0700235// gLogVerbosity.signals = true; // TODO: don't check this in!
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800236
237 method_trace_ = false;
238 method_trace_file_ = "/data/method-trace-file.bin";
239 method_trace_file_size_ = 10 * MB;
240
241 profile_ = false;
242 profile_period_s_ = 10; // Seconds.
243 profile_duration_s_ = 20; // Seconds.
244 profile_interval_us_ = 500; // Microseconds.
245 profile_backoff_coefficient_ = 2.0;
Calin Juravle16590062014-04-07 18:07:43 +0300246 profile_start_immediately_ = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800247 profile_clock_source_ = kDefaultProfilerClockSource;
248
Jeff Hao4a200f52014-04-01 14:58:49 -0700249 verify_ = true;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100250 image_isa_ = kRuntimeISA;
Jeff Hao4a200f52014-04-01 14:58:49 -0700251
Dave Allisonb373e092014-02-20 16:06:36 -0800252 // Default to explicit checks. Switch off with -implicit-checks:.
253 // or setprop dalvik.vm.implicit_checks check1,check2,...
254#ifdef HAVE_ANDROID_OS
255 {
256 char buf[PROP_VALUE_MAX];
Dave Allison05266432014-05-05 13:17:37 -0700257 property_get("dalvik.vm.implicit_checks", buf, "null,stack");
Dave Allisonb373e092014-02-20 16:06:36 -0800258 std::string checks(buf);
259 std::vector<std::string> checkvec;
260 Split(checks, ',', checkvec);
Dave Allisondd2e8252014-03-20 14:45:17 -0700261 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
262 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800263 for (auto& str : checkvec) {
264 std::string val = Trim(str);
265 if (val == "none") {
266 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
Dave Allisondd2e8252014-03-20 14:45:17 -0700267 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800268 } else if (val == "null") {
269 explicit_checks_ &= ~kExplicitNullCheck;
270 } else if (val == "suspend") {
271 explicit_checks_ &= ~kExplicitSuspendCheck;
272 } else if (val == "stack") {
273 explicit_checks_ &= ~kExplicitStackOverflowCheck;
274 } else if (val == "all") {
275 explicit_checks_ = 0;
276 }
277 }
278 }
279#else
280 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
281 kExplicitStackOverflowCheck;
282#endif
283
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800284 for (size_t i = 0; i < options.size(); ++i) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800285 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800286 LOG(INFO) << "option[" << i << "]=" << options[i].first;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800287 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800288 }
289 for (size_t i = 0; i < options.size(); ++i) {
290 const std::string option(options[i].first);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800291 if (StartsWith(option, "-help")) {
292 Usage(nullptr);
293 return false;
294 } else if (StartsWith(option, "-showversion")) {
295 UsageMessage(stdout, "ART version %s\n", Runtime::GetVersion());
296 Exit(0);
297 } else if (StartsWith(option, "-Xbootclasspath:")) {
298 boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
299 } else if (option == "-classpath" || option == "-cp") {
300 // TODO: support -Djava.class.path
301 i++;
302 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700303 Usage("Missing required class path value for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800304 return false;
305 }
306 const StringPiece& value = options[i].first;
307 class_path_string_ = value.data();
308 } else if (option == "bootclasspath") {
309 boot_class_path_
310 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
311 } else if (StartsWith(option, "-Ximage:")) {
312 if (!ParseStringAfterChar(option, ':', &image_)) {
313 return false;
314 }
315 } else if (StartsWith(option, "-Xcheck:jni")) {
316 check_jni_ = true;
317 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
318 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
319 // TODO: move parsing logic out of Dbg
320 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
321 if (tail != "help") {
322 UsageMessage(stderr, "Failed to parse JDWP option %s\n", tail.c_str());
323 }
324 Usage("Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
325 "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n");
326 return false;
327 }
328 } else if (StartsWith(option, "-Xms")) {
329 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
330 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700331 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800332 return false;
333 }
334 heap_initial_size_ = size;
335 } else if (StartsWith(option, "-Xmx")) {
336 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
337 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700338 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800339 return false;
340 }
341 heap_maximum_size_ = size;
342 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
343 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
344 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700345 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800346 return false;
347 }
348 heap_growth_limit_ = size;
349 } else if (StartsWith(option, "-XX:HeapMinFree=")) {
350 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMinFree=")).c_str(), 1024);
351 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700352 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800353 return false;
354 }
355 heap_min_free_ = size;
356 } else if (StartsWith(option, "-XX:HeapMaxFree=")) {
357 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMaxFree=")).c_str(), 1024);
358 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700359 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800360 return false;
361 }
362 heap_max_free_ = size;
363 } else if (StartsWith(option, "-XX:HeapTargetUtilization=")) {
364 if (!ParseDouble(option, '=', 0.1, 0.9, &heap_target_utilization_)) {
365 return false;
366 }
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700367 } else if (StartsWith(option, "-XX:ForegroundHeapGrowthMultiplier=")) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700368 if (!ParseDouble(option, '=', 0.1, 10.0, &foreground_heap_growth_multiplier_)) {
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700369 return false;
370 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800371 } else if (StartsWith(option, "-XX:ParallelGCThreads=")) {
372 if (!ParseUnsignedInteger(option, '=', &parallel_gc_threads_)) {
373 return false;
374 }
375 } else if (StartsWith(option, "-XX:ConcGCThreads=")) {
376 if (!ParseUnsignedInteger(option, '=', &conc_gc_threads_)) {
377 return false;
378 }
379 } else if (StartsWith(option, "-Xss")) {
380 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
381 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700382 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800383 return false;
384 }
385 stack_size_ = size;
386 } else if (StartsWith(option, "-XX:MaxSpinsBeforeThinLockInflation=")) {
387 if (!ParseUnsignedInteger(option, '=', &max_spins_before_thin_lock_inflation_)) {
388 return false;
389 }
390 } else if (StartsWith(option, "-XX:LongPauseLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800391 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800392 if (!ParseUnsignedInteger(option, '=', &value)) {
393 return false;
394 }
395 long_pause_log_threshold_ = MsToNs(value);
396 } else if (StartsWith(option, "-XX:LongGCLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800397 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800398 if (!ParseUnsignedInteger(option, '=', &value)) {
399 return false;
400 }
401 long_gc_log_threshold_ = MsToNs(value);
402 } else if (option == "-XX:DumpGCPerformanceOnShutdown") {
403 dump_gc_performance_on_shutdown_ = true;
404 } else if (option == "-XX:IgnoreMaxFootprint") {
405 ignore_max_footprint_ = true;
406 } else if (option == "-XX:LowMemoryMode") {
407 low_memory_mode_ = true;
408 } else if (option == "-XX:UseTLAB") {
409 use_tlab_ = true;
410 } else if (StartsWith(option, "-D")) {
411 properties_.push_back(option.substr(strlen("-D")));
412 } else if (StartsWith(option, "-Xjnitrace:")) {
413 jni_trace_ = option.substr(strlen("-Xjnitrace:"));
414 } else if (option == "compilercallbacks") {
415 compiler_callbacks_ =
416 reinterpret_cast<CompilerCallbacks*>(const_cast<void*>(options[i].second));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100417 } else if (option == "imageinstructionset") {
418 image_isa_ = GetInstructionSetFromString(
419 reinterpret_cast<const char*>(options[i].second));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800420 } else if (option == "-Xzygote") {
421 is_zygote_ = true;
422 } else if (option == "-Xint") {
423 interpreter_only_ = true;
424 } else if (StartsWith(option, "-Xgc:")) {
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700425 if (!ParseXGcOption(option)) {
426 return false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800427 }
428 } else if (StartsWith(option, "-XX:BackgroundGC=")) {
429 std::string substring;
430 if (!ParseStringAfterChar(option, '=', &substring)) {
431 return false;
432 }
433 gc::CollectorType collector_type = ParseCollectorType(substring);
434 if (collector_type != gc::kCollectorTypeNone) {
435 background_collector_type_ = collector_type;
436 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700437 Usage("Unknown -XX:BackgroundGC option %s\n", substring.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800438 return false;
439 }
440 } else if (option == "-XX:+DisableExplicitGC") {
441 is_explicit_gc_disabled_ = true;
442 } else if (StartsWith(option, "-verbose:")) {
443 std::vector<std::string> verbose_options;
444 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
445 for (size_t i = 0; i < verbose_options.size(); ++i) {
446 if (verbose_options[i] == "class") {
447 gLogVerbosity.class_linker = true;
448 } else if (verbose_options[i] == "verifier") {
449 gLogVerbosity.verifier = true;
450 } else if (verbose_options[i] == "compiler") {
451 gLogVerbosity.compiler = true;
452 } else if (verbose_options[i] == "heap") {
453 gLogVerbosity.heap = true;
454 } else if (verbose_options[i] == "gc") {
455 gLogVerbosity.gc = true;
456 } else if (verbose_options[i] == "jdwp") {
457 gLogVerbosity.jdwp = true;
458 } else if (verbose_options[i] == "jni") {
459 gLogVerbosity.jni = true;
460 } else if (verbose_options[i] == "monitor") {
461 gLogVerbosity.monitor = true;
462 } else if (verbose_options[i] == "startup") {
463 gLogVerbosity.startup = true;
464 } else if (verbose_options[i] == "third-party-jni") {
465 gLogVerbosity.third_party_jni = true;
466 } else if (verbose_options[i] == "threads") {
467 gLogVerbosity.threads = true;
Dave Allison5cd33752014-04-15 15:57:58 -0700468 } else if (verbose_options[i] == "signals") {
469 gLogVerbosity.signals = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800470 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700471 Usage("Unknown -verbose option %s\n", verbose_options[i].c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800472 return false;
473 }
474 }
Mingyao Yang42d65c52014-04-18 16:49:39 -0700475 } else if (StartsWith(option, "-verbose-methods:")) {
476 gLogVerbosity.compiler = false;
477 Split(option.substr(strlen("-verbose-methods:")), ',', gVerboseMethods);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800478 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
479 if (!ParseUnsignedInteger(option, ':', &lock_profiling_threshold_)) {
480 return false;
481 }
482 } else if (StartsWith(option, "-Xstacktracefile:")) {
483 if (!ParseStringAfterChar(option, ':', &stack_trace_file_)) {
484 return false;
485 }
486 } else if (option == "sensitiveThread") {
487 const void* hook = options[i].second;
488 hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(hook));
489 } else if (option == "vfprintf") {
490 const void* hook = options[i].second;
491 if (hook == nullptr) {
492 Usage("vfprintf argument was NULL");
493 return false;
494 }
495 hook_vfprintf_ =
496 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(hook));
497 } else if (option == "exit") {
498 const void* hook = options[i].second;
499 if (hook == nullptr) {
500 Usage("exit argument was NULL");
501 return false;
502 }
503 hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(hook));
504 } else if (option == "abort") {
505 const void* hook = options[i].second;
506 if (hook == nullptr) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700507 Usage("abort was NULL\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800508 return false;
509 }
510 hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(hook));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800511 } else if (option == "-Xmethod-trace") {
512 method_trace_ = true;
513 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
514 method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
515 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
516 if (!ParseUnsignedInteger(option, ':', &method_trace_file_size_)) {
517 return false;
518 }
519 } else if (option == "-Xprofile:threadcpuclock") {
520 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
521 } else if (option == "-Xprofile:wallclock") {
522 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
523 } else if (option == "-Xprofile:dualclock") {
524 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
525 } else if (StartsWith(option, "-Xprofile:")) {
526 if (!ParseStringAfterChar(option, ';', &profile_output_filename_)) {
527 return false;
528 }
529 profile_ = true;
530 } else if (StartsWith(option, "-Xprofile-period:")) {
531 if (!ParseUnsignedInteger(option, ':', &profile_period_s_)) {
532 return false;
533 }
534 } else if (StartsWith(option, "-Xprofile-duration:")) {
535 if (!ParseUnsignedInteger(option, ':', &profile_duration_s_)) {
536 return false;
537 }
538 } else if (StartsWith(option, "-Xprofile-interval:")) {
539 if (!ParseUnsignedInteger(option, ':', &profile_interval_us_)) {
540 return false;
541 }
542 } else if (StartsWith(option, "-Xprofile-backoff:")) {
543 if (!ParseDouble(option, ':', 1.0, 10.0, &profile_backoff_coefficient_)) {
544 return false;
545 }
Calin Juravle16590062014-04-07 18:07:43 +0300546 } else if (option == "-Xprofile-start-lazy") {
547 profile_start_immediately_ = false;
Dave Allisonb373e092014-02-20 16:06:36 -0800548 } else if (StartsWith(option, "-implicit-checks:")) {
549 std::string checks;
550 if (!ParseStringAfterChar(option, ':', &checks)) {
551 return false;
552 }
553 std::vector<std::string> checkvec;
554 Split(checks, ',', checkvec);
555 for (auto& str : checkvec) {
556 std::string val = Trim(str);
557 if (val == "none") {
558 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
559 kExplicitStackOverflowCheck;
560 } else if (val == "null") {
561 explicit_checks_ &= ~kExplicitNullCheck;
562 } else if (val == "suspend") {
563 explicit_checks_ &= ~kExplicitSuspendCheck;
564 } else if (val == "stack") {
565 explicit_checks_ &= ~kExplicitStackOverflowCheck;
566 } else if (val == "all") {
567 explicit_checks_ = 0;
568 } else {
569 return false;
570 }
571 }
572 } else if (StartsWith(option, "-explicit-checks:")) {
573 std::string checks;
574 if (!ParseStringAfterChar(option, ':', &checks)) {
575 return false;
576 }
577 std::vector<std::string> checkvec;
578 Split(checks, ',', checkvec);
579 for (auto& str : checkvec) {
580 std::string val = Trim(str);
581 if (val == "none") {
582 explicit_checks_ = 0;
583 } else if (val == "null") {
584 explicit_checks_ |= kExplicitNullCheck;
585 } else if (val == "suspend") {
586 explicit_checks_ |= kExplicitSuspendCheck;
587 } else if (val == "stack") {
588 explicit_checks_ |= kExplicitStackOverflowCheck;
589 } else if (val == "all") {
590 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
591 kExplicitStackOverflowCheck;
592 } else {
593 return false;
594 }
595 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800596 } else if (option == "-Xcompiler-option") {
597 i++;
598 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700599 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800600 return false;
601 }
602 compiler_options_.push_back(options[i].first);
603 } else if (option == "-Ximage-compiler-option") {
604 i++;
605 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700606 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800607 return false;
608 }
609 image_compiler_options_.push_back(options[i].first);
Jeff Hao4a200f52014-04-01 14:58:49 -0700610 } else if (StartsWith(option, "-Xverify:")) {
611 std::string verify_mode = option.substr(strlen("-Xverify:"));
612 if (verify_mode == "none") {
613 verify_ = false;
614 } else if (verify_mode == "remote" || verify_mode == "all") {
615 verify_ = true;
616 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700617 Usage("Unknown -Xverify option %s\n", verify_mode.c_str());
Jeff Hao4a200f52014-04-01 14:58:49 -0700618 return false;
619 }
Yevgeny Roubana6119a22014-03-24 11:31:24 +0700620 } else if (StartsWith(option, "-ea") ||
621 StartsWith(option, "-da") ||
622 StartsWith(option, "-enableassertions") ||
623 StartsWith(option, "-disableassertions") ||
Dave Allisonb373e092014-02-20 16:06:36 -0800624 (option == "--runtime-arg") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800625 (option == "-esa") ||
626 (option == "-dsa") ||
627 (option == "-enablesystemassertions") ||
628 (option == "-disablesystemassertions") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800629 (option == "-Xrs") ||
630 StartsWith(option, "-Xint:") ||
631 StartsWith(option, "-Xdexopt:") ||
632 (option == "-Xnoquithandler") ||
633 StartsWith(option, "-Xjniopts:") ||
634 StartsWith(option, "-Xjnigreflimit:") ||
635 (option == "-Xgenregmap") ||
636 (option == "-Xnogenregmap") ||
637 StartsWith(option, "-Xverifyopt:") ||
638 (option == "-Xcheckdexsum") ||
639 (option == "-Xincludeselectedop") ||
640 StartsWith(option, "-Xjitop:") ||
641 (option == "-Xincludeselectedmethod") ||
642 StartsWith(option, "-Xjitthreshold:") ||
643 StartsWith(option, "-Xjitcodecachesize:") ||
644 (option == "-Xjitblocking") ||
645 StartsWith(option, "-Xjitmethod:") ||
646 StartsWith(option, "-Xjitclass:") ||
647 StartsWith(option, "-Xjitoffset:") ||
648 StartsWith(option, "-Xjitconfig:") ||
649 (option == "-Xjitcheckcg") ||
650 (option == "-Xjitverbose") ||
651 (option == "-Xjitprofile") ||
652 (option == "-Xjitdisableopt") ||
653 (option == "-Xjitsuspendpoll") ||
654 StartsWith(option, "-XX:mainThreadStackSize=")) {
655 // Ignored for backwards compatibility.
656 } else if (!ignore_unrecognized) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700657 Usage("Unrecognized option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800658 return false;
659 }
660 }
661
662 // If a reference to the dalvik core.jar snuck in, replace it with
663 // the art specific version. This can happen with on device
664 // boot.art/boot.oat generation by GenerateImage which relies on the
665 // value of BOOTCLASSPATH.
666 std::string core_jar("/core.jar");
667 size_t core_jar_pos = boot_class_path_string_.find(core_jar);
668 if (core_jar_pos != std::string::npos) {
669 boot_class_path_string_.replace(core_jar_pos, core_jar.size(), "/core-libart.jar");
670 }
671
672 if (compiler_callbacks_ == nullptr && image_.empty()) {
673 image_ += GetAndroidRoot();
674 image_ += "/framework/boot.art";
675 }
676 if (heap_growth_limit_ == 0) {
677 heap_growth_limit_ = heap_maximum_size_;
678 }
679 if (background_collector_type_ == gc::kCollectorTypeNone) {
680 background_collector_type_ = collector_type_;
681 }
682 return true;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100683} // NOLINT(readability/fn_size)
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800684
685void ParsedOptions::Exit(int status) {
686 hook_exit_(status);
687}
688
689void ParsedOptions::Abort() {
690 hook_abort_();
691}
692
693void ParsedOptions::UsageMessageV(FILE* stream, const char* fmt, va_list ap) {
694 hook_vfprintf_(stderr, fmt, ap);
695}
696
697void ParsedOptions::UsageMessage(FILE* stream, const char* fmt, ...) {
698 va_list ap;
699 va_start(ap, fmt);
700 UsageMessageV(stream, fmt, ap);
701 va_end(ap);
702}
703
704void ParsedOptions::Usage(const char* fmt, ...) {
705 bool error = (fmt != nullptr);
706 FILE* stream = error ? stderr : stdout;
707
708 if (fmt != nullptr) {
709 va_list ap;
710 va_start(ap, fmt);
711 UsageMessageV(stream, fmt, ap);
712 va_end(ap);
713 }
714
715 const char* program = "dalvikvm";
716 UsageMessage(stream, "%s: [options] class [argument ...]\n", program);
717 UsageMessage(stream, "\n");
718 UsageMessage(stream, "The following standard options are supported:\n");
719 UsageMessage(stream, " -classpath classpath (-cp classpath)\n");
720 UsageMessage(stream, " -Dproperty=value\n");
721 UsageMessage(stream, " -verbose:tag ('gc', 'jni', or 'class')\n");
722 UsageMessage(stream, " -showversion\n");
723 UsageMessage(stream, " -help\n");
724 UsageMessage(stream, " -agentlib:jdwp=options\n");
725 UsageMessage(stream, "\n");
726
727 UsageMessage(stream, "The following extended options are supported:\n");
728 UsageMessage(stream, " -Xrunjdwp:<options>\n");
729 UsageMessage(stream, " -Xbootclasspath:bootclasspath\n");
730 UsageMessage(stream, " -Xcheck:tag (e.g. 'jni')\n");
731 UsageMessage(stream, " -XmsN (min heap, must be multiple of 1K, >= 1MB)\n");
732 UsageMessage(stream, " -XmxN (max heap, must be multiple of 1K, >= 2MB)\n");
733 UsageMessage(stream, " -XssN (stack size)\n");
734 UsageMessage(stream, " -Xint\n");
735 UsageMessage(stream, "\n");
736
737 UsageMessage(stream, "The following Dalvik options are supported:\n");
738 UsageMessage(stream, " -Xzygote\n");
739 UsageMessage(stream, " -Xjnitrace:substring (eg NativeClass or nativeMethod)\n");
740 UsageMessage(stream, " -Xstacktracefile:<filename>\n");
741 UsageMessage(stream, " -Xgc:[no]preverify\n");
742 UsageMessage(stream, " -Xgc:[no]postverify\n");
743 UsageMessage(stream, " -XX:+DisableExplicitGC\n");
744 UsageMessage(stream, " -XX:HeapGrowthLimit=N\n");
745 UsageMessage(stream, " -XX:HeapMinFree=N\n");
746 UsageMessage(stream, " -XX:HeapMaxFree=N\n");
747 UsageMessage(stream, " -XX:HeapTargetUtilization=doublevalue\n");
Mathieu Chartier455820e2014-04-18 12:02:39 -0700748 UsageMessage(stream, " -XX:ForegroundHeapGrowthMultiplier=doublevalue\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800749 UsageMessage(stream, " -XX:LowMemoryMode\n");
750 UsageMessage(stream, " -Xprofile:{threadcpuclock,wallclock,dualclock}\n");
751 UsageMessage(stream, "\n");
752
753 UsageMessage(stream, "The following unique to ART options are supported:\n");
754 UsageMessage(stream, " -Xgc:[no]preverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700755 UsageMessage(stream, " -Xgc:[no]postsweepingverify_rosalloc\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800756 UsageMessage(stream, " -Xgc:[no]postverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700757 UsageMessage(stream, " -Xgc:[no]presweepingverify\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800758 UsageMessage(stream, " -Ximage:filename\n");
759 UsageMessage(stream, " -XX:ParallelGCThreads=integervalue\n");
760 UsageMessage(stream, " -XX:ConcGCThreads=integervalue\n");
761 UsageMessage(stream, " -XX:MaxSpinsBeforeThinLockInflation=integervalue\n");
762 UsageMessage(stream, " -XX:LongPauseLogThreshold=integervalue\n");
763 UsageMessage(stream, " -XX:LongGCLogThreshold=integervalue\n");
764 UsageMessage(stream, " -XX:DumpGCPerformanceOnShutdown\n");
765 UsageMessage(stream, " -XX:IgnoreMaxFootprint\n");
766 UsageMessage(stream, " -XX:UseTLAB\n");
767 UsageMessage(stream, " -XX:BackgroundGC=none\n");
768 UsageMessage(stream, " -Xmethod-trace\n");
769 UsageMessage(stream, " -Xmethod-trace-file:filename");
770 UsageMessage(stream, " -Xmethod-trace-file-size:integervalue\n");
771 UsageMessage(stream, " -Xprofile=filename\n");
772 UsageMessage(stream, " -Xprofile-period:integervalue\n");
773 UsageMessage(stream, " -Xprofile-duration:integervalue\n");
774 UsageMessage(stream, " -Xprofile-interval:integervalue\n");
775 UsageMessage(stream, " -Xprofile-backoff:integervalue\n");
776 UsageMessage(stream, " -Xcompiler-option dex2oat-option\n");
777 UsageMessage(stream, " -Ximage-compiler-option dex2oat-option\n");
778 UsageMessage(stream, "\n");
779
780 UsageMessage(stream, "The following previously supported Dalvik options are ignored:\n");
781 UsageMessage(stream, " -ea[:<package name>... |:<class name>]\n");
782 UsageMessage(stream, " -da[:<package name>... |:<class name>]\n");
783 UsageMessage(stream, " (-enableassertions, -disableassertions)\n");
784 UsageMessage(stream, " -esa\n");
785 UsageMessage(stream, " -dsa\n");
786 UsageMessage(stream, " (-enablesystemassertions, -disablesystemassertions)\n");
787 UsageMessage(stream, " -Xverify:{none,remote,all}\n");
788 UsageMessage(stream, " -Xrs\n");
789 UsageMessage(stream, " -Xint:portable, -Xint:fast, -Xint:jit\n");
790 UsageMessage(stream, " -Xdexopt:{none,verified,all,full}\n");
791 UsageMessage(stream, " -Xnoquithandler\n");
792 UsageMessage(stream, " -Xjniopts:{warnonly,forcecopy}\n");
793 UsageMessage(stream, " -Xjnigreflimit:integervalue\n");
794 UsageMessage(stream, " -Xgc:[no]precise\n");
795 UsageMessage(stream, " -Xgc:[no]verifycardtable\n");
796 UsageMessage(stream, " -X[no]genregmap\n");
797 UsageMessage(stream, " -Xverifyopt:[no]checkmon\n");
798 UsageMessage(stream, " -Xcheckdexsum\n");
799 UsageMessage(stream, " -Xincludeselectedop\n");
800 UsageMessage(stream, " -Xjitop:hexopvalue[-endvalue][,hexopvalue[-endvalue]]*\n");
801 UsageMessage(stream, " -Xincludeselectedmethod\n");
802 UsageMessage(stream, " -Xjitthreshold:integervalue\n");
803 UsageMessage(stream, " -Xjitcodecachesize:decimalvalueofkbytes\n");
804 UsageMessage(stream, " -Xjitblocking\n");
805 UsageMessage(stream, " -Xjitmethod:signature[,signature]* (eg Ljava/lang/String\\;replace)\n");
806 UsageMessage(stream, " -Xjitclass:classname[,classname]*\n");
807 UsageMessage(stream, " -Xjitoffset:offset[,offset]\n");
808 UsageMessage(stream, " -Xjitconfig:filename\n");
809 UsageMessage(stream, " -Xjitcheckcg\n");
810 UsageMessage(stream, " -Xjitverbose\n");
811 UsageMessage(stream, " -Xjitprofile\n");
812 UsageMessage(stream, " -Xjitdisableopt\n");
813 UsageMessage(stream, " -Xjitsuspendpoll\n");
814 UsageMessage(stream, " -XX:mainThreadStackSize=N\n");
815 UsageMessage(stream, "\n");
816
817 Exit((error) ? 1 : 0);
818}
819
820bool ParsedOptions::ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) {
821 std::string::size_type colon = s.find(c);
822 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700823 Usage("Missing char %c in option %s\n", c, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800824 return false;
825 }
826 // Add one to remove the char we were trimming until.
827 *parsed_value = s.substr(colon + 1);
828 return true;
829}
830
831bool ParsedOptions::ParseInteger(const std::string& s, char after_char, int* parsed_value) {
832 std::string::size_type colon = s.find(after_char);
833 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700834 Usage("Missing char %c in option %s\n", after_char, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800835 return false;
836 }
837 const char* begin = &s[colon + 1];
838 char* end;
839 size_t result = strtoul(begin, &end, 10);
840 if (begin == end || *end != '\0') {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700841 Usage("Failed to parse integer from %s\n", s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800842 return false;
843 }
844 *parsed_value = result;
845 return true;
846}
847
848bool ParsedOptions::ParseUnsignedInteger(const std::string& s, char after_char,
849 unsigned int* parsed_value) {
850 int i;
851 if (!ParseInteger(s, after_char, &i)) {
852 return false;
853 }
854 if (i < 0) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700855 Usage("Negative value %d passed for unsigned option %s\n", i, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800856 return false;
857 }
858 *parsed_value = i;
859 return true;
860}
861
862bool ParsedOptions::ParseDouble(const std::string& option, char after_char,
863 double min, double max, double* parsed_value) {
864 std::string substring;
865 if (!ParseStringAfterChar(option, after_char, &substring)) {
866 return false;
867 }
868 std::istringstream iss(substring);
869 double value;
870 iss >> value;
871 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
872 const bool sane_val = iss.eof() && (value >= min) && (value <= max);
873 if (!sane_val) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700874 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800875 return false;
876 }
877 *parsed_value = value;
878 return true;
879}
880
881} // namespace art