blob: 3c14c230358a1b8806c1e82ce32a4fe8ea53db7e [file] [log] [blame]
Brian Carlstrom491ca9e2014-03-02 18:24:38 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "parsed_options.h"
Dave Allisonb373e092014-02-20 16:06:36 -080018#ifdef HAVE_ANDROID_OS
19#include "cutils/properties.h"
20#endif
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080021
22#include "debugger.h"
23#include "monitor.h"
24
25namespace art {
26
27ParsedOptions* ParsedOptions::Create(const Runtime::Options& options, bool ignore_unrecognized) {
28 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
29 if (parsed->Parse(options, ignore_unrecognized)) {
30 return parsed.release();
31 }
32 return nullptr;
33}
34
35// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
36// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
37// [gG] gigabytes.
38//
39// "s" should point just past the "-Xm?" part of the string.
40// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
41// of 1024.
42//
43// The spec says the -Xmx and -Xms options must be multiples of 1024. It
44// doesn't say anything about -Xss.
45//
46// Returns 0 (a useless size) if "s" is malformed or specifies a low or
47// non-evenly-divisible value.
48//
49size_t ParseMemoryOption(const char* s, size_t div) {
50 // strtoul accepts a leading [+-], which we don't want,
51 // so make sure our string starts with a decimal digit.
52 if (isdigit(*s)) {
53 char* s2;
54 size_t val = strtoul(s, &s2, 10);
55 if (s2 != s) {
56 // s2 should be pointing just after the number.
57 // If this is the end of the string, the user
58 // has specified a number of bytes. Otherwise,
59 // there should be exactly one more character
60 // that specifies a multiplier.
61 if (*s2 != '\0') {
62 // The remainder of the string is either a single multiplier
63 // character, or nothing to indicate that the value is in
64 // bytes.
65 char c = *s2++;
66 if (*s2 == '\0') {
67 size_t mul;
68 if (c == '\0') {
69 mul = 1;
70 } else if (c == 'k' || c == 'K') {
71 mul = KB;
72 } else if (c == 'm' || c == 'M') {
73 mul = MB;
74 } else if (c == 'g' || c == 'G') {
75 mul = GB;
76 } else {
77 // Unknown multiplier character.
78 return 0;
79 }
80
81 if (val <= std::numeric_limits<size_t>::max() / mul) {
82 val *= mul;
83 } else {
84 // Clamp to a multiple of 1024.
85 val = std::numeric_limits<size_t>::max() & ~(1024-1);
86 }
87 } else {
88 // There's more than one character after the numeric part.
89 return 0;
90 }
91 }
92 // The man page says that a -Xm value must be a multiple of 1024.
93 if (val % div == 0) {
94 return val;
95 }
96 }
97 }
98 return 0;
99}
100
101static gc::CollectorType ParseCollectorType(const std::string& option) {
102 if (option == "MS" || option == "nonconcurrent") {
103 return gc::kCollectorTypeMS;
104 } else if (option == "CMS" || option == "concurrent") {
105 return gc::kCollectorTypeCMS;
106 } else if (option == "SS") {
107 return gc::kCollectorTypeSS;
108 } else if (option == "GSS") {
109 return gc::kCollectorTypeGSS;
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700110 } else if (option == "CC") {
111 return gc::kCollectorTypeCC;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800112 } else {
113 return gc::kCollectorTypeNone;
114 }
115}
116
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700117bool ParsedOptions::ParseXGcOption(const std::string& option) {
118 std::vector<std::string> gc_options;
119 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
120 for (const std::string& gc_option : gc_options) {
121 gc::CollectorType collector_type = ParseCollectorType(gc_option);
122 if (collector_type != gc::kCollectorTypeNone) {
123 collector_type_ = collector_type;
124 } else if (gc_option == "preverify") {
125 verify_pre_gc_heap_ = true;
126 } else if (gc_option == "nopreverify") {
127 verify_pre_gc_heap_ = false;
128 } else if (gc_option == "presweepingverify") {
129 verify_pre_sweeping_heap_ = true;
130 } else if (gc_option == "nopresweepingverify") {
131 verify_pre_sweeping_heap_ = false;
132 } else if (gc_option == "postverify") {
133 verify_post_gc_heap_ = true;
134 } else if (gc_option == "nopostverify") {
135 verify_post_gc_heap_ = false;
136 } else if (gc_option == "preverify_rosalloc") {
137 verify_pre_gc_rosalloc_ = true;
138 } else if (gc_option == "nopreverify_rosalloc") {
139 verify_pre_gc_rosalloc_ = false;
140 } else if (gc_option == "presweepingverify_rosalloc") {
141 verify_pre_sweeping_rosalloc_ = true;
142 } else if (gc_option == "nopresweepingverify_rosalloc") {
143 verify_pre_sweeping_rosalloc_ = false;
144 } else if (gc_option == "postverify_rosalloc") {
145 verify_post_gc_rosalloc_ = true;
146 } else if (gc_option == "nopostverify_rosalloc") {
147 verify_post_gc_rosalloc_ = false;
148 } else if ((gc_option == "precise") ||
149 (gc_option == "noprecise") ||
150 (gc_option == "verifycardtable") ||
151 (gc_option == "noverifycardtable")) {
152 // Ignored for backwards compatibility.
153 } else {
154 Usage("Unknown -Xgc option %s\n", gc_option.c_str());
155 return false;
156 }
157 }
158 return true;
159}
160
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800161bool ParsedOptions::Parse(const Runtime::Options& options, bool ignore_unrecognized) {
162 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
163 if (boot_class_path_string != NULL) {
164 boot_class_path_string_ = boot_class_path_string;
165 }
166 const char* class_path_string = getenv("CLASSPATH");
167 if (class_path_string != NULL) {
168 class_path_string_ = class_path_string;
169 }
170 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
171 check_jni_ = kIsDebugBuild;
172
173 heap_initial_size_ = gc::Heap::kDefaultInitialSize;
174 heap_maximum_size_ = gc::Heap::kDefaultMaximumSize;
175 heap_min_free_ = gc::Heap::kDefaultMinFree;
176 heap_max_free_ = gc::Heap::kDefaultMaxFree;
177 heap_target_utilization_ = gc::Heap::kDefaultTargetUtilization;
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700178 foreground_heap_growth_multiplier_ = gc::Heap::kDefaultHeapGrowthMultiplier;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800179 heap_growth_limit_ = 0; // 0 means no growth limit .
180 // Default to number of processors minus one since the main GC thread also does work.
181 parallel_gc_threads_ = sysconf(_SC_NPROCESSORS_CONF) - 1;
182 // Only the main GC thread, no workers.
183 conc_gc_threads_ = 0;
184 // Default is CMS which is Sticky + Partial + Full CMS GC.
185 collector_type_ = gc::kCollectorTypeCMS;
186 // If background_collector_type_ is kCollectorTypeNone, it defaults to the collector_type_ after
187 // parsing options.
188 background_collector_type_ = gc::kCollectorTypeNone;
189 stack_size_ = 0; // 0 means default.
190 max_spins_before_thin_lock_inflation_ = Monitor::kDefaultMaxSpinsBeforeThinLockInflation;
191 low_memory_mode_ = false;
192 use_tlab_ = false;
193 verify_pre_gc_heap_ = false;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700194 // Pre sweeping is the one that usually fails if the GC corrupted the heap.
195 verify_pre_sweeping_heap_ = kIsDebugBuild;
196 verify_post_gc_heap_ = false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800197 verify_pre_gc_rosalloc_ = kIsDebugBuild;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700198 verify_pre_sweeping_rosalloc_ = false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800199 verify_post_gc_rosalloc_ = false;
200
201 compiler_callbacks_ = nullptr;
202 is_zygote_ = false;
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -0800203 if (kPoisonHeapReferences) {
204 // kPoisonHeapReferences currently works only with the interpreter only.
205 // TODO: make it work with the compiler.
206 interpreter_only_ = true;
207 } else {
208 interpreter_only_ = false;
209 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800210 is_explicit_gc_disabled_ = false;
211
212 long_pause_log_threshold_ = gc::Heap::kDefaultLongPauseLogThreshold;
213 long_gc_log_threshold_ = gc::Heap::kDefaultLongGCLogThreshold;
214 dump_gc_performance_on_shutdown_ = false;
215 ignore_max_footprint_ = false;
216
217 lock_profiling_threshold_ = 0;
218 hook_is_sensitive_thread_ = NULL;
219
220 hook_vfprintf_ = vfprintf;
221 hook_exit_ = exit;
222 hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
223
224// gLogVerbosity.class_linker = true; // TODO: don't check this in!
225// gLogVerbosity.compiler = true; // TODO: don't check this in!
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800226// gLogVerbosity.gc = true; // TODO: don't check this in!
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700227// gLogVerbosity.heap = true; // TODO: don't check this in!
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800228// gLogVerbosity.jdwp = true; // TODO: don't check this in!
229// gLogVerbosity.jni = true; // TODO: don't check this in!
230// gLogVerbosity.monitor = true; // TODO: don't check this in!
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700231// gLogVerbosity.profiler = true; // TODO: don't check this in!
232// gLogVerbosity.signals = true; // TODO: don't check this in!
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800233// gLogVerbosity.startup = true; // TODO: don't check this in!
234// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
235// gLogVerbosity.threads = true; // TODO: don't check this in!
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700236// gLogVerbosity.verifier = true; // TODO: don't check this in!
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800237
238 method_trace_ = false;
239 method_trace_file_ = "/data/method-trace-file.bin";
240 method_trace_file_size_ = 10 * MB;
241
242 profile_ = false;
243 profile_period_s_ = 10; // Seconds.
244 profile_duration_s_ = 20; // Seconds.
245 profile_interval_us_ = 500; // Microseconds.
246 profile_backoff_coefficient_ = 2.0;
Calin Juravle16590062014-04-07 18:07:43 +0300247 profile_start_immediately_ = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800248 profile_clock_source_ = kDefaultProfilerClockSource;
249
Jeff Hao4a200f52014-04-01 14:58:49 -0700250 verify_ = true;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100251 image_isa_ = kRuntimeISA;
Jeff Hao4a200f52014-04-01 14:58:49 -0700252
Dave Allisonb373e092014-02-20 16:06:36 -0800253 // Default to explicit checks. Switch off with -implicit-checks:.
254 // or setprop dalvik.vm.implicit_checks check1,check2,...
255#ifdef HAVE_ANDROID_OS
256 {
257 char buf[PROP_VALUE_MAX];
Dave Allison05266432014-05-05 13:17:37 -0700258 property_get("dalvik.vm.implicit_checks", buf, "null,stack");
Dave Allisonb373e092014-02-20 16:06:36 -0800259 std::string checks(buf);
260 std::vector<std::string> checkvec;
261 Split(checks, ',', checkvec);
Dave Allisondd2e8252014-03-20 14:45:17 -0700262 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
263 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800264 for (auto& str : checkvec) {
265 std::string val = Trim(str);
266 if (val == "none") {
267 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
Dave Allisondd2e8252014-03-20 14:45:17 -0700268 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800269 } else if (val == "null") {
270 explicit_checks_ &= ~kExplicitNullCheck;
271 } else if (val == "suspend") {
272 explicit_checks_ &= ~kExplicitSuspendCheck;
273 } else if (val == "stack") {
274 explicit_checks_ &= ~kExplicitStackOverflowCheck;
275 } else if (val == "all") {
276 explicit_checks_ = 0;
277 }
278 }
279 }
280#else
281 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
282 kExplicitStackOverflowCheck;
283#endif
284
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800285 for (size_t i = 0; i < options.size(); ++i) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800286 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800287 LOG(INFO) << "option[" << i << "]=" << options[i].first;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800288 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800289 }
290 for (size_t i = 0; i < options.size(); ++i) {
291 const std::string option(options[i].first);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800292 if (StartsWith(option, "-help")) {
293 Usage(nullptr);
294 return false;
295 } else if (StartsWith(option, "-showversion")) {
296 UsageMessage(stdout, "ART version %s\n", Runtime::GetVersion());
297 Exit(0);
298 } else if (StartsWith(option, "-Xbootclasspath:")) {
299 boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
300 } else if (option == "-classpath" || option == "-cp") {
301 // TODO: support -Djava.class.path
302 i++;
303 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700304 Usage("Missing required class path value for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800305 return false;
306 }
307 const StringPiece& value = options[i].first;
308 class_path_string_ = value.data();
309 } else if (option == "bootclasspath") {
310 boot_class_path_
311 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
312 } else if (StartsWith(option, "-Ximage:")) {
313 if (!ParseStringAfterChar(option, ':', &image_)) {
314 return false;
315 }
316 } else if (StartsWith(option, "-Xcheck:jni")) {
317 check_jni_ = true;
318 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
319 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
320 // TODO: move parsing logic out of Dbg
321 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
322 if (tail != "help") {
323 UsageMessage(stderr, "Failed to parse JDWP option %s\n", tail.c_str());
324 }
325 Usage("Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
326 "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n");
327 return false;
328 }
329 } else if (StartsWith(option, "-Xms")) {
330 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
331 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700332 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800333 return false;
334 }
335 heap_initial_size_ = size;
336 } else if (StartsWith(option, "-Xmx")) {
337 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
338 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700339 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800340 return false;
341 }
342 heap_maximum_size_ = size;
343 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
344 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
345 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700346 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800347 return false;
348 }
349 heap_growth_limit_ = size;
350 } else if (StartsWith(option, "-XX:HeapMinFree=")) {
351 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMinFree=")).c_str(), 1024);
352 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700353 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800354 return false;
355 }
356 heap_min_free_ = size;
357 } else if (StartsWith(option, "-XX:HeapMaxFree=")) {
358 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMaxFree=")).c_str(), 1024);
359 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700360 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800361 return false;
362 }
363 heap_max_free_ = size;
364 } else if (StartsWith(option, "-XX:HeapTargetUtilization=")) {
365 if (!ParseDouble(option, '=', 0.1, 0.9, &heap_target_utilization_)) {
366 return false;
367 }
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700368 } else if (StartsWith(option, "-XX:ForegroundHeapGrowthMultiplier=")) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700369 if (!ParseDouble(option, '=', 0.1, 10.0, &foreground_heap_growth_multiplier_)) {
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700370 return false;
371 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800372 } else if (StartsWith(option, "-XX:ParallelGCThreads=")) {
373 if (!ParseUnsignedInteger(option, '=', &parallel_gc_threads_)) {
374 return false;
375 }
376 } else if (StartsWith(option, "-XX:ConcGCThreads=")) {
377 if (!ParseUnsignedInteger(option, '=', &conc_gc_threads_)) {
378 return false;
379 }
380 } else if (StartsWith(option, "-Xss")) {
381 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
382 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700383 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800384 return false;
385 }
386 stack_size_ = size;
387 } else if (StartsWith(option, "-XX:MaxSpinsBeforeThinLockInflation=")) {
388 if (!ParseUnsignedInteger(option, '=', &max_spins_before_thin_lock_inflation_)) {
389 return false;
390 }
391 } else if (StartsWith(option, "-XX:LongPauseLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800392 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800393 if (!ParseUnsignedInteger(option, '=', &value)) {
394 return false;
395 }
396 long_pause_log_threshold_ = MsToNs(value);
397 } else if (StartsWith(option, "-XX:LongGCLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800398 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800399 if (!ParseUnsignedInteger(option, '=', &value)) {
400 return false;
401 }
402 long_gc_log_threshold_ = MsToNs(value);
403 } else if (option == "-XX:DumpGCPerformanceOnShutdown") {
404 dump_gc_performance_on_shutdown_ = true;
405 } else if (option == "-XX:IgnoreMaxFootprint") {
406 ignore_max_footprint_ = true;
407 } else if (option == "-XX:LowMemoryMode") {
408 low_memory_mode_ = true;
409 } else if (option == "-XX:UseTLAB") {
410 use_tlab_ = true;
411 } else if (StartsWith(option, "-D")) {
412 properties_.push_back(option.substr(strlen("-D")));
413 } else if (StartsWith(option, "-Xjnitrace:")) {
414 jni_trace_ = option.substr(strlen("-Xjnitrace:"));
415 } else if (option == "compilercallbacks") {
416 compiler_callbacks_ =
417 reinterpret_cast<CompilerCallbacks*>(const_cast<void*>(options[i].second));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100418 } else if (option == "imageinstructionset") {
419 image_isa_ = GetInstructionSetFromString(
420 reinterpret_cast<const char*>(options[i].second));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800421 } else if (option == "-Xzygote") {
422 is_zygote_ = true;
423 } else if (option == "-Xint") {
424 interpreter_only_ = true;
425 } else if (StartsWith(option, "-Xgc:")) {
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700426 if (!ParseXGcOption(option)) {
427 return false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800428 }
429 } else if (StartsWith(option, "-XX:BackgroundGC=")) {
430 std::string substring;
431 if (!ParseStringAfterChar(option, '=', &substring)) {
432 return false;
433 }
434 gc::CollectorType collector_type = ParseCollectorType(substring);
435 if (collector_type != gc::kCollectorTypeNone) {
436 background_collector_type_ = collector_type;
437 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700438 Usage("Unknown -XX:BackgroundGC option %s\n", substring.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800439 return false;
440 }
441 } else if (option == "-XX:+DisableExplicitGC") {
442 is_explicit_gc_disabled_ = true;
443 } else if (StartsWith(option, "-verbose:")) {
444 std::vector<std::string> verbose_options;
445 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
446 for (size_t i = 0; i < verbose_options.size(); ++i) {
447 if (verbose_options[i] == "class") {
448 gLogVerbosity.class_linker = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800449 } else if (verbose_options[i] == "compiler") {
450 gLogVerbosity.compiler = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800451 } else if (verbose_options[i] == "gc") {
452 gLogVerbosity.gc = true;
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700453 } else if (verbose_options[i] == "heap") {
454 gLogVerbosity.heap = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800455 } else if (verbose_options[i] == "jdwp") {
456 gLogVerbosity.jdwp = true;
457 } else if (verbose_options[i] == "jni") {
458 gLogVerbosity.jni = true;
459 } else if (verbose_options[i] == "monitor") {
460 gLogVerbosity.monitor = true;
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700461 } else if (verbose_options[i] == "profiler") {
462 gLogVerbosity.profiler = true;
463 } else if (verbose_options[i] == "signals") {
464 gLogVerbosity.signals = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800465 } else if (verbose_options[i] == "startup") {
466 gLogVerbosity.startup = true;
467 } else if (verbose_options[i] == "third-party-jni") {
468 gLogVerbosity.third_party_jni = true;
469 } else if (verbose_options[i] == "threads") {
470 gLogVerbosity.threads = true;
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700471 } else if (verbose_options[i] == "verifier") {
472 gLogVerbosity.verifier = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800473 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700474 Usage("Unknown -verbose option %s\n", verbose_options[i].c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800475 return false;
476 }
477 }
Mingyao Yang42d65c52014-04-18 16:49:39 -0700478 } else if (StartsWith(option, "-verbose-methods:")) {
479 gLogVerbosity.compiler = false;
480 Split(option.substr(strlen("-verbose-methods:")), ',', gVerboseMethods);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800481 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
482 if (!ParseUnsignedInteger(option, ':', &lock_profiling_threshold_)) {
483 return false;
484 }
485 } else if (StartsWith(option, "-Xstacktracefile:")) {
486 if (!ParseStringAfterChar(option, ':', &stack_trace_file_)) {
487 return false;
488 }
489 } else if (option == "sensitiveThread") {
490 const void* hook = options[i].second;
491 hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(hook));
492 } else if (option == "vfprintf") {
493 const void* hook = options[i].second;
494 if (hook == nullptr) {
495 Usage("vfprintf argument was NULL");
496 return false;
497 }
498 hook_vfprintf_ =
499 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(hook));
500 } else if (option == "exit") {
501 const void* hook = options[i].second;
502 if (hook == nullptr) {
503 Usage("exit argument was NULL");
504 return false;
505 }
506 hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(hook));
507 } else if (option == "abort") {
508 const void* hook = options[i].second;
509 if (hook == nullptr) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700510 Usage("abort was NULL\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800511 return false;
512 }
513 hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(hook));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800514 } else if (option == "-Xmethod-trace") {
515 method_trace_ = true;
516 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
517 method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
518 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
519 if (!ParseUnsignedInteger(option, ':', &method_trace_file_size_)) {
520 return false;
521 }
522 } else if (option == "-Xprofile:threadcpuclock") {
523 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
524 } else if (option == "-Xprofile:wallclock") {
525 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
526 } else if (option == "-Xprofile:dualclock") {
527 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
528 } else if (StartsWith(option, "-Xprofile:")) {
529 if (!ParseStringAfterChar(option, ';', &profile_output_filename_)) {
530 return false;
531 }
532 profile_ = true;
533 } else if (StartsWith(option, "-Xprofile-period:")) {
534 if (!ParseUnsignedInteger(option, ':', &profile_period_s_)) {
535 return false;
536 }
537 } else if (StartsWith(option, "-Xprofile-duration:")) {
538 if (!ParseUnsignedInteger(option, ':', &profile_duration_s_)) {
539 return false;
540 }
541 } else if (StartsWith(option, "-Xprofile-interval:")) {
542 if (!ParseUnsignedInteger(option, ':', &profile_interval_us_)) {
543 return false;
544 }
545 } else if (StartsWith(option, "-Xprofile-backoff:")) {
546 if (!ParseDouble(option, ':', 1.0, 10.0, &profile_backoff_coefficient_)) {
547 return false;
548 }
Calin Juravle16590062014-04-07 18:07:43 +0300549 } else if (option == "-Xprofile-start-lazy") {
550 profile_start_immediately_ = false;
Dave Allisonb373e092014-02-20 16:06:36 -0800551 } else if (StartsWith(option, "-implicit-checks:")) {
552 std::string checks;
553 if (!ParseStringAfterChar(option, ':', &checks)) {
554 return false;
555 }
556 std::vector<std::string> checkvec;
557 Split(checks, ',', checkvec);
558 for (auto& str : checkvec) {
559 std::string val = Trim(str);
560 if (val == "none") {
561 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
562 kExplicitStackOverflowCheck;
563 } else if (val == "null") {
564 explicit_checks_ &= ~kExplicitNullCheck;
565 } else if (val == "suspend") {
566 explicit_checks_ &= ~kExplicitSuspendCheck;
567 } else if (val == "stack") {
568 explicit_checks_ &= ~kExplicitStackOverflowCheck;
569 } else if (val == "all") {
570 explicit_checks_ = 0;
571 } else {
572 return false;
573 }
574 }
575 } else if (StartsWith(option, "-explicit-checks:")) {
576 std::string checks;
577 if (!ParseStringAfterChar(option, ':', &checks)) {
578 return false;
579 }
580 std::vector<std::string> checkvec;
581 Split(checks, ',', checkvec);
582 for (auto& str : checkvec) {
583 std::string val = Trim(str);
584 if (val == "none") {
585 explicit_checks_ = 0;
586 } else if (val == "null") {
587 explicit_checks_ |= kExplicitNullCheck;
588 } else if (val == "suspend") {
589 explicit_checks_ |= kExplicitSuspendCheck;
590 } else if (val == "stack") {
591 explicit_checks_ |= kExplicitStackOverflowCheck;
592 } else if (val == "all") {
593 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
594 kExplicitStackOverflowCheck;
595 } else {
596 return false;
597 }
598 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800599 } else if (option == "-Xcompiler-option") {
600 i++;
601 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700602 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800603 return false;
604 }
605 compiler_options_.push_back(options[i].first);
606 } else if (option == "-Ximage-compiler-option") {
607 i++;
608 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700609 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800610 return false;
611 }
612 image_compiler_options_.push_back(options[i].first);
Jeff Hao4a200f52014-04-01 14:58:49 -0700613 } else if (StartsWith(option, "-Xverify:")) {
614 std::string verify_mode = option.substr(strlen("-Xverify:"));
615 if (verify_mode == "none") {
616 verify_ = false;
617 } else if (verify_mode == "remote" || verify_mode == "all") {
618 verify_ = true;
619 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700620 Usage("Unknown -Xverify option %s\n", verify_mode.c_str());
Jeff Hao4a200f52014-04-01 14:58:49 -0700621 return false;
622 }
Yevgeny Roubana6119a22014-03-24 11:31:24 +0700623 } else if (StartsWith(option, "-ea") ||
624 StartsWith(option, "-da") ||
625 StartsWith(option, "-enableassertions") ||
626 StartsWith(option, "-disableassertions") ||
Dave Allisonb373e092014-02-20 16:06:36 -0800627 (option == "--runtime-arg") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800628 (option == "-esa") ||
629 (option == "-dsa") ||
630 (option == "-enablesystemassertions") ||
631 (option == "-disablesystemassertions") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800632 (option == "-Xrs") ||
633 StartsWith(option, "-Xint:") ||
634 StartsWith(option, "-Xdexopt:") ||
635 (option == "-Xnoquithandler") ||
636 StartsWith(option, "-Xjniopts:") ||
637 StartsWith(option, "-Xjnigreflimit:") ||
638 (option == "-Xgenregmap") ||
639 (option == "-Xnogenregmap") ||
640 StartsWith(option, "-Xverifyopt:") ||
641 (option == "-Xcheckdexsum") ||
642 (option == "-Xincludeselectedop") ||
643 StartsWith(option, "-Xjitop:") ||
644 (option == "-Xincludeselectedmethod") ||
645 StartsWith(option, "-Xjitthreshold:") ||
646 StartsWith(option, "-Xjitcodecachesize:") ||
647 (option == "-Xjitblocking") ||
648 StartsWith(option, "-Xjitmethod:") ||
649 StartsWith(option, "-Xjitclass:") ||
650 StartsWith(option, "-Xjitoffset:") ||
651 StartsWith(option, "-Xjitconfig:") ||
652 (option == "-Xjitcheckcg") ||
653 (option == "-Xjitverbose") ||
654 (option == "-Xjitprofile") ||
655 (option == "-Xjitdisableopt") ||
656 (option == "-Xjitsuspendpoll") ||
657 StartsWith(option, "-XX:mainThreadStackSize=")) {
658 // Ignored for backwards compatibility.
659 } else if (!ignore_unrecognized) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700660 Usage("Unrecognized option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800661 return false;
662 }
663 }
664
665 // If a reference to the dalvik core.jar snuck in, replace it with
666 // the art specific version. This can happen with on device
667 // boot.art/boot.oat generation by GenerateImage which relies on the
668 // value of BOOTCLASSPATH.
669 std::string core_jar("/core.jar");
670 size_t core_jar_pos = boot_class_path_string_.find(core_jar);
671 if (core_jar_pos != std::string::npos) {
672 boot_class_path_string_.replace(core_jar_pos, core_jar.size(), "/core-libart.jar");
673 }
674
675 if (compiler_callbacks_ == nullptr && image_.empty()) {
676 image_ += GetAndroidRoot();
677 image_ += "/framework/boot.art";
678 }
679 if (heap_growth_limit_ == 0) {
680 heap_growth_limit_ = heap_maximum_size_;
681 }
682 if (background_collector_type_ == gc::kCollectorTypeNone) {
683 background_collector_type_ = collector_type_;
684 }
685 return true;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100686} // NOLINT(readability/fn_size)
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800687
688void ParsedOptions::Exit(int status) {
689 hook_exit_(status);
690}
691
692void ParsedOptions::Abort() {
693 hook_abort_();
694}
695
696void ParsedOptions::UsageMessageV(FILE* stream, const char* fmt, va_list ap) {
697 hook_vfprintf_(stderr, fmt, ap);
698}
699
700void ParsedOptions::UsageMessage(FILE* stream, const char* fmt, ...) {
701 va_list ap;
702 va_start(ap, fmt);
703 UsageMessageV(stream, fmt, ap);
704 va_end(ap);
705}
706
707void ParsedOptions::Usage(const char* fmt, ...) {
708 bool error = (fmt != nullptr);
709 FILE* stream = error ? stderr : stdout;
710
711 if (fmt != nullptr) {
712 va_list ap;
713 va_start(ap, fmt);
714 UsageMessageV(stream, fmt, ap);
715 va_end(ap);
716 }
717
718 const char* program = "dalvikvm";
719 UsageMessage(stream, "%s: [options] class [argument ...]\n", program);
720 UsageMessage(stream, "\n");
721 UsageMessage(stream, "The following standard options are supported:\n");
722 UsageMessage(stream, " -classpath classpath (-cp classpath)\n");
723 UsageMessage(stream, " -Dproperty=value\n");
724 UsageMessage(stream, " -verbose:tag ('gc', 'jni', or 'class')\n");
725 UsageMessage(stream, " -showversion\n");
726 UsageMessage(stream, " -help\n");
727 UsageMessage(stream, " -agentlib:jdwp=options\n");
728 UsageMessage(stream, "\n");
729
730 UsageMessage(stream, "The following extended options are supported:\n");
731 UsageMessage(stream, " -Xrunjdwp:<options>\n");
732 UsageMessage(stream, " -Xbootclasspath:bootclasspath\n");
733 UsageMessage(stream, " -Xcheck:tag (e.g. 'jni')\n");
734 UsageMessage(stream, " -XmsN (min heap, must be multiple of 1K, >= 1MB)\n");
735 UsageMessage(stream, " -XmxN (max heap, must be multiple of 1K, >= 2MB)\n");
736 UsageMessage(stream, " -XssN (stack size)\n");
737 UsageMessage(stream, " -Xint\n");
738 UsageMessage(stream, "\n");
739
740 UsageMessage(stream, "The following Dalvik options are supported:\n");
741 UsageMessage(stream, " -Xzygote\n");
742 UsageMessage(stream, " -Xjnitrace:substring (eg NativeClass or nativeMethod)\n");
743 UsageMessage(stream, " -Xstacktracefile:<filename>\n");
744 UsageMessage(stream, " -Xgc:[no]preverify\n");
745 UsageMessage(stream, " -Xgc:[no]postverify\n");
746 UsageMessage(stream, " -XX:+DisableExplicitGC\n");
747 UsageMessage(stream, " -XX:HeapGrowthLimit=N\n");
748 UsageMessage(stream, " -XX:HeapMinFree=N\n");
749 UsageMessage(stream, " -XX:HeapMaxFree=N\n");
750 UsageMessage(stream, " -XX:HeapTargetUtilization=doublevalue\n");
Mathieu Chartier455820e2014-04-18 12:02:39 -0700751 UsageMessage(stream, " -XX:ForegroundHeapGrowthMultiplier=doublevalue\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800752 UsageMessage(stream, " -XX:LowMemoryMode\n");
753 UsageMessage(stream, " -Xprofile:{threadcpuclock,wallclock,dualclock}\n");
754 UsageMessage(stream, "\n");
755
756 UsageMessage(stream, "The following unique to ART options are supported:\n");
757 UsageMessage(stream, " -Xgc:[no]preverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700758 UsageMessage(stream, " -Xgc:[no]postsweepingverify_rosalloc\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800759 UsageMessage(stream, " -Xgc:[no]postverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700760 UsageMessage(stream, " -Xgc:[no]presweepingverify\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800761 UsageMessage(stream, " -Ximage:filename\n");
762 UsageMessage(stream, " -XX:ParallelGCThreads=integervalue\n");
763 UsageMessage(stream, " -XX:ConcGCThreads=integervalue\n");
764 UsageMessage(stream, " -XX:MaxSpinsBeforeThinLockInflation=integervalue\n");
765 UsageMessage(stream, " -XX:LongPauseLogThreshold=integervalue\n");
766 UsageMessage(stream, " -XX:LongGCLogThreshold=integervalue\n");
767 UsageMessage(stream, " -XX:DumpGCPerformanceOnShutdown\n");
768 UsageMessage(stream, " -XX:IgnoreMaxFootprint\n");
769 UsageMessage(stream, " -XX:UseTLAB\n");
770 UsageMessage(stream, " -XX:BackgroundGC=none\n");
771 UsageMessage(stream, " -Xmethod-trace\n");
772 UsageMessage(stream, " -Xmethod-trace-file:filename");
773 UsageMessage(stream, " -Xmethod-trace-file-size:integervalue\n");
774 UsageMessage(stream, " -Xprofile=filename\n");
775 UsageMessage(stream, " -Xprofile-period:integervalue\n");
776 UsageMessage(stream, " -Xprofile-duration:integervalue\n");
777 UsageMessage(stream, " -Xprofile-interval:integervalue\n");
778 UsageMessage(stream, " -Xprofile-backoff:integervalue\n");
779 UsageMessage(stream, " -Xcompiler-option dex2oat-option\n");
780 UsageMessage(stream, " -Ximage-compiler-option dex2oat-option\n");
781 UsageMessage(stream, "\n");
782
783 UsageMessage(stream, "The following previously supported Dalvik options are ignored:\n");
784 UsageMessage(stream, " -ea[:<package name>... |:<class name>]\n");
785 UsageMessage(stream, " -da[:<package name>... |:<class name>]\n");
786 UsageMessage(stream, " (-enableassertions, -disableassertions)\n");
787 UsageMessage(stream, " -esa\n");
788 UsageMessage(stream, " -dsa\n");
789 UsageMessage(stream, " (-enablesystemassertions, -disablesystemassertions)\n");
790 UsageMessage(stream, " -Xverify:{none,remote,all}\n");
791 UsageMessage(stream, " -Xrs\n");
792 UsageMessage(stream, " -Xint:portable, -Xint:fast, -Xint:jit\n");
793 UsageMessage(stream, " -Xdexopt:{none,verified,all,full}\n");
794 UsageMessage(stream, " -Xnoquithandler\n");
795 UsageMessage(stream, " -Xjniopts:{warnonly,forcecopy}\n");
796 UsageMessage(stream, " -Xjnigreflimit:integervalue\n");
797 UsageMessage(stream, " -Xgc:[no]precise\n");
798 UsageMessage(stream, " -Xgc:[no]verifycardtable\n");
799 UsageMessage(stream, " -X[no]genregmap\n");
800 UsageMessage(stream, " -Xverifyopt:[no]checkmon\n");
801 UsageMessage(stream, " -Xcheckdexsum\n");
802 UsageMessage(stream, " -Xincludeselectedop\n");
803 UsageMessage(stream, " -Xjitop:hexopvalue[-endvalue][,hexopvalue[-endvalue]]*\n");
804 UsageMessage(stream, " -Xincludeselectedmethod\n");
805 UsageMessage(stream, " -Xjitthreshold:integervalue\n");
806 UsageMessage(stream, " -Xjitcodecachesize:decimalvalueofkbytes\n");
807 UsageMessage(stream, " -Xjitblocking\n");
808 UsageMessage(stream, " -Xjitmethod:signature[,signature]* (eg Ljava/lang/String\\;replace)\n");
809 UsageMessage(stream, " -Xjitclass:classname[,classname]*\n");
810 UsageMessage(stream, " -Xjitoffset:offset[,offset]\n");
811 UsageMessage(stream, " -Xjitconfig:filename\n");
812 UsageMessage(stream, " -Xjitcheckcg\n");
813 UsageMessage(stream, " -Xjitverbose\n");
814 UsageMessage(stream, " -Xjitprofile\n");
815 UsageMessage(stream, " -Xjitdisableopt\n");
816 UsageMessage(stream, " -Xjitsuspendpoll\n");
817 UsageMessage(stream, " -XX:mainThreadStackSize=N\n");
818 UsageMessage(stream, "\n");
819
820 Exit((error) ? 1 : 0);
821}
822
823bool ParsedOptions::ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) {
824 std::string::size_type colon = s.find(c);
825 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700826 Usage("Missing char %c in option %s\n", c, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800827 return false;
828 }
829 // Add one to remove the char we were trimming until.
830 *parsed_value = s.substr(colon + 1);
831 return true;
832}
833
834bool ParsedOptions::ParseInteger(const std::string& s, char after_char, int* parsed_value) {
835 std::string::size_type colon = s.find(after_char);
836 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700837 Usage("Missing char %c in option %s\n", after_char, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800838 return false;
839 }
840 const char* begin = &s[colon + 1];
841 char* end;
842 size_t result = strtoul(begin, &end, 10);
843 if (begin == end || *end != '\0') {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700844 Usage("Failed to parse integer from %s\n", s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800845 return false;
846 }
847 *parsed_value = result;
848 return true;
849}
850
851bool ParsedOptions::ParseUnsignedInteger(const std::string& s, char after_char,
852 unsigned int* parsed_value) {
853 int i;
854 if (!ParseInteger(s, after_char, &i)) {
855 return false;
856 }
857 if (i < 0) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700858 Usage("Negative value %d passed for unsigned option %s\n", i, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800859 return false;
860 }
861 *parsed_value = i;
862 return true;
863}
864
865bool ParsedOptions::ParseDouble(const std::string& option, char after_char,
866 double min, double max, double* parsed_value) {
867 std::string substring;
868 if (!ParseStringAfterChar(option, after_char, &substring)) {
869 return false;
870 }
871 std::istringstream iss(substring);
872 double value;
873 iss >> value;
874 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
875 const bool sane_val = iss.eof() && (value >= min) && (value <= max);
876 if (!sane_val) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700877 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800878 return false;
879 }
880 *parsed_value = value;
881 return true;
882}
883
884} // namespace art