blob: e11435d9495df3c5b8404da73efe2b73b4eb7d3b [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
117bool ParsedOptions::Parse(const Runtime::Options& options, bool ignore_unrecognized) {
118 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
119 if (boot_class_path_string != NULL) {
120 boot_class_path_string_ = boot_class_path_string;
121 }
122 const char* class_path_string = getenv("CLASSPATH");
123 if (class_path_string != NULL) {
124 class_path_string_ = class_path_string;
125 }
126 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
127 check_jni_ = kIsDebugBuild;
128
129 heap_initial_size_ = gc::Heap::kDefaultInitialSize;
130 heap_maximum_size_ = gc::Heap::kDefaultMaximumSize;
131 heap_min_free_ = gc::Heap::kDefaultMinFree;
132 heap_max_free_ = gc::Heap::kDefaultMaxFree;
133 heap_target_utilization_ = gc::Heap::kDefaultTargetUtilization;
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700134 foreground_heap_growth_multiplier_ = gc::Heap::kDefaultHeapGrowthMultiplier;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800135 heap_growth_limit_ = 0; // 0 means no growth limit .
136 // Default to number of processors minus one since the main GC thread also does work.
137 parallel_gc_threads_ = sysconf(_SC_NPROCESSORS_CONF) - 1;
138 // Only the main GC thread, no workers.
139 conc_gc_threads_ = 0;
140 // Default is CMS which is Sticky + Partial + Full CMS GC.
141 collector_type_ = gc::kCollectorTypeCMS;
142 // If background_collector_type_ is kCollectorTypeNone, it defaults to the collector_type_ after
143 // parsing options.
144 background_collector_type_ = gc::kCollectorTypeNone;
145 stack_size_ = 0; // 0 means default.
146 max_spins_before_thin_lock_inflation_ = Monitor::kDefaultMaxSpinsBeforeThinLockInflation;
147 low_memory_mode_ = false;
148 use_tlab_ = false;
149 verify_pre_gc_heap_ = false;
150 verify_post_gc_heap_ = kIsDebugBuild;
151 verify_pre_gc_rosalloc_ = kIsDebugBuild;
152 verify_post_gc_rosalloc_ = false;
153
154 compiler_callbacks_ = nullptr;
155 is_zygote_ = false;
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -0800156 if (kPoisonHeapReferences) {
157 // kPoisonHeapReferences currently works only with the interpreter only.
158 // TODO: make it work with the compiler.
159 interpreter_only_ = true;
160 } else {
161 interpreter_only_ = false;
162 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800163 is_explicit_gc_disabled_ = false;
164
165 long_pause_log_threshold_ = gc::Heap::kDefaultLongPauseLogThreshold;
166 long_gc_log_threshold_ = gc::Heap::kDefaultLongGCLogThreshold;
167 dump_gc_performance_on_shutdown_ = false;
168 ignore_max_footprint_ = false;
169
170 lock_profiling_threshold_ = 0;
171 hook_is_sensitive_thread_ = NULL;
172
173 hook_vfprintf_ = vfprintf;
174 hook_exit_ = exit;
175 hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
176
177// gLogVerbosity.class_linker = true; // TODO: don't check this in!
178// gLogVerbosity.compiler = true; // TODO: don't check this in!
179// gLogVerbosity.verifier = true; // TODO: don't check this in!
180// gLogVerbosity.heap = true; // TODO: don't check this in!
181// gLogVerbosity.gc = true; // TODO: don't check this in!
182// gLogVerbosity.jdwp = true; // TODO: don't check this in!
183// gLogVerbosity.jni = true; // TODO: don't check this in!
184// gLogVerbosity.monitor = true; // TODO: don't check this in!
185// gLogVerbosity.startup = true; // TODO: don't check this in!
186// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
187// gLogVerbosity.threads = true; // TODO: don't check this in!
188
189 method_trace_ = false;
190 method_trace_file_ = "/data/method-trace-file.bin";
191 method_trace_file_size_ = 10 * MB;
192
193 profile_ = false;
194 profile_period_s_ = 10; // Seconds.
195 profile_duration_s_ = 20; // Seconds.
196 profile_interval_us_ = 500; // Microseconds.
197 profile_backoff_coefficient_ = 2.0;
Calin Juravle16590062014-04-07 18:07:43 +0300198 profile_start_immediately_ = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800199 profile_clock_source_ = kDefaultProfilerClockSource;
200
Jeff Hao4a200f52014-04-01 14:58:49 -0700201 verify_ = true;
202
Dave Allisonb373e092014-02-20 16:06:36 -0800203 // Default to explicit checks. Switch off with -implicit-checks:.
204 // or setprop dalvik.vm.implicit_checks check1,check2,...
205#ifdef HAVE_ANDROID_OS
206 {
207 char buf[PROP_VALUE_MAX];
208 property_get("dalvik.vm.implicit_checks", buf, "none");
209 std::string checks(buf);
210 std::vector<std::string> checkvec;
211 Split(checks, ',', checkvec);
Dave Allisondd2e8252014-03-20 14:45:17 -0700212 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
213 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800214 for (auto& str : checkvec) {
215 std::string val = Trim(str);
216 if (val == "none") {
217 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
Dave Allisondd2e8252014-03-20 14:45:17 -0700218 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800219 } else if (val == "null") {
220 explicit_checks_ &= ~kExplicitNullCheck;
221 } else if (val == "suspend") {
222 explicit_checks_ &= ~kExplicitSuspendCheck;
223 } else if (val == "stack") {
224 explicit_checks_ &= ~kExplicitStackOverflowCheck;
225 } else if (val == "all") {
226 explicit_checks_ = 0;
227 }
228 }
229 }
230#else
231 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
232 kExplicitStackOverflowCheck;
233#endif
234
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800235 for (size_t i = 0; i < options.size(); ++i) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800236 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800237 LOG(INFO) << "option[" << i << "]=" << options[i].first;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800238 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800239 }
240 for (size_t i = 0; i < options.size(); ++i) {
241 const std::string option(options[i].first);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800242 if (StartsWith(option, "-help")) {
243 Usage(nullptr);
244 return false;
245 } else if (StartsWith(option, "-showversion")) {
246 UsageMessage(stdout, "ART version %s\n", Runtime::GetVersion());
247 Exit(0);
248 } else if (StartsWith(option, "-Xbootclasspath:")) {
249 boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
250 } else if (option == "-classpath" || option == "-cp") {
251 // TODO: support -Djava.class.path
252 i++;
253 if (i == options.size()) {
254 Usage("Missing required class path value for %s", option.c_str());
255 return false;
256 }
257 const StringPiece& value = options[i].first;
258 class_path_string_ = value.data();
259 } else if (option == "bootclasspath") {
260 boot_class_path_
261 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
262 } else if (StartsWith(option, "-Ximage:")) {
263 if (!ParseStringAfterChar(option, ':', &image_)) {
264 return false;
265 }
266 } else if (StartsWith(option, "-Xcheck:jni")) {
267 check_jni_ = true;
268 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
269 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
270 // TODO: move parsing logic out of Dbg
271 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
272 if (tail != "help") {
273 UsageMessage(stderr, "Failed to parse JDWP option %s\n", tail.c_str());
274 }
275 Usage("Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
276 "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n");
277 return false;
278 }
279 } else if (StartsWith(option, "-Xms")) {
280 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
281 if (size == 0) {
282 Usage("Failed to parse memory option %s", option.c_str());
283 return false;
284 }
285 heap_initial_size_ = size;
286 } else if (StartsWith(option, "-Xmx")) {
287 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
288 if (size == 0) {
289 Usage("Failed to parse memory option %s", option.c_str());
290 return false;
291 }
292 heap_maximum_size_ = size;
293 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
294 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
295 if (size == 0) {
296 Usage("Failed to parse memory option %s", option.c_str());
297 return false;
298 }
299 heap_growth_limit_ = size;
300 } else if (StartsWith(option, "-XX:HeapMinFree=")) {
301 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMinFree=")).c_str(), 1024);
302 if (size == 0) {
303 Usage("Failed to parse memory option %s", option.c_str());
304 return false;
305 }
306 heap_min_free_ = size;
307 } else if (StartsWith(option, "-XX:HeapMaxFree=")) {
308 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMaxFree=")).c_str(), 1024);
309 if (size == 0) {
310 Usage("Failed to parse memory option %s", option.c_str());
311 return false;
312 }
313 heap_max_free_ = size;
314 } else if (StartsWith(option, "-XX:HeapTargetUtilization=")) {
315 if (!ParseDouble(option, '=', 0.1, 0.9, &heap_target_utilization_)) {
316 return false;
317 }
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700318 } else if (StartsWith(option, "-XX:ForegroundHeapGrowthMultiplier=")) {
319 if (!ParseDouble(option, '=', 0.1, 0.9, &foreground_heap_growth_multiplier_)) {
320 return false;
321 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800322 } else if (StartsWith(option, "-XX:ParallelGCThreads=")) {
323 if (!ParseUnsignedInteger(option, '=', &parallel_gc_threads_)) {
324 return false;
325 }
326 } else if (StartsWith(option, "-XX:ConcGCThreads=")) {
327 if (!ParseUnsignedInteger(option, '=', &conc_gc_threads_)) {
328 return false;
329 }
330 } else if (StartsWith(option, "-Xss")) {
331 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
332 if (size == 0) {
333 Usage("Failed to parse memory option %s", option.c_str());
334 return false;
335 }
336 stack_size_ = size;
337 } else if (StartsWith(option, "-XX:MaxSpinsBeforeThinLockInflation=")) {
338 if (!ParseUnsignedInteger(option, '=', &max_spins_before_thin_lock_inflation_)) {
339 return false;
340 }
341 } else if (StartsWith(option, "-XX:LongPauseLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800342 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800343 if (!ParseUnsignedInteger(option, '=', &value)) {
344 return false;
345 }
346 long_pause_log_threshold_ = MsToNs(value);
347 } else if (StartsWith(option, "-XX:LongGCLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800348 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800349 if (!ParseUnsignedInteger(option, '=', &value)) {
350 return false;
351 }
352 long_gc_log_threshold_ = MsToNs(value);
353 } else if (option == "-XX:DumpGCPerformanceOnShutdown") {
354 dump_gc_performance_on_shutdown_ = true;
355 } else if (option == "-XX:IgnoreMaxFootprint") {
356 ignore_max_footprint_ = true;
357 } else if (option == "-XX:LowMemoryMode") {
358 low_memory_mode_ = true;
359 } else if (option == "-XX:UseTLAB") {
360 use_tlab_ = true;
361 } else if (StartsWith(option, "-D")) {
362 properties_.push_back(option.substr(strlen("-D")));
363 } else if (StartsWith(option, "-Xjnitrace:")) {
364 jni_trace_ = option.substr(strlen("-Xjnitrace:"));
365 } else if (option == "compilercallbacks") {
366 compiler_callbacks_ =
367 reinterpret_cast<CompilerCallbacks*>(const_cast<void*>(options[i].second));
368 } else if (option == "-Xzygote") {
369 is_zygote_ = true;
370 } else if (option == "-Xint") {
371 interpreter_only_ = true;
372 } else if (StartsWith(option, "-Xgc:")) {
373 std::vector<std::string> gc_options;
374 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
375 for (const std::string& gc_option : gc_options) {
376 gc::CollectorType collector_type = ParseCollectorType(gc_option);
377 if (collector_type != gc::kCollectorTypeNone) {
378 collector_type_ = collector_type;
379 } else if (gc_option == "preverify") {
380 verify_pre_gc_heap_ = true;
381 } else if (gc_option == "nopreverify") {
382 verify_pre_gc_heap_ = false;
383 } else if (gc_option == "postverify") {
384 verify_post_gc_heap_ = true;
385 } else if (gc_option == "nopostverify") {
386 verify_post_gc_heap_ = false;
387 } else if (gc_option == "preverify_rosalloc") {
388 verify_pre_gc_rosalloc_ = true;
389 } else if (gc_option == "nopreverify_rosalloc") {
390 verify_pre_gc_rosalloc_ = false;
391 } else if (gc_option == "postverify_rosalloc") {
392 verify_post_gc_rosalloc_ = true;
393 } else if (gc_option == "nopostverify_rosalloc") {
394 verify_post_gc_rosalloc_ = false;
395 } else if ((gc_option == "precise") ||
396 (gc_option == "noprecise") ||
397 (gc_option == "verifycardtable") ||
398 (gc_option == "noverifycardtable")) {
399 // Ignored for backwards compatibility.
400 } else {
401 Usage("Unknown -Xgc option %s", gc_option.c_str());
402 return false;
403 }
404 }
405 } else if (StartsWith(option, "-XX:BackgroundGC=")) {
406 std::string substring;
407 if (!ParseStringAfterChar(option, '=', &substring)) {
408 return false;
409 }
410 gc::CollectorType collector_type = ParseCollectorType(substring);
411 if (collector_type != gc::kCollectorTypeNone) {
412 background_collector_type_ = collector_type;
413 } else {
414 Usage("Unknown -XX:BackgroundGC option %s", substring.c_str());
415 return false;
416 }
417 } else if (option == "-XX:+DisableExplicitGC") {
418 is_explicit_gc_disabled_ = true;
419 } else if (StartsWith(option, "-verbose:")) {
420 std::vector<std::string> verbose_options;
421 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
422 for (size_t i = 0; i < verbose_options.size(); ++i) {
423 if (verbose_options[i] == "class") {
424 gLogVerbosity.class_linker = true;
425 } else if (verbose_options[i] == "verifier") {
426 gLogVerbosity.verifier = true;
427 } else if (verbose_options[i] == "compiler") {
428 gLogVerbosity.compiler = true;
429 } else if (verbose_options[i] == "heap") {
430 gLogVerbosity.heap = true;
431 } else if (verbose_options[i] == "gc") {
432 gLogVerbosity.gc = true;
433 } else if (verbose_options[i] == "jdwp") {
434 gLogVerbosity.jdwp = true;
435 } else if (verbose_options[i] == "jni") {
436 gLogVerbosity.jni = true;
437 } else if (verbose_options[i] == "monitor") {
438 gLogVerbosity.monitor = true;
439 } else if (verbose_options[i] == "startup") {
440 gLogVerbosity.startup = true;
441 } else if (verbose_options[i] == "third-party-jni") {
442 gLogVerbosity.third_party_jni = true;
443 } else if (verbose_options[i] == "threads") {
444 gLogVerbosity.threads = true;
445 } else {
446 Usage("Unknown -verbose option %s", verbose_options[i].c_str());
447 return false;
448 }
449 }
Mingyao Yang42d65c52014-04-18 16:49:39 -0700450 } else if (StartsWith(option, "-verbose-methods:")) {
451 gLogVerbosity.compiler = false;
452 Split(option.substr(strlen("-verbose-methods:")), ',', gVerboseMethods);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800453 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
454 if (!ParseUnsignedInteger(option, ':', &lock_profiling_threshold_)) {
455 return false;
456 }
457 } else if (StartsWith(option, "-Xstacktracefile:")) {
458 if (!ParseStringAfterChar(option, ':', &stack_trace_file_)) {
459 return false;
460 }
461 } else if (option == "sensitiveThread") {
462 const void* hook = options[i].second;
463 hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(hook));
464 } else if (option == "vfprintf") {
465 const void* hook = options[i].second;
466 if (hook == nullptr) {
467 Usage("vfprintf argument was NULL");
468 return false;
469 }
470 hook_vfprintf_ =
471 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(hook));
472 } else if (option == "exit") {
473 const void* hook = options[i].second;
474 if (hook == nullptr) {
475 Usage("exit argument was NULL");
476 return false;
477 }
478 hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(hook));
479 } else if (option == "abort") {
480 const void* hook = options[i].second;
481 if (hook == nullptr) {
482 Usage("abort was NULL");
483 return false;
484 }
485 hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(hook));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800486 } else if (option == "-Xmethod-trace") {
487 method_trace_ = true;
488 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
489 method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
490 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
491 if (!ParseUnsignedInteger(option, ':', &method_trace_file_size_)) {
492 return false;
493 }
494 } else if (option == "-Xprofile:threadcpuclock") {
495 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
496 } else if (option == "-Xprofile:wallclock") {
497 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
498 } else if (option == "-Xprofile:dualclock") {
499 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
500 } else if (StartsWith(option, "-Xprofile:")) {
501 if (!ParseStringAfterChar(option, ';', &profile_output_filename_)) {
502 return false;
503 }
504 profile_ = true;
505 } else if (StartsWith(option, "-Xprofile-period:")) {
506 if (!ParseUnsignedInteger(option, ':', &profile_period_s_)) {
507 return false;
508 }
509 } else if (StartsWith(option, "-Xprofile-duration:")) {
510 if (!ParseUnsignedInteger(option, ':', &profile_duration_s_)) {
511 return false;
512 }
513 } else if (StartsWith(option, "-Xprofile-interval:")) {
514 if (!ParseUnsignedInteger(option, ':', &profile_interval_us_)) {
515 return false;
516 }
517 } else if (StartsWith(option, "-Xprofile-backoff:")) {
518 if (!ParseDouble(option, ':', 1.0, 10.0, &profile_backoff_coefficient_)) {
519 return false;
520 }
Calin Juravle16590062014-04-07 18:07:43 +0300521 } else if (option == "-Xprofile-start-lazy") {
522 profile_start_immediately_ = false;
Dave Allisonb373e092014-02-20 16:06:36 -0800523 } else if (StartsWith(option, "-implicit-checks:")) {
524 std::string checks;
525 if (!ParseStringAfterChar(option, ':', &checks)) {
526 return false;
527 }
528 std::vector<std::string> checkvec;
529 Split(checks, ',', checkvec);
530 for (auto& str : checkvec) {
531 std::string val = Trim(str);
532 if (val == "none") {
533 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
534 kExplicitStackOverflowCheck;
535 } else if (val == "null") {
536 explicit_checks_ &= ~kExplicitNullCheck;
537 } else if (val == "suspend") {
538 explicit_checks_ &= ~kExplicitSuspendCheck;
539 } else if (val == "stack") {
540 explicit_checks_ &= ~kExplicitStackOverflowCheck;
541 } else if (val == "all") {
542 explicit_checks_ = 0;
543 } else {
544 return false;
545 }
546 }
547 } else if (StartsWith(option, "-explicit-checks:")) {
548 std::string checks;
549 if (!ParseStringAfterChar(option, ':', &checks)) {
550 return false;
551 }
552 std::vector<std::string> checkvec;
553 Split(checks, ',', checkvec);
554 for (auto& str : checkvec) {
555 std::string val = Trim(str);
556 if (val == "none") {
557 explicit_checks_ = 0;
558 } else if (val == "null") {
559 explicit_checks_ |= kExplicitNullCheck;
560 } else if (val == "suspend") {
561 explicit_checks_ |= kExplicitSuspendCheck;
562 } else if (val == "stack") {
563 explicit_checks_ |= kExplicitStackOverflowCheck;
564 } else if (val == "all") {
565 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
566 kExplicitStackOverflowCheck;
567 } else {
568 return false;
569 }
570 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800571 } else if (option == "-Xcompiler-option") {
572 i++;
573 if (i == options.size()) {
574 Usage("Missing required compiler option for %s", option.c_str());
575 return false;
576 }
577 compiler_options_.push_back(options[i].first);
578 } else if (option == "-Ximage-compiler-option") {
579 i++;
580 if (i == options.size()) {
581 Usage("Missing required compiler option for %s", option.c_str());
582 return false;
583 }
584 image_compiler_options_.push_back(options[i].first);
Jeff Hao4a200f52014-04-01 14:58:49 -0700585 } else if (StartsWith(option, "-Xverify:")) {
586 std::string verify_mode = option.substr(strlen("-Xverify:"));
587 if (verify_mode == "none") {
588 verify_ = false;
589 } else if (verify_mode == "remote" || verify_mode == "all") {
590 verify_ = true;
591 } else {
592 Usage("Unknown -Xverify option %s", verify_mode.c_str());
593 return false;
594 }
Yevgeny Roubana6119a22014-03-24 11:31:24 +0700595 } else if (StartsWith(option, "-ea") ||
596 StartsWith(option, "-da") ||
597 StartsWith(option, "-enableassertions") ||
598 StartsWith(option, "-disableassertions") ||
Dave Allisonb373e092014-02-20 16:06:36 -0800599 (option == "--runtime-arg") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800600 (option == "-esa") ||
601 (option == "-dsa") ||
602 (option == "-enablesystemassertions") ||
603 (option == "-disablesystemassertions") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800604 (option == "-Xrs") ||
605 StartsWith(option, "-Xint:") ||
606 StartsWith(option, "-Xdexopt:") ||
607 (option == "-Xnoquithandler") ||
608 StartsWith(option, "-Xjniopts:") ||
609 StartsWith(option, "-Xjnigreflimit:") ||
610 (option == "-Xgenregmap") ||
611 (option == "-Xnogenregmap") ||
612 StartsWith(option, "-Xverifyopt:") ||
613 (option == "-Xcheckdexsum") ||
614 (option == "-Xincludeselectedop") ||
615 StartsWith(option, "-Xjitop:") ||
616 (option == "-Xincludeselectedmethod") ||
617 StartsWith(option, "-Xjitthreshold:") ||
618 StartsWith(option, "-Xjitcodecachesize:") ||
619 (option == "-Xjitblocking") ||
620 StartsWith(option, "-Xjitmethod:") ||
621 StartsWith(option, "-Xjitclass:") ||
622 StartsWith(option, "-Xjitoffset:") ||
623 StartsWith(option, "-Xjitconfig:") ||
624 (option == "-Xjitcheckcg") ||
625 (option == "-Xjitverbose") ||
626 (option == "-Xjitprofile") ||
627 (option == "-Xjitdisableopt") ||
628 (option == "-Xjitsuspendpoll") ||
629 StartsWith(option, "-XX:mainThreadStackSize=")) {
630 // Ignored for backwards compatibility.
631 } else if (!ignore_unrecognized) {
632 Usage("Unrecognized option %s", option.c_str());
633 return false;
634 }
635 }
636
637 // If a reference to the dalvik core.jar snuck in, replace it with
638 // the art specific version. This can happen with on device
639 // boot.art/boot.oat generation by GenerateImage which relies on the
640 // value of BOOTCLASSPATH.
641 std::string core_jar("/core.jar");
642 size_t core_jar_pos = boot_class_path_string_.find(core_jar);
643 if (core_jar_pos != std::string::npos) {
644 boot_class_path_string_.replace(core_jar_pos, core_jar.size(), "/core-libart.jar");
645 }
646
647 if (compiler_callbacks_ == nullptr && image_.empty()) {
648 image_ += GetAndroidRoot();
649 image_ += "/framework/boot.art";
650 }
651 if (heap_growth_limit_ == 0) {
652 heap_growth_limit_ = heap_maximum_size_;
653 }
654 if (background_collector_type_ == gc::kCollectorTypeNone) {
655 background_collector_type_ = collector_type_;
656 }
657 return true;
658}
659
660void ParsedOptions::Exit(int status) {
661 hook_exit_(status);
662}
663
664void ParsedOptions::Abort() {
665 hook_abort_();
666}
667
668void ParsedOptions::UsageMessageV(FILE* stream, const char* fmt, va_list ap) {
669 hook_vfprintf_(stderr, fmt, ap);
670}
671
672void ParsedOptions::UsageMessage(FILE* stream, const char* fmt, ...) {
673 va_list ap;
674 va_start(ap, fmt);
675 UsageMessageV(stream, fmt, ap);
676 va_end(ap);
677}
678
679void ParsedOptions::Usage(const char* fmt, ...) {
680 bool error = (fmt != nullptr);
681 FILE* stream = error ? stderr : stdout;
682
683 if (fmt != nullptr) {
684 va_list ap;
685 va_start(ap, fmt);
686 UsageMessageV(stream, fmt, ap);
687 va_end(ap);
688 }
689
690 const char* program = "dalvikvm";
691 UsageMessage(stream, "%s: [options] class [argument ...]\n", program);
692 UsageMessage(stream, "\n");
693 UsageMessage(stream, "The following standard options are supported:\n");
694 UsageMessage(stream, " -classpath classpath (-cp classpath)\n");
695 UsageMessage(stream, " -Dproperty=value\n");
696 UsageMessage(stream, " -verbose:tag ('gc', 'jni', or 'class')\n");
697 UsageMessage(stream, " -showversion\n");
698 UsageMessage(stream, " -help\n");
699 UsageMessage(stream, " -agentlib:jdwp=options\n");
700 UsageMessage(stream, "\n");
701
702 UsageMessage(stream, "The following extended options are supported:\n");
703 UsageMessage(stream, " -Xrunjdwp:<options>\n");
704 UsageMessage(stream, " -Xbootclasspath:bootclasspath\n");
705 UsageMessage(stream, " -Xcheck:tag (e.g. 'jni')\n");
706 UsageMessage(stream, " -XmsN (min heap, must be multiple of 1K, >= 1MB)\n");
707 UsageMessage(stream, " -XmxN (max heap, must be multiple of 1K, >= 2MB)\n");
708 UsageMessage(stream, " -XssN (stack size)\n");
709 UsageMessage(stream, " -Xint\n");
710 UsageMessage(stream, "\n");
711
712 UsageMessage(stream, "The following Dalvik options are supported:\n");
713 UsageMessage(stream, " -Xzygote\n");
714 UsageMessage(stream, " -Xjnitrace:substring (eg NativeClass or nativeMethod)\n");
715 UsageMessage(stream, " -Xstacktracefile:<filename>\n");
716 UsageMessage(stream, " -Xgc:[no]preverify\n");
717 UsageMessage(stream, " -Xgc:[no]postverify\n");
718 UsageMessage(stream, " -XX:+DisableExplicitGC\n");
719 UsageMessage(stream, " -XX:HeapGrowthLimit=N\n");
720 UsageMessage(stream, " -XX:HeapMinFree=N\n");
721 UsageMessage(stream, " -XX:HeapMaxFree=N\n");
722 UsageMessage(stream, " -XX:HeapTargetUtilization=doublevalue\n");
723 UsageMessage(stream, " -XX:LowMemoryMode\n");
724 UsageMessage(stream, " -Xprofile:{threadcpuclock,wallclock,dualclock}\n");
725 UsageMessage(stream, "\n");
726
727 UsageMessage(stream, "The following unique to ART options are supported:\n");
728 UsageMessage(stream, " -Xgc:[no]preverify_rosalloc\n");
729 UsageMessage(stream, " -Xgc:[no]postverify_rosalloc\n");
730 UsageMessage(stream, " -Ximage:filename\n");
731 UsageMessage(stream, " -XX:ParallelGCThreads=integervalue\n");
732 UsageMessage(stream, " -XX:ConcGCThreads=integervalue\n");
733 UsageMessage(stream, " -XX:MaxSpinsBeforeThinLockInflation=integervalue\n");
734 UsageMessage(stream, " -XX:LongPauseLogThreshold=integervalue\n");
735 UsageMessage(stream, " -XX:LongGCLogThreshold=integervalue\n");
736 UsageMessage(stream, " -XX:DumpGCPerformanceOnShutdown\n");
737 UsageMessage(stream, " -XX:IgnoreMaxFootprint\n");
738 UsageMessage(stream, " -XX:UseTLAB\n");
739 UsageMessage(stream, " -XX:BackgroundGC=none\n");
740 UsageMessage(stream, " -Xmethod-trace\n");
741 UsageMessage(stream, " -Xmethod-trace-file:filename");
742 UsageMessage(stream, " -Xmethod-trace-file-size:integervalue\n");
743 UsageMessage(stream, " -Xprofile=filename\n");
744 UsageMessage(stream, " -Xprofile-period:integervalue\n");
745 UsageMessage(stream, " -Xprofile-duration:integervalue\n");
746 UsageMessage(stream, " -Xprofile-interval:integervalue\n");
747 UsageMessage(stream, " -Xprofile-backoff:integervalue\n");
748 UsageMessage(stream, " -Xcompiler-option dex2oat-option\n");
749 UsageMessage(stream, " -Ximage-compiler-option dex2oat-option\n");
750 UsageMessage(stream, "\n");
751
752 UsageMessage(stream, "The following previously supported Dalvik options are ignored:\n");
753 UsageMessage(stream, " -ea[:<package name>... |:<class name>]\n");
754 UsageMessage(stream, " -da[:<package name>... |:<class name>]\n");
755 UsageMessage(stream, " (-enableassertions, -disableassertions)\n");
756 UsageMessage(stream, " -esa\n");
757 UsageMessage(stream, " -dsa\n");
758 UsageMessage(stream, " (-enablesystemassertions, -disablesystemassertions)\n");
759 UsageMessage(stream, " -Xverify:{none,remote,all}\n");
760 UsageMessage(stream, " -Xrs\n");
761 UsageMessage(stream, " -Xint:portable, -Xint:fast, -Xint:jit\n");
762 UsageMessage(stream, " -Xdexopt:{none,verified,all,full}\n");
763 UsageMessage(stream, " -Xnoquithandler\n");
764 UsageMessage(stream, " -Xjniopts:{warnonly,forcecopy}\n");
765 UsageMessage(stream, " -Xjnigreflimit:integervalue\n");
766 UsageMessage(stream, " -Xgc:[no]precise\n");
767 UsageMessage(stream, " -Xgc:[no]verifycardtable\n");
768 UsageMessage(stream, " -X[no]genregmap\n");
769 UsageMessage(stream, " -Xverifyopt:[no]checkmon\n");
770 UsageMessage(stream, " -Xcheckdexsum\n");
771 UsageMessage(stream, " -Xincludeselectedop\n");
772 UsageMessage(stream, " -Xjitop:hexopvalue[-endvalue][,hexopvalue[-endvalue]]*\n");
773 UsageMessage(stream, " -Xincludeselectedmethod\n");
774 UsageMessage(stream, " -Xjitthreshold:integervalue\n");
775 UsageMessage(stream, " -Xjitcodecachesize:decimalvalueofkbytes\n");
776 UsageMessage(stream, " -Xjitblocking\n");
777 UsageMessage(stream, " -Xjitmethod:signature[,signature]* (eg Ljava/lang/String\\;replace)\n");
778 UsageMessage(stream, " -Xjitclass:classname[,classname]*\n");
779 UsageMessage(stream, " -Xjitoffset:offset[,offset]\n");
780 UsageMessage(stream, " -Xjitconfig:filename\n");
781 UsageMessage(stream, " -Xjitcheckcg\n");
782 UsageMessage(stream, " -Xjitverbose\n");
783 UsageMessage(stream, " -Xjitprofile\n");
784 UsageMessage(stream, " -Xjitdisableopt\n");
785 UsageMessage(stream, " -Xjitsuspendpoll\n");
786 UsageMessage(stream, " -XX:mainThreadStackSize=N\n");
787 UsageMessage(stream, "\n");
788
789 Exit((error) ? 1 : 0);
790}
791
792bool ParsedOptions::ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) {
793 std::string::size_type colon = s.find(c);
794 if (colon == std::string::npos) {
795 Usage("Missing char %c in option %s", c, s.c_str());
796 return false;
797 }
798 // Add one to remove the char we were trimming until.
799 *parsed_value = s.substr(colon + 1);
800 return true;
801}
802
803bool ParsedOptions::ParseInteger(const std::string& s, char after_char, int* parsed_value) {
804 std::string::size_type colon = s.find(after_char);
805 if (colon == std::string::npos) {
806 Usage("Missing char %c in option %s", after_char, s.c_str());
807 return false;
808 }
809 const char* begin = &s[colon + 1];
810 char* end;
811 size_t result = strtoul(begin, &end, 10);
812 if (begin == end || *end != '\0') {
813 Usage("Failed to parse integer from %s ", s.c_str());
814 return false;
815 }
816 *parsed_value = result;
817 return true;
818}
819
820bool ParsedOptions::ParseUnsignedInteger(const std::string& s, char after_char,
821 unsigned int* parsed_value) {
822 int i;
823 if (!ParseInteger(s, after_char, &i)) {
824 return false;
825 }
826 if (i < 0) {
827 Usage("Negative value %d passed for unsigned option %s", i, s.c_str());
828 return false;
829 }
830 *parsed_value = i;
831 return true;
832}
833
834bool ParsedOptions::ParseDouble(const std::string& option, char after_char,
835 double min, double max, double* parsed_value) {
836 std::string substring;
837 if (!ParseStringAfterChar(option, after_char, &substring)) {
838 return false;
839 }
840 std::istringstream iss(substring);
841 double value;
842 iss >> value;
843 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
844 const bool sane_val = iss.eof() && (value >= min) && (value <= max);
845 if (!sane_val) {
846 Usage("Invalid double value %s for option %s", option.c_str());
847 return false;
848 }
849 *parsed_value = value;
850 return true;
851}
852
853} // namespace art