blob: c0dc94bf67f99662ffb42dc2b56e85aa55b7885d [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;
249
Dave Allisonb373e092014-02-20 16:06:36 -0800250 // Default to explicit checks. Switch off with -implicit-checks:.
251 // or setprop dalvik.vm.implicit_checks check1,check2,...
252#ifdef HAVE_ANDROID_OS
253 {
254 char buf[PROP_VALUE_MAX];
255 property_get("dalvik.vm.implicit_checks", buf, "none");
256 std::string checks(buf);
257 std::vector<std::string> checkvec;
258 Split(checks, ',', checkvec);
Dave Allisondd2e8252014-03-20 14:45:17 -0700259 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
260 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800261 for (auto& str : checkvec) {
262 std::string val = Trim(str);
263 if (val == "none") {
264 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
Dave Allisondd2e8252014-03-20 14:45:17 -0700265 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800266 } else if (val == "null") {
267 explicit_checks_ &= ~kExplicitNullCheck;
268 } else if (val == "suspend") {
269 explicit_checks_ &= ~kExplicitSuspendCheck;
270 } else if (val == "stack") {
271 explicit_checks_ &= ~kExplicitStackOverflowCheck;
272 } else if (val == "all") {
273 explicit_checks_ = 0;
274 }
275 }
276 }
277#else
278 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
279 kExplicitStackOverflowCheck;
280#endif
281
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800282 for (size_t i = 0; i < options.size(); ++i) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800283 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800284 LOG(INFO) << "option[" << i << "]=" << options[i].first;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800285 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800286 }
287 for (size_t i = 0; i < options.size(); ++i) {
288 const std::string option(options[i].first);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800289 if (StartsWith(option, "-help")) {
290 Usage(nullptr);
291 return false;
292 } else if (StartsWith(option, "-showversion")) {
293 UsageMessage(stdout, "ART version %s\n", Runtime::GetVersion());
294 Exit(0);
295 } else if (StartsWith(option, "-Xbootclasspath:")) {
296 boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
297 } else if (option == "-classpath" || option == "-cp") {
298 // TODO: support -Djava.class.path
299 i++;
300 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700301 Usage("Missing required class path value for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800302 return false;
303 }
304 const StringPiece& value = options[i].first;
305 class_path_string_ = value.data();
306 } else if (option == "bootclasspath") {
307 boot_class_path_
308 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
309 } else if (StartsWith(option, "-Ximage:")) {
310 if (!ParseStringAfterChar(option, ':', &image_)) {
311 return false;
312 }
313 } else if (StartsWith(option, "-Xcheck:jni")) {
314 check_jni_ = true;
315 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
316 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
317 // TODO: move parsing logic out of Dbg
318 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
319 if (tail != "help") {
320 UsageMessage(stderr, "Failed to parse JDWP option %s\n", tail.c_str());
321 }
322 Usage("Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
323 "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n");
324 return false;
325 }
326 } else if (StartsWith(option, "-Xms")) {
327 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
328 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700329 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800330 return false;
331 }
332 heap_initial_size_ = size;
333 } else if (StartsWith(option, "-Xmx")) {
334 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
335 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700336 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800337 return false;
338 }
339 heap_maximum_size_ = size;
340 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
341 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
342 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700343 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800344 return false;
345 }
346 heap_growth_limit_ = size;
347 } else if (StartsWith(option, "-XX:HeapMinFree=")) {
348 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMinFree=")).c_str(), 1024);
349 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700350 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800351 return false;
352 }
353 heap_min_free_ = size;
354 } else if (StartsWith(option, "-XX:HeapMaxFree=")) {
355 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMaxFree=")).c_str(), 1024);
356 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700357 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800358 return false;
359 }
360 heap_max_free_ = size;
361 } else if (StartsWith(option, "-XX:HeapTargetUtilization=")) {
362 if (!ParseDouble(option, '=', 0.1, 0.9, &heap_target_utilization_)) {
363 return false;
364 }
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700365 } else if (StartsWith(option, "-XX:ForegroundHeapGrowthMultiplier=")) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700366 if (!ParseDouble(option, '=', 0.1, 10.0, &foreground_heap_growth_multiplier_)) {
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700367 return false;
368 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800369 } else if (StartsWith(option, "-XX:ParallelGCThreads=")) {
370 if (!ParseUnsignedInteger(option, '=', &parallel_gc_threads_)) {
371 return false;
372 }
373 } else if (StartsWith(option, "-XX:ConcGCThreads=")) {
374 if (!ParseUnsignedInteger(option, '=', &conc_gc_threads_)) {
375 return false;
376 }
377 } else if (StartsWith(option, "-Xss")) {
378 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
379 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700380 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800381 return false;
382 }
383 stack_size_ = size;
384 } else if (StartsWith(option, "-XX:MaxSpinsBeforeThinLockInflation=")) {
385 if (!ParseUnsignedInteger(option, '=', &max_spins_before_thin_lock_inflation_)) {
386 return false;
387 }
388 } else if (StartsWith(option, "-XX:LongPauseLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800389 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800390 if (!ParseUnsignedInteger(option, '=', &value)) {
391 return false;
392 }
393 long_pause_log_threshold_ = MsToNs(value);
394 } else if (StartsWith(option, "-XX:LongGCLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800395 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800396 if (!ParseUnsignedInteger(option, '=', &value)) {
397 return false;
398 }
399 long_gc_log_threshold_ = MsToNs(value);
400 } else if (option == "-XX:DumpGCPerformanceOnShutdown") {
401 dump_gc_performance_on_shutdown_ = true;
402 } else if (option == "-XX:IgnoreMaxFootprint") {
403 ignore_max_footprint_ = true;
404 } else if (option == "-XX:LowMemoryMode") {
405 low_memory_mode_ = true;
406 } else if (option == "-XX:UseTLAB") {
407 use_tlab_ = true;
408 } else if (StartsWith(option, "-D")) {
409 properties_.push_back(option.substr(strlen("-D")));
410 } else if (StartsWith(option, "-Xjnitrace:")) {
411 jni_trace_ = option.substr(strlen("-Xjnitrace:"));
412 } else if (option == "compilercallbacks") {
413 compiler_callbacks_ =
414 reinterpret_cast<CompilerCallbacks*>(const_cast<void*>(options[i].second));
415 } else if (option == "-Xzygote") {
416 is_zygote_ = true;
417 } else if (option == "-Xint") {
418 interpreter_only_ = true;
419 } else if (StartsWith(option, "-Xgc:")) {
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700420 if (!ParseXGcOption(option)) {
421 return false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800422 }
423 } else if (StartsWith(option, "-XX:BackgroundGC=")) {
424 std::string substring;
425 if (!ParseStringAfterChar(option, '=', &substring)) {
426 return false;
427 }
428 gc::CollectorType collector_type = ParseCollectorType(substring);
429 if (collector_type != gc::kCollectorTypeNone) {
430 background_collector_type_ = collector_type;
431 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700432 Usage("Unknown -XX:BackgroundGC option %s\n", substring.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800433 return false;
434 }
435 } else if (option == "-XX:+DisableExplicitGC") {
436 is_explicit_gc_disabled_ = true;
437 } else if (StartsWith(option, "-verbose:")) {
438 std::vector<std::string> verbose_options;
439 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
440 for (size_t i = 0; i < verbose_options.size(); ++i) {
441 if (verbose_options[i] == "class") {
442 gLogVerbosity.class_linker = true;
443 } else if (verbose_options[i] == "verifier") {
444 gLogVerbosity.verifier = true;
445 } else if (verbose_options[i] == "compiler") {
446 gLogVerbosity.compiler = true;
447 } else if (verbose_options[i] == "heap") {
448 gLogVerbosity.heap = true;
449 } else if (verbose_options[i] == "gc") {
450 gLogVerbosity.gc = true;
451 } else if (verbose_options[i] == "jdwp") {
452 gLogVerbosity.jdwp = true;
453 } else if (verbose_options[i] == "jni") {
454 gLogVerbosity.jni = true;
455 } else if (verbose_options[i] == "monitor") {
456 gLogVerbosity.monitor = true;
457 } else if (verbose_options[i] == "startup") {
458 gLogVerbosity.startup = true;
459 } else if (verbose_options[i] == "third-party-jni") {
460 gLogVerbosity.third_party_jni = true;
461 } else if (verbose_options[i] == "threads") {
462 gLogVerbosity.threads = true;
463 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700464 Usage("Unknown -verbose option %s\n", verbose_options[i].c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800465 return false;
466 }
467 }
Mingyao Yang42d65c52014-04-18 16:49:39 -0700468 } else if (StartsWith(option, "-verbose-methods:")) {
469 gLogVerbosity.compiler = false;
470 Split(option.substr(strlen("-verbose-methods:")), ',', gVerboseMethods);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800471 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
472 if (!ParseUnsignedInteger(option, ':', &lock_profiling_threshold_)) {
473 return false;
474 }
475 } else if (StartsWith(option, "-Xstacktracefile:")) {
476 if (!ParseStringAfterChar(option, ':', &stack_trace_file_)) {
477 return false;
478 }
479 } else if (option == "sensitiveThread") {
480 const void* hook = options[i].second;
481 hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(hook));
482 } else if (option == "vfprintf") {
483 const void* hook = options[i].second;
484 if (hook == nullptr) {
485 Usage("vfprintf argument was NULL");
486 return false;
487 }
488 hook_vfprintf_ =
489 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(hook));
490 } else if (option == "exit") {
491 const void* hook = options[i].second;
492 if (hook == nullptr) {
493 Usage("exit argument was NULL");
494 return false;
495 }
496 hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(hook));
497 } else if (option == "abort") {
498 const void* hook = options[i].second;
499 if (hook == nullptr) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700500 Usage("abort was NULL\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800501 return false;
502 }
503 hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(hook));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800504 } else if (option == "-Xmethod-trace") {
505 method_trace_ = true;
506 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
507 method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
508 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
509 if (!ParseUnsignedInteger(option, ':', &method_trace_file_size_)) {
510 return false;
511 }
512 } else if (option == "-Xprofile:threadcpuclock") {
513 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
514 } else if (option == "-Xprofile:wallclock") {
515 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
516 } else if (option == "-Xprofile:dualclock") {
517 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
518 } else if (StartsWith(option, "-Xprofile:")) {
519 if (!ParseStringAfterChar(option, ';', &profile_output_filename_)) {
520 return false;
521 }
522 profile_ = true;
523 } else if (StartsWith(option, "-Xprofile-period:")) {
524 if (!ParseUnsignedInteger(option, ':', &profile_period_s_)) {
525 return false;
526 }
527 } else if (StartsWith(option, "-Xprofile-duration:")) {
528 if (!ParseUnsignedInteger(option, ':', &profile_duration_s_)) {
529 return false;
530 }
531 } else if (StartsWith(option, "-Xprofile-interval:")) {
532 if (!ParseUnsignedInteger(option, ':', &profile_interval_us_)) {
533 return false;
534 }
535 } else if (StartsWith(option, "-Xprofile-backoff:")) {
536 if (!ParseDouble(option, ':', 1.0, 10.0, &profile_backoff_coefficient_)) {
537 return false;
538 }
Calin Juravle16590062014-04-07 18:07:43 +0300539 } else if (option == "-Xprofile-start-lazy") {
540 profile_start_immediately_ = false;
Dave Allisonb373e092014-02-20 16:06:36 -0800541 } else if (StartsWith(option, "-implicit-checks:")) {
542 std::string checks;
543 if (!ParseStringAfterChar(option, ':', &checks)) {
544 return false;
545 }
546 std::vector<std::string> checkvec;
547 Split(checks, ',', checkvec);
548 for (auto& str : checkvec) {
549 std::string val = Trim(str);
550 if (val == "none") {
551 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
552 kExplicitStackOverflowCheck;
553 } else if (val == "null") {
554 explicit_checks_ &= ~kExplicitNullCheck;
555 } else if (val == "suspend") {
556 explicit_checks_ &= ~kExplicitSuspendCheck;
557 } else if (val == "stack") {
558 explicit_checks_ &= ~kExplicitStackOverflowCheck;
559 } else if (val == "all") {
560 explicit_checks_ = 0;
561 } else {
562 return false;
563 }
564 }
565 } else if (StartsWith(option, "-explicit-checks:")) {
566 std::string checks;
567 if (!ParseStringAfterChar(option, ':', &checks)) {
568 return false;
569 }
570 std::vector<std::string> checkvec;
571 Split(checks, ',', checkvec);
572 for (auto& str : checkvec) {
573 std::string val = Trim(str);
574 if (val == "none") {
575 explicit_checks_ = 0;
576 } else if (val == "null") {
577 explicit_checks_ |= kExplicitNullCheck;
578 } else if (val == "suspend") {
579 explicit_checks_ |= kExplicitSuspendCheck;
580 } else if (val == "stack") {
581 explicit_checks_ |= kExplicitStackOverflowCheck;
582 } else if (val == "all") {
583 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
584 kExplicitStackOverflowCheck;
585 } else {
586 return false;
587 }
588 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800589 } else if (option == "-Xcompiler-option") {
590 i++;
591 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700592 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800593 return false;
594 }
595 compiler_options_.push_back(options[i].first);
596 } else if (option == "-Ximage-compiler-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 image_compiler_options_.push_back(options[i].first);
Jeff Hao4a200f52014-04-01 14:58:49 -0700603 } else if (StartsWith(option, "-Xverify:")) {
604 std::string verify_mode = option.substr(strlen("-Xverify:"));
605 if (verify_mode == "none") {
606 verify_ = false;
607 } else if (verify_mode == "remote" || verify_mode == "all") {
608 verify_ = true;
609 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700610 Usage("Unknown -Xverify option %s\n", verify_mode.c_str());
Jeff Hao4a200f52014-04-01 14:58:49 -0700611 return false;
612 }
Yevgeny Roubana6119a22014-03-24 11:31:24 +0700613 } else if (StartsWith(option, "-ea") ||
614 StartsWith(option, "-da") ||
615 StartsWith(option, "-enableassertions") ||
616 StartsWith(option, "-disableassertions") ||
Dave Allisonb373e092014-02-20 16:06:36 -0800617 (option == "--runtime-arg") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800618 (option == "-esa") ||
619 (option == "-dsa") ||
620 (option == "-enablesystemassertions") ||
621 (option == "-disablesystemassertions") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800622 (option == "-Xrs") ||
623 StartsWith(option, "-Xint:") ||
624 StartsWith(option, "-Xdexopt:") ||
625 (option == "-Xnoquithandler") ||
626 StartsWith(option, "-Xjniopts:") ||
627 StartsWith(option, "-Xjnigreflimit:") ||
628 (option == "-Xgenregmap") ||
629 (option == "-Xnogenregmap") ||
630 StartsWith(option, "-Xverifyopt:") ||
631 (option == "-Xcheckdexsum") ||
632 (option == "-Xincludeselectedop") ||
633 StartsWith(option, "-Xjitop:") ||
634 (option == "-Xincludeselectedmethod") ||
635 StartsWith(option, "-Xjitthreshold:") ||
636 StartsWith(option, "-Xjitcodecachesize:") ||
637 (option == "-Xjitblocking") ||
638 StartsWith(option, "-Xjitmethod:") ||
639 StartsWith(option, "-Xjitclass:") ||
640 StartsWith(option, "-Xjitoffset:") ||
641 StartsWith(option, "-Xjitconfig:") ||
642 (option == "-Xjitcheckcg") ||
643 (option == "-Xjitverbose") ||
644 (option == "-Xjitprofile") ||
645 (option == "-Xjitdisableopt") ||
646 (option == "-Xjitsuspendpoll") ||
647 StartsWith(option, "-XX:mainThreadStackSize=")) {
648 // Ignored for backwards compatibility.
649 } else if (!ignore_unrecognized) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700650 Usage("Unrecognized option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800651 return false;
652 }
653 }
654
655 // If a reference to the dalvik core.jar snuck in, replace it with
656 // the art specific version. This can happen with on device
657 // boot.art/boot.oat generation by GenerateImage which relies on the
658 // value of BOOTCLASSPATH.
659 std::string core_jar("/core.jar");
660 size_t core_jar_pos = boot_class_path_string_.find(core_jar);
661 if (core_jar_pos != std::string::npos) {
662 boot_class_path_string_.replace(core_jar_pos, core_jar.size(), "/core-libart.jar");
663 }
664
665 if (compiler_callbacks_ == nullptr && image_.empty()) {
666 image_ += GetAndroidRoot();
667 image_ += "/framework/boot.art";
668 }
669 if (heap_growth_limit_ == 0) {
670 heap_growth_limit_ = heap_maximum_size_;
671 }
672 if (background_collector_type_ == gc::kCollectorTypeNone) {
673 background_collector_type_ = collector_type_;
674 }
675 return true;
676}
677
678void ParsedOptions::Exit(int status) {
679 hook_exit_(status);
680}
681
682void ParsedOptions::Abort() {
683 hook_abort_();
684}
685
686void ParsedOptions::UsageMessageV(FILE* stream, const char* fmt, va_list ap) {
687 hook_vfprintf_(stderr, fmt, ap);
688}
689
690void ParsedOptions::UsageMessage(FILE* stream, const char* fmt, ...) {
691 va_list ap;
692 va_start(ap, fmt);
693 UsageMessageV(stream, fmt, ap);
694 va_end(ap);
695}
696
697void ParsedOptions::Usage(const char* fmt, ...) {
698 bool error = (fmt != nullptr);
699 FILE* stream = error ? stderr : stdout;
700
701 if (fmt != nullptr) {
702 va_list ap;
703 va_start(ap, fmt);
704 UsageMessageV(stream, fmt, ap);
705 va_end(ap);
706 }
707
708 const char* program = "dalvikvm";
709 UsageMessage(stream, "%s: [options] class [argument ...]\n", program);
710 UsageMessage(stream, "\n");
711 UsageMessage(stream, "The following standard options are supported:\n");
712 UsageMessage(stream, " -classpath classpath (-cp classpath)\n");
713 UsageMessage(stream, " -Dproperty=value\n");
714 UsageMessage(stream, " -verbose:tag ('gc', 'jni', or 'class')\n");
715 UsageMessage(stream, " -showversion\n");
716 UsageMessage(stream, " -help\n");
717 UsageMessage(stream, " -agentlib:jdwp=options\n");
718 UsageMessage(stream, "\n");
719
720 UsageMessage(stream, "The following extended options are supported:\n");
721 UsageMessage(stream, " -Xrunjdwp:<options>\n");
722 UsageMessage(stream, " -Xbootclasspath:bootclasspath\n");
723 UsageMessage(stream, " -Xcheck:tag (e.g. 'jni')\n");
724 UsageMessage(stream, " -XmsN (min heap, must be multiple of 1K, >= 1MB)\n");
725 UsageMessage(stream, " -XmxN (max heap, must be multiple of 1K, >= 2MB)\n");
726 UsageMessage(stream, " -XssN (stack size)\n");
727 UsageMessage(stream, " -Xint\n");
728 UsageMessage(stream, "\n");
729
730 UsageMessage(stream, "The following Dalvik options are supported:\n");
731 UsageMessage(stream, " -Xzygote\n");
732 UsageMessage(stream, " -Xjnitrace:substring (eg NativeClass or nativeMethod)\n");
733 UsageMessage(stream, " -Xstacktracefile:<filename>\n");
734 UsageMessage(stream, " -Xgc:[no]preverify\n");
735 UsageMessage(stream, " -Xgc:[no]postverify\n");
736 UsageMessage(stream, " -XX:+DisableExplicitGC\n");
737 UsageMessage(stream, " -XX:HeapGrowthLimit=N\n");
738 UsageMessage(stream, " -XX:HeapMinFree=N\n");
739 UsageMessage(stream, " -XX:HeapMaxFree=N\n");
740 UsageMessage(stream, " -XX:HeapTargetUtilization=doublevalue\n");
Mathieu Chartier455820e2014-04-18 12:02:39 -0700741 UsageMessage(stream, " -XX:ForegroundHeapGrowthMultiplier=doublevalue\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800742 UsageMessage(stream, " -XX:LowMemoryMode\n");
743 UsageMessage(stream, " -Xprofile:{threadcpuclock,wallclock,dualclock}\n");
744 UsageMessage(stream, "\n");
745
746 UsageMessage(stream, "The following unique to ART options are supported:\n");
747 UsageMessage(stream, " -Xgc:[no]preverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700748 UsageMessage(stream, " -Xgc:[no]postsweepingverify_rosalloc\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800749 UsageMessage(stream, " -Xgc:[no]postverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700750 UsageMessage(stream, " -Xgc:[no]presweepingverify\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800751 UsageMessage(stream, " -Ximage:filename\n");
752 UsageMessage(stream, " -XX:ParallelGCThreads=integervalue\n");
753 UsageMessage(stream, " -XX:ConcGCThreads=integervalue\n");
754 UsageMessage(stream, " -XX:MaxSpinsBeforeThinLockInflation=integervalue\n");
755 UsageMessage(stream, " -XX:LongPauseLogThreshold=integervalue\n");
756 UsageMessage(stream, " -XX:LongGCLogThreshold=integervalue\n");
757 UsageMessage(stream, " -XX:DumpGCPerformanceOnShutdown\n");
758 UsageMessage(stream, " -XX:IgnoreMaxFootprint\n");
759 UsageMessage(stream, " -XX:UseTLAB\n");
760 UsageMessage(stream, " -XX:BackgroundGC=none\n");
761 UsageMessage(stream, " -Xmethod-trace\n");
762 UsageMessage(stream, " -Xmethod-trace-file:filename");
763 UsageMessage(stream, " -Xmethod-trace-file-size:integervalue\n");
764 UsageMessage(stream, " -Xprofile=filename\n");
765 UsageMessage(stream, " -Xprofile-period:integervalue\n");
766 UsageMessage(stream, " -Xprofile-duration:integervalue\n");
767 UsageMessage(stream, " -Xprofile-interval:integervalue\n");
768 UsageMessage(stream, " -Xprofile-backoff:integervalue\n");
769 UsageMessage(stream, " -Xcompiler-option dex2oat-option\n");
770 UsageMessage(stream, " -Ximage-compiler-option dex2oat-option\n");
771 UsageMessage(stream, "\n");
772
773 UsageMessage(stream, "The following previously supported Dalvik options are ignored:\n");
774 UsageMessage(stream, " -ea[:<package name>... |:<class name>]\n");
775 UsageMessage(stream, " -da[:<package name>... |:<class name>]\n");
776 UsageMessage(stream, " (-enableassertions, -disableassertions)\n");
777 UsageMessage(stream, " -esa\n");
778 UsageMessage(stream, " -dsa\n");
779 UsageMessage(stream, " (-enablesystemassertions, -disablesystemassertions)\n");
780 UsageMessage(stream, " -Xverify:{none,remote,all}\n");
781 UsageMessage(stream, " -Xrs\n");
782 UsageMessage(stream, " -Xint:portable, -Xint:fast, -Xint:jit\n");
783 UsageMessage(stream, " -Xdexopt:{none,verified,all,full}\n");
784 UsageMessage(stream, " -Xnoquithandler\n");
785 UsageMessage(stream, " -Xjniopts:{warnonly,forcecopy}\n");
786 UsageMessage(stream, " -Xjnigreflimit:integervalue\n");
787 UsageMessage(stream, " -Xgc:[no]precise\n");
788 UsageMessage(stream, " -Xgc:[no]verifycardtable\n");
789 UsageMessage(stream, " -X[no]genregmap\n");
790 UsageMessage(stream, " -Xverifyopt:[no]checkmon\n");
791 UsageMessage(stream, " -Xcheckdexsum\n");
792 UsageMessage(stream, " -Xincludeselectedop\n");
793 UsageMessage(stream, " -Xjitop:hexopvalue[-endvalue][,hexopvalue[-endvalue]]*\n");
794 UsageMessage(stream, " -Xincludeselectedmethod\n");
795 UsageMessage(stream, " -Xjitthreshold:integervalue\n");
796 UsageMessage(stream, " -Xjitcodecachesize:decimalvalueofkbytes\n");
797 UsageMessage(stream, " -Xjitblocking\n");
798 UsageMessage(stream, " -Xjitmethod:signature[,signature]* (eg Ljava/lang/String\\;replace)\n");
799 UsageMessage(stream, " -Xjitclass:classname[,classname]*\n");
800 UsageMessage(stream, " -Xjitoffset:offset[,offset]\n");
801 UsageMessage(stream, " -Xjitconfig:filename\n");
802 UsageMessage(stream, " -Xjitcheckcg\n");
803 UsageMessage(stream, " -Xjitverbose\n");
804 UsageMessage(stream, " -Xjitprofile\n");
805 UsageMessage(stream, " -Xjitdisableopt\n");
806 UsageMessage(stream, " -Xjitsuspendpoll\n");
807 UsageMessage(stream, " -XX:mainThreadStackSize=N\n");
808 UsageMessage(stream, "\n");
809
810 Exit((error) ? 1 : 0);
811}
812
813bool ParsedOptions::ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) {
814 std::string::size_type colon = s.find(c);
815 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700816 Usage("Missing char %c in option %s\n", c, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800817 return false;
818 }
819 // Add one to remove the char we were trimming until.
820 *parsed_value = s.substr(colon + 1);
821 return true;
822}
823
824bool ParsedOptions::ParseInteger(const std::string& s, char after_char, int* parsed_value) {
825 std::string::size_type colon = s.find(after_char);
826 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700827 Usage("Missing char %c in option %s\n", after_char, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800828 return false;
829 }
830 const char* begin = &s[colon + 1];
831 char* end;
832 size_t result = strtoul(begin, &end, 10);
833 if (begin == end || *end != '\0') {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700834 Usage("Failed to parse integer from %s\n", s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800835 return false;
836 }
837 *parsed_value = result;
838 return true;
839}
840
841bool ParsedOptions::ParseUnsignedInteger(const std::string& s, char after_char,
842 unsigned int* parsed_value) {
843 int i;
844 if (!ParseInteger(s, after_char, &i)) {
845 return false;
846 }
847 if (i < 0) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700848 Usage("Negative value %d passed for unsigned option %s\n", i, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800849 return false;
850 }
851 *parsed_value = i;
852 return true;
853}
854
855bool ParsedOptions::ParseDouble(const std::string& option, char after_char,
856 double min, double max, double* parsed_value) {
857 std::string substring;
858 if (!ParseStringAfterChar(option, after_char, &substring)) {
859 return false;
860 }
861 std::istringstream iss(substring);
862 double value;
863 iss >> value;
864 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
865 const bool sane_val = iss.eof() && (value >= min) && (value <= max);
866 if (!sane_val) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700867 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800868 return false;
869 }
870 *parsed_value = value;
871 return true;
872}
873
874} // namespace art