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