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