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