1 /*
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"
18
19 #ifdef HAVE_ANDROID_OS
20 #include "cutils/properties.h"
21 #endif
22
23 #include "base/stringpiece.h"
24 #include "debugger.h"
25 #include "gc/heap.h"
26 #include "monitor.h"
27 #include "runtime.h"
28 #include "trace.h"
29 #include "utils.h"
30
31 namespace art {
32
Create(const RuntimeOptions & options,bool ignore_unrecognized)33 ParsedOptions* ParsedOptions::Create(const RuntimeOptions& options, bool ignore_unrecognized) {
34 std::unique_ptr<ParsedOptions> parsed(new ParsedOptions());
35 if (parsed->Parse(options, ignore_unrecognized)) {
36 return parsed.release();
37 }
38 return nullptr;
39 }
40
41 // Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
42 // memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
43 // [gG] gigabytes.
44 //
45 // "s" should point just past the "-Xm?" part of the string.
46 // "div" specifies a divisor, e.g. 1024 if the value must be a multiple
47 // of 1024.
48 //
49 // The spec says the -Xmx and -Xms options must be multiples of 1024. It
50 // doesn't say anything about -Xss.
51 //
52 // Returns 0 (a useless size) if "s" is malformed or specifies a low or
53 // non-evenly-divisible value.
54 //
ParseMemoryOption(const char * s,size_t div)55 size_t ParseMemoryOption(const char* s, size_t div) {
56 // strtoul accepts a leading [+-], which we don't want,
57 // so make sure our string starts with a decimal digit.
58 if (isdigit(*s)) {
59 char* s2;
60 size_t val = strtoul(s, &s2, 10);
61 if (s2 != s) {
62 // s2 should be pointing just after the number.
63 // If this is the end of the string, the user
64 // has specified a number of bytes. Otherwise,
65 // there should be exactly one more character
66 // that specifies a multiplier.
67 if (*s2 != '\0') {
68 // The remainder of the string is either a single multiplier
69 // character, or nothing to indicate that the value is in
70 // bytes.
71 char c = *s2++;
72 if (*s2 == '\0') {
73 size_t mul;
74 if (c == '\0') {
75 mul = 1;
76 } else if (c == 'k' || c == 'K') {
77 mul = KB;
78 } else if (c == 'm' || c == 'M') {
79 mul = MB;
80 } else if (c == 'g' || c == 'G') {
81 mul = GB;
82 } else {
83 // Unknown multiplier character.
84 return 0;
85 }
86
87 if (val <= std::numeric_limits<size_t>::max() / mul) {
88 val *= mul;
89 } else {
90 // Clamp to a multiple of 1024.
91 val = std::numeric_limits<size_t>::max() & ~(1024-1);
92 }
93 } else {
94 // There's more than one character after the numeric part.
95 return 0;
96 }
97 }
98 // The man page says that a -Xm value must be a multiple of 1024.
99 if (val % div == 0) {
100 return val;
101 }
102 }
103 }
104 return 0;
105 }
106
ParseCollectorType(const std::string & option)107 static gc::CollectorType ParseCollectorType(const std::string& option) {
108 if (option == "MS" || option == "nonconcurrent") {
109 return gc::kCollectorTypeMS;
110 } else if (option == "CMS" || option == "concurrent") {
111 return gc::kCollectorTypeCMS;
112 } else if (option == "SS") {
113 return gc::kCollectorTypeSS;
114 } else if (option == "GSS") {
115 return gc::kCollectorTypeGSS;
116 } else if (option == "CC") {
117 return gc::kCollectorTypeCC;
118 } else if (option == "MC") {
119 return gc::kCollectorTypeMC;
120 } else {
121 return gc::kCollectorTypeNone;
122 }
123 }
124
ParseXGcOption(const std::string & option)125 bool ParsedOptions::ParseXGcOption(const std::string& option) {
126 std::vector<std::string> gc_options;
127 Split(option.substr(strlen("-Xgc:")), ',', gc_options);
128 for (const std::string& gc_option : gc_options) {
129 gc::CollectorType collector_type = ParseCollectorType(gc_option);
130 if (collector_type != gc::kCollectorTypeNone) {
131 collector_type_ = collector_type;
132 } else if (gc_option == "preverify") {
133 verify_pre_gc_heap_ = true;
134 } else if (gc_option == "nopreverify") {
135 verify_pre_gc_heap_ = false;
136 } else if (gc_option == "presweepingverify") {
137 verify_pre_sweeping_heap_ = true;
138 } else if (gc_option == "nopresweepingverify") {
139 verify_pre_sweeping_heap_ = false;
140 } else if (gc_option == "postverify") {
141 verify_post_gc_heap_ = true;
142 } else if (gc_option == "nopostverify") {
143 verify_post_gc_heap_ = false;
144 } else if (gc_option == "preverify_rosalloc") {
145 verify_pre_gc_rosalloc_ = true;
146 } else if (gc_option == "nopreverify_rosalloc") {
147 verify_pre_gc_rosalloc_ = false;
148 } else if (gc_option == "presweepingverify_rosalloc") {
149 verify_pre_sweeping_rosalloc_ = true;
150 } else if (gc_option == "nopresweepingverify_rosalloc") {
151 verify_pre_sweeping_rosalloc_ = false;
152 } else if (gc_option == "postverify_rosalloc") {
153 verify_post_gc_rosalloc_ = true;
154 } else if (gc_option == "nopostverify_rosalloc") {
155 verify_post_gc_rosalloc_ = false;
156 } else if ((gc_option == "precise") ||
157 (gc_option == "noprecise") ||
158 (gc_option == "verifycardtable") ||
159 (gc_option == "noverifycardtable")) {
160 // Ignored for backwards compatibility.
161 } else {
162 Usage("Unknown -Xgc option %s\n", gc_option.c_str());
163 return false;
164 }
165 }
166 return true;
167 }
168
Parse(const RuntimeOptions & options,bool ignore_unrecognized)169 bool ParsedOptions::Parse(const RuntimeOptions& options, bool ignore_unrecognized) {
170 const char* boot_class_path_string = getenv("BOOTCLASSPATH");
171 if (boot_class_path_string != NULL) {
172 boot_class_path_string_ = boot_class_path_string;
173 }
174 const char* class_path_string = getenv("CLASSPATH");
175 if (class_path_string != NULL) {
176 class_path_string_ = class_path_string;
177 }
178 // -Xcheck:jni is off by default for regular builds but on by default in debug builds.
179 check_jni_ = kIsDebugBuild;
180
181 heap_initial_size_ = gc::Heap::kDefaultInitialSize;
182 heap_maximum_size_ = gc::Heap::kDefaultMaximumSize;
183 heap_min_free_ = gc::Heap::kDefaultMinFree;
184 heap_max_free_ = gc::Heap::kDefaultMaxFree;
185 heap_non_moving_space_capacity_ = gc::Heap::kDefaultNonMovingSpaceCapacity;
186 heap_target_utilization_ = gc::Heap::kDefaultTargetUtilization;
187 foreground_heap_growth_multiplier_ = gc::Heap::kDefaultHeapGrowthMultiplier;
188 heap_growth_limit_ = 0; // 0 means no growth limit .
189 // Default to number of processors minus one since the main GC thread also does work.
190 parallel_gc_threads_ = sysconf(_SC_NPROCESSORS_CONF) - 1;
191 // Only the main GC thread, no workers.
192 conc_gc_threads_ = 0;
193 // The default GC type is set in makefiles.
194 #if ART_DEFAULT_GC_TYPE_IS_CMS
195 collector_type_ = gc::kCollectorTypeCMS;
196 #elif ART_DEFAULT_GC_TYPE_IS_SS
197 collector_type_ = gc::kCollectorTypeSS;
198 #elif ART_DEFAULT_GC_TYPE_IS_GSS
199 collector_type_ = gc::kCollectorTypeGSS;
200 #else
201 #error "ART default GC type must be set"
202 #endif
203 // Enable hspace compaction on OOM by default.
204 use_homogeneous_space_compaction_for_oom_ = true;
205 // If background_collector_type_ is kCollectorTypeNone, it defaults to the collector_type_ after
206 // parsing options. If you set this to kCollectorTypeHSpaceCompact then we will do an hspace
207 // compaction when we transition to background instead of a normal collector transition.
208 background_collector_type_ = gc::kCollectorTypeNone;
209 #ifdef ART_USE_HSPACE_COMPACT
210 background_collector_type_ = gc::kCollectorTypeHomogeneousSpaceCompact;
211 #endif
212 #ifdef ART_USE_BACKGROUND_COMPACT
213 background_collector_type_ = gc::kCollectorTypeSS;
214 #endif
215 stack_size_ = 0; // 0 means default.
216 max_spins_before_thin_lock_inflation_ = Monitor::kDefaultMaxSpinsBeforeThinLockInflation;
217 low_memory_mode_ = false;
218 use_tlab_ = false;
219 min_interval_homogeneous_space_compaction_by_oom_ = MsToNs(100 * 1000); // 100s.
220 verify_pre_gc_heap_ = false;
221 // Pre sweeping is the one that usually fails if the GC corrupted the heap.
222 verify_pre_sweeping_heap_ = kIsDebugBuild;
223 verify_post_gc_heap_ = false;
224 verify_pre_gc_rosalloc_ = kIsDebugBuild;
225 verify_pre_sweeping_rosalloc_ = false;
226 verify_post_gc_rosalloc_ = false;
227
228 compiler_callbacks_ = nullptr;
229 is_zygote_ = false;
230 must_relocate_ = kDefaultMustRelocate;
231 dex2oat_enabled_ = true;
232 image_dex2oat_enabled_ = true;
233 if (kPoisonHeapReferences) {
234 // kPoisonHeapReferences currently works only with the interpreter only.
235 // TODO: make it work with the compiler.
236 interpreter_only_ = true;
237 } else {
238 interpreter_only_ = false;
239 }
240 is_explicit_gc_disabled_ = false;
241
242 long_pause_log_threshold_ = gc::Heap::kDefaultLongPauseLogThreshold;
243 long_gc_log_threshold_ = gc::Heap::kDefaultLongGCLogThreshold;
244 dump_gc_performance_on_shutdown_ = false;
245 ignore_max_footprint_ = false;
246
247 lock_profiling_threshold_ = 0;
248 hook_is_sensitive_thread_ = NULL;
249
250 hook_vfprintf_ = vfprintf;
251 hook_exit_ = exit;
252 hook_abort_ = NULL; // We don't call abort(3) by default; see Runtime::Abort.
253
254 // gLogVerbosity.class_linker = true; // TODO: don't check this in!
255 // gLogVerbosity.compiler = true; // TODO: don't check this in!
256 // gLogVerbosity.gc = true; // TODO: don't check this in!
257 // gLogVerbosity.heap = true; // TODO: don't check this in!
258 // gLogVerbosity.jdwp = true; // TODO: don't check this in!
259 // gLogVerbosity.jni = true; // TODO: don't check this in!
260 // gLogVerbosity.monitor = true; // TODO: don't check this in!
261 // gLogVerbosity.profiler = true; // TODO: don't check this in!
262 // gLogVerbosity.signals = true; // TODO: don't check this in!
263 // gLogVerbosity.startup = true; // TODO: don't check this in!
264 // gLogVerbosity.third_party_jni = true; // TODO: don't check this in!
265 // gLogVerbosity.threads = true; // TODO: don't check this in!
266 // gLogVerbosity.verifier = true; // TODO: don't check this in!
267
268 method_trace_ = false;
269 method_trace_file_ = "/data/method-trace-file.bin";
270 method_trace_file_size_ = 10 * MB;
271
272 profile_clock_source_ = kDefaultTraceClockSource;
273
274 verify_ = true;
275 image_isa_ = kRuntimeISA;
276
277 for (size_t i = 0; i < options.size(); ++i) {
278 if (true && options[0].first == "-Xzygote") {
279 LOG(INFO) << "option[" << i << "]=" << options[i].first;
280 }
281 }
282 for (size_t i = 0; i < options.size(); ++i) {
283 const std::string option(options[i].first);
284 if (StartsWith(option, "-help")) {
285 Usage(nullptr);
286 return false;
287 } else if (StartsWith(option, "-showversion")) {
288 UsageMessage(stdout, "ART version %s\n", Runtime::GetVersion());
289 Exit(0);
290 } else if (StartsWith(option, "-Xbootclasspath:")) {
291 boot_class_path_string_ = option.substr(strlen("-Xbootclasspath:")).data();
292 LOG(INFO) << "setting boot class path to " << boot_class_path_string_;
293 } else if (option == "-classpath" || option == "-cp") {
294 // TODO: support -Djava.class.path
295 i++;
296 if (i == options.size()) {
297 Usage("Missing required class path value for %s\n", option.c_str());
298 return false;
299 }
300 const StringPiece& value = options[i].first;
301 class_path_string_ = value.data();
302 } else if (option == "bootclasspath") {
303 boot_class_path_
304 = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
305 } else if (StartsWith(option, "-Ximage:")) {
306 if (!ParseStringAfterChar(option, ':', &image_)) {
307 return false;
308 }
309 } else if (StartsWith(option, "-Xcheck:jni")) {
310 check_jni_ = true;
311 } else if (StartsWith(option, "-Xrunjdwp:") || StartsWith(option, "-agentlib:jdwp=")) {
312 std::string tail(option.substr(option[1] == 'X' ? 10 : 15));
313 // TODO: move parsing logic out of Dbg
314 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
315 if (tail != "help") {
316 UsageMessage(stderr, "Failed to parse JDWP option %s\n", tail.c_str());
317 }
318 Usage("Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
319 "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n");
320 return false;
321 }
322 } else if (StartsWith(option, "-Xms")) {
323 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).c_str(), 1024);
324 if (size == 0) {
325 Usage("Failed to parse memory option %s\n", option.c_str());
326 return false;
327 }
328 heap_initial_size_ = size;
329 } else if (StartsWith(option, "-Xmx")) {
330 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).c_str(), 1024);
331 if (size == 0) {
332 Usage("Failed to parse memory option %s\n", option.c_str());
333 return false;
334 }
335 heap_maximum_size_ = size;
336 } else if (StartsWith(option, "-XX:HeapGrowthLimit=")) {
337 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapGrowthLimit=")).c_str(), 1024);
338 if (size == 0) {
339 Usage("Failed to parse memory option %s\n", option.c_str());
340 return false;
341 }
342 heap_growth_limit_ = size;
343 } else if (StartsWith(option, "-XX:HeapMinFree=")) {
344 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMinFree=")).c_str(), 1024);
345 if (size == 0) {
346 Usage("Failed to parse memory option %s\n", option.c_str());
347 return false;
348 }
349 heap_min_free_ = size;
350 } else if (StartsWith(option, "-XX:HeapMaxFree=")) {
351 size_t size = ParseMemoryOption(option.substr(strlen("-XX:HeapMaxFree=")).c_str(), 1024);
352 if (size == 0) {
353 Usage("Failed to parse memory option %s\n", option.c_str());
354 return false;
355 }
356 heap_max_free_ = size;
357 } else if (StartsWith(option, "-XX:NonMovingSpaceCapacity=")) {
358 size_t size = ParseMemoryOption(
359 option.substr(strlen("-XX:NonMovingSpaceCapacity=")).c_str(), 1024);
360 if (size == 0) {
361 Usage("Failed to parse memory option %s\n", option.c_str());
362 return false;
363 }
364 heap_non_moving_space_capacity_ = size;
365 } else if (StartsWith(option, "-XX:HeapTargetUtilization=")) {
366 if (!ParseDouble(option, '=', 0.1, 0.9, &heap_target_utilization_)) {
367 return false;
368 }
369 } else if (StartsWith(option, "-XX:ForegroundHeapGrowthMultiplier=")) {
370 if (!ParseDouble(option, '=', 0.1, 10.0, &foreground_heap_growth_multiplier_)) {
371 return false;
372 }
373 } else if (StartsWith(option, "-XX:ParallelGCThreads=")) {
374 if (!ParseUnsignedInteger(option, '=', ¶llel_gc_threads_)) {
375 return false;
376 }
377 } else if (StartsWith(option, "-XX:ConcGCThreads=")) {
378 if (!ParseUnsignedInteger(option, '=', &conc_gc_threads_)) {
379 return false;
380 }
381 } else if (StartsWith(option, "-Xss")) {
382 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).c_str(), 1);
383 if (size == 0) {
384 Usage("Failed to parse memory option %s\n", option.c_str());
385 return false;
386 }
387 stack_size_ = size;
388 } else if (StartsWith(option, "-XX:MaxSpinsBeforeThinLockInflation=")) {
389 if (!ParseUnsignedInteger(option, '=', &max_spins_before_thin_lock_inflation_)) {
390 return false;
391 }
392 } else if (StartsWith(option, "-XX:LongPauseLogThreshold=")) {
393 unsigned int value;
394 if (!ParseUnsignedInteger(option, '=', &value)) {
395 return false;
396 }
397 long_pause_log_threshold_ = MsToNs(value);
398 } else if (StartsWith(option, "-XX:LongGCLogThreshold=")) {
399 unsigned int value;
400 if (!ParseUnsignedInteger(option, '=', &value)) {
401 return false;
402 }
403 long_gc_log_threshold_ = MsToNs(value);
404 } else if (option == "-XX:DumpGCPerformanceOnShutdown") {
405 dump_gc_performance_on_shutdown_ = true;
406 } else if (option == "-XX:IgnoreMaxFootprint") {
407 ignore_max_footprint_ = true;
408 } else if (option == "-XX:LowMemoryMode") {
409 low_memory_mode_ = true;
410 // TODO Might want to turn off must_relocate here.
411 } else if (option == "-XX:UseTLAB") {
412 use_tlab_ = true;
413 } else if (option == "-XX:EnableHSpaceCompactForOOM") {
414 use_homogeneous_space_compaction_for_oom_ = true;
415 } else if (option == "-XX:DisableHSpaceCompactForOOM") {
416 use_homogeneous_space_compaction_for_oom_ = false;
417 } else if (StartsWith(option, "-D")) {
418 properties_.push_back(option.substr(strlen("-D")));
419 } else if (StartsWith(option, "-Xjnitrace:")) {
420 jni_trace_ = option.substr(strlen("-Xjnitrace:"));
421 } else if (option == "compilercallbacks") {
422 compiler_callbacks_ =
423 reinterpret_cast<CompilerCallbacks*>(const_cast<void*>(options[i].second));
424 } else if (option == "imageinstructionset") {
425 const char* isa_str = reinterpret_cast<const char*>(options[i].second);
426 image_isa_ = GetInstructionSetFromString(isa_str);
427 if (image_isa_ == kNone) {
428 Usage("%s is not a valid instruction set.", isa_str);
429 return false;
430 }
431 } else if (option == "-Xzygote") {
432 is_zygote_ = true;
433 } else if (StartsWith(option, "-Xpatchoat:")) {
434 if (!ParseStringAfterChar(option, ':', &patchoat_executable_)) {
435 return false;
436 }
437 } else if (option == "-Xrelocate") {
438 must_relocate_ = true;
439 } else if (option == "-Xnorelocate") {
440 must_relocate_ = false;
441 } else if (option == "-Xnodex2oat") {
442 dex2oat_enabled_ = false;
443 } else if (option == "-Xdex2oat") {
444 dex2oat_enabled_ = true;
445 } else if (option == "-Xnoimage-dex2oat") {
446 image_dex2oat_enabled_ = false;
447 } else if (option == "-Ximage-dex2oat") {
448 image_dex2oat_enabled_ = true;
449 } else if (option == "-Xint") {
450 interpreter_only_ = true;
451 } else if (StartsWith(option, "-Xgc:")) {
452 if (!ParseXGcOption(option)) {
453 return false;
454 }
455 } else if (StartsWith(option, "-XX:BackgroundGC=")) {
456 std::string substring;
457 if (!ParseStringAfterChar(option, '=', &substring)) {
458 return false;
459 }
460 // Special handling for HSpaceCompact since this is only valid as a background GC type.
461 if (substring == "HSpaceCompact") {
462 background_collector_type_ = gc::kCollectorTypeHomogeneousSpaceCompact;
463 } else {
464 gc::CollectorType collector_type = ParseCollectorType(substring);
465 if (collector_type != gc::kCollectorTypeNone) {
466 background_collector_type_ = collector_type;
467 } else {
468 Usage("Unknown -XX:BackgroundGC option %s\n", substring.c_str());
469 return false;
470 }
471 }
472 } else if (option == "-XX:+DisableExplicitGC") {
473 is_explicit_gc_disabled_ = true;
474 } else if (StartsWith(option, "-verbose:")) {
475 std::vector<std::string> verbose_options;
476 Split(option.substr(strlen("-verbose:")), ',', verbose_options);
477 for (size_t i = 0; i < verbose_options.size(); ++i) {
478 if (verbose_options[i] == "class") {
479 gLogVerbosity.class_linker = true;
480 } else if (verbose_options[i] == "compiler") {
481 gLogVerbosity.compiler = true;
482 } else if (verbose_options[i] == "gc") {
483 gLogVerbosity.gc = true;
484 } else if (verbose_options[i] == "heap") {
485 gLogVerbosity.heap = true;
486 } else if (verbose_options[i] == "jdwp") {
487 gLogVerbosity.jdwp = true;
488 } else if (verbose_options[i] == "jni") {
489 gLogVerbosity.jni = true;
490 } else if (verbose_options[i] == "monitor") {
491 gLogVerbosity.monitor = true;
492 } else if (verbose_options[i] == "profiler") {
493 gLogVerbosity.profiler = true;
494 } else if (verbose_options[i] == "signals") {
495 gLogVerbosity.signals = true;
496 } else if (verbose_options[i] == "startup") {
497 gLogVerbosity.startup = true;
498 } else if (verbose_options[i] == "third-party-jni") {
499 gLogVerbosity.third_party_jni = true;
500 } else if (verbose_options[i] == "threads") {
501 gLogVerbosity.threads = true;
502 } else if (verbose_options[i] == "verifier") {
503 gLogVerbosity.verifier = true;
504 } else {
505 Usage("Unknown -verbose option %s\n", verbose_options[i].c_str());
506 return false;
507 }
508 }
509 } else if (StartsWith(option, "-verbose-methods:")) {
510 gLogVerbosity.compiler = false;
511 Split(option.substr(strlen("-verbose-methods:")), ',', gVerboseMethods);
512 } else if (StartsWith(option, "-Xlockprofthreshold:")) {
513 if (!ParseUnsignedInteger(option, ':', &lock_profiling_threshold_)) {
514 return false;
515 }
516 } else if (StartsWith(option, "-Xstacktracefile:")) {
517 if (!ParseStringAfterChar(option, ':', &stack_trace_file_)) {
518 return false;
519 }
520 } else if (option == "sensitiveThread") {
521 const void* hook = options[i].second;
522 hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(const_cast<void*>(hook));
523 } else if (option == "vfprintf") {
524 const void* hook = options[i].second;
525 if (hook == nullptr) {
526 Usage("vfprintf argument was NULL");
527 return false;
528 }
529 hook_vfprintf_ =
530 reinterpret_cast<int (*)(FILE *, const char*, va_list)>(const_cast<void*>(hook));
531 } else if (option == "exit") {
532 const void* hook = options[i].second;
533 if (hook == nullptr) {
534 Usage("exit argument was NULL");
535 return false;
536 }
537 hook_exit_ = reinterpret_cast<void(*)(jint)>(const_cast<void*>(hook));
538 } else if (option == "abort") {
539 const void* hook = options[i].second;
540 if (hook == nullptr) {
541 Usage("abort was NULL\n");
542 return false;
543 }
544 hook_abort_ = reinterpret_cast<void(*)()>(const_cast<void*>(hook));
545 } else if (option == "-Xmethod-trace") {
546 method_trace_ = true;
547 } else if (StartsWith(option, "-Xmethod-trace-file:")) {
548 method_trace_file_ = option.substr(strlen("-Xmethod-trace-file:"));
549 } else if (StartsWith(option, "-Xmethod-trace-file-size:")) {
550 if (!ParseUnsignedInteger(option, ':', &method_trace_file_size_)) {
551 return false;
552 }
553 } else if (option == "-Xprofile:threadcpuclock") {
554 Trace::SetDefaultClockSource(kTraceClockSourceThreadCpu);
555 } else if (option == "-Xprofile:wallclock") {
556 Trace::SetDefaultClockSource(kTraceClockSourceWall);
557 } else if (option == "-Xprofile:dualclock") {
558 Trace::SetDefaultClockSource(kTraceClockSourceDual);
559 } else if (option == "-Xenable-profiler") {
560 profiler_options_.enabled_ = true;
561 } else if (StartsWith(option, "-Xprofile-filename:")) {
562 if (!ParseStringAfterChar(option, ':', &profile_output_filename_)) {
563 return false;
564 }
565 } else if (StartsWith(option, "-Xprofile-period:")) {
566 if (!ParseUnsignedInteger(option, ':', &profiler_options_.period_s_)) {
567 return false;
568 }
569 } else if (StartsWith(option, "-Xprofile-duration:")) {
570 if (!ParseUnsignedInteger(option, ':', &profiler_options_.duration_s_)) {
571 return false;
572 }
573 } else if (StartsWith(option, "-Xprofile-interval:")) {
574 if (!ParseUnsignedInteger(option, ':', &profiler_options_.interval_us_)) {
575 return false;
576 }
577 } else if (StartsWith(option, "-Xprofile-backoff:")) {
578 if (!ParseDouble(option, ':', 1.0, 10.0, &profiler_options_.backoff_coefficient_)) {
579 return false;
580 }
581 } else if (option == "-Xprofile-start-immediately") {
582 profiler_options_.start_immediately_ = true;
583 } else if (StartsWith(option, "-Xprofile-top-k-threshold:")) {
584 if (!ParseDouble(option, ':', 0.0, 100.0, &profiler_options_.top_k_threshold_)) {
585 return false;
586 }
587 } else if (StartsWith(option, "-Xprofile-top-k-change-threshold:")) {
588 if (!ParseDouble(option, ':', 0.0, 100.0, &profiler_options_.top_k_change_threshold_)) {
589 return false;
590 }
591 } else if (option == "-Xprofile-type:method") {
592 profiler_options_.profile_type_ = kProfilerMethod;
593 } else if (option == "-Xprofile-type:stack") {
594 profiler_options_.profile_type_ = kProfilerBoundedStack;
595 } else if (StartsWith(option, "-Xprofile-max-stack-depth:")) {
596 if (!ParseUnsignedInteger(option, ':', &profiler_options_.max_stack_depth_)) {
597 return false;
598 }
599 } else if (StartsWith(option, "-Xcompiler:")) {
600 if (!ParseStringAfterChar(option, ':', &compiler_executable_)) {
601 return false;
602 }
603 } else if (option == "-Xcompiler-option") {
604 i++;
605 if (i == options.size()) {
606 Usage("Missing required compiler option for %s\n", option.c_str());
607 return false;
608 }
609 compiler_options_.push_back(options[i].first);
610 } else if (option == "-Ximage-compiler-option") {
611 i++;
612 if (i == options.size()) {
613 Usage("Missing required compiler option for %s\n", option.c_str());
614 return false;
615 }
616 image_compiler_options_.push_back(options[i].first);
617 } else if (StartsWith(option, "-Xverify:")) {
618 std::string verify_mode = option.substr(strlen("-Xverify:"));
619 if (verify_mode == "none") {
620 verify_ = false;
621 } else if (verify_mode == "remote" || verify_mode == "all") {
622 verify_ = true;
623 } else {
624 Usage("Unknown -Xverify option %s\n", verify_mode.c_str());
625 return false;
626 }
627 } else if (StartsWith(option, "-XX:NativeBridge=")) {
628 if (!ParseStringAfterChar(option, '=', &native_bridge_library_filename_)) {
629 return false;
630 }
631 } else if (StartsWith(option, "-ea") ||
632 StartsWith(option, "-da") ||
633 StartsWith(option, "-enableassertions") ||
634 StartsWith(option, "-disableassertions") ||
635 (option == "--runtime-arg") ||
636 (option == "-esa") ||
637 (option == "-dsa") ||
638 (option == "-enablesystemassertions") ||
639 (option == "-disablesystemassertions") ||
640 (option == "-Xrs") ||
641 StartsWith(option, "-Xint:") ||
642 StartsWith(option, "-Xdexopt:") ||
643 (option == "-Xnoquithandler") ||
644 StartsWith(option, "-Xjniopts:") ||
645 StartsWith(option, "-Xjnigreflimit:") ||
646 (option == "-Xgenregmap") ||
647 (option == "-Xnogenregmap") ||
648 StartsWith(option, "-Xverifyopt:") ||
649 (option == "-Xcheckdexsum") ||
650 (option == "-Xincludeselectedop") ||
651 StartsWith(option, "-Xjitop:") ||
652 (option == "-Xincludeselectedmethod") ||
653 StartsWith(option, "-Xjitthreshold:") ||
654 StartsWith(option, "-Xjitcodecachesize:") ||
655 (option == "-Xjitblocking") ||
656 StartsWith(option, "-Xjitmethod:") ||
657 StartsWith(option, "-Xjitclass:") ||
658 StartsWith(option, "-Xjitoffset:") ||
659 StartsWith(option, "-Xjitconfig:") ||
660 (option == "-Xjitcheckcg") ||
661 (option == "-Xjitverbose") ||
662 (option == "-Xjitprofile") ||
663 (option == "-Xjitdisableopt") ||
664 (option == "-Xjitsuspendpoll") ||
665 StartsWith(option, "-XX:mainThreadStackSize=")) {
666 // Ignored for backwards compatibility.
667 } else if (!ignore_unrecognized) {
668 Usage("Unrecognized option %s\n", option.c_str());
669 return false;
670 }
671 }
672 // If not set, background collector type defaults to homogeneous compaction
673 // if not low memory mode, semispace otherwise.
674 if (background_collector_type_ == gc::kCollectorTypeNone) {
675 background_collector_type_ = low_memory_mode_ ?
676 gc::kCollectorTypeSS : gc::kCollectorTypeHomogeneousSpaceCompact;
677 }
678
679 // If a reference to the dalvik core.jar snuck in, replace it with
680 // the art specific version. This can happen with on device
681 // boot.art/boot.oat generation by GenerateImage which relies on the
682 // value of BOOTCLASSPATH.
683 #if defined(ART_TARGET)
684 std::string core_jar("/core.jar");
685 std::string core_libart_jar("/core-libart.jar");
686 #else
687 // The host uses hostdex files.
688 std::string core_jar("/core-hostdex.jar");
689 std::string core_libart_jar("/core-libart-hostdex.jar");
690 #endif
691 size_t core_jar_pos = boot_class_path_string_.find(core_jar);
692 if (core_jar_pos != std::string::npos) {
693 boot_class_path_string_.replace(core_jar_pos, core_jar.size(), core_libart_jar);
694 }
695
696 if (compiler_callbacks_ == nullptr && image_.empty()) {
697 image_ += GetAndroidRoot();
698 image_ += "/framework/boot.art";
699 }
700 if (heap_growth_limit_ == 0) {
701 heap_growth_limit_ = heap_maximum_size_;
702 }
703 if (background_collector_type_ == gc::kCollectorTypeNone) {
704 background_collector_type_ = collector_type_;
705 }
706 return true;
707 } // NOLINT(readability/fn_size)
708
Exit(int status)709 void ParsedOptions::Exit(int status) {
710 hook_exit_(status);
711 }
712
Abort()713 void ParsedOptions::Abort() {
714 hook_abort_();
715 }
716
UsageMessageV(FILE * stream,const char * fmt,va_list ap)717 void ParsedOptions::UsageMessageV(FILE* stream, const char* fmt, va_list ap) {
718 hook_vfprintf_(stderr, fmt, ap);
719 }
720
UsageMessage(FILE * stream,const char * fmt,...)721 void ParsedOptions::UsageMessage(FILE* stream, const char* fmt, ...) {
722 va_list ap;
723 va_start(ap, fmt);
724 UsageMessageV(stream, fmt, ap);
725 va_end(ap);
726 }
727
Usage(const char * fmt,...)728 void ParsedOptions::Usage(const char* fmt, ...) {
729 bool error = (fmt != nullptr);
730 FILE* stream = error ? stderr : stdout;
731
732 if (fmt != nullptr) {
733 va_list ap;
734 va_start(ap, fmt);
735 UsageMessageV(stream, fmt, ap);
736 va_end(ap);
737 }
738
739 const char* program = "dalvikvm";
740 UsageMessage(stream, "%s: [options] class [argument ...]\n", program);
741 UsageMessage(stream, "\n");
742 UsageMessage(stream, "The following standard options are supported:\n");
743 UsageMessage(stream, " -classpath classpath (-cp classpath)\n");
744 UsageMessage(stream, " -Dproperty=value\n");
745 UsageMessage(stream, " -verbose:tag ('gc', 'jni', or 'class')\n");
746 UsageMessage(stream, " -showversion\n");
747 UsageMessage(stream, " -help\n");
748 UsageMessage(stream, " -agentlib:jdwp=options\n");
749 UsageMessage(stream, "\n");
750
751 UsageMessage(stream, "The following extended options are supported:\n");
752 UsageMessage(stream, " -Xrunjdwp:<options>\n");
753 UsageMessage(stream, " -Xbootclasspath:bootclasspath\n");
754 UsageMessage(stream, " -Xcheck:tag (e.g. 'jni')\n");
755 UsageMessage(stream, " -XmsN (min heap, must be multiple of 1K, >= 1MB)\n");
756 UsageMessage(stream, " -XmxN (max heap, must be multiple of 1K, >= 2MB)\n");
757 UsageMessage(stream, " -XssN (stack size)\n");
758 UsageMessage(stream, " -Xint\n");
759 UsageMessage(stream, "\n");
760
761 UsageMessage(stream, "The following Dalvik options are supported:\n");
762 UsageMessage(stream, " -Xzygote\n");
763 UsageMessage(stream, " -Xjnitrace:substring (eg NativeClass or nativeMethod)\n");
764 UsageMessage(stream, " -Xstacktracefile:<filename>\n");
765 UsageMessage(stream, " -Xgc:[no]preverify\n");
766 UsageMessage(stream, " -Xgc:[no]postverify\n");
767 UsageMessage(stream, " -XX:+DisableExplicitGC\n");
768 UsageMessage(stream, " -XX:HeapGrowthLimit=N\n");
769 UsageMessage(stream, " -XX:HeapMinFree=N\n");
770 UsageMessage(stream, " -XX:HeapMaxFree=N\n");
771 UsageMessage(stream, " -XX:NonMovingSpaceCapacity=N\n");
772 UsageMessage(stream, " -XX:HeapTargetUtilization=doublevalue\n");
773 UsageMessage(stream, " -XX:ForegroundHeapGrowthMultiplier=doublevalue\n");
774 UsageMessage(stream, " -XX:LowMemoryMode\n");
775 UsageMessage(stream, " -Xprofile:{threadcpuclock,wallclock,dualclock}\n");
776 UsageMessage(stream, "\n");
777
778 UsageMessage(stream, "The following unique to ART options are supported:\n");
779 UsageMessage(stream, " -Xgc:[no]preverify_rosalloc\n");
780 UsageMessage(stream, " -Xgc:[no]postsweepingverify_rosalloc\n");
781 UsageMessage(stream, " -Xgc:[no]postverify_rosalloc\n");
782 UsageMessage(stream, " -Xgc:[no]presweepingverify\n");
783 UsageMessage(stream, " -Ximage:filename\n");
784 UsageMessage(stream, " -XX:ParallelGCThreads=integervalue\n");
785 UsageMessage(stream, " -XX:ConcGCThreads=integervalue\n");
786 UsageMessage(stream, " -XX:MaxSpinsBeforeThinLockInflation=integervalue\n");
787 UsageMessage(stream, " -XX:LongPauseLogThreshold=integervalue\n");
788 UsageMessage(stream, " -XX:LongGCLogThreshold=integervalue\n");
789 UsageMessage(stream, " -XX:DumpGCPerformanceOnShutdown\n");
790 UsageMessage(stream, " -XX:IgnoreMaxFootprint\n");
791 UsageMessage(stream, " -XX:UseTLAB\n");
792 UsageMessage(stream, " -XX:BackgroundGC=none\n");
793 UsageMessage(stream, " -Xmethod-trace\n");
794 UsageMessage(stream, " -Xmethod-trace-file:filename");
795 UsageMessage(stream, " -Xmethod-trace-file-size:integervalue\n");
796 UsageMessage(stream, " -Xenable-profiler\n");
797 UsageMessage(stream, " -Xprofile-filename:filename\n");
798 UsageMessage(stream, " -Xprofile-period:integervalue\n");
799 UsageMessage(stream, " -Xprofile-duration:integervalue\n");
800 UsageMessage(stream, " -Xprofile-interval:integervalue\n");
801 UsageMessage(stream, " -Xprofile-backoff:doublevalue\n");
802 UsageMessage(stream, " -Xprofile-start-immediately\n");
803 UsageMessage(stream, " -Xprofile-top-k-threshold:doublevalue\n");
804 UsageMessage(stream, " -Xprofile-top-k-change-threshold:doublevalue\n");
805 UsageMessage(stream, " -Xprofile-type:{method,stack}\n");
806 UsageMessage(stream, " -Xprofile-max-stack-depth:integervalue\n");
807 UsageMessage(stream, " -Xcompiler:filename\n");
808 UsageMessage(stream, " -Xcompiler-option dex2oat-option\n");
809 UsageMessage(stream, " -Ximage-compiler-option dex2oat-option\n");
810 UsageMessage(stream, " -Xpatchoat:filename\n");
811 UsageMessage(stream, " -X[no]relocate\n");
812 UsageMessage(stream, " -X[no]dex2oat (Whether to invoke dex2oat on the application)\n");
813 UsageMessage(stream, " -X[no]image-dex2oat (Whether to create and use a boot image)\n");
814 UsageMessage(stream, "\n");
815
816 UsageMessage(stream, "The following previously supported Dalvik options are ignored:\n");
817 UsageMessage(stream, " -ea[:<package name>... |:<class name>]\n");
818 UsageMessage(stream, " -da[:<package name>... |:<class name>]\n");
819 UsageMessage(stream, " (-enableassertions, -disableassertions)\n");
820 UsageMessage(stream, " -esa\n");
821 UsageMessage(stream, " -dsa\n");
822 UsageMessage(stream, " (-enablesystemassertions, -disablesystemassertions)\n");
823 UsageMessage(stream, " -Xverify:{none,remote,all}\n");
824 UsageMessage(stream, " -Xrs\n");
825 UsageMessage(stream, " -Xint:portable, -Xint:fast, -Xint:jit\n");
826 UsageMessage(stream, " -Xdexopt:{none,verified,all,full}\n");
827 UsageMessage(stream, " -Xnoquithandler\n");
828 UsageMessage(stream, " -Xjniopts:{warnonly,forcecopy}\n");
829 UsageMessage(stream, " -Xjnigreflimit:integervalue\n");
830 UsageMessage(stream, " -Xgc:[no]precise\n");
831 UsageMessage(stream, " -Xgc:[no]verifycardtable\n");
832 UsageMessage(stream, " -X[no]genregmap\n");
833 UsageMessage(stream, " -Xverifyopt:[no]checkmon\n");
834 UsageMessage(stream, " -Xcheckdexsum\n");
835 UsageMessage(stream, " -Xincludeselectedop\n");
836 UsageMessage(stream, " -Xjitop:hexopvalue[-endvalue][,hexopvalue[-endvalue]]*\n");
837 UsageMessage(stream, " -Xincludeselectedmethod\n");
838 UsageMessage(stream, " -Xjitthreshold:integervalue\n");
839 UsageMessage(stream, " -Xjitcodecachesize:decimalvalueofkbytes\n");
840 UsageMessage(stream, " -Xjitblocking\n");
841 UsageMessage(stream, " -Xjitmethod:signature[,signature]* (eg Ljava/lang/String\\;replace)\n");
842 UsageMessage(stream, " -Xjitclass:classname[,classname]*\n");
843 UsageMessage(stream, " -Xjitoffset:offset[,offset]\n");
844 UsageMessage(stream, " -Xjitconfig:filename\n");
845 UsageMessage(stream, " -Xjitcheckcg\n");
846 UsageMessage(stream, " -Xjitverbose\n");
847 UsageMessage(stream, " -Xjitprofile\n");
848 UsageMessage(stream, " -Xjitdisableopt\n");
849 UsageMessage(stream, " -Xjitsuspendpoll\n");
850 UsageMessage(stream, " -XX:mainThreadStackSize=N\n");
851 UsageMessage(stream, "\n");
852
853 Exit((error) ? 1 : 0);
854 }
855
ParseStringAfterChar(const std::string & s,char c,std::string * parsed_value)856 bool ParsedOptions::ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) {
857 std::string::size_type colon = s.find(c);
858 if (colon == std::string::npos) {
859 Usage("Missing char %c in option %s\n", c, s.c_str());
860 return false;
861 }
862 // Add one to remove the char we were trimming until.
863 *parsed_value = s.substr(colon + 1);
864 return true;
865 }
866
ParseInteger(const std::string & s,char after_char,int * parsed_value)867 bool ParsedOptions::ParseInteger(const std::string& s, char after_char, int* parsed_value) {
868 std::string::size_type colon = s.find(after_char);
869 if (colon == std::string::npos) {
870 Usage("Missing char %c in option %s\n", after_char, s.c_str());
871 return false;
872 }
873 const char* begin = &s[colon + 1];
874 char* end;
875 size_t result = strtoul(begin, &end, 10);
876 if (begin == end || *end != '\0') {
877 Usage("Failed to parse integer from %s\n", s.c_str());
878 return false;
879 }
880 *parsed_value = result;
881 return true;
882 }
883
ParseUnsignedInteger(const std::string & s,char after_char,unsigned int * parsed_value)884 bool ParsedOptions::ParseUnsignedInteger(const std::string& s, char after_char,
885 unsigned int* parsed_value) {
886 int i;
887 if (!ParseInteger(s, after_char, &i)) {
888 return false;
889 }
890 if (i < 0) {
891 Usage("Negative value %d passed for unsigned option %s\n", i, s.c_str());
892 return false;
893 }
894 *parsed_value = i;
895 return true;
896 }
897
ParseDouble(const std::string & option,char after_char,double min,double max,double * parsed_value)898 bool ParsedOptions::ParseDouble(const std::string& option, char after_char,
899 double min, double max, double* parsed_value) {
900 std::string substring;
901 if (!ParseStringAfterChar(option, after_char, &substring)) {
902 return false;
903 }
904 bool sane_val = true;
905 double value;
906 if (false) {
907 // TODO: this doesn't seem to work on the emulator. b/15114595
908 std::stringstream iss(substring);
909 iss >> value;
910 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
911 sane_val = iss.eof() && (value >= min) && (value <= max);
912 } else {
913 char* end = nullptr;
914 value = strtod(substring.c_str(), &end);
915 sane_val = *end == '\0' && value >= min && value <= max;
916 }
917 if (!sane_val) {
918 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
919 return false;
920 }
921 *parsed_value = value;
922 return true;
923 }
924
925 } // namespace art
926