blob: 03679fa62817e0e2361932379e21537c1f3e4af2 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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
Andreas Gampe19ad5822015-04-28 23:16:03 -070017#include <inttypes.h>
Brian Carlstrom7940e442013-07-12 13:46:57 -070018#include <stdio.h>
19#include <stdlib.h>
20#include <sys/stat.h>
Ian Rogers2672a9f2013-09-05 17:24:22 -070021#include <valgrind.h>
Brian Carlstrom7940e442013-07-12 13:46:57 -070022
23#include <fstream>
24#include <iostream>
25#include <sstream>
26#include <string>
Andreas Gampeb1fcead2015-04-20 18:53:51 -070027#include <unordered_set>
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include <vector>
29
Vladimir Markof94b7812014-06-05 15:48:04 +010030#if defined(__linux__) && defined(__arm__)
31#include <sys/personality.h>
32#include <sys/utsname.h>
33#endif
34
Ian Rogerscf7f1912014-10-22 22:06:39 -070035#define ATRACE_TAG ATRACE_TAG_DALVIK
Ian Rogersd582fa42014-11-05 23:46:43 -080036#include <cutils/trace.h>
Ian Rogerscf7f1912014-10-22 22:06:39 -070037
Mathieu Chartier3d21bdf2015-04-22 13:56:20 -070038#include "art_method-inl.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080039#include "arch/instruction_set_features.h"
Andreas Gampec5a3ea72015-01-13 16:41:53 -080040#include "arch/mips/instruction_set_features_mips.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070041#include "base/dumpable.h"
Andreas Gampe794ad762015-02-23 08:12:24 -080042#include "base/macros.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070043#include "base/stl_util.h"
44#include "base/stringpiece.h"
Vladimir Marko41b175a2015-05-19 18:08:00 +010045#include "base/time_utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070046#include "base/timing_logger.h"
47#include "base/unix_file/fd_file.h"
48#include "class_linker.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000049#include "compiler.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000050#include "compiler_callbacks.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070051#include "dex_file-inl.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080052#include "dex/pass_manager.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000053#include "dex/verification_results.h"
Ian Rogerse63db272014-07-15 15:36:11 -070054#include "dex/quick_compiler_callbacks.h"
55#include "dex/quick/dex_file_to_method_inliner_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070056#include "driver/compiler_driver.h"
Brian Carlstrom6449c622014-02-10 23:48:36 -080057#include "driver/compiler_options.h"
Andreas Gampe88ec7f42014-11-05 10:18:32 -080058#include "elf_file.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070059#include "elf_writer.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070060#include "gc/space/image_space.h"
61#include "gc/space/space-inl.h"
62#include "image_writer.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070063#include "interpreter/unstarted_runtime.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070064#include "leb128.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070065#include "mirror/class-inl.h"
66#include "mirror/class_loader.h"
67#include "mirror/object-inl.h"
68#include "mirror/object_array-inl.h"
69#include "oat_writer.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070070#include "os.h"
71#include "runtime.h"
72#include "ScopedLocalRef.h"
73#include "scoped_thread_state_change.h"
Alex Light53cb16b2014-06-12 11:26:29 -070074#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070075#include "vector_output_stream.h"
76#include "well_known_classes.h"
77#include "zip_archive.h"
Chitti Babu Theegalac06972f2015-08-06 19:52:05 +053078#ifdef HAVE_ANDROID_OS
79#include "cutils/properties.h"
80#endif
Brian Carlstrom7940e442013-07-12 13:46:57 -070081
82namespace art {
83
Brian Carlstrom6449c622014-02-10 23:48:36 -080084static int original_argc;
85static char** original_argv;
86
87static std::string CommandLine() {
88 std::vector<std::string> command;
89 for (int i = 0; i < original_argc; ++i) {
90 command.push_back(original_argv[i]);
91 }
92 return Join(command, ' ');
93}
94
Andreas Gampead805682015-05-18 23:22:54 -070095// A stripped version. Remove some less essential parameters. If we see a "--zip-fd=" parameter, be
96// even more aggressive. There won't be much reasonable data here for us in that case anyways (the
97// locations are all staged).
98static std::string StrippedCommandLine() {
99 std::vector<std::string> command;
100
101 // Do a pre-pass to look for zip-fd.
102 bool saw_zip_fd = false;
103 for (int i = 0; i < original_argc; ++i) {
104 if (StartsWith(original_argv[i], "--zip-fd=")) {
105 saw_zip_fd = true;
106 break;
107 }
108 }
109
110 // Now filter out things.
111 for (int i = 0; i < original_argc; ++i) {
112 // All runtime-arg parameters are dropped.
113 if (strcmp(original_argv[i], "--runtime-arg") == 0) {
114 i++; // Drop the next part, too.
115 continue;
116 }
117
118 // Any instruction-setXXX is dropped.
119 if (StartsWith(original_argv[i], "--instruction-set")) {
120 continue;
121 }
122
123 // The boot image is dropped.
124 if (StartsWith(original_argv[i], "--boot-image=")) {
125 continue;
126 }
127
128 // This should leave any dex-file and oat-file options, describing what we compiled.
129
130 // However, we prefer to drop this when we saw --zip-fd.
131 if (saw_zip_fd) {
132 // Drop anything --zip-X, --dex-X, --oat-X, --swap-X.
133 if (StartsWith(original_argv[i], "--zip-") ||
134 StartsWith(original_argv[i], "--dex-") ||
135 StartsWith(original_argv[i], "--oat-") ||
136 StartsWith(original_argv[i], "--swap-")) {
137 continue;
138 }
139 }
140
141 command.push_back(original_argv[i]);
142 }
143
144 // Construct the final output.
145 if (command.size() <= 1U) {
146 // It seems only "/system/bin/dex2oat" is left, or not even that. Use a pretty line.
147 return "Starting dex2oat.";
148 }
149 return Join(command, ' ');
150}
151
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152static void UsageErrorV(const char* fmt, va_list ap) {
153 std::string error;
154 StringAppendV(&error, fmt, ap);
155 LOG(ERROR) << error;
156}
157
158static void UsageError(const char* fmt, ...) {
159 va_list ap;
160 va_start(ap, fmt);
161 UsageErrorV(fmt, ap);
162 va_end(ap);
163}
164
Andreas Gampe794ad762015-02-23 08:12:24 -0800165NO_RETURN static void Usage(const char* fmt, ...) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 va_list ap;
167 va_start(ap, fmt);
168 UsageErrorV(fmt, ap);
169 va_end(ap);
170
Brian Carlstrom6449c622014-02-10 23:48:36 -0800171 UsageError("Command: %s", CommandLine().c_str());
172
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 UsageError("Usage: dex2oat [options]...");
174 UsageError("");
Jean-Philippe Halimi3d329d72015-03-23 14:09:48 +0100175 UsageError(" -j<number>: specifies the number of threads used for compilation.");
176 UsageError(" Default is the number of detected hardware threads available on the");
177 UsageError(" host system.");
178 UsageError(" Example: -j12");
179 UsageError("");
Richard Uhlere934df22015-03-17 11:26:16 -0700180 UsageError(" --dex-file=<dex-file>: specifies a .dex, .jar, or .apk file to compile.");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 UsageError(" Example: --dex-file=/system/framework/core.jar");
182 UsageError("");
Richard Uhlere934df22015-03-17 11:26:16 -0700183 UsageError(" --dex-location=<dex-location>: specifies an alternative dex location to");
184 UsageError(" encode in the oat file for the corresponding --dex-file argument.");
185 UsageError(" Example: --dex-file=/home/build/out/system/framework/core.jar");
186 UsageError(" --dex-location=/system/framework/core.jar");
187 UsageError("");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file");
189 UsageError(" containing a classes.dex file to compile.");
190 UsageError(" Example: --zip-fd=5");
191 UsageError("");
Brian Carlstrom45602482013-07-21 22:07:55 -0700192 UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file");
193 UsageError(" corresponding to the file descriptor specified by --zip-fd.");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194 UsageError(" Example: --zip-location=/system/app/Calculator.apk");
195 UsageError("");
196 UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename.");
197 UsageError(" Example: --oat-file=/system/framework/boot.oat");
198 UsageError("");
199 UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor.");
Wonil Kim9cb554a2014-04-28 11:26:55 +0900200 UsageError(" Example: --oat-fd=6");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 UsageError("");
202 UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding");
203 UsageError(" to the file descriptor specified by --oat-fd.");
204 UsageError(" Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat");
205 UsageError("");
206 UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols.");
207 UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat");
208 UsageError("");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 UsageError(" --image=<file.art>: specifies the output image filename.");
210 UsageError(" Example: --image=/system/framework/boot.art");
211 UsageError("");
212 UsageError(" --image-classes=<classname-file>: specifies classes to include in an image.");
213 UsageError(" Example: --image=frameworks/base/preloaded-classes");
214 UsageError("");
215 UsageError(" --base=<hex-address>: specifies the base address when creating a boot image.");
216 UsageError(" Example: --base=0x50000000");
217 UsageError("");
218 UsageError(" --boot-image=<file.art>: provide the image file for the boot class path.");
219 UsageError(" Example: --boot-image=/system/framework/boot.art");
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000220 UsageError(" Default: $ANDROID_ROOT/system/framework/boot.art");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 UsageError("");
222 UsageError(" --android-root=<path>: used to locate libraries for portable linking.");
223 UsageError(" Example: --android-root=out/host/linux-x86");
224 UsageError(" Default: $ANDROID_ROOT");
225 UsageError("");
Andreas Gampe57b34292015-01-14 15:45:59 -0800226 UsageError(" --instruction-set=(arm|arm64|mips|mips64|x86|x86_64): compile for a particular");
Alex Light53cb16b2014-06-12 11:26:29 -0700227 UsageError(" instruction set.");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 UsageError(" Example: --instruction-set=x86");
229 UsageError(" Default: arm");
230 UsageError("");
Dave Allison70202782013-10-22 17:52:19 -0700231 UsageError(" --instruction-set-features=...,: Specify instruction set features");
232 UsageError(" Example: --instruction-set-features=div");
233 UsageError(" Default: default");
234 UsageError("");
Igor Murashkin46774762014-10-22 11:37:02 -0700235 UsageError(" --compile-pic: Force indirect use of code, methods, and classes");
236 UsageError(" Default: disabled");
237 UsageError("");
Elliott Hughes956af0f2014-12-11 14:34:28 -0800238 UsageError(" --compiler-backend=(Quick|Optimizing): select compiler backend");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239 UsageError(" set.");
Elliott Hughes956af0f2014-12-11 14:34:28 -0800240 UsageError(" Example: --compiler-backend=Optimizing");
241 if (kUseOptimizingCompiler) {
Nicolas Geoffray4586fb62014-11-28 16:22:11 +0000242 UsageError(" Default: Optimizing");
243 } else {
244 UsageError(" Default: Quick");
245 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 UsageError("");
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100247 UsageError(" --compiler-filter="
248 "(verify-none"
249 "|interpret-only"
250 "|space"
251 "|balanced"
252 "|speed"
253 "|everything"
254 "|time):");
Jeff Hao4a200f52014-04-01 14:58:49 -0700255 UsageError(" select compiler filter.");
Brian Carlstrom6449c622014-02-10 23:48:36 -0800256 UsageError(" Example: --compiler-filter=everything");
Brian Carlstrom6449c622014-02-10 23:48:36 -0800257 UsageError(" Default: speed");
Brian Carlstrom6449c622014-02-10 23:48:36 -0800258 UsageError("");
Brian Carlstrom6449c622014-02-10 23:48:36 -0800259 UsageError(" --huge-method-max=<method-instruction-count>: threshold size for a huge");
260 UsageError(" method for compiler filter tuning.");
261 UsageError(" Example: --huge-method-max=%d", CompilerOptions::kDefaultHugeMethodThreshold);
262 UsageError(" Default: %d", CompilerOptions::kDefaultHugeMethodThreshold);
263 UsageError("");
264 UsageError(" --large-method-max=<method-instruction-count>: threshold size for a large");
265 UsageError(" method for compiler filter tuning.");
266 UsageError(" Example: --large-method-max=%d", CompilerOptions::kDefaultLargeMethodThreshold);
267 UsageError(" Default: %d", CompilerOptions::kDefaultLargeMethodThreshold);
268 UsageError("");
269 UsageError(" --small-method-max=<method-instruction-count>: threshold size for a small");
270 UsageError(" method for compiler filter tuning.");
271 UsageError(" Example: --small-method-max=%d", CompilerOptions::kDefaultSmallMethodThreshold);
272 UsageError(" Default: %d", CompilerOptions::kDefaultSmallMethodThreshold);
273 UsageError("");
274 UsageError(" --tiny-method-max=<method-instruction-count>: threshold size for a tiny");
275 UsageError(" method for compiler filter tuning.");
276 UsageError(" Example: --tiny-method-max=%d", CompilerOptions::kDefaultTinyMethodThreshold);
277 UsageError(" Default: %d", CompilerOptions::kDefaultTinyMethodThreshold);
278 UsageError("");
279 UsageError(" --num-dex-methods=<method-count>: threshold size for a small dex file for");
280 UsageError(" compiler filter tuning. If the input has fewer than this many methods");
Jeff Hao4a200f52014-04-01 14:58:49 -0700281 UsageError(" and the filter is not interpret-only or verify-none, overrides the");
282 UsageError(" filter to use speed");
Brian Carlstrom6449c622014-02-10 23:48:36 -0800283 UsageError(" Example: --num-dex-method=%d", CompilerOptions::kDefaultNumDexMethodsThreshold);
284 UsageError(" Default: %d", CompilerOptions::kDefaultNumDexMethodsThreshold);
285 UsageError("");
Calin Juravle8f96df82015-07-29 15:58:48 +0100286 UsageError(" --inline-depth-limit=<depth-limit>: the depth limit of inlining for fine tuning");
287 UsageError(" the compiler. A zero value will disable inlining. Honored only by Optimizing.");
Roland Levillaind3333892015-08-07 12:22:28 +0100288 UsageError(" Has priority over the --compiler-filter option. Intended for ");
289 UsageError(" development/experimental use.");
Calin Juravle8f96df82015-07-29 15:58:48 +0100290 UsageError(" Example: --inline-depth-limit=%d", CompilerOptions::kDefaultInlineDepthLimit);
291 UsageError(" Default: %d", CompilerOptions::kDefaultInlineDepthLimit);
292 UsageError("");
293 UsageError(" --inline-max-code-units=<code-units-count>: the maximum code units that a method");
294 UsageError(" can have to be considered for inlining. A zero value will disable inlining.");
Roland Levillaind3333892015-08-07 12:22:28 +0100295 UsageError(" Honored only by Optimizing. Has priority over the --compiler-filter option.");
296 UsageError(" Intended for development/experimental use.");
Calin Juravle8f96df82015-07-29 15:58:48 +0100297 UsageError(" Example: --inline-max-code-units=%d",
298 CompilerOptions::kDefaultInlineMaxCodeUnits);
299 UsageError(" Default: %d", CompilerOptions::kDefaultInlineMaxCodeUnits);
300 UsageError("");
Ian Rogers46398602013-08-20 07:50:36 -0700301 UsageError(" --dump-timing: display a breakdown of where time was spent");
302 UsageError("");
Alex Light53cb16b2014-06-12 11:26:29 -0700303 UsageError(" --include-patch-information: Include patching information so the generated code");
304 UsageError(" can have its base address moved without full recompilation.");
305 UsageError("");
306 UsageError(" --no-include-patch-information: Do not include patching information.");
307 UsageError("");
David Srbeckya26cb572015-04-22 18:57:06 -0700308 UsageError(" -g");
309 UsageError(" --generate-debug-info: Generate debug information for native debugging,");
310 UsageError(" such as stack unwinding information, ELF symbols and DWARF sections.");
311 UsageError(" This generates all the available information. Unneeded parts can be");
312 UsageError(" stripped using standard command line tools such as strip or objcopy.");
313 UsageError(" (enabled by default in debug builds, disabled by default otherwise)");
Alex Light78382fa2014-06-06 15:45:32 -0700314 UsageError("");
David Srbeckya26cb572015-04-22 18:57:06 -0700315 UsageError(" --no-generate-debug-info: Do not generate debug information for native debugging.");
David Srbecky8dc73242015-04-12 11:40:39 +0100316 UsageError("");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
318 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
319 UsageError(" Use a separate --runtime-arg switch for each argument.");
320 UsageError(" Example: --runtime-arg -Xms256m");
Jeff Hao4a200f52014-04-01 14:58:49 -0700321 UsageError("");
Dave Allisond6ed6422014-04-09 23:36:15 +0000322 UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation.");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 UsageError("");
Chao-ying Fucd8ce662014-03-11 14:57:19 -0700324 UsageError(" --print-pass-names: print a list of pass names");
325 UsageError("");
326 UsageError(" --disable-passes=<pass-names>: disable one or more passes separated by comma.");
327 UsageError(" Example: --disable-passes=UseCount,BBOptimizations");
328 UsageError("");
Razvan A Lupusorubd25d4b2014-07-02 18:16:51 -0700329 UsageError(" --print-pass-options: print a list of passes that have configurable options along "
330 "with the setting.");
331 UsageError(" Will print default if no overridden setting exists.");
332 UsageError("");
333 UsageError(" --pass-options=Pass1Name:Pass1OptionName:Pass1Option#,"
334 "Pass2Name:Pass2OptionName:Pass2Option#");
335 UsageError(" Used to specify a pass specific option. The setting itself must be integer.");
336 UsageError(" Separator used between options is a comma.");
337 UsageError("");
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800338 UsageError(" --swap-file=<file-name>: specifies a file to use for swap.");
339 UsageError(" Example: --swap-file=/data/tmp/swap.001");
340 UsageError("");
341 UsageError(" --swap-fd=<file-descriptor>: specifies a file to use for swap (by descriptor).");
342 UsageError(" Example: --swap-fd=10");
343 UsageError("");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 std::cerr << "See log for usage error information\n";
345 exit(EXIT_FAILURE);
346}
347
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348// The primary goal of the watchdog is to prevent stuck build servers
349// during development when fatal aborts lead to a cascade of failures
350// that result in a deadlock.
351class WatchDog {
Brian Carlstrom95b033b2014-12-03 22:29:37 -0800352// WatchDog defines its own CHECK_PTHREAD_CALL to avoid using LOG which uses locks
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353#undef CHECK_PTHREAD_CALL
354#define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \
355 do { \
356 int rc = call args; \
357 if (rc != 0) { \
358 errno = rc; \
359 std::string message(# call); \
360 message += " failed for "; \
361 message += reason; \
362 Fatal(message); \
363 } \
364 } while (false)
365
366 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700367 explicit WatchDog(bool is_watch_dog_enabled) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 is_watch_dog_enabled_ = is_watch_dog_enabled;
369 if (!is_watch_dog_enabled_) {
370 return;
371 }
372 shutting_down_ = false;
373 const char* reason = "dex2oat watch dog thread startup";
Kenny Root51316382014-05-13 14:59:37 -0700374 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, nullptr), reason);
375 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, nullptr), reason);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason);
377 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason);
378 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason);
379 }
380 ~WatchDog() {
381 if (!is_watch_dog_enabled_) {
382 return;
383 }
384 const char* reason = "dex2oat watch dog thread shutdown";
385 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
386 shutting_down_ = true;
387 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason);
388 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
389
Kenny Root51316382014-05-13 14:59:37 -0700390 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, nullptr), reason);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391
392 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason);
393 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason);
394 }
395
396 private:
397 static void* CallBack(void* arg) {
398 WatchDog* self = reinterpret_cast<WatchDog*>(arg);
399 ::art::SetThreadName("dex2oat watch dog");
400 self->Wait();
Kenny Root51316382014-05-13 14:59:37 -0700401 return nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 }
403
Andreas Gampe794ad762015-02-23 08:12:24 -0800404 NO_RETURN static void Fatal(const std::string& message) {
Andreas Gampe19ad5822015-04-28 23:16:03 -0700405 // TODO: When we can guarantee it won't prevent shutdown in error cases, move to LOG. However,
406 // it's rather easy to hang in unwinding.
407 // LogLine also avoids ART logging lock issues, as it's really only a wrapper around
408 // logcat logging or stderr output.
409 LogMessage::LogLine(__FILE__, __LINE__, LogSeverity::FATAL, message.c_str());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 exit(1);
411 }
412
413 void Wait() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 // TODO: tune the multiplier for GC verification, the following is just to make the timeout
415 // large.
Andreas Gampe19ad5822015-04-28 23:16:03 -0700416 constexpr int64_t multiplier = kVerifyObjectSupport > kVerifyObjectModeFast ? 100 : 1;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 timespec timeout_ts;
418 InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts);
419 const char* reason = "dex2oat watch dog thread waiting";
420 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason);
421 while (!shutting_down_) {
Brian Carlstrom95b033b2014-12-03 22:29:37 -0800422 int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_, &timeout_ts));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 if (rc == ETIMEDOUT) {
Andreas Gampe19ad5822015-04-28 23:16:03 -0700424 Fatal(StringPrintf("dex2oat did not finish after %" PRId64 " seconds",
425 kWatchDogTimeoutSeconds));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 } else if (rc != 0) {
427 std::string message(StringPrintf("pthread_cond_timedwait failed: %s",
428 strerror(errno)));
429 Fatal(message.c_str());
430 }
431 }
432 CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason);
433 }
434
435 // When setting timeouts, keep in mind that the build server may not be as fast as your desktop.
Mathieu Chartier13b9f432014-09-09 17:26:58 -0700436 // Debug builds are slower so they have larger timeouts.
Andreas Gampe19ad5822015-04-28 23:16:03 -0700437 static constexpr int64_t kSlowdownFactor = kIsDebugBuild ? 5U : 1U;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800438
Andreas Gampe2602aa82015-09-14 15:34:38 -0700439 // 9.5 minutes scaled by kSlowdownFactor. This is slightly smaller than the Package Manager
440 // watchdog (PackageManagerService.WATCHDOG_TIMEOUT, 10 minutes), so that dex2oat will abort
441 // itself before that watchdog would take down the system server.
442 static constexpr int64_t kWatchDogTimeoutSeconds = kSlowdownFactor * (9 * 60 + 30);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443
444 bool is_watch_dog_enabled_;
445 bool shutting_down_;
446 // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases.
447 pthread_mutex_t mutex_;
448 pthread_cond_t cond_;
449 pthread_attr_t attr_;
450 pthread_t pthread_;
451};
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800453static void ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) {
Calin Juravlec1b643c2014-05-30 23:44:11 +0100454 std::string::size_type colon = s.find(c);
455 if (colon == std::string::npos) {
456 Usage("Missing char %c in option %s\n", c, s.c_str());
457 }
458 // Add one to remove the char we were trimming until.
459 *parsed_value = s.substr(colon + 1);
460}
461
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800462static void ParseDouble(const std::string& option, char after_char, double min, double max,
463 double* parsed_value) {
Calin Juravlec1b643c2014-05-30 23:44:11 +0100464 std::string substring;
465 ParseStringAfterChar(option, after_char, &substring);
466 bool sane_val = true;
467 double value;
468 if (false) {
469 // TODO: this doesn't seem to work on the emulator. b/15114595
470 std::stringstream iss(substring);
471 iss >> value;
472 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
473 sane_val = iss.eof() && (value >= min) && (value <= max);
474 } else {
475 char* end = nullptr;
476 value = strtod(substring.c_str(), &end);
477 sane_val = *end == '\0' && value >= min && value <= max;
478 }
479 if (!sane_val) {
480 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
481 }
482 *parsed_value = value;
483}
484
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800485static constexpr size_t kMinDexFilesForSwap = 2;
486static constexpr size_t kMinDexFileCumulativeSizeForSwap = 20 * MB;
487
488static bool UseSwap(bool is_image, std::vector<const DexFile*>& dex_files) {
489 if (is_image) {
490 // Don't use swap, we know generation should succeed, and we don't want to slow it down.
491 return false;
492 }
493 if (dex_files.size() < kMinDexFilesForSwap) {
494 // If there are less dex files than the threshold, assume it's gonna be fine.
495 return false;
496 }
497 size_t dex_files_size = 0;
498 for (const auto* dex_file : dex_files) {
499 dex_files_size += dex_file->GetHeader().file_size_;
500 }
501 return dex_files_size >= kMinDexFileCumulativeSizeForSwap;
502}
503
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800504class Dex2Oat FINAL {
505 public:
506 explicit Dex2Oat(TimingLogger* timings) :
Elliott Hughes956af0f2014-12-11 14:34:28 -0800507 compiler_kind_(kUseOptimizingCompiler ? Compiler::kOptimizing : Compiler::kQuick),
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800508 instruction_set_(kRuntimeISA),
509 // Take the default set of instruction features from the build.
Andreas Gampecc077602015-09-17 14:03:21 -0700510 verification_results_(nullptr),
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800511 method_inliner_map_(),
512 runtime_(nullptr),
513 thread_count_(sysconf(_SC_NPROCESSORS_CONF)),
514 start_ns_(NanoTime()),
515 oat_fd_(-1),
516 zip_fd_(-1),
517 image_base_(0U),
518 image_classes_zip_filename_(nullptr),
519 image_classes_filename_(nullptr),
Andreas Gampe4bf3ae92014-11-11 13:28:29 -0800520 compiled_classes_zip_filename_(nullptr),
521 compiled_classes_filename_(nullptr),
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700522 compiled_methods_zip_filename_(nullptr),
523 compiled_methods_filename_(nullptr),
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800524 image_(false),
525 is_host_(false),
Andreas Gampecc077602015-09-17 14:03:21 -0700526 driver_(nullptr),
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800527 dump_stats_(false),
528 dump_passes_(false),
529 dump_timing_(false),
530 dump_slow_timing_(kIsDebugBuild),
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800531 swap_fd_(-1),
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800532 timings_(timings) {}
533
534 ~Dex2Oat() {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800535 // Free opened dex files before deleting the runtime_, because ~DexFile
536 // uses MemMap, which is shut down by ~Runtime.
537 class_path_files_.clear();
538 opened_dex_files_.clear();
539
540 // Log completion time before deleting the runtime_, because this accesses
541 // the runtime.
542 LogCompletionTime();
543
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800544 if (kIsDebugBuild || (RUNNING_ON_VALGRIND != 0)) {
545 delete runtime_; // See field declaration for why this is manual.
Andreas Gampecc077602015-09-17 14:03:21 -0700546 delete driver_;
547 delete verification_results_;
Vladimir Markof94b7812014-06-05 15:48:04 +0100548 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 }
550
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800551 // Parse the arguments from the command line. In case of an unrecognized option or impossible
552 // values/combinations, a usage error will be displayed and exit() is called. Thus, if the method
553 // returns, arguments have been successfully parsed.
554 void ParseArgs(int argc, char** argv) {
555 original_argc = argc;
556 original_argv = argv;
Dave Allison70202782013-10-22 17:52:19 -0700557
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800558 InitLogging(argv);
Dave Allison70202782013-10-22 17:52:19 -0700559
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800560 // Skip over argv[0].
561 argv++;
562 argc--;
Dave Allison70202782013-10-22 17:52:19 -0700563
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800564 if (argc == 0) {
565 Usage("No arguments specified");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800567
568 std::string oat_symbols;
569 std::string boot_image_filename;
570 const char* compiler_filter_string = nullptr;
571 bool compile_pic = false;
572 int huge_method_threshold = CompilerOptions::kDefaultHugeMethodThreshold;
573 int large_method_threshold = CompilerOptions::kDefaultLargeMethodThreshold;
574 int small_method_threshold = CompilerOptions::kDefaultSmallMethodThreshold;
575 int tiny_method_threshold = CompilerOptions::kDefaultTinyMethodThreshold;
576 int num_dex_methods_threshold = CompilerOptions::kDefaultNumDexMethodsThreshold;
Roland Levillaind3333892015-08-07 12:22:28 +0100577 static constexpr int kUnsetInlineDepthLimit = -1;
578 int inline_depth_limit = kUnsetInlineDepthLimit;
579 static constexpr int kUnsetInlineMaxCodeUnits = -1;
580 int inline_max_code_units = kUnsetInlineMaxCodeUnits;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800581
582 // Profile file to use
583 double top_k_profile_threshold = CompilerOptions::kDefaultTopKProfileThreshold;
584
Andreas Gampe7b2f09e2015-03-02 14:07:33 -0800585 bool debuggable = false;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800586 bool include_patch_information = CompilerOptions::kDefaultIncludePatchInformation;
David Srbeckya26cb572015-04-22 18:57:06 -0700587 bool generate_debug_info = kIsDebugBuild;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800588 bool watch_dog_enabled = true;
Andreas Gampe6cf49e52015-03-05 13:08:45 -0800589 bool abort_on_hard_verifier_error = false;
Nicolas Geoffray1412dfa2015-03-20 14:48:13 +0000590 bool requested_specific_compiler = false;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800591
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800592 PassManagerOptions pass_manager_options;
593
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800594 std::string error_msg;
595
596 for (int i = 0; i < argc; i++) {
597 const StringPiece option(argv[i]);
598 const bool log_options = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 if (log_options) {
600 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
601 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800602 if (option.starts_with("--dex-file=")) {
603 dex_filenames_.push_back(option.substr(strlen("--dex-file=")).data());
604 } else if (option.starts_with("--dex-location=")) {
605 dex_locations_.push_back(option.substr(strlen("--dex-location=")).data());
606 } else if (option.starts_with("--zip-fd=")) {
607 const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
608 if (!ParseInt(zip_fd_str, &zip_fd_)) {
609 Usage("Failed to parse --zip-fd argument '%s' as an integer", zip_fd_str);
610 }
611 if (zip_fd_ < 0) {
612 Usage("--zip-fd passed a negative value %d", zip_fd_);
613 }
614 } else if (option.starts_with("--zip-location=")) {
615 zip_location_ = option.substr(strlen("--zip-location=")).data();
616 } else if (option.starts_with("--oat-file=")) {
617 oat_filename_ = option.substr(strlen("--oat-file=")).data();
618 } else if (option.starts_with("--oat-symbols=")) {
619 oat_symbols = option.substr(strlen("--oat-symbols=")).data();
620 } else if (option.starts_with("--oat-fd=")) {
621 const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
622 if (!ParseInt(oat_fd_str, &oat_fd_)) {
623 Usage("Failed to parse --oat-fd argument '%s' as an integer", oat_fd_str);
624 }
625 if (oat_fd_ < 0) {
626 Usage("--oat-fd passed a negative value %d", oat_fd_);
627 }
628 } else if (option == "--watch-dog") {
629 watch_dog_enabled = true;
630 } else if (option == "--no-watch-dog") {
631 watch_dog_enabled = false;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800632 } else if (option.starts_with("-j")) {
633 const char* thread_count_str = option.substr(strlen("-j")).data();
634 if (!ParseUint(thread_count_str, &thread_count_)) {
635 Usage("Failed to parse -j argument '%s' as an integer", thread_count_str);
636 }
637 } else if (option.starts_with("--oat-location=")) {
638 oat_location_ = option.substr(strlen("--oat-location=")).data();
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800639 } else if (option.starts_with("--image=")) {
640 image_filename_ = option.substr(strlen("--image=")).data();
641 } else if (option.starts_with("--image-classes=")) {
642 image_classes_filename_ = option.substr(strlen("--image-classes=")).data();
643 } else if (option.starts_with("--image-classes-zip=")) {
644 image_classes_zip_filename_ = option.substr(strlen("--image-classes-zip=")).data();
Andreas Gampe4bf3ae92014-11-11 13:28:29 -0800645 } else if (option.starts_with("--compiled-classes=")) {
646 compiled_classes_filename_ = option.substr(strlen("--compiled-classes=")).data();
647 } else if (option.starts_with("--compiled-classes-zip=")) {
648 compiled_classes_zip_filename_ = option.substr(strlen("--compiled-classes-zip=")).data();
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700649 } else if (option.starts_with("--compiled-methods=")) {
650 compiled_methods_filename_ = option.substr(strlen("--compiled-methods=")).data();
651 } else if (option.starts_with("--compiled-methods-zip=")) {
652 compiled_methods_zip_filename_ = option.substr(strlen("--compiled-methods-zip=")).data();
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800653 } else if (option.starts_with("--base=")) {
654 const char* image_base_str = option.substr(strlen("--base=")).data();
655 char* end;
656 image_base_ = strtoul(image_base_str, &end, 16);
657 if (end == image_base_str || *end != '\0') {
658 Usage("Failed to parse hexadecimal value for option %s", option.data());
659 }
660 } else if (option.starts_with("--boot-image=")) {
661 boot_image_filename = option.substr(strlen("--boot-image=")).data();
662 } else if (option.starts_with("--android-root=")) {
663 android_root_ = option.substr(strlen("--android-root=")).data();
664 } else if (option.starts_with("--instruction-set=")) {
665 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
666 // StringPiece is not necessarily zero-terminated, so need to make a copy and ensure it.
Dan Albert6fc59ab2014-12-11 14:09:51 -0800667 std::unique_ptr<char[]> buf(new char[instruction_set_str.length() + 1]);
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800668 strncpy(buf.get(), instruction_set_str.data(), instruction_set_str.length());
669 buf.get()[instruction_set_str.length()] = 0;
670 instruction_set_ = GetInstructionSetFromString(buf.get());
671 // arm actually means thumb2.
672 if (instruction_set_ == InstructionSet::kArm) {
673 instruction_set_ = InstructionSet::kThumb2;
674 }
675 } else if (option.starts_with("--instruction-set-variant=")) {
676 StringPiece str = option.substr(strlen("--instruction-set-variant=")).data();
677 instruction_set_features_.reset(
678 InstructionSetFeatures::FromVariant(instruction_set_, str.as_string(), &error_msg));
679 if (instruction_set_features_.get() == nullptr) {
680 Usage("%s", error_msg.c_str());
681 }
682 } else if (option.starts_with("--instruction-set-features=")) {
683 StringPiece str = option.substr(strlen("--instruction-set-features=")).data();
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800684 if (instruction_set_features_.get() == nullptr) {
Ian Rogersd582fa42014-11-05 23:46:43 -0800685 instruction_set_features_.reset(
686 InstructionSetFeatures::FromVariant(instruction_set_, "default", &error_msg));
687 if (instruction_set_features_.get() == nullptr) {
688 Usage("Problem initializing default instruction set features variant: %s",
689 error_msg.c_str());
690 }
691 }
692 instruction_set_features_.reset(
693 instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg));
694 if (instruction_set_features_.get() == nullptr) {
695 Usage("Error parsing '%s': %s", option.data(), error_msg.c_str());
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800696 }
697 } else if (option.starts_with("--compiler-backend=")) {
Nicolas Geoffray1412dfa2015-03-20 14:48:13 +0000698 requested_specific_compiler = true;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800699 StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data();
700 if (backend_str == "Quick") {
701 compiler_kind_ = Compiler::kQuick;
702 } else if (backend_str == "Optimizing") {
703 compiler_kind_ = Compiler::kOptimizing;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800704 } else {
705 Usage("Unknown compiler backend: %s", backend_str.data());
706 }
707 } else if (option.starts_with("--compiler-filter=")) {
708 compiler_filter_string = option.substr(strlen("--compiler-filter=")).data();
709 } else if (option == "--compile-pic") {
710 compile_pic = true;
711 } else if (option.starts_with("--huge-method-max=")) {
712 const char* threshold = option.substr(strlen("--huge-method-max=")).data();
713 if (!ParseInt(threshold, &huge_method_threshold)) {
714 Usage("Failed to parse --huge-method-max '%s' as an integer", threshold);
715 }
716 if (huge_method_threshold < 0) {
717 Usage("--huge-method-max passed a negative value %s", huge_method_threshold);
718 }
719 } else if (option.starts_with("--large-method-max=")) {
720 const char* threshold = option.substr(strlen("--large-method-max=")).data();
721 if (!ParseInt(threshold, &large_method_threshold)) {
722 Usage("Failed to parse --large-method-max '%s' as an integer", threshold);
723 }
724 if (large_method_threshold < 0) {
725 Usage("--large-method-max passed a negative value %s", large_method_threshold);
726 }
727 } else if (option.starts_with("--small-method-max=")) {
728 const char* threshold = option.substr(strlen("--small-method-max=")).data();
729 if (!ParseInt(threshold, &small_method_threshold)) {
730 Usage("Failed to parse --small-method-max '%s' as an integer", threshold);
731 }
732 if (small_method_threshold < 0) {
733 Usage("--small-method-max passed a negative value %s", small_method_threshold);
734 }
735 } else if (option.starts_with("--tiny-method-max=")) {
736 const char* threshold = option.substr(strlen("--tiny-method-max=")).data();
737 if (!ParseInt(threshold, &tiny_method_threshold)) {
738 Usage("Failed to parse --tiny-method-max '%s' as an integer", threshold);
739 }
740 if (tiny_method_threshold < 0) {
741 Usage("--tiny-method-max passed a negative value %s", tiny_method_threshold);
742 }
743 } else if (option.starts_with("--num-dex-methods=")) {
744 const char* threshold = option.substr(strlen("--num-dex-methods=")).data();
745 if (!ParseInt(threshold, &num_dex_methods_threshold)) {
746 Usage("Failed to parse --num-dex-methods '%s' as an integer", threshold);
747 }
748 if (num_dex_methods_threshold < 0) {
749 Usage("--num-dex-methods passed a negative value %s", num_dex_methods_threshold);
750 }
Calin Juravle8f96df82015-07-29 15:58:48 +0100751 } else if (option.starts_with("--inline-depth-limit=")) {
752 const char* limit = option.substr(strlen("--inline-depth-limit=")).data();
753 if (!ParseInt(limit, &inline_depth_limit)) {
754 Usage("Failed to parse --inline-depth-limit '%s' as an integer", limit);
755 }
756 if (inline_depth_limit < 0) {
757 Usage("--inline-depth-limit passed a negative value %s", inline_depth_limit);
758 }
759 } else if (option.starts_with("--inline-max-code-units=")) {
760 const char* code_units = option.substr(strlen("--inline-max-code-units=")).data();
761 if (!ParseInt(code_units, &inline_max_code_units)) {
762 Usage("Failed to parse --inline-max-code-units '%s' as an integer", code_units);
763 }
764 if (inline_max_code_units < 0) {
765 Usage("--inline-max-code-units passed a negative value %s", inline_max_code_units);
766 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800767 } else if (option == "--host") {
768 is_host_ = true;
769 } else if (option == "--runtime-arg") {
770 if (++i >= argc) {
771 Usage("Missing required argument for --runtime-arg");
772 }
773 if (log_options) {
774 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
775 }
776 runtime_args_.push_back(argv[i]);
777 } else if (option == "--dump-timing") {
778 dump_timing_ = true;
779 } else if (option == "--dump-passes") {
780 dump_passes_ = true;
David Brazdil866c0312015-01-13 21:21:31 +0000781 } else if (option.starts_with("--dump-cfg=")) {
782 dump_cfg_file_name_ = option.substr(strlen("--dump-cfg=")).data();
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800783 } else if (option == "--dump-stats") {
784 dump_stats_ = true;
David Srbeckya26cb572015-04-22 18:57:06 -0700785 } else if (option == "--generate-debug-info" || option == "-g") {
786 generate_debug_info = true;
787 } else if (option == "--no-generate-debug-info") {
788 generate_debug_info = false;
Andreas Gampe7b2f09e2015-03-02 14:07:33 -0800789 } else if (option == "--debuggable") {
790 debuggable = true;
David Srbeckya26cb572015-04-22 18:57:06 -0700791 generate_debug_info = true;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800792 } else if (option.starts_with("--profile-file=")) {
793 profile_file_ = option.substr(strlen("--profile-file=")).data();
794 VLOG(compiler) << "dex2oat: profile file is " << profile_file_;
795 } else if (option == "--no-profile-file") {
796 // No profile
797 } else if (option.starts_with("--top-k-profile-threshold=")) {
798 ParseDouble(option.data(), '=', 0.0, 100.0, &top_k_profile_threshold);
799 } else if (option == "--print-pass-names") {
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800800 pass_manager_options.SetPrintPassNames(true);
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800801 } else if (option.starts_with("--disable-passes=")) {
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800802 const std::string disable_passes = option.substr(strlen("--disable-passes=")).data();
803 pass_manager_options.SetDisablePassList(disable_passes);
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800804 } else if (option.starts_with("--print-passes=")) {
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800805 const std::string print_passes = option.substr(strlen("--print-passes=")).data();
806 pass_manager_options.SetPrintPassList(print_passes);
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800807 } else if (option == "--print-all-passes") {
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800808 pass_manager_options.SetPrintAllPasses();
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800809 } else if (option.starts_with("--dump-cfg-passes=")) {
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800810 const std::string dump_passes_string = option.substr(strlen("--dump-cfg-passes=")).data();
811 pass_manager_options.SetDumpPassList(dump_passes_string);
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800812 } else if (option == "--print-pass-options") {
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800813 pass_manager_options.SetPrintPassOptions(true);
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800814 } else if (option.starts_with("--pass-options=")) {
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800815 const std::string options = option.substr(strlen("--pass-options=")).data();
816 pass_manager_options.SetOverriddenPassOptions(options);
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800817 } else if (option == "--include-patch-information") {
818 include_patch_information = true;
819 } else if (option == "--no-include-patch-information") {
820 include_patch_information = false;
821 } else if (option.starts_with("--verbose-methods=")) {
Brian Carlstrom95b033b2014-12-03 22:29:37 -0800822 // TODO: rather than switch off compiler logging, make all VLOG(compiler) messages
823 // conditional on having verbost methods.
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800824 gLogVerbosity.compiler = false;
825 Split(option.substr(strlen("--verbose-methods=")).ToString(), ',', &verbose_methods_);
Andreas Gampedbfe2542014-11-25 22:21:42 -0800826 } else if (option.starts_with("--dump-init-failures=")) {
827 std::string file_name = option.substr(strlen("--dump-init-failures=")).data();
828 init_failure_output_.reset(new std::ofstream(file_name));
829 if (init_failure_output_.get() == nullptr) {
830 LOG(ERROR) << "Failed to allocate ofstream";
831 } else if (init_failure_output_->fail()) {
832 LOG(ERROR) << "Failed to open " << file_name << " for writing the initialization "
833 << "failures.";
834 init_failure_output_.reset();
835 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800836 } else if (option.starts_with("--swap-file=")) {
837 swap_file_name_ = option.substr(strlen("--swap-file=")).data();
838 } else if (option.starts_with("--swap-fd=")) {
839 const char* swap_fd_str = option.substr(strlen("--swap-fd=")).data();
840 if (!ParseInt(swap_fd_str, &swap_fd_)) {
841 Usage("Failed to parse --swap-fd argument '%s' as an integer", swap_fd_str);
842 }
843 if (swap_fd_ < 0) {
844 Usage("--swap-fd passed a negative value %d", swap_fd_);
845 }
Andreas Gampe6cf49e52015-03-05 13:08:45 -0800846 } else if (option == "--abort-on-hard-verifier-error") {
847 abort_on_hard_verifier_error = true;
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800848 } else {
849 Usage("Unknown argument %s", option.data());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800851 }
852
Chitti Babu Theegalac06972f2015-08-06 19:52:05 +0530853 // Override the number of compiler threads with optimal value (thru system property)
854 #ifdef HAVE_ANDROID_OS
855 const char* propertyName = "ro.sys.fw.dex2oat_thread_count";
856 char thread_count_str[PROPERTY_VALUE_MAX];
857
858 if (property_get(propertyName, thread_count_str, "") > 0) {
859 if (ParseUint(thread_count_str, &thread_count_)) {
860 LOG(INFO) << "Adjusted thread count (for runtime dex2oat): " << thread_count_ << ", " << thread_count_str;
861 }
862 }
863 #endif
864
Nicolas Geoffray1412dfa2015-03-20 14:48:13 +0000865 image_ = (!image_filename_.empty());
866 if (!requested_specific_compiler && !kUseOptimizingCompiler) {
867 // If no specific compiler is requested, the current behavior is
868 // to compile the boot image with Quick, and the rest with Optimizing.
869 compiler_kind_ = image_ ? Compiler::kQuick : Compiler::kOptimizing;
870 }
871
Nicolas Geoffray9bb492a2014-11-25 23:42:00 +0000872 if (compiler_kind_ == Compiler::kOptimizing) {
873 // Optimizing only supports PIC mode.
874 compile_pic = true;
875 }
876
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800877 if (oat_filename_.empty() && oat_fd_ == -1) {
878 Usage("Output must be supplied with either --oat-file or --oat-fd");
879 }
880
881 if (!oat_filename_.empty() && oat_fd_ != -1) {
882 Usage("--oat-file should not be used with --oat-fd");
883 }
884
885 if (!oat_symbols.empty() && oat_fd_ != -1) {
886 Usage("--oat-symbols should not be used with --oat-fd");
887 }
888
889 if (!oat_symbols.empty() && is_host_) {
890 Usage("--oat-symbols should not be used with --host");
891 }
892
893 if (oat_fd_ != -1 && !image_filename_.empty()) {
894 Usage("--oat-fd should not be used with --image");
895 }
896
897 if (android_root_.empty()) {
898 const char* android_root_env_var = getenv("ANDROID_ROOT");
899 if (android_root_env_var == nullptr) {
900 Usage("--android-root unspecified and ANDROID_ROOT not set");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700901 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800902 android_root_ += android_root_env_var;
903 }
904
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800905 if (!image_ && boot_image_filename.empty()) {
906 boot_image_filename += android_root_;
907 boot_image_filename += "/framework/boot.art";
908 }
909 if (!boot_image_filename.empty()) {
910 boot_image_option_ += "-Ximage:";
911 boot_image_option_ += boot_image_filename;
912 }
913
914 if (image_classes_filename_ != nullptr && !image_) {
915 Usage("--image-classes should only be used with --image");
916 }
917
918 if (image_classes_filename_ != nullptr && !boot_image_option_.empty()) {
919 Usage("--image-classes should not be used with --boot-image");
920 }
921
922 if (image_classes_zip_filename_ != nullptr && image_classes_filename_ == nullptr) {
923 Usage("--image-classes-zip should be used with --image-classes");
924 }
925
Andreas Gampe4bf3ae92014-11-11 13:28:29 -0800926 if (compiled_classes_filename_ != nullptr && !image_) {
927 Usage("--compiled-classes should only be used with --image");
928 }
929
930 if (compiled_classes_filename_ != nullptr && !boot_image_option_.empty()) {
931 Usage("--compiled-classes should not be used with --boot-image");
932 }
933
934 if (compiled_classes_zip_filename_ != nullptr && compiled_classes_filename_ == nullptr) {
935 Usage("--compiled-classes-zip should be used with --compiled-classes");
936 }
937
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800938 if (dex_filenames_.empty() && zip_fd_ == -1) {
939 Usage("Input must be supplied with either --dex-file or --zip-fd");
940 }
941
942 if (!dex_filenames_.empty() && zip_fd_ != -1) {
943 Usage("--dex-file should not be used with --zip-fd");
944 }
945
946 if (!dex_filenames_.empty() && !zip_location_.empty()) {
947 Usage("--dex-file should not be used with --zip-location");
948 }
949
950 if (dex_locations_.empty()) {
951 for (const char* dex_file_name : dex_filenames_) {
952 dex_locations_.push_back(dex_file_name);
953 }
954 } else if (dex_locations_.size() != dex_filenames_.size()) {
955 Usage("--dex-location arguments do not match --dex-file arguments");
956 }
957
958 if (zip_fd_ != -1 && zip_location_.empty()) {
959 Usage("--zip-location should be supplied with --zip-fd");
960 }
961
962 if (boot_image_option_.empty()) {
963 if (image_base_ == 0) {
964 Usage("Non-zero --base not specified");
965 }
966 }
967
968 oat_stripped_ = oat_filename_;
969 if (!oat_symbols.empty()) {
970 oat_unstripped_ = oat_symbols;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700971 } else {
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800972 oat_unstripped_ = oat_filename_;
973 }
974
975 // If no instruction set feature was given, use the default one for the target
976 // instruction set.
977 if (instruction_set_features_.get() == nullptr) {
978 instruction_set_features_.reset(
Ian Rogersd582fa42014-11-05 23:46:43 -0800979 InstructionSetFeatures::FromVariant(instruction_set_, "default", &error_msg));
980 if (instruction_set_features_.get() == nullptr) {
981 Usage("Problem initializing default instruction set features variant: %s",
982 error_msg.c_str());
983 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800984 }
985
986 if (instruction_set_ == kRuntimeISA) {
987 std::unique_ptr<const InstructionSetFeatures> runtime_features(
988 InstructionSetFeatures::FromCppDefines());
989 if (!instruction_set_features_->Equals(runtime_features.get())) {
990 LOG(WARNING) << "Mismatch between dex2oat instruction set features ("
991 << *instruction_set_features_ << ") and those of dex2oat executable ("
992 << *runtime_features <<") for the command line:\n"
993 << CommandLine();
994 }
995 }
996
997 if (compiler_filter_string == nullptr) {
Douglas Leung027f0ff2015-02-27 19:05:03 -0800998 compiler_filter_string = "speed";
Andreas Gampe88ec7f42014-11-05 10:18:32 -0800999 }
Maja Gagic6ea651f2015-02-24 16:55:04 +01001000
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001001 CHECK(compiler_filter_string != nullptr);
1002 CompilerOptions::CompilerFilter compiler_filter = CompilerOptions::kDefaultCompilerFilter;
1003 if (strcmp(compiler_filter_string, "verify-none") == 0) {
1004 compiler_filter = CompilerOptions::kVerifyNone;
1005 } else if (strcmp(compiler_filter_string, "interpret-only") == 0) {
1006 compiler_filter = CompilerOptions::kInterpretOnly;
Mathieu Chartiere86deef2015-03-19 13:43:37 -07001007 } else if (strcmp(compiler_filter_string, "verify-at-runtime") == 0) {
1008 compiler_filter = CompilerOptions::kVerifyAtRuntime;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001009 } else if (strcmp(compiler_filter_string, "space") == 0) {
1010 compiler_filter = CompilerOptions::kSpace;
1011 } else if (strcmp(compiler_filter_string, "balanced") == 0) {
1012 compiler_filter = CompilerOptions::kBalanced;
1013 } else if (strcmp(compiler_filter_string, "speed") == 0) {
1014 compiler_filter = CompilerOptions::kSpeed;
1015 } else if (strcmp(compiler_filter_string, "everything") == 0) {
1016 compiler_filter = CompilerOptions::kEverything;
1017 } else if (strcmp(compiler_filter_string, "time") == 0) {
1018 compiler_filter = CompilerOptions::kTime;
1019 } else {
1020 Usage("Unknown --compiler-filter value %s", compiler_filter_string);
1021 }
1022
Roland Levillaind3333892015-08-07 12:22:28 +01001023 // It they are not set, use default values for inlining settings.
1024 // TODO: We should rethink the compiler filter. We mostly save
1025 // time here, which is orthogonal to space.
1026 if (inline_depth_limit == kUnsetInlineDepthLimit) {
1027 inline_depth_limit = (compiler_filter == CompilerOptions::kSpace)
1028 // Implementation of the space filter: limit inlining depth.
1029 ? CompilerOptions::kSpaceFilterInlineDepthLimit
1030 : CompilerOptions::kDefaultInlineDepthLimit;
1031 }
1032 if (inline_max_code_units == kUnsetInlineMaxCodeUnits) {
1033 inline_max_code_units = (compiler_filter == CompilerOptions::kSpace)
1034 // Implementation of the space filter: limit inlining max code units.
1035 ? CompilerOptions::kSpaceFilterInlineMaxCodeUnits
1036 : CompilerOptions::kDefaultInlineMaxCodeUnits;
1037 }
1038
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001039 // Checks are all explicit until we know the architecture.
1040 bool implicit_null_checks = false;
1041 bool implicit_so_checks = false;
1042 bool implicit_suspend_checks = false;
1043 // Set the compilation target's implicit checks options.
1044 switch (instruction_set_) {
1045 case kArm:
1046 case kThumb2:
1047 case kArm64:
1048 case kX86:
1049 case kX86_64:
Douglas Leungccbbda22015-07-02 16:42:08 -07001050 case kMips:
1051 case kMips64:
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001052 implicit_null_checks = true;
1053 implicit_so_checks = true;
1054 break;
1055
1056 default:
1057 // Defaults are correct.
1058 break;
1059 }
1060
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001061 compiler_options_.reset(new CompilerOptions(compiler_filter,
1062 huge_method_threshold,
1063 large_method_threshold,
1064 small_method_threshold,
1065 tiny_method_threshold,
1066 num_dex_methods_threshold,
Calin Juravle8f96df82015-07-29 15:58:48 +01001067 inline_depth_limit,
1068 inline_max_code_units,
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001069 include_patch_information,
1070 top_k_profile_threshold,
Andreas Gampe7b2f09e2015-03-02 14:07:33 -08001071 debuggable,
David Srbeckya26cb572015-04-22 18:57:06 -07001072 generate_debug_info,
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001073 implicit_null_checks,
1074 implicit_so_checks,
1075 implicit_suspend_checks,
1076 compile_pic,
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001077 verbose_methods_.empty() ?
1078 nullptr :
Andreas Gampedbfe2542014-11-25 22:21:42 -08001079 &verbose_methods_,
Mathieu Chartier5bdab122015-01-26 18:30:19 -08001080 new PassManagerOptions(pass_manager_options),
Andreas Gampe6cf49e52015-03-05 13:08:45 -08001081 init_failure_output_.get(),
1082 abort_on_hard_verifier_error));
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001083
1084 // Done with usage checks, enable watchdog if requested
1085 if (watch_dog_enabled) {
1086 watchdog_.reset(new WatchDog(true));
1087 }
1088
1089 // Fill some values into the key-value store for the oat header.
1090 key_value_store_.reset(new SafeMap<std::string, std::string>());
1091
1092 // Insert some compiler things.
1093 {
1094 std::ostringstream oss;
1095 for (int i = 0; i < argc; ++i) {
1096 if (i > 0) {
1097 oss << ' ';
1098 }
1099 oss << argv[i];
1100 }
1101 key_value_store_->Put(OatHeader::kDex2OatCmdLineKey, oss.str());
1102 oss.str(""); // Reset.
1103 oss << kRuntimeISA;
1104 key_value_store_->Put(OatHeader::kDex2OatHostKey, oss.str());
Sebastien Hertz094ff2c2015-05-13 12:14:05 +02001105 key_value_store_->Put(OatHeader::kPicKey,
1106 compile_pic ? OatHeader::kTrueValue : OatHeader::kFalseValue);
1107 key_value_store_->Put(OatHeader::kDebuggableKey,
1108 debuggable ? OatHeader::kTrueValue : OatHeader::kFalseValue);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001109 }
1110 }
1111
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001112 // Check whether the oat output file is writable, and open it for later. Also open a swap file,
1113 // if a name is given.
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001114 bool OpenFile() {
1115 bool create_file = !oat_unstripped_.empty(); // as opposed to using open file descriptor
1116 if (create_file) {
Andreas Gampe52f0aeb2015-06-26 18:58:42 -07001117 // We're supposed to create this file. If the file already exists, it may be in use currently.
1118 // We must not change the content of that file, then. So unlink it first.
1119 unlink(oat_unstripped_.c_str());
1120
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001121 oat_file_.reset(OS::CreateEmptyFile(oat_unstripped_.c_str()));
1122 if (oat_location_.empty()) {
1123 oat_location_ = oat_filename_;
1124 }
1125 } else {
Andreas Gampe4303ba92014-11-06 01:00:46 -08001126 oat_file_.reset(new File(oat_fd_, oat_location_, true));
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001127 oat_file_->DisableAutoClose();
Andreas Gampe4303ba92014-11-06 01:00:46 -08001128 if (oat_file_->SetLength(0) != 0) {
1129 PLOG(WARNING) << "Truncating oat file " << oat_location_ << " failed.";
1130 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001131 }
1132 if (oat_file_.get() == nullptr) {
1133 PLOG(ERROR) << "Failed to create oat file: " << oat_location_;
1134 return false;
1135 }
1136 if (create_file && fchmod(oat_file_->Fd(), 0644) != 0) {
1137 PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location_;
Andreas Gampe4303ba92014-11-06 01:00:46 -08001138 oat_file_->Erase();
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001139 return false;
1140 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001141
1142 // Swap file handling.
1143 //
1144 // If the swap fd is not -1, we assume this is the file descriptor of an open but unlinked file
1145 // that we can use for swap.
1146 //
1147 // If the swap fd is -1 and we have a swap-file string, open the given file as a swap file. We
1148 // will immediately unlink to satisfy the swap fd assumption.
1149 if (swap_fd_ == -1 && !swap_file_name_.empty()) {
1150 std::unique_ptr<File> swap_file(OS::CreateEmptyFile(swap_file_name_.c_str()));
1151 if (swap_file.get() == nullptr) {
1152 PLOG(ERROR) << "Failed to create swap file: " << swap_file_name_;
1153 return false;
1154 }
1155 swap_fd_ = swap_file->Fd();
1156 swap_file->MarkUnchecked(); // We don't we to track this, it will be unlinked immediately.
1157 swap_file->DisableAutoClose(); // We'll handle it ourselves, the File object will be
1158 // released immediately.
1159 unlink(swap_file_name_.c_str());
1160 }
1161
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001162 return true;
1163 }
1164
Andreas Gampea650e702014-12-03 14:28:02 -08001165 void EraseOatFile() {
1166 DCHECK(oat_file_.get() != nullptr);
1167 oat_file_->Erase();
1168 oat_file_.reset();
1169 }
1170
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001171 // Set up the environment for compilation. Includes starting the runtime and loading/opening the
1172 // boot class path.
1173 bool Setup() {
1174 TimingLogger::ScopedTiming t("dex2oat Setup", timings_);
1175 RuntimeOptions runtime_options;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001176 art::MemMap::Init(); // For ZipEntry::ExtractToMemMap.
1177 if (boot_image_option_.empty()) {
Richard Uhlerc2752592015-01-02 13:28:22 -08001178 std::string boot_class_path = "-Xbootclasspath:";
1179 boot_class_path += Join(dex_filenames_, ':');
1180 runtime_options.push_back(std::make_pair(boot_class_path, nullptr));
1181 std::string boot_class_path_locations = "-Xbootclasspath-locations:";
1182 boot_class_path_locations += Join(dex_locations_, ':');
1183 runtime_options.push_back(std::make_pair(boot_class_path_locations, nullptr));
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001184 } else {
Richard Uhlerc2752592015-01-02 13:28:22 -08001185 runtime_options.push_back(std::make_pair(boot_image_option_, nullptr));
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001186 }
1187 for (size_t i = 0; i < runtime_args_.size(); i++) {
1188 runtime_options.push_back(std::make_pair(runtime_args_[i], nullptr));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001189 }
Brian Carlstromd76e0832013-08-29 15:17:42 -07001190
Andreas Gampecc077602015-09-17 14:03:21 -07001191 verification_results_ = new VerificationResults(compiler_options_.get());
Andreas Gampe4585f872015-03-27 23:45:15 -07001192 callbacks_.reset(new QuickCompilerCallbacks(
Andreas Gampecc077602015-09-17 14:03:21 -07001193 verification_results_,
Andreas Gampe4585f872015-03-27 23:45:15 -07001194 &method_inliner_map_,
1195 image_ ?
1196 CompilerCallbacks::CallbackMode::kCompileBootImage :
1197 CompilerCallbacks::CallbackMode::kCompileApp));
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001198 runtime_options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
1199 runtime_options.push_back(
1200 std::make_pair("imageinstructionset", GetInstructionSetString(instruction_set_)));
1201
Andreas Gampe1d00add2015-02-27 19:35:46 -08001202 // Only allow no boot image for the runtime if we're compiling one. When we compile an app,
1203 // we don't want fallback mode, it will abort as we do not push a boot classpath (it might
1204 // have been stripped in preopting, anyways).
1205 if (!image_) {
1206 runtime_options.push_back(std::make_pair("-Xno-dex-file-fallback", nullptr));
1207 }
1208
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001209 if (!CreateRuntime(runtime_options)) {
1210 return false;
1211 }
1212
1213 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
1214 // Runtime::Start, give it away now so that we don't starve GC.
1215 Thread* self = Thread::Current();
1216 self->TransitionFromRunnableToSuspended(kNative);
1217 // If we're doing the image, override the compiler filter to force full compilation. Must be
1218 // done ahead of WellKnownClasses::Init that causes verification. Note: doesn't force
1219 // compilation of class initializers.
1220 // Whilst we're in native take the opportunity to initialize well known classes.
1221 WellKnownClasses::Init(self->GetJniEnv());
1222
1223 // If --image-classes was specified, calculate the full list of classes to include in the image
1224 if (image_classes_filename_ != nullptr) {
1225 std::string error_msg;
1226 if (image_classes_zip_filename_ != nullptr) {
1227 image_classes_.reset(ReadImageClassesFromZip(image_classes_zip_filename_,
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001228 image_classes_filename_,
1229 &error_msg));
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001230 } else {
1231 image_classes_.reset(ReadImageClassesFromFile(image_classes_filename_));
1232 }
1233 if (image_classes_.get() == nullptr) {
1234 LOG(ERROR) << "Failed to create list of image classes from '" << image_classes_filename_ <<
1235 "': " << error_msg;
1236 return false;
1237 }
1238 } else if (image_) {
Andreas Gampeb1fcead2015-04-20 18:53:51 -07001239 image_classes_.reset(new std::unordered_set<std::string>);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001240 }
Andreas Gampe4bf3ae92014-11-11 13:28:29 -08001241 // If --compiled-classes was specified, calculate the full list of classes to compile in the
1242 // image.
1243 if (compiled_classes_filename_ != nullptr) {
1244 std::string error_msg;
1245 if (compiled_classes_zip_filename_ != nullptr) {
1246 compiled_classes_.reset(ReadImageClassesFromZip(compiled_classes_zip_filename_,
1247 compiled_classes_filename_,
1248 &error_msg));
1249 } else {
1250 compiled_classes_.reset(ReadImageClassesFromFile(compiled_classes_filename_));
1251 }
1252 if (compiled_classes_.get() == nullptr) {
1253 LOG(ERROR) << "Failed to create list of compiled classes from '"
1254 << compiled_classes_filename_ << "': " << error_msg;
1255 return false;
1256 }
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001257 } else {
Andreas Gampe4bf3ae92014-11-11 13:28:29 -08001258 compiled_classes_.reset(nullptr); // By default compile everything.
1259 }
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001260 // If --compiled-methods was specified, read the methods to compile from the given file(s).
1261 if (compiled_methods_filename_ != nullptr) {
1262 std::string error_msg;
1263 if (compiled_methods_zip_filename_ != nullptr) {
1264 compiled_methods_.reset(ReadCommentedInputFromZip(compiled_methods_zip_filename_,
1265 compiled_methods_filename_,
1266 nullptr, // No post-processing.
1267 &error_msg));
1268 } else {
1269 compiled_methods_.reset(ReadCommentedInputFromFile(compiled_methods_filename_,
1270 nullptr)); // No post-processing.
1271 }
1272 if (compiled_methods_.get() == nullptr) {
1273 LOG(ERROR) << "Failed to create list of compiled methods from '"
1274 << compiled_methods_filename_ << "': " << error_msg;
1275 return false;
1276 }
1277 } else {
1278 compiled_methods_.reset(nullptr); // By default compile everything.
1279 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001280
1281 if (boot_image_option_.empty()) {
1282 dex_files_ = Runtime::Current()->GetClassLinker()->GetBootClassPath();
1283 } else {
1284 if (dex_filenames_.empty()) {
1285 ATRACE_BEGIN("Opening zip archive from file descriptor");
1286 std::string error_msg;
1287 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd_,
1288 zip_location_.c_str(),
1289 &error_msg));
1290 if (zip_archive.get() == nullptr) {
1291 LOG(ERROR) << "Failed to open zip from file descriptor for '" << zip_location_ << "': "
1292 << error_msg;
1293 return false;
Brian Carlstromf79fccb2014-02-20 08:55:10 -08001294 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001295 if (!DexFile::OpenFromZip(*zip_archive.get(), zip_location_, &error_msg, &opened_dex_files_)) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001296 LOG(ERROR) << "Failed to open dex from file descriptor for zip file '" << zip_location_
1297 << "': " << error_msg;
1298 return false;
1299 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001300 for (auto& dex_file : opened_dex_files_) {
1301 dex_files_.push_back(dex_file.get());
1302 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001303 ATRACE_END();
1304 } else {
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001305 size_t failure_count = OpenDexFiles(dex_filenames_, dex_locations_, &opened_dex_files_);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001306 if (failure_count > 0) {
1307 LOG(ERROR) << "Failed to open some dex files: " << failure_count;
1308 return false;
1309 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001310 for (auto& dex_file : opened_dex_files_) {
1311 dex_files_.push_back(dex_file.get());
1312 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001313 }
1314
1315 constexpr bool kSaveDexInput = false;
1316 if (kSaveDexInput) {
1317 for (size_t i = 0; i < dex_files_.size(); ++i) {
1318 const DexFile* dex_file = dex_files_[i];
Brian Carlstrom95b033b2014-12-03 22:29:37 -08001319 std::string tmp_file_name(StringPrintf("/data/local/tmp/dex2oat.%d.%zd.dex",
1320 getpid(), i));
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001321 std::unique_ptr<File> tmp_file(OS::CreateEmptyFile(tmp_file_name.c_str()));
1322 if (tmp_file.get() == nullptr) {
1323 PLOG(ERROR) << "Failed to open file " << tmp_file_name
1324 << ". Try: adb shell chmod 777 /data/local/tmp";
1325 continue;
1326 }
Andreas Gampe4303ba92014-11-06 01:00:46 -08001327 // This is just dumping files for debugging. Ignore errors, and leave remnants.
1328 UNUSED(tmp_file->WriteFully(dex_file->Begin(), dex_file->Size()));
1329 UNUSED(tmp_file->Flush());
1330 UNUSED(tmp_file->Close());
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001331 LOG(INFO) << "Wrote input to " << tmp_file_name;
1332 }
Brian Carlstromf79fccb2014-02-20 08:55:10 -08001333 }
1334 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001335 // Ensure opened dex files are writable for dex-to-dex transformations.
1336 for (const auto& dex_file : dex_files_) {
1337 if (!dex_file->EnableWrite()) {
1338 PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_file->GetLocation() << "'\n";
Andreas Gampe7ba64962014-10-23 11:37:40 -07001339 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001340 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001341
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001342 // If we use a swap file, ensure we are above the threshold to make it necessary.
1343 if (swap_fd_ != -1) {
1344 if (!UseSwap(image_, dex_files_)) {
1345 close(swap_fd_);
1346 swap_fd_ = -1;
Andreas Gampef99bcd22015-04-24 16:22:18 -07001347 VLOG(compiler) << "Decided to run without swap.";
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001348 } else {
Andreas Gampef99bcd22015-04-24 16:22:18 -07001349 LOG(INFO) << "Large app, accepted running with swap.";
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001350 }
1351 }
1352 // Note that dex2oat won't close the swap_fd_. The compiler driver's swap space will do that.
1353
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001354 /*
1355 * If we're not in interpret-only or verify-none mode, go ahead and compile small applications.
1356 * Don't bother to check if we're doing the image.
1357 */
Brian Carlstrom95b033b2014-12-03 22:29:37 -08001358 if (!image_ &&
1359 compiler_options_->IsCompilationEnabled() &&
1360 compiler_kind_ == Compiler::kQuick) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001361 size_t num_methods = 0;
1362 for (size_t i = 0; i != dex_files_.size(); ++i) {
1363 const DexFile* dex_file = dex_files_[i];
1364 CHECK(dex_file != nullptr);
1365 num_methods += dex_file->NumMethodIds();
1366 }
1367 if (num_methods <= compiler_options_->GetNumDexMethodsThreshold()) {
1368 compiler_options_->SetCompilerFilter(CompilerOptions::kSpeed);
1369 VLOG(compiler) << "Below method threshold, compiling anyways";
1370 }
1371 }
1372
1373 return true;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001374 }
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001375
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001376 // Create and invoke the compiler driver. This will compile all the dex files.
1377 void Compile() {
1378 TimingLogger::ScopedTiming t("dex2oat Compile", timings_);
1379 compiler_phases_timings_.reset(new CumulativeLogger("compilation times"));
Vladimir Markof4da6752014-08-01 19:04:18 +01001380
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001381 // Handle and ClassLoader creation needs to come after Runtime::Create
1382 jobject class_loader = nullptr;
1383 Thread* self = Thread::Current();
1384 if (!boot_image_option_.empty()) {
1385 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001386 OpenClassPathFiles(runtime_->GetClassPathString(), dex_files_, &class_path_files_);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001387 ScopedObjectAccess soa(self);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001388
1389 // Classpath: first the class-path given.
1390 std::vector<const DexFile*> class_path_files;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001391 for (auto& class_path_file : class_path_files_) {
1392 class_path_files.push_back(class_path_file.get());
1393 }
Andreas Gampe7848da42015-04-09 11:15:04 -07001394
1395 // Store the classpath we have right now.
1396 key_value_store_->Put(OatHeader::kClassPathKey,
1397 OatFile::EncodeDexFileDependencies(class_path_files));
1398
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001399 // Then the dex files we'll compile. Thus we'll resolve the class-path first.
1400 class_path_files.insert(class_path_files.end(), dex_files_.begin(), dex_files_.end());
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001401
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07001402 class_loader = class_linker->CreatePathClassLoader(self, class_path_files);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001403 }
1404
Andreas Gampecc077602015-09-17 14:03:21 -07001405 driver_ = new CompilerDriver(compiler_options_.get(),
1406 verification_results_,
1407 &method_inliner_map_,
1408 compiler_kind_,
1409 instruction_set_,
1410 instruction_set_features_.get(),
1411 image_,
1412 image_classes_.release(),
1413 compiled_classes_.release(),
1414 nullptr,
1415 thread_count_,
1416 dump_stats_,
1417 dump_passes_,
1418 dump_cfg_file_name_,
1419 compiler_phases_timings_.get(),
1420 swap_fd_,
1421 profile_file_);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001422
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001423 driver_->CompileAll(class_loader, dex_files_, timings_);
Vladimir Markof4da6752014-08-01 19:04:18 +01001424 }
1425
Brian Carlstrom7940e442013-07-12 13:46:57 -07001426 // Notes on the interleaving of creating the image and oat file to
1427 // ensure the references between the two are correct.
1428 //
1429 // Currently we have a memory layout that looks something like this:
1430 //
1431 // +--------------+
1432 // | image |
1433 // +--------------+
1434 // | boot oat |
1435 // +--------------+
1436 // | alloc spaces |
1437 // +--------------+
1438 //
Brian Carlstrom45602482013-07-21 22:07:55 -07001439 // There are several constraints on the loading of the image and boot.oat.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001440 //
1441 // 1. The image is expected to be loaded at an absolute address and
1442 // contains Objects with absolute pointers within the image.
1443 //
1444 // 2. There are absolute pointers from Methods in the image to their
1445 // code in the oat.
1446 //
1447 // 3. There are absolute pointers from the code in the oat to Methods
1448 // in the image.
1449 //
1450 // 4. There are absolute pointers from code in the oat to other code
1451 // in the oat.
1452 //
1453 // To get this all correct, we go through several steps.
1454 //
Vladimir Markof4da6752014-08-01 19:04:18 +01001455 // 1. We prepare offsets for all data in the oat file and calculate
1456 // the oat data size and code size. During this stage, we also set
1457 // oat code offsets in methods for use by the image writer.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001458 //
Vladimir Markof4da6752014-08-01 19:04:18 +01001459 // 2. We prepare offsets for the objects in the image and calculate
1460 // the image size.
1461 //
1462 // 3. We create the oat file. Originally this was just our own proprietary
1463 // file but now it is contained within an ELF dynamic object (aka an .so
1464 // file). Since we know the image size and oat data size and code size we
1465 // can prepare the ELF headers and we then know the ELF memory segment
1466 // layout and we can now resolve all references. The compiler provides
1467 // LinkerPatch information in each CompiledMethod and we resolve these,
1468 // using the layout information and image object locations provided by
1469 // image writer, as we're writing the method code.
1470 //
1471 // 4. We create the image file. It needs to know where the oat file
Brian Carlstrom7940e442013-07-12 13:46:57 -07001472 // will be loaded after itself. Originally when oat file was simply
1473 // memory mapped so we could predict where its contents were based
1474 // on the file size. Now that it is an ELF file, we need to inspect
1475 // the ELF file to understand the in memory segment layout including
Vladimir Markof4da6752014-08-01 19:04:18 +01001476 // where the oat header is located within.
1477 // TODO: We could just remember this information from step 3.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001478 //
Vladimir Markof4da6752014-08-01 19:04:18 +01001479 // 5. We fixup the ELF program headers so that dlopen will try to
Brian Carlstrom7940e442013-07-12 13:46:57 -07001480 // load the .so at the desired location at runtime by offsetting the
1481 // Elf32_Phdr.p_vaddr values by the desired base address.
Vladimir Markof4da6752014-08-01 19:04:18 +01001482 // TODO: Do this in step 3. We already know the layout there.
1483 //
1484 // Steps 1.-3. are done by the CreateOatFile() above, steps 4.-5.
1485 // are done by the CreateImageFile() below.
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001486
1487
1488 // Write out the generated code part. Calls the OatWriter and ElfBuilder. Also prepares the
1489 // ImageWriter, if necessary.
Andreas Gampe10e477d2014-11-19 12:57:42 -08001490 // Note: Flushing (and closing) the file is the caller's responsibility, except for the failure
1491 // case (when the file will be explicitly erased).
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001492 bool CreateOatFile() {
1493 CHECK(key_value_store_.get() != nullptr);
1494
1495 TimingLogger::ScopedTiming t("dex2oat Oat", timings_);
1496
1497 std::unique_ptr<OatWriter> oat_writer;
1498 {
1499 TimingLogger::ScopedTiming t2("dex2oat OatWriter", timings_);
1500 std::string image_file_location;
1501 uint32_t image_file_location_oat_checksum = 0;
1502 uintptr_t image_file_location_oat_data_begin = 0;
1503 int32_t image_patch_delta = 0;
1504 if (image_) {
1505 PrepareImageWriter(image_base_);
1506 } else {
1507 TimingLogger::ScopedTiming t3("Loading image checksum", timings_);
1508 gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace();
1509 image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum();
1510 image_file_location_oat_data_begin =
1511 reinterpret_cast<uintptr_t>(image_space->GetImageHeader().GetOatDataBegin());
1512 image_file_location = image_space->GetImageFilename();
1513 image_patch_delta = image_space->GetImageHeader().GetPatchDelta();
1514 }
1515
1516 if (!image_file_location.empty()) {
1517 key_value_store_->Put(OatHeader::kImageLocationKey, image_file_location);
1518 }
1519
1520 oat_writer.reset(new OatWriter(dex_files_, image_file_location_oat_checksum,
1521 image_file_location_oat_data_begin,
1522 image_patch_delta,
Andreas Gampecc077602015-09-17 14:03:21 -07001523 driver_,
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001524 image_writer_.get(),
1525 timings_,
1526 key_value_store_.get()));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001527 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001528
1529 if (image_) {
1530 // The OatWriter constructor has already updated offsets in methods and we need to
1531 // prepare method offsets in the image address space for direct method patching.
1532 TimingLogger::ScopedTiming t2("dex2oat Prepare image address space", timings_);
1533 if (!image_writer_->PrepareImageAddressSpace()) {
1534 LOG(ERROR) << "Failed to prepare image address space.";
1535 return false;
1536 }
1537 }
1538
1539 {
1540 TimingLogger::ScopedTiming t2("dex2oat Write ELF", timings_);
1541 if (!driver_->WriteElf(android_root_, is_host_, dex_files_, oat_writer.get(),
1542 oat_file_.get())) {
1543 LOG(ERROR) << "Failed to write ELF file " << oat_file_->GetPath();
1544 return false;
1545 }
1546 }
1547
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001548 VLOG(compiler) << "Oat file written successfully (unstripped): " << oat_location_;
1549 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001550 }
1551
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001552 // If we are compiling an image, invoke the image creation routine. Else just skip.
1553 bool HandleImage() {
1554 if (image_) {
1555 TimingLogger::ScopedTiming t("dex2oat ImageWriter", timings_);
1556 if (!CreateImageFile()) {
1557 return false;
1558 }
1559 VLOG(compiler) << "Image written successfully: " << image_filename_;
Brian Carlstrom45602482013-07-21 22:07:55 -07001560 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001561 return true;
1562 }
1563
Andreas Gampe10e477d2014-11-19 12:57:42 -08001564 // Create a copy from unstripped to stripped.
1565 bool CopyUnstrippedToStripped() {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001566 // If we don't want to strip in place, copy from unstripped location to stripped location.
1567 // We need to strip after image creation because FixupElf needs to use .strtab.
1568 if (oat_unstripped_ != oat_stripped_) {
Andreas Gampe10e477d2014-11-19 12:57:42 -08001569 // If the oat file is still open, flush it.
1570 if (oat_file_.get() != nullptr && oat_file_->IsOpened()) {
1571 if (!FlushCloseOatFile()) {
1572 return false;
Andreas Gampe4303ba92014-11-06 01:00:46 -08001573 }
Andreas Gampe4303ba92014-11-06 01:00:46 -08001574 }
Andreas Gampe10e477d2014-11-19 12:57:42 -08001575
1576 TimingLogger::ScopedTiming t("dex2oat OatFile copy", timings_);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001577 std::unique_ptr<File> in(OS::OpenFileForReading(oat_unstripped_.c_str()));
1578 std::unique_ptr<File> out(OS::CreateEmptyFile(oat_stripped_.c_str()));
1579 size_t buffer_size = 8192;
Dan Albert6fc59ab2014-12-11 14:09:51 -08001580 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001581 while (true) {
1582 int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size));
1583 if (bytes_read <= 0) {
1584 break;
1585 }
1586 bool write_ok = out->WriteFully(buffer.get(), bytes_read);
1587 CHECK(write_ok);
1588 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001589 if (out->FlushCloseOrErase() != 0) {
1590 PLOG(ERROR) << "Failed to flush and close copied oat file: " << oat_stripped_;
1591 return false;
Andreas Gampe10e477d2014-11-19 12:57:42 -08001592 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001593 VLOG(compiler) << "Oat file copied successfully (stripped): " << oat_stripped_;
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +00001594 }
Andreas Gampe10e477d2014-11-19 12:57:42 -08001595 return true;
1596 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001597
Andreas Gampe10e477d2014-11-19 12:57:42 -08001598 bool FlushOatFile() {
Andreas Gampe4303ba92014-11-06 01:00:46 -08001599 if (oat_file_.get() != nullptr) {
Andreas Gampe10e477d2014-11-19 12:57:42 -08001600 TimingLogger::ScopedTiming t2("dex2oat Flush ELF", timings_);
1601 if (oat_file_->Flush() != 0) {
1602 PLOG(ERROR) << "Failed to flush oat file: " << oat_location_ << " / "
1603 << oat_filename_;
1604 oat_file_->Erase();
1605 return false;
1606 }
1607 }
1608 return true;
1609 }
1610
1611 bool FlushCloseOatFile() {
1612 if (oat_file_.get() != nullptr) {
1613 std::unique_ptr<File> tmp(oat_file_.release());
1614 if (tmp->FlushCloseOrErase() != 0) {
1615 PLOG(ERROR) << "Failed to flush and close oat file: " << oat_location_ << " / "
1616 << oat_filename_;
1617 return false;
Andreas Gampe4303ba92014-11-06 01:00:46 -08001618 }
1619 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001620 return true;
1621 }
1622
1623 void DumpTiming() {
1624 if (dump_timing_ || (dump_slow_timing_ && timings_->GetTotalNs() > MsToNs(1000))) {
1625 LOG(INFO) << Dumpable<TimingLogger>(*timings_);
1626 }
1627 if (dump_passes_) {
1628 LOG(INFO) << Dumpable<CumulativeLogger>(*driver_->GetTimingsLogger());
1629 }
1630 }
1631
1632 CompilerOptions* GetCompilerOptions() const {
1633 return compiler_options_.get();
1634 }
1635
Andreas Gampe10e477d2014-11-19 12:57:42 -08001636 bool IsImage() const {
1637 return image_;
1638 }
1639
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001640 bool IsHost() const {
1641 return is_host_;
1642 }
1643
1644 private:
1645 static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames,
1646 const std::vector<const char*>& dex_locations,
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001647 std::vector<std::unique_ptr<const DexFile>>* dex_files) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001648 DCHECK(dex_files != nullptr) << "OpenDexFiles out-param is nullptr";
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001649 size_t failure_count = 0;
1650 for (size_t i = 0; i < dex_filenames.size(); i++) {
1651 const char* dex_filename = dex_filenames[i];
1652 const char* dex_location = dex_locations[i];
1653 ATRACE_BEGIN(StringPrintf("Opening dex file '%s'", dex_filenames[i]).c_str());
1654 std::string error_msg;
1655 if (!OS::FileExists(dex_filename)) {
1656 LOG(WARNING) << "Skipping non-existent dex file '" << dex_filename << "'";
1657 continue;
1658 }
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001659 if (!DexFile::Open(dex_filename, dex_location, &error_msg, dex_files)) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001660 LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "': " << error_msg;
1661 ++failure_count;
1662 }
1663 ATRACE_END();
1664 }
1665 return failure_count;
1666 }
1667
Andreas Gampee3712d02015-04-09 14:46:31 -07001668 // Returns true if dex_files has a dex with the named location. We compare canonical locations,
1669 // so that relative and absolute paths will match. Not caching for the dex_files isn't very
1670 // efficient, but under normal circumstances the list is neither large nor is this part too
1671 // sensitive.
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001672 static bool DexFilesContains(const std::vector<const DexFile*>& dex_files,
1673 const std::string& location) {
Andreas Gampee3712d02015-04-09 14:46:31 -07001674 std::string canonical_location(DexFile::GetDexCanonicalLocation(location.c_str()));
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001675 for (size_t i = 0; i < dex_files.size(); ++i) {
Andreas Gampee3712d02015-04-09 14:46:31 -07001676 if (DexFile::GetDexCanonicalLocation(dex_files[i]->GetLocation().c_str()) ==
1677 canonical_location) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001678 return true;
1679 }
1680 }
1681 return false;
1682 }
1683
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001684 // Appends to opened_dex_files any elements of class_path that dex_files
1685 // doesn't already contain. This will open those dex files as necessary.
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001686 static void OpenClassPathFiles(const std::string& class_path,
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001687 std::vector<const DexFile*> dex_files,
1688 std::vector<std::unique_ptr<const DexFile>>* opened_dex_files) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001689 DCHECK(opened_dex_files != nullptr) << "OpenClassPathFiles out-param is nullptr";
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001690 std::vector<std::string> parsed;
1691 Split(class_path, ':', &parsed);
1692 // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained.
1693 ScopedObjectAccess soa(Thread::Current());
1694 for (size_t i = 0; i < parsed.size(); ++i) {
1695 if (DexFilesContains(dex_files, parsed[i])) {
1696 continue;
1697 }
1698 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001699 if (!DexFile::Open(parsed[i].c_str(), parsed[i].c_str(), &error_msg, opened_dex_files)) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001700 LOG(WARNING) << "Failed to open dex file '" << parsed[i] << "': " << error_msg;
1701 }
1702 }
1703 }
1704
1705 // Create a runtime necessary for compilation.
1706 bool CreateRuntime(const RuntimeOptions& runtime_options)
1707 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) {
1708 if (!Runtime::Create(runtime_options, false)) {
1709 LOG(ERROR) << "Failed to create runtime";
1710 return false;
1711 }
1712 Runtime* runtime = Runtime::Current();
1713 runtime->SetInstructionSet(instruction_set_);
1714 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
1715 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
1716 if (!runtime->HasCalleeSaveMethod(type)) {
1717 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(), type);
1718 }
1719 }
1720 runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001721
1722 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
1723 // set up.
Andreas Gampe99ca40c2015-05-15 19:24:12 -07001724 interpreter::UnstartedRuntime::Initialize();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001725
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001726 runtime->GetClassLinker()->RunRootClinits();
1727 runtime_ = runtime;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001728
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001729 return true;
1730 }
1731
1732 void PrepareImageWriter(uintptr_t image_base) {
1733 image_writer_.reset(new ImageWriter(*driver_, image_base, compiler_options_->GetCompilePic()));
1734 }
1735
1736 // Let the ImageWriter write the image file. If we do not compile PIC, also fix up the oat file.
1737 bool CreateImageFile()
1738 LOCKS_EXCLUDED(Locks::mutator_lock_) {
1739 CHECK(image_writer_ != nullptr);
1740 if (!image_writer_->Write(image_filename_, oat_unstripped_, oat_location_)) {
1741 LOG(ERROR) << "Failed to create image file " << image_filename_;
1742 return false;
1743 }
1744 uintptr_t oat_data_begin = image_writer_->GetOatDataBegin();
1745
1746 // Destroy ImageWriter before doing FixupElf.
1747 image_writer_.reset();
1748
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001749 // Do not fix up the ELF file if we are --compile-pic
1750 if (!compiler_options_->GetCompilePic()) {
Andreas Gampe4303ba92014-11-06 01:00:46 -08001751 std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_unstripped_.c_str()));
1752 if (oat_file.get() == nullptr) {
1753 PLOG(ERROR) << "Failed to open ELF file: " << oat_unstripped_;
1754 return false;
1755 }
1756
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001757 if (!ElfWriter::Fixup(oat_file.get(), oat_data_begin)) {
Andreas Gampe4303ba92014-11-06 01:00:46 -08001758 oat_file->Erase();
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001759 LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath();
1760 return false;
1761 }
Andreas Gampe4303ba92014-11-06 01:00:46 -08001762
1763 if (oat_file->FlushCloseOrErase()) {
1764 PLOG(ERROR) << "Failed to flush and close fixed ELF file " << oat_file->GetPath();
1765 return false;
1766 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001767 }
1768
1769 return true;
1770 }
1771
1772 // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;)
Andreas Gampeb1fcead2015-04-20 18:53:51 -07001773 static std::unordered_set<std::string>* ReadImageClassesFromFile(
1774 const char* image_classes_filename) {
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001775 std::function<std::string(const char*)> process = DotToDescriptor;
1776 return ReadCommentedInputFromFile(image_classes_filename, &process);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001777 }
1778
1779 // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;)
Andreas Gampeb1fcead2015-04-20 18:53:51 -07001780 static std::unordered_set<std::string>* ReadImageClassesFromZip(
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001781 const char* zip_filename,
1782 const char* image_classes_filename,
1783 std::string* error_msg) {
1784 std::function<std::string(const char*)> process = DotToDescriptor;
1785 return ReadCommentedInputFromZip(zip_filename, image_classes_filename, &process, error_msg);
1786 }
1787
1788 // Read lines from the given file, dropping comments and empty lines. Post-process each line with
1789 // the given function.
1790 static std::unordered_set<std::string>* ReadCommentedInputFromFile(
1791 const char* input_filename, std::function<std::string(const char*)>* process) {
1792 std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
1793 if (input_file.get() == nullptr) {
1794 LOG(ERROR) << "Failed to open input file " << input_filename;
1795 return nullptr;
1796 }
1797 std::unique_ptr<std::unordered_set<std::string>> result(
1798 ReadCommentedInputStream(*input_file, process));
1799 input_file->close();
1800 return result.release();
1801 }
1802
1803 // Read lines from the given file from the given zip file, dropping comments and empty lines.
1804 // Post-process each line with the given function.
1805 static std::unordered_set<std::string>* ReadCommentedInputFromZip(
Andreas Gampeb1fcead2015-04-20 18:53:51 -07001806 const char* zip_filename,
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001807 const char* input_filename,
1808 std::function<std::string(const char*)>* process,
Andreas Gampeb1fcead2015-04-20 18:53:51 -07001809 std::string* error_msg) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001810 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename, error_msg));
1811 if (zip_archive.get() == nullptr) {
1812 return nullptr;
1813 }
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001814 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(input_filename, error_msg));
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001815 if (zip_entry.get() == nullptr) {
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001816 *error_msg = StringPrintf("Failed to find '%s' within '%s': %s", input_filename,
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001817 zip_filename, error_msg->c_str());
1818 return nullptr;
1819 }
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001820 std::unique_ptr<MemMap> input_file(zip_entry->ExtractToMemMap(zip_filename,
1821 input_filename,
1822 error_msg));
1823 if (input_file.get() == nullptr) {
1824 *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", input_filename,
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001825 zip_filename, error_msg->c_str());
1826 return nullptr;
1827 }
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001828 const std::string input_string(reinterpret_cast<char*>(input_file->Begin()),
1829 input_file->Size());
1830 std::istringstream input_stream(input_string);
1831 return ReadCommentedInputStream(input_stream, process);
1832 }
1833
1834 // Read lines from the given stream, dropping comments and empty lines. Post-process each line
1835 // with the given function.
1836 static std::unordered_set<std::string>* ReadCommentedInputStream(
1837 std::istream& in_stream,
1838 std::function<std::string(const char*)>* process) {
1839 std::unique_ptr<std::unordered_set<std::string>> image_classes(
1840 new std::unordered_set<std::string>);
1841 while (in_stream.good()) {
1842 std::string dot;
1843 std::getline(in_stream, dot);
1844 if (StartsWith(dot, "#") || dot.empty()) {
1845 continue;
1846 }
1847 if (process != nullptr) {
1848 std::string descriptor((*process)(dot.c_str()));
1849 image_classes->insert(descriptor);
1850 } else {
1851 image_classes->insert(dot);
1852 }
1853 }
1854 return image_classes.release();
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001855 }
1856
Mathieu Chartier49285c52014-12-02 15:43:48 -08001857 void LogCompletionTime() {
Andreas Gampe1d00add2015-02-27 19:35:46 -08001858 // Note: when creation of a runtime fails, e.g., when trying to compile an app but when there
1859 // is no image, there won't be a Runtime::Current().
Brian Carlstroma11a34c2015-03-06 08:44:45 -08001860 // Note: driver creation can fail when loading an invalid dex file.
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001861 LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_)
Mathieu Chartierab972ef2014-12-03 17:38:22 -08001862 << " (threads: " << thread_count_ << ") "
Andreas Gampecc077602015-09-17 14:03:21 -07001863 << ((Runtime::Current() != nullptr && driver_ != nullptr) ?
Andreas Gampe1d00add2015-02-27 19:35:46 -08001864 driver_->GetMemoryUsageString(kIsDebugBuild || VLOG_IS_ON(compiler)) :
1865 "");
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001866 }
1867
1868 std::unique_ptr<CompilerOptions> compiler_options_;
1869 Compiler::Kind compiler_kind_;
1870
1871 InstructionSet instruction_set_;
1872 std::unique_ptr<const InstructionSetFeatures> instruction_set_features_;
1873
1874 std::unique_ptr<SafeMap<std::string, std::string> > key_value_store_;
1875
Andreas Gampecc077602015-09-17 14:03:21 -07001876 // Not a unique_ptr as we want to just exit on non-debug builds, not bringing the compiler down
1877 // in an orderly fashion. The destructor takes care of deleting this.
1878 VerificationResults* verification_results_;
1879
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001880 DexFileToMethodInlinerMap method_inliner_map_;
1881 std::unique_ptr<QuickCompilerCallbacks> callbacks_;
1882
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001883 // Ownership for the class path files.
1884 std::vector<std::unique_ptr<const DexFile>> class_path_files_;
1885
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001886 // Not a unique_ptr as we want to just exit on non-debug builds, not bringing the runtime down
1887 // in an orderly fashion. The destructor takes care of deleting this.
1888 Runtime* runtime_;
1889
1890 size_t thread_count_;
1891 uint64_t start_ns_;
1892 std::unique_ptr<WatchDog> watchdog_;
1893 std::unique_ptr<File> oat_file_;
1894 std::string oat_stripped_;
1895 std::string oat_unstripped_;
1896 std::string oat_location_;
1897 std::string oat_filename_;
1898 int oat_fd_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001899 std::vector<const char*> dex_filenames_;
1900 std::vector<const char*> dex_locations_;
1901 int zip_fd_;
1902 std::string zip_location_;
1903 std::string boot_image_option_;
1904 std::vector<const char*> runtime_args_;
1905 std::string image_filename_;
1906 uintptr_t image_base_;
1907 const char* image_classes_zip_filename_;
1908 const char* image_classes_filename_;
Andreas Gampe4bf3ae92014-11-11 13:28:29 -08001909 const char* compiled_classes_zip_filename_;
1910 const char* compiled_classes_filename_;
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001911 const char* compiled_methods_zip_filename_;
1912 const char* compiled_methods_filename_;
Andreas Gampeb1fcead2015-04-20 18:53:51 -07001913 std::unique_ptr<std::unordered_set<std::string>> image_classes_;
1914 std::unique_ptr<std::unordered_set<std::string>> compiled_classes_;
Andreas Gampe70bef0d2015-04-15 02:37:28 -07001915 std::unique_ptr<std::unordered_set<std::string>> compiled_methods_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001916 bool image_;
1917 std::unique_ptr<ImageWriter> image_writer_;
1918 bool is_host_;
1919 std::string android_root_;
1920 std::vector<const DexFile*> dex_files_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08001921 std::vector<std::unique_ptr<const DexFile>> opened_dex_files_;
Andreas Gampecc077602015-09-17 14:03:21 -07001922
1923 // Not a unique_ptr as we want to just exit on non-debug builds, not bringing the driver down
1924 // in an orderly fashion. The destructor takes care of deleting this.
1925 CompilerDriver* driver_;
1926
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001927 std::vector<std::string> verbose_methods_;
1928 bool dump_stats_;
1929 bool dump_passes_;
1930 bool dump_timing_;
1931 bool dump_slow_timing_;
David Brazdil866c0312015-01-13 21:21:31 +00001932 std::string dump_cfg_file_name_;
Andreas Gampee21dc3d2014-12-08 16:59:43 -08001933 std::string swap_file_name_;
1934 int swap_fd_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001935 std::string profile_file_; // Profile file to use
1936 TimingLogger* timings_;
1937 std::unique_ptr<CumulativeLogger> compiler_phases_timings_;
Andreas Gampedbfe2542014-11-25 22:21:42 -08001938 std::unique_ptr<std::ostream> init_failure_output_;
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001939
1940 DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat);
1941};
1942
Andreas Gampe88ec7f42014-11-05 10:18:32 -08001943static void b13564922() {
1944#if defined(__linux__) && defined(__arm__)
1945 int major, minor;
1946 struct utsname uts;
1947 if (uname(&uts) != -1 &&
1948 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
1949 ((major < 3) || ((major == 3) && (minor < 4)))) {
1950 // Kernels before 3.4 don't handle the ASLR well and we can run out of address
1951 // space (http://b/13564922). Work around the issue by inhibiting further mmap() randomization.
1952 int old_personality = personality(0xffffffff);
1953 if ((old_personality & ADDR_NO_RANDOMIZE) == 0) {
1954 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
1955 if (new_personality == -1) {
1956 LOG(WARNING) << "personality(. | ADDR_NO_RANDOMIZE) failed.";
1957 }
1958 }
1959 }
1960#endif
1961}
1962
Andreas Gampe10e477d2014-11-19 12:57:42 -08001963static int CompileImage(Dex2Oat& dex2oat) {
1964 dex2oat.Compile();
1965
1966 // Create the boot.oat.
1967 if (!dex2oat.CreateOatFile()) {
Andreas Gampea650e702014-12-03 14:28:02 -08001968 dex2oat.EraseOatFile();
Andreas Gampe10e477d2014-11-19 12:57:42 -08001969 return EXIT_FAILURE;
1970 }
1971
1972 // Flush and close the boot.oat. We always expect the output file by name, and it will be
1973 // re-opened from the unstripped name.
1974 if (!dex2oat.FlushCloseOatFile()) {
1975 return EXIT_FAILURE;
1976 }
1977
1978 // Creates the boot.art and patches the boot.oat.
1979 if (!dex2oat.HandleImage()) {
1980 return EXIT_FAILURE;
1981 }
1982
1983 // When given --host, finish early without stripping.
1984 if (dex2oat.IsHost()) {
1985 dex2oat.DumpTiming();
1986 return EXIT_SUCCESS;
1987 }
1988
1989 // Copy unstripped to stripped location, if necessary.
1990 if (!dex2oat.CopyUnstrippedToStripped()) {
1991 return EXIT_FAILURE;
1992 }
1993
Andreas Gampe10e477d2014-11-19 12:57:42 -08001994 // FlushClose again, as stripping might have re-opened the oat file.
1995 if (!dex2oat.FlushCloseOatFile()) {
1996 return EXIT_FAILURE;
1997 }
1998
1999 dex2oat.DumpTiming();
2000 return EXIT_SUCCESS;
2001}
2002
2003static int CompileApp(Dex2Oat& dex2oat) {
2004 dex2oat.Compile();
2005
2006 // Create the app oat.
2007 if (!dex2oat.CreateOatFile()) {
Andreas Gampea650e702014-12-03 14:28:02 -08002008 dex2oat.EraseOatFile();
Andreas Gampe10e477d2014-11-19 12:57:42 -08002009 return EXIT_FAILURE;
2010 }
2011
2012 // Do not close the oat file here. We might haven gotten the output file by file descriptor,
2013 // which we would lose.
2014 if (!dex2oat.FlushOatFile()) {
2015 return EXIT_FAILURE;
2016 }
2017
2018 // When given --host, finish early without stripping.
2019 if (dex2oat.IsHost()) {
2020 if (!dex2oat.FlushCloseOatFile()) {
2021 return EXIT_FAILURE;
2022 }
2023
2024 dex2oat.DumpTiming();
2025 return EXIT_SUCCESS;
2026 }
2027
2028 // Copy unstripped to stripped location, if necessary. This will implicitly flush & close the
2029 // unstripped version. If this is given, we expect to be able to open writable files by name.
2030 if (!dex2oat.CopyUnstrippedToStripped()) {
2031 return EXIT_FAILURE;
2032 }
2033
Andreas Gampe10e477d2014-11-19 12:57:42 -08002034 // Flush and close the file.
2035 if (!dex2oat.FlushCloseOatFile()) {
2036 return EXIT_FAILURE;
2037 }
2038
2039 dex2oat.DumpTiming();
2040 return EXIT_SUCCESS;
2041}
2042
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002043static int dex2oat(int argc, char** argv) {
2044 b13564922();
2045
2046 TimingLogger timings("compiler", false, false);
2047
2048 Dex2Oat dex2oat(&timings);
2049
2050 // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError.
2051 dex2oat.ParseArgs(argc, argv);
2052
2053 // Check early that the result of compilation can be written
2054 if (!dex2oat.OpenFile()) {
2055 return EXIT_FAILURE;
2056 }
2057
Andreas Gampead805682015-05-18 23:22:54 -07002058 // Print the complete line when any of the following is true:
2059 // 1) Debug build
2060 // 2) Compiling an image
2061 // 3) Compiling with --host
2062 // 4) Compiling on the host (not a target build)
2063 // Otherwise, print a stripped command line.
2064 if (kIsDebugBuild || dex2oat.IsImage() || dex2oat.IsHost() || !kIsTargetBuild) {
2065 LOG(INFO) << CommandLine();
2066 } else {
2067 LOG(INFO) << StrippedCommandLine();
2068 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002069
2070 if (!dex2oat.Setup()) {
Andreas Gampea650e702014-12-03 14:28:02 -08002071 dex2oat.EraseOatFile();
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002072 return EXIT_FAILURE;
2073 }
2074
Andreas Gampe10e477d2014-11-19 12:57:42 -08002075 if (dex2oat.IsImage()) {
2076 return CompileImage(dex2oat);
2077 } else {
2078 return CompileApp(dex2oat);
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002079 }
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002080}
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002081} // namespace art
Brian Carlstrom7940e442013-07-12 13:46:57 -07002082
2083int main(int argc, char** argv) {
Andreas Gampe88ec7f42014-11-05 10:18:32 -08002084 int result = art::dex2oat(argc, argv);
2085 // Everything was done, do an explicit exit here to avoid running Runtime destructors that take
2086 // time (bug 10645725) unless we're a debug build or running on valgrind. Note: The Dex2Oat class
2087 // should not destruct the runtime in this case.
2088 if (!art::kIsDebugBuild && (RUNNING_ON_VALGRIND == 0)) {
2089 exit(result);
2090 }
2091 return result;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002092}