blob: fac9965d60263b773fcb227d211e61425785d3b0 [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"
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -070018#include "utils.h"
Dave Allisonb373e092014-02-20 16:06:36 -080019#ifdef HAVE_ANDROID_OS
20#include "cutils/properties.h"
21#endif
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080022
23#include "debugger.h"
24#include "monitor.h"
25
26namespace art {
27
28ParsedOptions* ParsedOptions::Create(const Runtime::Options& options, bool ignore_unrecognized) {
Ian Rogers700a4022014-05-19 16:49:03 -070029 std::unique_ptr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -080030 if (parsed->Parse(options, ignore_unrecognized)) {
31 return parsed.release();
32 }
33 return nullptr;
34}
35
36// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
37// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
38// [gG] gigabytes.
39//
40// "s" should point just past the "-Xm?" part of the string.
41// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
42// of 1024.
43//
44// The spec says the -Xmx and -Xms options must be multiples of 1024. It
45// doesn't say anything about -Xss.
46//
47// Returns 0 (a useless size) if "s" is malformed or specifies a low or
48// non-evenly-divisible value.
49//
50size_t ParseMemoryOption(const char* s, size_t div) {
51 // strtoul accepts a leading [+-], which we don't want,
52 // so make sure our string starts with a decimal digit.
53 if (isdigit(*s)) {
54 char* s2;
55 size_t val = strtoul(s, &s2, 10);
56 if (s2 != s) {
57 // s2 should be pointing just after the number.
58 // If this is the end of the string, the user
59 // has specified a number of bytes. Otherwise,
60 // there should be exactly one more character
61 // that specifies a multiplier.
62 if (*s2 != '\0') {
63 // The remainder of the string is either a single multiplier
64 // character, or nothing to indicate that the value is in
65 // bytes.
66 char c = *s2++;
67 if (*s2 == '\0') {
68 size_t mul;
69 if (c == '\0') {
70 mul = 1;
71 } else if (c == 'k' || c == 'K') {
72 mul = KB;
73 } else if (c == 'm' || c == 'M') {
74 mul = MB;
75 } else if (c == 'g' || c == 'G') {
76 mul = GB;
77 } else {
78 // Unknown multiplier character.
79 return 0;
80 }
81
82 if (val <= std::numeric_limits<size_t>::max() / mul) {
83 val *= mul;
84 } else {
85 // Clamp to a multiple of 1024.
86 val = std::numeric_limits<size_t>::max() & ~(1024-1);
87 }
88 } else {
89 // There's more than one character after the numeric part.
90 return 0;
91 }
92 }
93 // The man page says that a -Xm value must be a multiple of 1024.
94 if (val % div == 0) {
95 return val;
96 }
97 }
98 }
99 return 0;
100}
101
102static gc::CollectorType ParseCollectorType(const std::string& option) {
103 if (option == "MS" || option == "nonconcurrent") {
104 return gc::kCollectorTypeMS;
105 } else if (option == "CMS" || option == "concurrent") {
106 return gc::kCollectorTypeCMS;
107 } else if (option == "SS") {
108 return gc::kCollectorTypeSS;
109 } else if (option == "GSS") {
110 return gc::kCollectorTypeGSS;
Hiroshi Yamauchid5307ec2014-03-27 21:07:51 -0700111 } else if (option == "CC") {
112 return gc::kCollectorTypeCC;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800113 } else {
114 return gc::kCollectorTypeNone;
115 }
116}
117
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700118bool ParsedOptions::ParseXGcOption(const std::string& option) {
119 std::vector<std::string> gc_options;
120 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
121 for (const std::string& gc_option : gc_options) {
122 gc::CollectorType collector_type = ParseCollectorType(gc_option);
123 if (collector_type != gc::kCollectorTypeNone) {
124 collector_type_ = collector_type;
125 } else if (gc_option == "preverify") {
126 verify_pre_gc_heap_ = true;
127 } else if (gc_option == "nopreverify") {
128 verify_pre_gc_heap_ = false;
129 } else if (gc_option == "presweepingverify") {
130 verify_pre_sweeping_heap_ = true;
131 } else if (gc_option == "nopresweepingverify") {
132 verify_pre_sweeping_heap_ = false;
133 } else if (gc_option == "postverify") {
134 verify_post_gc_heap_ = true;
135 } else if (gc_option == "nopostverify") {
136 verify_post_gc_heap_ = false;
137 } else if (gc_option == "preverify_rosalloc") {
138 verify_pre_gc_rosalloc_ = true;
139 } else if (gc_option == "nopreverify_rosalloc") {
140 verify_pre_gc_rosalloc_ = false;
141 } else if (gc_option == "presweepingverify_rosalloc") {
142 verify_pre_sweeping_rosalloc_ = true;
143 } else if (gc_option == "nopresweepingverify_rosalloc") {
144 verify_pre_sweeping_rosalloc_ = false;
145 } else if (gc_option == "postverify_rosalloc") {
146 verify_post_gc_rosalloc_ = true;
147 } else if (gc_option == "nopostverify_rosalloc") {
148 verify_post_gc_rosalloc_ = false;
149 } else if ((gc_option == "precise") ||
150 (gc_option == "noprecise") ||
151 (gc_option == "verifycardtable") ||
152 (gc_option == "noverifycardtable")) {
153 // Ignored for backwards compatibility.
154 } else {
155 Usage("Unknown -Xgc option %s\n", gc_option.c_str());
156 return false;
157 }
158 }
159 return true;
160}
161
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800162bool ParsedOptions::Parse(const Runtime::Options& options, bool ignore_unrecognized) {
163 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
164 if (boot_class_path_string != NULL) {
165 boot_class_path_string_ = boot_class_path_string;
166 }
167 const char* class_path_string = getenv("CLASSPATH");
168 if (class_path_string != NULL) {
169 class_path_string_ = class_path_string;
170 }
171 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
172 check_jni_ = kIsDebugBuild;
173
174 heap_initial_size_ = gc::Heap::kDefaultInitialSize;
175 heap_maximum_size_ = gc::Heap::kDefaultMaximumSize;
176 heap_min_free_ = gc::Heap::kDefaultMinFree;
177 heap_max_free_ = gc::Heap::kDefaultMaxFree;
178 heap_target_utilization_ = gc::Heap::kDefaultTargetUtilization;
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700179 foreground_heap_growth_multiplier_ = gc::Heap::kDefaultHeapGrowthMultiplier;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800180 heap_growth_limit_ = 0; // 0 means no growth limit .
181 // Default to number of processors minus one since the main GC thread also does work.
182 parallel_gc_threads_ = sysconf(_SC_NPROCESSORS_CONF) - 1;
183 // Only the main GC thread, no workers.
184 conc_gc_threads_ = 0;
Hiroshi Yamauchi1dda0602014-05-12 12:32:32 -0700185 // The default GC type is set in makefiles.
186#if ART_DEFAULT_GC_TYPE_IS_CMS
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800187 collector_type_ = gc::kCollectorTypeCMS;
Hiroshi Yamauchi1dda0602014-05-12 12:32:32 -0700188#elif ART_DEFAULT_GC_TYPE_IS_SS
189 collector_type_ = gc::kCollectorTypeSS;
190#elif ART_DEFAULT_GC_TYPE_IS_GSS
191 collector_type_ = gc::kCollectorTypeGSS;
192#else
193#error "ART default GC type must be set"
194#endif
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800195 // If background_collector_type_ is kCollectorTypeNone, it defaults to the collector_type_ after
196 // parsing options.
197 background_collector_type_ = gc::kCollectorTypeNone;
198 stack_size_ = 0; // 0 means default.
199 max_spins_before_thin_lock_inflation_ = Monitor::kDefaultMaxSpinsBeforeThinLockInflation;
200 low_memory_mode_ = false;
201 use_tlab_ = false;
202 verify_pre_gc_heap_ = false;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700203 // Pre sweeping is the one that usually fails if the GC corrupted the heap.
204 verify_pre_sweeping_heap_ = kIsDebugBuild;
205 verify_post_gc_heap_ = false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800206 verify_pre_gc_rosalloc_ = kIsDebugBuild;
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700207 verify_pre_sweeping_rosalloc_ = false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800208 verify_post_gc_rosalloc_ = false;
209
210 compiler_callbacks_ = nullptr;
211 is_zygote_ = false;
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -0800212 if (kPoisonHeapReferences) {
213 // kPoisonHeapReferences currently works only with the interpreter only.
214 // TODO: make it work with the compiler.
215 interpreter_only_ = true;
216 } else {
217 interpreter_only_ = false;
218 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800219 is_explicit_gc_disabled_ = false;
220
221 long_pause_log_threshold_ = gc::Heap::kDefaultLongPauseLogThreshold;
222 long_gc_log_threshold_ = gc::Heap::kDefaultLongGCLogThreshold;
223 dump_gc_performance_on_shutdown_ = false;
224 ignore_max_footprint_ = false;
225
226 lock_profiling_threshold_ = 0;
227 hook_is_sensitive_thread_ = NULL;
228
229 hook_vfprintf_ = vfprintf;
230 hook_exit_ = exit;
231 hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
232
233// gLogVerbosity.class_linker = true; // TODO: don't check this in!
234// gLogVerbosity.compiler = true; // TODO: don't check this in!
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800235// gLogVerbosity.gc = true; // TODO: don't check this in!
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700236// gLogVerbosity.heap = true; // TODO: don't check this in!
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800237// gLogVerbosity.jdwp = true; // TODO: don't check this in!
238// gLogVerbosity.jni = true; // TODO: don't check this in!
239// gLogVerbosity.monitor = true; // TODO: don't check this in!
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700240// gLogVerbosity.profiler = true; // TODO: don't check this in!
241// gLogVerbosity.signals = true; // TODO: don't check this in!
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800242// gLogVerbosity.startup = true; // TODO: don't check this in!
243// gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
244// gLogVerbosity.threads = true; // TODO: don't check this in!
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700245// gLogVerbosity.verifier = true; // TODO: don't check this in!
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800246
247 method_trace_ = false;
248 method_trace_file_ = "/data/method-trace-file.bin";
249 method_trace_file_size_ = 10 * MB;
250
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800251 profile_clock_source_ = kDefaultProfilerClockSource;
252
Jeff Hao4a200f52014-04-01 14:58:49 -0700253 verify_ = true;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100254 image_isa_ = kRuntimeISA;
Jeff Hao4a200f52014-04-01 14:58:49 -0700255
Dave Allisonb373e092014-02-20 16:06:36 -0800256 // Default to explicit checks. Switch off with -implicit-checks:.
257 // or setprop dalvik.vm.implicit_checks check1,check2,...
258#ifdef HAVE_ANDROID_OS
259 {
260 char buf[PROP_VALUE_MAX];
Dave Allisonad9697a2014-05-09 21:42:36 +0000261 property_get("dalvik.vm.implicit_checks", buf, "none");
Dave Allisonb373e092014-02-20 16:06:36 -0800262 std::string checks(buf);
263 std::vector<std::string> checkvec;
264 Split(checks, ',', checkvec);
Dave Allisondd2e8252014-03-20 14:45:17 -0700265 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
266 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800267 for (auto& str : checkvec) {
268 std::string val = Trim(str);
269 if (val == "none") {
270 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
Dave Allisondd2e8252014-03-20 14:45:17 -0700271 kExplicitStackOverflowCheck;
Dave Allisonb373e092014-02-20 16:06:36 -0800272 } else if (val == "null") {
273 explicit_checks_ &= ~kExplicitNullCheck;
274 } else if (val == "suspend") {
275 explicit_checks_ &= ~kExplicitSuspendCheck;
276 } else if (val == "stack") {
277 explicit_checks_ &= ~kExplicitStackOverflowCheck;
278 } else if (val == "all") {
279 explicit_checks_ = 0;
280 }
281 }
282 }
283#else
284 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
285 kExplicitStackOverflowCheck;
286#endif
287
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800288 for (size_t i = 0; i < options.size(); ++i) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800289 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800290 LOG(INFO) << "option[" << i << "]=" << options[i].first;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800291 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800292 }
293 for (size_t i = 0; i < options.size(); ++i) {
294 const std::string option(options[i].first);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800295 if (StartsWith(option, "-help")) {
296 Usage(nullptr);
297 return false;
298 } else if (StartsWith(option, "-showversion")) {
299 UsageMessage(stdout, "ART version %s\n", Runtime::GetVersion());
300 Exit(0);
301 } else if (StartsWith(option, "-Xbootclasspath:")) {
302 boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
303 } else if (option == "-classpath" || option == "-cp") {
304 // TODO: support -Djava.class.path
305 i++;
306 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700307 Usage("Missing required class path value for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800308 return false;
309 }
310 const StringPiece& value = options[i].first;
311 class_path_string_ = value.data();
312 } else if (option == "bootclasspath") {
313 boot_class_path_
314 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
315 } else if (StartsWith(option, "-Ximage:")) {
316 if (!ParseStringAfterChar(option, ':', &image_)) {
317 return false;
318 }
319 } else if (StartsWith(option, "-Xcheck:jni")) {
320 check_jni_ = true;
321 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
322 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
323 // TODO: move parsing logic out of Dbg
324 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
325 if (tail != "help") {
326 UsageMessage(stderr, "Failed to parse JDWP option %s\n", tail.c_str());
327 }
328 Usage("Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
329 "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n");
330 return false;
331 }
332 } else if (StartsWith(option, "-Xms")) {
333 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
334 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700335 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800336 return false;
337 }
338 heap_initial_size_ = size;
339 } else if (StartsWith(option, "-Xmx")) {
340 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
341 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700342 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800343 return false;
344 }
345 heap_maximum_size_ = size;
346 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
347 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
348 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700349 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800350 return false;
351 }
352 heap_growth_limit_ = size;
353 } else if (StartsWith(option, "-XX:HeapMinFree=")) {
354 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMinFree=")).c_str(), 1024);
355 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700356 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800357 return false;
358 }
359 heap_min_free_ = size;
360 } else if (StartsWith(option, "-XX:HeapMaxFree=")) {
361 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMaxFree=")).c_str(), 1024);
362 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700363 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800364 return false;
365 }
366 heap_max_free_ = size;
367 } else if (StartsWith(option, "-XX:HeapTargetUtilization=")) {
368 if (!ParseDouble(option, '=', 0.1, 0.9, &heap_target_utilization_)) {
369 return false;
370 }
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700371 } else if (StartsWith(option, "-XX:ForegroundHeapGrowthMultiplier=")) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700372 if (!ParseDouble(option, '=', 0.1, 10.0, &foreground_heap_growth_multiplier_)) {
Mathieu Chartier2f8da3e2014-04-15 15:37:02 -0700373 return false;
374 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800375 } else if (StartsWith(option, "-XX:ParallelGCThreads=")) {
376 if (!ParseUnsignedInteger(option, '=', &parallel_gc_threads_)) {
377 return false;
378 }
379 } else if (StartsWith(option, "-XX:ConcGCThreads=")) {
380 if (!ParseUnsignedInteger(option, '=', &conc_gc_threads_)) {
381 return false;
382 }
383 } else if (StartsWith(option, "-Xss")) {
384 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
385 if (size == 0) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700386 Usage("Failed to parse memory option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800387 return false;
388 }
389 stack_size_ = size;
390 } else if (StartsWith(option, "-XX:MaxSpinsBeforeThinLockInflation=")) {
391 if (!ParseUnsignedInteger(option, '=', &max_spins_before_thin_lock_inflation_)) {
392 return false;
393 }
394 } else if (StartsWith(option, "-XX:LongPauseLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800395 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800396 if (!ParseUnsignedInteger(option, '=', &value)) {
397 return false;
398 }
399 long_pause_log_threshold_ = MsToNs(value);
400 } else if (StartsWith(option, "-XX:LongGCLogThreshold=")) {
Andreas Gampe39d92182014-03-05 16:46:44 -0800401 unsigned int value;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800402 if (!ParseUnsignedInteger(option, '=', &value)) {
403 return false;
404 }
405 long_gc_log_threshold_ = MsToNs(value);
406 } else if (option == "-XX:DumpGCPerformanceOnShutdown") {
407 dump_gc_performance_on_shutdown_ = true;
408 } else if (option == "-XX:IgnoreMaxFootprint") {
409 ignore_max_footprint_ = true;
410 } else if (option == "-XX:LowMemoryMode") {
411 low_memory_mode_ = true;
412 } else if (option == "-XX:UseTLAB") {
413 use_tlab_ = true;
414 } else if (StartsWith(option, "-D")) {
415 properties_.push_back(option.substr(strlen("-D")));
416 } else if (StartsWith(option, "-Xjnitrace:")) {
417 jni_trace_ = option.substr(strlen("-Xjnitrace:"));
418 } else if (option == "compilercallbacks") {
419 compiler_callbacks_ =
420 reinterpret_cast<CompilerCallbacks*>(const_cast<void*>(options[i].second));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100421 } else if (option == "imageinstructionset") {
422 image_isa_ = GetInstructionSetFromString(
423 reinterpret_cast<const char*>(options[i].second));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800424 } else if (option == "-Xzygote") {
425 is_zygote_ = true;
426 } else if (option == "-Xint") {
427 interpreter_only_ = true;
428 } else if (StartsWith(option, "-Xgc:")) {
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700429 if (!ParseXGcOption(option)) {
430 return false;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800431 }
432 } else if (StartsWith(option, "-XX:BackgroundGC=")) {
433 std::string substring;
434 if (!ParseStringAfterChar(option, '=', &substring)) {
435 return false;
436 }
437 gc::CollectorType collector_type = ParseCollectorType(substring);
438 if (collector_type != gc::kCollectorTypeNone) {
439 background_collector_type_ = collector_type;
440 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700441 Usage("Unknown -XX:BackgroundGC option %s\n", substring.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800442 return false;
443 }
444 } else if (option == "-XX:+DisableExplicitGC") {
445 is_explicit_gc_disabled_ = true;
446 } else if (StartsWith(option, "-verbose:")) {
447 std::vector<std::string> verbose_options;
448 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
449 for (size_t i = 0; i < verbose_options.size(); ++i) {
450 if (verbose_options[i] == "class") {
451 gLogVerbosity.class_linker = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800452 } else if (verbose_options[i] == "compiler") {
453 gLogVerbosity.compiler = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800454 } else if (verbose_options[i] == "gc") {
455 gLogVerbosity.gc = true;
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700456 } else if (verbose_options[i] == "heap") {
457 gLogVerbosity.heap = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800458 } else if (verbose_options[i] == "jdwp") {
459 gLogVerbosity.jdwp = true;
460 } else if (verbose_options[i] == "jni") {
461 gLogVerbosity.jni = true;
462 } else if (verbose_options[i] == "monitor") {
463 gLogVerbosity.monitor = true;
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700464 } else if (verbose_options[i] == "profiler") {
465 gLogVerbosity.profiler = true;
466 } else if (verbose_options[i] == "signals") {
467 gLogVerbosity.signals = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800468 } else if (verbose_options[i] == "startup") {
469 gLogVerbosity.startup = true;
470 } else if (verbose_options[i] == "third-party-jni") {
471 gLogVerbosity.third_party_jni = true;
472 } else if (verbose_options[i] == "threads") {
473 gLogVerbosity.threads = true;
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700474 } else if (verbose_options[i] == "verifier") {
475 gLogVerbosity.verifier = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800476 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700477 Usage("Unknown -verbose option %s\n", verbose_options[i].c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800478 return false;
479 }
480 }
Mingyao Yang42d65c52014-04-18 16:49:39 -0700481 } else if (StartsWith(option, "-verbose-methods:")) {
482 gLogVerbosity.compiler = false;
483 Split(option.substr(strlen("-verbose-methods:")), ',', gVerboseMethods);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800484 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
485 if (!ParseUnsignedInteger(option, ':', &lock_profiling_threshold_)) {
486 return false;
487 }
488 } else if (StartsWith(option, "-Xstacktracefile:")) {
489 if (!ParseStringAfterChar(option, ':', &stack_trace_file_)) {
490 return false;
491 }
492 } else if (option == "sensitiveThread") {
493 const void* hook = options[i].second;
494 hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(hook));
495 } else if (option == "vfprintf") {
496 const void* hook = options[i].second;
497 if (hook == nullptr) {
498 Usage("vfprintf argument was NULL");
499 return false;
500 }
501 hook_vfprintf_ =
502 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(hook));
503 } else if (option == "exit") {
504 const void* hook = options[i].second;
505 if (hook == nullptr) {
506 Usage("exit argument was NULL");
507 return false;
508 }
509 hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(hook));
510 } else if (option == "abort") {
511 const void* hook = options[i].second;
512 if (hook == nullptr) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700513 Usage("abort was NULL\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800514 return false;
515 }
516 hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(hook));
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800517 } else if (option == "-Xmethod-trace") {
518 method_trace_ = true;
519 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
520 method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
521 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
522 if (!ParseUnsignedInteger(option, ':', &method_trace_file_size_)) {
523 return false;
524 }
525 } else if (option == "-Xprofile:threadcpuclock") {
526 Trace::SetDefaultClockSource(kProfilerClockSourceThreadCpu);
527 } else if (option == "-Xprofile:wallclock") {
528 Trace::SetDefaultClockSource(kProfilerClockSourceWall);
529 } else if (option == "-Xprofile:dualclock") {
530 Trace::SetDefaultClockSource(kProfilerClockSourceDual);
Calin Juravlec1b643c2014-05-30 23:44:11 +0100531 } else if (option == "-Xenable-profiler") {
532 profiler_options_.enabled_ = true;
Wei Jin2221e3b2014-05-21 18:35:19 -0700533 } else if (StartsWith(option, "-Xprofile-filename:")) {
Ian Rogersf7fd3cb2014-05-19 22:57:34 -0700534 if (!ParseStringAfterChar(option, ':', &profile_output_filename_)) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800535 return false;
536 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800537 } else if (StartsWith(option, "-Xprofile-period:")) {
Calin Juravlec1b643c2014-05-30 23:44:11 +0100538 if (!ParseUnsignedInteger(option, ':', &profiler_options_.period_s_)) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800539 return false;
540 }
541 } else if (StartsWith(option, "-Xprofile-duration:")) {
Calin Juravlec1b643c2014-05-30 23:44:11 +0100542 if (!ParseUnsignedInteger(option, ':', &profiler_options_.duration_s_)) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800543 return false;
544 }
545 } else if (StartsWith(option, "-Xprofile-interval:")) {
Calin Juravlec1b643c2014-05-30 23:44:11 +0100546 if (!ParseUnsignedInteger(option, ':', &profiler_options_.interval_us_)) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800547 return false;
548 }
549 } else if (StartsWith(option, "-Xprofile-backoff:")) {
Calin Juravlec1b643c2014-05-30 23:44:11 +0100550 if (!ParseDouble(option, ':', 1.0, 10.0, &profiler_options_.backoff_coefficient_)) {
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800551 return false;
552 }
Calin Juravlec1b643c2014-05-30 23:44:11 +0100553 } else if (option == "-Xprofile-start-immediately") {
554 profiler_options_.start_immediately_ = true;
555 } else if (StartsWith(option, "-Xprofile-top-k-threshold:")) {
556 if (!ParseDouble(option, ':', 10.0, 90.0, &profiler_options_.top_k_threshold_)) {
557 return false;
558 }
559 } else if (StartsWith(option, "-Xprofile-top-k-change-threshold:")) {
560 if (!ParseDouble(option, ':', 10.0, 90.0, &profiler_options_.top_k_change_threshold_)) {
561 return false;
562 }
Dave Allisonb373e092014-02-20 16:06:36 -0800563 } else if (StartsWith(option, "-implicit-checks:")) {
564 std::string checks;
565 if (!ParseStringAfterChar(option, ':', &checks)) {
566 return false;
567 }
568 std::vector<std::string> checkvec;
569 Split(checks, ',', checkvec);
570 for (auto& str : checkvec) {
571 std::string val = Trim(str);
572 if (val == "none") {
573 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
574 kExplicitStackOverflowCheck;
575 } else if (val == "null") {
576 explicit_checks_ &= ~kExplicitNullCheck;
577 } else if (val == "suspend") {
578 explicit_checks_ &= ~kExplicitSuspendCheck;
579 } else if (val == "stack") {
580 explicit_checks_ &= ~kExplicitStackOverflowCheck;
581 } else if (val == "all") {
582 explicit_checks_ = 0;
583 } else {
584 return false;
585 }
586 }
587 } else if (StartsWith(option, "-explicit-checks:")) {
588 std::string checks;
589 if (!ParseStringAfterChar(option, ':', &checks)) {
590 return false;
591 }
592 std::vector<std::string> checkvec;
593 Split(checks, ',', checkvec);
594 for (auto& str : checkvec) {
595 std::string val = Trim(str);
596 if (val == "none") {
597 explicit_checks_ = 0;
598 } else if (val == "null") {
599 explicit_checks_ |= kExplicitNullCheck;
600 } else if (val == "suspend") {
601 explicit_checks_ |= kExplicitSuspendCheck;
602 } else if (val == "stack") {
603 explicit_checks_ |= kExplicitStackOverflowCheck;
604 } else if (val == "all") {
605 explicit_checks_ = kExplicitNullCheck | kExplicitSuspendCheck |
606 kExplicitStackOverflowCheck;
607 } else {
608 return false;
609 }
610 }
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700611 } else if (StartsWith(option, "-Xcompiler:")) {
612 if (!ParseStringAfterChar(option, ':', &compiler_executable_)) {
613 return false;
614 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800615 } else if (option == "-Xcompiler-option") {
616 i++;
617 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700618 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800619 return false;
620 }
621 compiler_options_.push_back(options[i].first);
622 } else if (option == "-Ximage-compiler-option") {
623 i++;
624 if (i == options.size()) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700625 Usage("Missing required compiler option for %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800626 return false;
627 }
628 image_compiler_options_.push_back(options[i].first);
Jeff Hao4a200f52014-04-01 14:58:49 -0700629 } else if (StartsWith(option, "-Xverify:")) {
630 std::string verify_mode = option.substr(strlen("-Xverify:"));
631 if (verify_mode == "none") {
632 verify_ = false;
633 } else if (verify_mode == "remote" || verify_mode == "all") {
634 verify_ = true;
635 } else {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700636 Usage("Unknown -Xverify option %s\n", verify_mode.c_str());
Jeff Hao4a200f52014-04-01 14:58:49 -0700637 return false;
638 }
Yevgeny Roubana6119a22014-03-24 11:31:24 +0700639 } else if (StartsWith(option, "-ea") ||
640 StartsWith(option, "-da") ||
641 StartsWith(option, "-enableassertions") ||
642 StartsWith(option, "-disableassertions") ||
Dave Allisonb373e092014-02-20 16:06:36 -0800643 (option == "--runtime-arg") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800644 (option == "-esa") ||
645 (option == "-dsa") ||
646 (option == "-enablesystemassertions") ||
647 (option == "-disablesystemassertions") ||
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800648 (option == "-Xrs") ||
649 StartsWith(option, "-Xint:") ||
650 StartsWith(option, "-Xdexopt:") ||
651 (option == "-Xnoquithandler") ||
652 StartsWith(option, "-Xjniopts:") ||
653 StartsWith(option, "-Xjnigreflimit:") ||
654 (option == "-Xgenregmap") ||
655 (option == "-Xnogenregmap") ||
656 StartsWith(option, "-Xverifyopt:") ||
657 (option == "-Xcheckdexsum") ||
658 (option == "-Xincludeselectedop") ||
659 StartsWith(option, "-Xjitop:") ||
660 (option == "-Xincludeselectedmethod") ||
661 StartsWith(option, "-Xjitthreshold:") ||
662 StartsWith(option, "-Xjitcodecachesize:") ||
663 (option == "-Xjitblocking") ||
664 StartsWith(option, "-Xjitmethod:") ||
665 StartsWith(option, "-Xjitclass:") ||
666 StartsWith(option, "-Xjitoffset:") ||
667 StartsWith(option, "-Xjitconfig:") ||
668 (option == "-Xjitcheckcg") ||
669 (option == "-Xjitverbose") ||
670 (option == "-Xjitprofile") ||
671 (option == "-Xjitdisableopt") ||
672 (option == "-Xjitsuspendpoll") ||
673 StartsWith(option, "-XX:mainThreadStackSize=")) {
674 // Ignored for backwards compatibility.
675 } else if (!ignore_unrecognized) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700676 Usage("Unrecognized option %s\n", option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800677 return false;
678 }
679 }
680
681 // If a reference to the dalvik core.jar snuck in, replace it with
682 // the art specific version. This can happen with on device
683 // boot.art/boot.oat generation by GenerateImage which relies on the
684 // value of BOOTCLASSPATH.
Kenny Rootd5185342014-05-13 14:47:05 -0700685#if defined(ART_TARGET)
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800686 std::string core_jar("/core.jar");
Kenny Rootd5185342014-05-13 14:47:05 -0700687 std::string core_libart_jar("/core-libart.jar");
688#else
689 // The host uses hostdex files.
690 std::string core_jar("/core-hostdex.jar");
691 std::string core_libart_jar("/core-libart-hostdex.jar");
692#endif
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800693 size_t core_jar_pos = boot_class_path_string_.find(core_jar);
694 if (core_jar_pos != std::string::npos) {
Kenny Rootd5185342014-05-13 14:47:05 -0700695 boot_class_path_string_.replace(core_jar_pos, core_jar.size(), core_libart_jar);
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800696 }
697
698 if (compiler_callbacks_ == nullptr && image_.empty()) {
699 image_ += GetAndroidRoot();
Brian Carlstrom3ac05bb2014-05-13 19:31:38 -0700700 image_ += "/framework/boot.art";
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800701 }
702 if (heap_growth_limit_ == 0) {
703 heap_growth_limit_ = heap_maximum_size_;
704 }
705 if (background_collector_type_ == gc::kCollectorTypeNone) {
706 background_collector_type_ = collector_type_;
707 }
708 return true;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100709} // NOLINT(readability/fn_size)
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800710
711void ParsedOptions::Exit(int status) {
712 hook_exit_(status);
713}
714
715void ParsedOptions::Abort() {
716 hook_abort_();
717}
718
719void ParsedOptions::UsageMessageV(FILE* stream, const char* fmt, va_list ap) {
720 hook_vfprintf_(stderr, fmt, ap);
721}
722
723void ParsedOptions::UsageMessage(FILE* stream, const char* fmt, ...) {
724 va_list ap;
725 va_start(ap, fmt);
726 UsageMessageV(stream, fmt, ap);
727 va_end(ap);
728}
729
730void ParsedOptions::Usage(const char* fmt, ...) {
731 bool error = (fmt != nullptr);
732 FILE* stream = error ? stderr : stdout;
733
734 if (fmt != nullptr) {
735 va_list ap;
736 va_start(ap, fmt);
737 UsageMessageV(stream, fmt, ap);
738 va_end(ap);
739 }
740
741 const char* program = "dalvikvm";
742 UsageMessage(stream, "%s: [options] class [argument ...]\n", program);
743 UsageMessage(stream, "\n");
744 UsageMessage(stream, "The following standard options are supported:\n");
745 UsageMessage(stream, " -classpath classpath (-cp classpath)\n");
746 UsageMessage(stream, " -Dproperty=value\n");
747 UsageMessage(stream, " -verbose:tag ('gc', 'jni', or 'class')\n");
748 UsageMessage(stream, " -showversion\n");
749 UsageMessage(stream, " -help\n");
750 UsageMessage(stream, " -agentlib:jdwp=options\n");
751 UsageMessage(stream, "\n");
752
753 UsageMessage(stream, "The following extended options are supported:\n");
754 UsageMessage(stream, " -Xrunjdwp:<options>\n");
755 UsageMessage(stream, " -Xbootclasspath:bootclasspath\n");
756 UsageMessage(stream, " -Xcheck:tag (e.g. 'jni')\n");
757 UsageMessage(stream, " -XmsN (min heap, must be multiple of 1K, >= 1MB)\n");
758 UsageMessage(stream, " -XmxN (max heap, must be multiple of 1K, >= 2MB)\n");
759 UsageMessage(stream, " -XssN (stack size)\n");
760 UsageMessage(stream, " -Xint\n");
761 UsageMessage(stream, "\n");
762
763 UsageMessage(stream, "The following Dalvik options are supported:\n");
764 UsageMessage(stream, " -Xzygote\n");
765 UsageMessage(stream, " -Xjnitrace:substring (eg NativeClass or nativeMethod)\n");
766 UsageMessage(stream, " -Xstacktracefile:<filename>\n");
767 UsageMessage(stream, " -Xgc:[no]preverify\n");
768 UsageMessage(stream, " -Xgc:[no]postverify\n");
769 UsageMessage(stream, " -XX:+DisableExplicitGC\n");
770 UsageMessage(stream, " -XX:HeapGrowthLimit=N\n");
771 UsageMessage(stream, " -XX:HeapMinFree=N\n");
772 UsageMessage(stream, " -XX:HeapMaxFree=N\n");
773 UsageMessage(stream, " -XX:HeapTargetUtilization=doublevalue\n");
Mathieu Chartier455820e2014-04-18 12:02:39 -0700774 UsageMessage(stream, " -XX:ForegroundHeapGrowthMultiplier=doublevalue\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800775 UsageMessage(stream, " -XX:LowMemoryMode\n");
776 UsageMessage(stream, " -Xprofile:{threadcpuclock,wallclock,dualclock}\n");
777 UsageMessage(stream, "\n");
778
779 UsageMessage(stream, "The following unique to ART options are supported:\n");
780 UsageMessage(stream, " -Xgc:[no]preverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700781 UsageMessage(stream, " -Xgc:[no]postsweepingverify_rosalloc\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800782 UsageMessage(stream, " -Xgc:[no]postverify_rosalloc\n");
Mathieu Chartier6f365cc2014-04-23 12:42:27 -0700783 UsageMessage(stream, " -Xgc:[no]presweepingverify\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800784 UsageMessage(stream, " -Ximage:filename\n");
785 UsageMessage(stream, " -XX:ParallelGCThreads=integervalue\n");
786 UsageMessage(stream, " -XX:ConcGCThreads=integervalue\n");
787 UsageMessage(stream, " -XX:MaxSpinsBeforeThinLockInflation=integervalue\n");
788 UsageMessage(stream, " -XX:LongPauseLogThreshold=integervalue\n");
789 UsageMessage(stream, " -XX:LongGCLogThreshold=integervalue\n");
790 UsageMessage(stream, " -XX:DumpGCPerformanceOnShutdown\n");
791 UsageMessage(stream, " -XX:IgnoreMaxFootprint\n");
792 UsageMessage(stream, " -XX:UseTLAB\n");
793 UsageMessage(stream, " -XX:BackgroundGC=none\n");
794 UsageMessage(stream, " -Xmethod-trace\n");
795 UsageMessage(stream, " -Xmethod-trace-file:filename");
796 UsageMessage(stream, " -Xmethod-trace-file-size:integervalue\n");
Calin Juravlec1b643c2014-05-30 23:44:11 +0100797 UsageMessage(stream, " -Xenable-profiler\n");
Wei Jin2221e3b2014-05-21 18:35:19 -0700798 UsageMessage(stream, " -Xprofile-filename:filename\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800799 UsageMessage(stream, " -Xprofile-period:integervalue\n");
800 UsageMessage(stream, " -Xprofile-duration:integervalue\n");
801 UsageMessage(stream, " -Xprofile-interval:integervalue\n");
Calin Juravle54c73ca2014-05-22 12:13:54 +0100802 UsageMessage(stream, " -Xprofile-backoff:doublevalue\n");
Calin Juravlec1b643c2014-05-30 23:44:11 +0100803 UsageMessage(stream, " -Xprofile-start-immediately\n");
804 UsageMessage(stream, " -Xprofile-top-k-threshold:doublevalue\n");
805 UsageMessage(stream, " -Xprofile-top-k-change-threshold:doublevalue\n");
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700806 UsageMessage(stream, " -Xcompiler:filename\n");
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800807 UsageMessage(stream, " -Xcompiler-option dex2oat-option\n");
808 UsageMessage(stream, " -Ximage-compiler-option dex2oat-option\n");
809 UsageMessage(stream, "\n");
810
811 UsageMessage(stream, "The following previously supported Dalvik options are ignored:\n");
812 UsageMessage(stream, " -ea[:<package name>... |:<class name>]\n");
813 UsageMessage(stream, " -da[:<package name>... |:<class name>]\n");
814 UsageMessage(stream, " (-enableassertions, -disableassertions)\n");
815 UsageMessage(stream, " -esa\n");
816 UsageMessage(stream, " -dsa\n");
817 UsageMessage(stream, " (-enablesystemassertions, -disablesystemassertions)\n");
818 UsageMessage(stream, " -Xverify:{none,remote,all}\n");
819 UsageMessage(stream, " -Xrs\n");
820 UsageMessage(stream, " -Xint:portable, -Xint:fast, -Xint:jit\n");
821 UsageMessage(stream, " -Xdexopt:{none,verified,all,full}\n");
822 UsageMessage(stream, " -Xnoquithandler\n");
823 UsageMessage(stream, " -Xjniopts:{warnonly,forcecopy}\n");
824 UsageMessage(stream, " -Xjnigreflimit:integervalue\n");
825 UsageMessage(stream, " -Xgc:[no]precise\n");
826 UsageMessage(stream, " -Xgc:[no]verifycardtable\n");
827 UsageMessage(stream, " -X[no]genregmap\n");
828 UsageMessage(stream, " -Xverifyopt:[no]checkmon\n");
829 UsageMessage(stream, " -Xcheckdexsum\n");
830 UsageMessage(stream, " -Xincludeselectedop\n");
831 UsageMessage(stream, " -Xjitop:hexopvalue[-endvalue][,hexopvalue[-endvalue]]*\n");
832 UsageMessage(stream, " -Xincludeselectedmethod\n");
833 UsageMessage(stream, " -Xjitthreshold:integervalue\n");
834 UsageMessage(stream, " -Xjitcodecachesize:decimalvalueofkbytes\n");
835 UsageMessage(stream, " -Xjitblocking\n");
836 UsageMessage(stream, " -Xjitmethod:signature[,signature]* (eg Ljava/lang/String\\;replace)\n");
837 UsageMessage(stream, " -Xjitclass:classname[,classname]*\n");
838 UsageMessage(stream, " -Xjitoffset:offset[,offset]\n");
839 UsageMessage(stream, " -Xjitconfig:filename\n");
840 UsageMessage(stream, " -Xjitcheckcg\n");
841 UsageMessage(stream, " -Xjitverbose\n");
842 UsageMessage(stream, " -Xjitprofile\n");
843 UsageMessage(stream, " -Xjitdisableopt\n");
844 UsageMessage(stream, " -Xjitsuspendpoll\n");
845 UsageMessage(stream, " -XX:mainThreadStackSize=N\n");
846 UsageMessage(stream, "\n");
847
848 Exit((error) ? 1 : 0);
849}
850
851bool ParsedOptions::ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) {
852 std::string::size_type colon = s.find(c);
853 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700854 Usage("Missing char %c in option %s\n", c, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800855 return false;
856 }
857 // Add one to remove the char we were trimming until.
858 *parsed_value = s.substr(colon + 1);
859 return true;
860}
861
862bool ParsedOptions::ParseInteger(const std::string& s, char after_char, int* parsed_value) {
863 std::string::size_type colon = s.find(after_char);
864 if (colon == std::string::npos) {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700865 Usage("Missing char %c in option %s\n", after_char, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800866 return false;
867 }
868 const char* begin = &s[colon + 1];
869 char* end;
870 size_t result = strtoul(begin, &end, 10);
871 if (begin == end || *end != '\0') {
Brian Carlstrom4ad33b32014-04-18 14:18:41 -0700872 Usage("Failed to parse integer from %s\n", s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800873 return false;
874 }
875 *parsed_value = result;
876 return true;
877}
878
879bool ParsedOptions::ParseUnsignedInteger(const std::string& s, char after_char,
880 unsigned int* parsed_value) {
881 int i;
882 if (!ParseInteger(s, after_char, &i)) {
883 return false;
884 }
885 if (i < 0) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700886 Usage("Negative value %d passed for unsigned option %s\n", i, s.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800887 return false;
888 }
889 *parsed_value = i;
890 return true;
891}
892
893bool ParsedOptions::ParseDouble(const std::string& option, char after_char,
894 double min, double max, double* parsed_value) {
895 std::string substring;
896 if (!ParseStringAfterChar(option, after_char, &substring)) {
897 return false;
898 }
Dave Allison999385c2014-05-20 15:16:02 -0700899 bool sane_val = true;
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800900 double value;
Dave Allison999385c2014-05-20 15:16:02 -0700901 if (false) {
902 // TODO: this doesn't seem to work on the emulator. b/15114595
903 std::stringstream iss(substring);
904 iss >> value;
905 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
906 sane_val = iss.eof() && (value >= min) && (value <= max);
907 } else {
908 char* end = nullptr;
909 value = strtod(substring.c_str(), &end);
910 sane_val = *end == '\0' && value >= min && value <= max;
911 }
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800912 if (!sane_val) {
Mathieu Chartier455820e2014-04-18 12:02:39 -0700913 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
Brian Carlstrom491ca9e2014-03-02 18:24:38 -0800914 return false;
915 }
916 *parsed_value = value;
917 return true;
918}
919
920} // namespace art