Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/stat.h> |
Ian Rogers | 2672a9f | 2013-09-05 17:24:22 -0700 | [diff] [blame] | 20 | #include <valgrind.h> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 21 | |
| 22 | #include <fstream> |
| 23 | #include <iostream> |
| 24 | #include <sstream> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Vladimir Marko | f94b781 | 2014-06-05 15:48:04 +0100 | [diff] [blame] | 28 | #if defined(__linux__) && defined(__arm__) |
| 29 | #include <sys/personality.h> |
| 30 | #include <sys/utsname.h> |
| 31 | #endif |
| 32 | |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 33 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 34 | #include <cutils/trace.h> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 35 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 36 | #include "arch/instruction_set_features.h" |
Andreas Gampe | c5a3ea7 | 2015-01-13 16:41:53 -0800 | [diff] [blame^] | 37 | #include "arch/mips/instruction_set_features_mips.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 38 | #include "base/dumpable.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 39 | #include "base/stl_util.h" |
| 40 | #include "base/stringpiece.h" |
| 41 | #include "base/timing_logger.h" |
| 42 | #include "base/unix_file/fd_file.h" |
| 43 | #include "class_linker.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 44 | #include "compiler.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 45 | #include "compiler_callbacks.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 46 | #include "dex_file-inl.h" |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 47 | #include "dex/pass_driver_me_opts.h" |
Vladimir Marko | c7f8320 | 2014-01-24 17:55:18 +0000 | [diff] [blame] | 48 | #include "dex/verification_results.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 49 | #include "dex/quick_compiler_callbacks.h" |
| 50 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 51 | #include "driver/compiler_driver.h" |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 52 | #include "driver/compiler_options.h" |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 53 | #include "elf_file.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 54 | #include "elf_writer.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 55 | #include "gc/space/image_space.h" |
| 56 | #include "gc/space/space-inl.h" |
| 57 | #include "image_writer.h" |
| 58 | #include "leb128.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 59 | #include "mirror/art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 60 | #include "mirror/class-inl.h" |
| 61 | #include "mirror/class_loader.h" |
| 62 | #include "mirror/object-inl.h" |
| 63 | #include "mirror/object_array-inl.h" |
| 64 | #include "oat_writer.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 65 | #include "os.h" |
| 66 | #include "runtime.h" |
| 67 | #include "ScopedLocalRef.h" |
| 68 | #include "scoped_thread_state_change.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 69 | #include "utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 70 | #include "vector_output_stream.h" |
| 71 | #include "well_known_classes.h" |
| 72 | #include "zip_archive.h" |
| 73 | |
| 74 | namespace art { |
| 75 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 76 | static int original_argc; |
| 77 | static char** original_argv; |
| 78 | |
| 79 | static std::string CommandLine() { |
| 80 | std::vector<std::string> command; |
| 81 | for (int i = 0; i < original_argc; ++i) { |
| 82 | command.push_back(original_argv[i]); |
| 83 | } |
| 84 | return Join(command, ' '); |
| 85 | } |
| 86 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 87 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 88 | std::string error; |
| 89 | StringAppendV(&error, fmt, ap); |
| 90 | LOG(ERROR) << error; |
| 91 | } |
| 92 | |
| 93 | static void UsageError(const char* fmt, ...) { |
| 94 | va_list ap; |
| 95 | va_start(ap, fmt); |
| 96 | UsageErrorV(fmt, ap); |
| 97 | va_end(ap); |
| 98 | } |
| 99 | |
Ian Rogers | 7223d44 | 2014-10-10 20:05:39 -0700 | [diff] [blame] | 100 | [[noreturn]] static void Usage(const char* fmt, ...) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 101 | va_list ap; |
| 102 | va_start(ap, fmt); |
| 103 | UsageErrorV(fmt, ap); |
| 104 | va_end(ap); |
| 105 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 106 | UsageError("Command: %s", CommandLine().c_str()); |
| 107 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 108 | UsageError("Usage: dex2oat [options]..."); |
| 109 | UsageError(""); |
| 110 | UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile."); |
| 111 | UsageError(" Example: --dex-file=/system/framework/core.jar"); |
| 112 | UsageError(""); |
| 113 | UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); |
| 114 | UsageError(" containing a classes.dex file to compile."); |
| 115 | UsageError(" Example: --zip-fd=5"); |
| 116 | UsageError(""); |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 117 | UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file"); |
| 118 | UsageError(" corresponding to the file descriptor specified by --zip-fd."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 119 | UsageError(" Example: --zip-location=/system/app/Calculator.apk"); |
| 120 | UsageError(""); |
| 121 | UsageError(" --oat-file=<file.oat>: specifies the oat output destination via a filename."); |
| 122 | UsageError(" Example: --oat-file=/system/framework/boot.oat"); |
| 123 | UsageError(""); |
| 124 | UsageError(" --oat-fd=<number>: specifies the oat output destination via a file descriptor."); |
Wonil Kim | 9cb554a | 2014-04-28 11:26:55 +0900 | [diff] [blame] | 125 | UsageError(" Example: --oat-fd=6"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 126 | UsageError(""); |
| 127 | UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); |
| 128 | UsageError(" to the file descriptor specified by --oat-fd."); |
| 129 | UsageError(" Example: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat"); |
| 130 | UsageError(""); |
| 131 | UsageError(" --oat-symbols=<file.oat>: specifies the oat output destination with full symbols."); |
| 132 | UsageError(" Example: --oat-symbols=/symbols/system/framework/boot.oat"); |
| 133 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 134 | UsageError(" --image=<file.art>: specifies the output image filename."); |
| 135 | UsageError(" Example: --image=/system/framework/boot.art"); |
| 136 | UsageError(""); |
| 137 | UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); |
| 138 | UsageError(" Example: --image=frameworks/base/preloaded-classes"); |
| 139 | UsageError(""); |
| 140 | UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); |
| 141 | UsageError(" Example: --base=0x50000000"); |
| 142 | UsageError(""); |
| 143 | UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); |
| 144 | UsageError(" Example: --boot-image=/system/framework/boot.art"); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 145 | UsageError(" Default: $ANDROID_ROOT/system/framework/boot.art"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 146 | UsageError(""); |
| 147 | UsageError(" --android-root=<path>: used to locate libraries for portable linking."); |
| 148 | UsageError(" Example: --android-root=out/host/linux-x86"); |
| 149 | UsageError(" Default: $ANDROID_ROOT"); |
| 150 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 151 | UsageError(" --instruction-set=(arm|arm64|mips|x86|x86_64): compile for a particular"); |
| 152 | UsageError(" instruction set."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 153 | UsageError(" Example: --instruction-set=x86"); |
| 154 | UsageError(" Default: arm"); |
| 155 | UsageError(""); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 156 | UsageError(" --instruction-set-features=...,: Specify instruction set features"); |
| 157 | UsageError(" Example: --instruction-set-features=div"); |
| 158 | UsageError(" Default: default"); |
| 159 | UsageError(""); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 160 | UsageError(" --compile-pic: Force indirect use of code, methods, and classes"); |
| 161 | UsageError(" Default: disabled"); |
| 162 | UsageError(""); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 163 | UsageError(" --compiler-backend=(Quick|Optimizing): select compiler backend"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 164 | UsageError(" set."); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 165 | UsageError(" Example: --compiler-backend=Optimizing"); |
| 166 | if (kUseOptimizingCompiler) { |
Nicolas Geoffray | 4586fb6 | 2014-11-28 16:22:11 +0000 | [diff] [blame] | 167 | UsageError(" Default: Optimizing"); |
| 168 | } else { |
| 169 | UsageError(" Default: Quick"); |
| 170 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 171 | UsageError(""); |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 172 | UsageError(" --compiler-filter=" |
| 173 | "(verify-none" |
| 174 | "|interpret-only" |
| 175 | "|space" |
| 176 | "|balanced" |
| 177 | "|speed" |
| 178 | "|everything" |
| 179 | "|time):"); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 180 | UsageError(" select compiler filter."); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 181 | UsageError(" Example: --compiler-filter=everything"); |
| 182 | #if ART_SMALL_MODE |
| 183 | UsageError(" Default: interpret-only"); |
| 184 | #else |
| 185 | UsageError(" Default: speed"); |
| 186 | #endif |
| 187 | UsageError(""); |
| 188 | UsageError(" --huge-method-max=<method-instruction-count>: the threshold size for a huge"); |
| 189 | UsageError(" method for compiler filter tuning."); |
| 190 | UsageError(" Example: --huge-method-max=%d", CompilerOptions::kDefaultHugeMethodThreshold); |
| 191 | UsageError(" Default: %d", CompilerOptions::kDefaultHugeMethodThreshold); |
| 192 | UsageError(""); |
| 193 | UsageError(" --huge-method-max=<method-instruction-count>: threshold size for a huge"); |
| 194 | UsageError(" method for compiler filter tuning."); |
| 195 | UsageError(" Example: --huge-method-max=%d", CompilerOptions::kDefaultHugeMethodThreshold); |
| 196 | UsageError(" Default: %d", CompilerOptions::kDefaultHugeMethodThreshold); |
| 197 | UsageError(""); |
| 198 | UsageError(" --large-method-max=<method-instruction-count>: threshold size for a large"); |
| 199 | UsageError(" method for compiler filter tuning."); |
| 200 | UsageError(" Example: --large-method-max=%d", CompilerOptions::kDefaultLargeMethodThreshold); |
| 201 | UsageError(" Default: %d", CompilerOptions::kDefaultLargeMethodThreshold); |
| 202 | UsageError(""); |
| 203 | UsageError(" --small-method-max=<method-instruction-count>: threshold size for a small"); |
| 204 | UsageError(" method for compiler filter tuning."); |
| 205 | UsageError(" Example: --small-method-max=%d", CompilerOptions::kDefaultSmallMethodThreshold); |
| 206 | UsageError(" Default: %d", CompilerOptions::kDefaultSmallMethodThreshold); |
| 207 | UsageError(""); |
| 208 | UsageError(" --tiny-method-max=<method-instruction-count>: threshold size for a tiny"); |
| 209 | UsageError(" method for compiler filter tuning."); |
| 210 | UsageError(" Example: --tiny-method-max=%d", CompilerOptions::kDefaultTinyMethodThreshold); |
| 211 | UsageError(" Default: %d", CompilerOptions::kDefaultTinyMethodThreshold); |
| 212 | UsageError(""); |
| 213 | UsageError(" --num-dex-methods=<method-count>: threshold size for a small dex file for"); |
| 214 | UsageError(" compiler filter tuning. If the input has fewer than this many methods"); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 215 | UsageError(" and the filter is not interpret-only or verify-none, overrides the"); |
| 216 | UsageError(" filter to use speed"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 217 | UsageError(" Example: --num-dex-method=%d", CompilerOptions::kDefaultNumDexMethodsThreshold); |
| 218 | UsageError(" Default: %d", CompilerOptions::kDefaultNumDexMethodsThreshold); |
| 219 | UsageError(""); |
Ian Rogers | 4639860 | 2013-08-20 07:50:36 -0700 | [diff] [blame] | 220 | UsageError(" --dump-timing: display a breakdown of where time was spent"); |
| 221 | UsageError(""); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 222 | UsageError(" --include-patch-information: Include patching information so the generated code"); |
| 223 | UsageError(" can have its base address moved without full recompilation."); |
| 224 | UsageError(""); |
| 225 | UsageError(" --no-include-patch-information: Do not include patching information."); |
| 226 | UsageError(""); |
Alex Light | 78382fa | 2014-06-06 15:45:32 -0700 | [diff] [blame] | 227 | UsageError(" --include-debug-symbols: Include ELF symbols in this oat file"); |
| 228 | UsageError(""); |
| 229 | UsageError(" --no-include-debug-symbols: Do not include ELF symbols in this oat file"); |
| 230 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 231 | UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); |
| 232 | UsageError(" such as initial heap size, maximum heap size, and verbose output."); |
| 233 | UsageError(" Use a separate --runtime-arg switch for each argument."); |
| 234 | UsageError(" Example: --runtime-arg -Xms256m"); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 235 | UsageError(""); |
Dave Allison | d6ed642 | 2014-04-09 23:36:15 +0000 | [diff] [blame] | 236 | UsageError(" --profile-file=<filename>: specify profiler output file to use for compilation."); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 237 | UsageError(""); |
Chao-ying Fu | cd8ce66 | 2014-03-11 14:57:19 -0700 | [diff] [blame] | 238 | UsageError(" --print-pass-names: print a list of pass names"); |
| 239 | UsageError(""); |
| 240 | UsageError(" --disable-passes=<pass-names>: disable one or more passes separated by comma."); |
| 241 | UsageError(" Example: --disable-passes=UseCount,BBOptimizations"); |
| 242 | UsageError(""); |
Razvan A Lupusoru | bd25d4b | 2014-07-02 18:16:51 -0700 | [diff] [blame] | 243 | UsageError(" --print-pass-options: print a list of passes that have configurable options along " |
| 244 | "with the setting."); |
| 245 | UsageError(" Will print default if no overridden setting exists."); |
| 246 | UsageError(""); |
| 247 | UsageError(" --pass-options=Pass1Name:Pass1OptionName:Pass1Option#," |
| 248 | "Pass2Name:Pass2OptionName:Pass2Option#"); |
| 249 | UsageError(" Used to specify a pass specific option. The setting itself must be integer."); |
| 250 | UsageError(" Separator used between options is a comma."); |
| 251 | UsageError(""); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 252 | UsageError(" --swap-file=<file-name>: specifies a file to use for swap."); |
| 253 | UsageError(" Example: --swap-file=/data/tmp/swap.001"); |
| 254 | UsageError(""); |
| 255 | UsageError(" --swap-fd=<file-descriptor>: specifies a file to use for swap (by descriptor)."); |
| 256 | UsageError(" Example: --swap-fd=10"); |
| 257 | UsageError(""); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 258 | std::cerr << "See log for usage error information\n"; |
| 259 | exit(EXIT_FAILURE); |
| 260 | } |
| 261 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 262 | // The primary goal of the watchdog is to prevent stuck build servers |
| 263 | // during development when fatal aborts lead to a cascade of failures |
| 264 | // that result in a deadlock. |
| 265 | class WatchDog { |
Brian Carlstrom | 95b033b | 2014-12-03 22:29:37 -0800 | [diff] [blame] | 266 | // WatchDog defines its own CHECK_PTHREAD_CALL to avoid using LOG which uses locks |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 267 | #undef CHECK_PTHREAD_CALL |
| 268 | #define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ |
| 269 | do { \ |
| 270 | int rc = call args; \ |
| 271 | if (rc != 0) { \ |
| 272 | errno = rc; \ |
| 273 | std::string message(# call); \ |
| 274 | message += " failed for "; \ |
| 275 | message += reason; \ |
| 276 | Fatal(message); \ |
| 277 | } \ |
| 278 | } while (false) |
| 279 | |
| 280 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 281 | explicit WatchDog(bool is_watch_dog_enabled) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 282 | is_watch_dog_enabled_ = is_watch_dog_enabled; |
| 283 | if (!is_watch_dog_enabled_) { |
| 284 | return; |
| 285 | } |
| 286 | shutting_down_ = false; |
| 287 | const char* reason = "dex2oat watch dog thread startup"; |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 288 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, nullptr), reason); |
| 289 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, nullptr), reason); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 290 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); |
| 291 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); |
| 292 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); |
| 293 | } |
| 294 | ~WatchDog() { |
| 295 | if (!is_watch_dog_enabled_) { |
| 296 | return; |
| 297 | } |
| 298 | const char* reason = "dex2oat watch dog thread shutdown"; |
| 299 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 300 | shutting_down_ = true; |
| 301 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); |
| 302 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 303 | |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 304 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, nullptr), reason); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 305 | |
| 306 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); |
| 307 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); |
| 308 | } |
| 309 | |
| 310 | private: |
| 311 | static void* CallBack(void* arg) { |
| 312 | WatchDog* self = reinterpret_cast<WatchDog*>(arg); |
| 313 | ::art::SetThreadName("dex2oat watch dog"); |
| 314 | self->Wait(); |
Kenny Root | 5131638 | 2014-05-13 14:59:37 -0700 | [diff] [blame] | 315 | return nullptr; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void Message(char severity, const std::string& message) { |
| 319 | // TODO: Remove when we switch to LOG when we can guarantee it won't prevent shutdown in error |
| 320 | // cases. |
| 321 | fprintf(stderr, "dex2oat%s %c %d %d %s\n", |
| 322 | kIsDebugBuild ? "d" : "", |
| 323 | severity, |
| 324 | getpid(), |
| 325 | GetTid(), |
| 326 | message.c_str()); |
| 327 | } |
| 328 | |
Ian Rogers | 7223d44 | 2014-10-10 20:05:39 -0700 | [diff] [blame] | 329 | [[noreturn]] static void Fatal(const std::string& message) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 330 | Message('F', message); |
| 331 | exit(1); |
| 332 | } |
| 333 | |
| 334 | void Wait() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 335 | // TODO: tune the multiplier for GC verification, the following is just to make the timeout |
| 336 | // large. |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 337 | int64_t multiplier = kVerifyObjectSupport > kVerifyObjectModeFast ? 100 : 1; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 338 | timespec timeout_ts; |
| 339 | InitTimeSpec(true, CLOCK_REALTIME, multiplier * kWatchDogTimeoutSeconds * 1000, 0, &timeout_ts); |
| 340 | const char* reason = "dex2oat watch dog thread waiting"; |
| 341 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 342 | while (!shutting_down_) { |
Brian Carlstrom | 95b033b | 2014-12-03 22:29:37 -0800 | [diff] [blame] | 343 | int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_, &timeout_ts)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 344 | if (rc == ETIMEDOUT) { |
Brian Carlstrom | 95b033b | 2014-12-03 22:29:37 -0800 | [diff] [blame] | 345 | Fatal(StringPrintf("dex2oat did not finish after %d seconds", kWatchDogTimeoutSeconds)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 346 | } else if (rc != 0) { |
| 347 | std::string message(StringPrintf("pthread_cond_timedwait failed: %s", |
| 348 | strerror(errno))); |
| 349 | Fatal(message.c_str()); |
| 350 | } |
| 351 | } |
| 352 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 353 | } |
| 354 | |
| 355 | // When setting timeouts, keep in mind that the build server may not be as fast as your desktop. |
Mathieu Chartier | 13b9f43 | 2014-09-09 17:26:58 -0700 | [diff] [blame] | 356 | // Debug builds are slower so they have larger timeouts. |
| 357 | static const unsigned int kSlowdownFactor = kIsDebugBuild ? 5U : 1U; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 358 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 359 | // 6 minutes scaled by kSlowdownFactor. |
| 360 | static const unsigned int kWatchDogTimeoutSeconds = kSlowdownFactor * 6 * 60; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 361 | |
| 362 | bool is_watch_dog_enabled_; |
| 363 | bool shutting_down_; |
| 364 | // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases. |
| 365 | pthread_mutex_t mutex_; |
| 366 | pthread_cond_t cond_; |
| 367 | pthread_attr_t attr_; |
| 368 | pthread_t pthread_; |
| 369 | }; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 370 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 371 | static void ParseStringAfterChar(const std::string& s, char c, std::string* parsed_value) { |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 372 | std::string::size_type colon = s.find(c); |
| 373 | if (colon == std::string::npos) { |
| 374 | Usage("Missing char %c in option %s\n", c, s.c_str()); |
| 375 | } |
| 376 | // Add one to remove the char we were trimming until. |
| 377 | *parsed_value = s.substr(colon + 1); |
| 378 | } |
| 379 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 380 | static void ParseDouble(const std::string& option, char after_char, double min, double max, |
| 381 | double* parsed_value) { |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 382 | std::string substring; |
| 383 | ParseStringAfterChar(option, after_char, &substring); |
| 384 | bool sane_val = true; |
| 385 | double value; |
| 386 | if (false) { |
| 387 | // TODO: this doesn't seem to work on the emulator. b/15114595 |
| 388 | std::stringstream iss(substring); |
| 389 | iss >> value; |
| 390 | // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range. |
| 391 | sane_val = iss.eof() && (value >= min) && (value <= max); |
| 392 | } else { |
| 393 | char* end = nullptr; |
| 394 | value = strtod(substring.c_str(), &end); |
| 395 | sane_val = *end == '\0' && value >= min && value <= max; |
| 396 | } |
| 397 | if (!sane_val) { |
| 398 | Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str()); |
| 399 | } |
| 400 | *parsed_value = value; |
| 401 | } |
| 402 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 403 | static constexpr size_t kMinDexFilesForSwap = 2; |
| 404 | static constexpr size_t kMinDexFileCumulativeSizeForSwap = 20 * MB; |
| 405 | |
| 406 | static bool UseSwap(bool is_image, std::vector<const DexFile*>& dex_files) { |
| 407 | if (is_image) { |
| 408 | // Don't use swap, we know generation should succeed, and we don't want to slow it down. |
| 409 | return false; |
| 410 | } |
| 411 | if (dex_files.size() < kMinDexFilesForSwap) { |
| 412 | // If there are less dex files than the threshold, assume it's gonna be fine. |
| 413 | return false; |
| 414 | } |
| 415 | size_t dex_files_size = 0; |
| 416 | for (const auto* dex_file : dex_files) { |
| 417 | dex_files_size += dex_file->GetHeader().file_size_; |
| 418 | } |
| 419 | return dex_files_size >= kMinDexFileCumulativeSizeForSwap; |
| 420 | } |
| 421 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 422 | class Dex2Oat FINAL { |
| 423 | public: |
| 424 | explicit Dex2Oat(TimingLogger* timings) : |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 425 | compiler_kind_(kUseOptimizingCompiler ? Compiler::kOptimizing : Compiler::kQuick), |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 426 | instruction_set_(kRuntimeISA), |
| 427 | // Take the default set of instruction features from the build. |
| 428 | method_inliner_map_(), |
| 429 | runtime_(nullptr), |
| 430 | thread_count_(sysconf(_SC_NPROCESSORS_CONF)), |
| 431 | start_ns_(NanoTime()), |
| 432 | oat_fd_(-1), |
| 433 | zip_fd_(-1), |
| 434 | image_base_(0U), |
| 435 | image_classes_zip_filename_(nullptr), |
| 436 | image_classes_filename_(nullptr), |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 437 | compiled_classes_zip_filename_(nullptr), |
| 438 | compiled_classes_filename_(nullptr), |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 439 | image_(false), |
| 440 | is_host_(false), |
| 441 | dump_stats_(false), |
| 442 | dump_passes_(false), |
| 443 | dump_timing_(false), |
| 444 | dump_slow_timing_(kIsDebugBuild), |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 445 | swap_fd_(-1), |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 446 | timings_(timings) {} |
| 447 | |
| 448 | ~Dex2Oat() { |
Mathieu Chartier | 49285c5 | 2014-12-02 15:43:48 -0800 | [diff] [blame] | 449 | LogCompletionTime(); // Needs to be before since it accesses the runtime. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 450 | if (kIsDebugBuild || (RUNNING_ON_VALGRIND != 0)) { |
| 451 | delete runtime_; // See field declaration for why this is manual. |
Vladimir Marko | f94b781 | 2014-06-05 15:48:04 +0100 | [diff] [blame] | 452 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 455 | // Parse the arguments from the command line. In case of an unrecognized option or impossible |
| 456 | // values/combinations, a usage error will be displayed and exit() is called. Thus, if the method |
| 457 | // returns, arguments have been successfully parsed. |
| 458 | void ParseArgs(int argc, char** argv) { |
| 459 | original_argc = argc; |
| 460 | original_argv = argv; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 461 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 462 | InitLogging(argv); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 463 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 464 | // Skip over argv[0]. |
| 465 | argv++; |
| 466 | argc--; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 467 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 468 | if (argc == 0) { |
| 469 | Usage("No arguments specified"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 470 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 471 | |
| 472 | std::string oat_symbols; |
| 473 | std::string boot_image_filename; |
| 474 | const char* compiler_filter_string = nullptr; |
| 475 | bool compile_pic = false; |
| 476 | int huge_method_threshold = CompilerOptions::kDefaultHugeMethodThreshold; |
| 477 | int large_method_threshold = CompilerOptions::kDefaultLargeMethodThreshold; |
| 478 | int small_method_threshold = CompilerOptions::kDefaultSmallMethodThreshold; |
| 479 | int tiny_method_threshold = CompilerOptions::kDefaultTinyMethodThreshold; |
| 480 | int num_dex_methods_threshold = CompilerOptions::kDefaultNumDexMethodsThreshold; |
| 481 | |
| 482 | // Profile file to use |
| 483 | double top_k_profile_threshold = CompilerOptions::kDefaultTopKProfileThreshold; |
| 484 | |
| 485 | bool print_pass_options = false; |
| 486 | bool include_patch_information = CompilerOptions::kDefaultIncludePatchInformation; |
| 487 | bool include_debug_symbols = kIsDebugBuild; |
| 488 | bool watch_dog_enabled = true; |
| 489 | bool generate_gdb_information = kIsDebugBuild; |
| 490 | |
| 491 | std::string error_msg; |
| 492 | |
| 493 | for (int i = 0; i < argc; i++) { |
| 494 | const StringPiece option(argv[i]); |
| 495 | const bool log_options = false; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 496 | if (log_options) { |
| 497 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 498 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 499 | if (option.starts_with("--dex-file=")) { |
| 500 | dex_filenames_.push_back(option.substr(strlen("--dex-file=")).data()); |
| 501 | } else if (option.starts_with("--dex-location=")) { |
| 502 | dex_locations_.push_back(option.substr(strlen("--dex-location=")).data()); |
| 503 | } else if (option.starts_with("--zip-fd=")) { |
| 504 | const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); |
| 505 | if (!ParseInt(zip_fd_str, &zip_fd_)) { |
| 506 | Usage("Failed to parse --zip-fd argument '%s' as an integer", zip_fd_str); |
| 507 | } |
| 508 | if (zip_fd_ < 0) { |
| 509 | Usage("--zip-fd passed a negative value %d", zip_fd_); |
| 510 | } |
| 511 | } else if (option.starts_with("--zip-location=")) { |
| 512 | zip_location_ = option.substr(strlen("--zip-location=")).data(); |
| 513 | } else if (option.starts_with("--oat-file=")) { |
| 514 | oat_filename_ = option.substr(strlen("--oat-file=")).data(); |
| 515 | } else if (option.starts_with("--oat-symbols=")) { |
| 516 | oat_symbols = option.substr(strlen("--oat-symbols=")).data(); |
| 517 | } else if (option.starts_with("--oat-fd=")) { |
| 518 | const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); |
| 519 | if (!ParseInt(oat_fd_str, &oat_fd_)) { |
| 520 | Usage("Failed to parse --oat-fd argument '%s' as an integer", oat_fd_str); |
| 521 | } |
| 522 | if (oat_fd_ < 0) { |
| 523 | Usage("--oat-fd passed a negative value %d", oat_fd_); |
| 524 | } |
| 525 | } else if (option == "--watch-dog") { |
| 526 | watch_dog_enabled = true; |
| 527 | } else if (option == "--no-watch-dog") { |
| 528 | watch_dog_enabled = false; |
| 529 | } else if (option == "--gen-gdb-info") { |
| 530 | generate_gdb_information = true; |
| 531 | // Debug symbols are needed for gdb information. |
| 532 | include_debug_symbols = true; |
| 533 | } else if (option == "--no-gen-gdb-info") { |
| 534 | generate_gdb_information = false; |
| 535 | } else if (option.starts_with("-j")) { |
| 536 | const char* thread_count_str = option.substr(strlen("-j")).data(); |
| 537 | if (!ParseUint(thread_count_str, &thread_count_)) { |
| 538 | Usage("Failed to parse -j argument '%s' as an integer", thread_count_str); |
| 539 | } |
| 540 | } else if (option.starts_with("--oat-location=")) { |
| 541 | oat_location_ = option.substr(strlen("--oat-location=")).data(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 542 | } else if (option.starts_with("--image=")) { |
| 543 | image_filename_ = option.substr(strlen("--image=")).data(); |
| 544 | } else if (option.starts_with("--image-classes=")) { |
| 545 | image_classes_filename_ = option.substr(strlen("--image-classes=")).data(); |
| 546 | } else if (option.starts_with("--image-classes-zip=")) { |
| 547 | image_classes_zip_filename_ = option.substr(strlen("--image-classes-zip=")).data(); |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 548 | } else if (option.starts_with("--compiled-classes=")) { |
| 549 | compiled_classes_filename_ = option.substr(strlen("--compiled-classes=")).data(); |
| 550 | } else if (option.starts_with("--compiled-classes-zip=")) { |
| 551 | compiled_classes_zip_filename_ = option.substr(strlen("--compiled-classes-zip=")).data(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 552 | } else if (option.starts_with("--base=")) { |
| 553 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 554 | char* end; |
| 555 | image_base_ = strtoul(image_base_str, &end, 16); |
| 556 | if (end == image_base_str || *end != '\0') { |
| 557 | Usage("Failed to parse hexadecimal value for option %s", option.data()); |
| 558 | } |
| 559 | } else if (option.starts_with("--boot-image=")) { |
| 560 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
| 561 | } else if (option.starts_with("--android-root=")) { |
| 562 | android_root_ = option.substr(strlen("--android-root=")).data(); |
| 563 | } else if (option.starts_with("--instruction-set=")) { |
| 564 | StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); |
| 565 | // StringPiece is not necessarily zero-terminated, so need to make a copy and ensure it. |
Dan Albert | 6fc59ab | 2014-12-11 14:09:51 -0800 | [diff] [blame] | 566 | std::unique_ptr<char[]> buf(new char[instruction_set_str.length() + 1]); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 567 | strncpy(buf.get(), instruction_set_str.data(), instruction_set_str.length()); |
| 568 | buf.get()[instruction_set_str.length()] = 0; |
| 569 | instruction_set_ = GetInstructionSetFromString(buf.get()); |
| 570 | // arm actually means thumb2. |
| 571 | if (instruction_set_ == InstructionSet::kArm) { |
| 572 | instruction_set_ = InstructionSet::kThumb2; |
| 573 | } |
| 574 | } else if (option.starts_with("--instruction-set-variant=")) { |
| 575 | StringPiece str = option.substr(strlen("--instruction-set-variant=")).data(); |
| 576 | instruction_set_features_.reset( |
| 577 | InstructionSetFeatures::FromVariant(instruction_set_, str.as_string(), &error_msg)); |
| 578 | if (instruction_set_features_.get() == nullptr) { |
| 579 | Usage("%s", error_msg.c_str()); |
| 580 | } |
| 581 | } else if (option.starts_with("--instruction-set-features=")) { |
| 582 | StringPiece str = option.substr(strlen("--instruction-set-features=")).data(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 583 | if (instruction_set_features_.get() == nullptr) { |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 584 | instruction_set_features_.reset( |
| 585 | InstructionSetFeatures::FromVariant(instruction_set_, "default", &error_msg)); |
| 586 | if (instruction_set_features_.get() == nullptr) { |
| 587 | Usage("Problem initializing default instruction set features variant: %s", |
| 588 | error_msg.c_str()); |
| 589 | } |
| 590 | } |
| 591 | instruction_set_features_.reset( |
| 592 | instruction_set_features_->AddFeaturesFromString(str.as_string(), &error_msg)); |
| 593 | if (instruction_set_features_.get() == nullptr) { |
| 594 | Usage("Error parsing '%s': %s", option.data(), error_msg.c_str()); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 595 | } |
| 596 | } else if (option.starts_with("--compiler-backend=")) { |
| 597 | StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data(); |
| 598 | if (backend_str == "Quick") { |
| 599 | compiler_kind_ = Compiler::kQuick; |
| 600 | } else if (backend_str == "Optimizing") { |
| 601 | compiler_kind_ = Compiler::kOptimizing; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 602 | } else { |
| 603 | Usage("Unknown compiler backend: %s", backend_str.data()); |
| 604 | } |
| 605 | } else if (option.starts_with("--compiler-filter=")) { |
| 606 | compiler_filter_string = option.substr(strlen("--compiler-filter=")).data(); |
| 607 | } else if (option == "--compile-pic") { |
| 608 | compile_pic = true; |
| 609 | } else if (option.starts_with("--huge-method-max=")) { |
| 610 | const char* threshold = option.substr(strlen("--huge-method-max=")).data(); |
| 611 | if (!ParseInt(threshold, &huge_method_threshold)) { |
| 612 | Usage("Failed to parse --huge-method-max '%s' as an integer", threshold); |
| 613 | } |
| 614 | if (huge_method_threshold < 0) { |
| 615 | Usage("--huge-method-max passed a negative value %s", huge_method_threshold); |
| 616 | } |
| 617 | } else if (option.starts_with("--large-method-max=")) { |
| 618 | const char* threshold = option.substr(strlen("--large-method-max=")).data(); |
| 619 | if (!ParseInt(threshold, &large_method_threshold)) { |
| 620 | Usage("Failed to parse --large-method-max '%s' as an integer", threshold); |
| 621 | } |
| 622 | if (large_method_threshold < 0) { |
| 623 | Usage("--large-method-max passed a negative value %s", large_method_threshold); |
| 624 | } |
| 625 | } else if (option.starts_with("--small-method-max=")) { |
| 626 | const char* threshold = option.substr(strlen("--small-method-max=")).data(); |
| 627 | if (!ParseInt(threshold, &small_method_threshold)) { |
| 628 | Usage("Failed to parse --small-method-max '%s' as an integer", threshold); |
| 629 | } |
| 630 | if (small_method_threshold < 0) { |
| 631 | Usage("--small-method-max passed a negative value %s", small_method_threshold); |
| 632 | } |
| 633 | } else if (option.starts_with("--tiny-method-max=")) { |
| 634 | const char* threshold = option.substr(strlen("--tiny-method-max=")).data(); |
| 635 | if (!ParseInt(threshold, &tiny_method_threshold)) { |
| 636 | Usage("Failed to parse --tiny-method-max '%s' as an integer", threshold); |
| 637 | } |
| 638 | if (tiny_method_threshold < 0) { |
| 639 | Usage("--tiny-method-max passed a negative value %s", tiny_method_threshold); |
| 640 | } |
| 641 | } else if (option.starts_with("--num-dex-methods=")) { |
| 642 | const char* threshold = option.substr(strlen("--num-dex-methods=")).data(); |
| 643 | if (!ParseInt(threshold, &num_dex_methods_threshold)) { |
| 644 | Usage("Failed to parse --num-dex-methods '%s' as an integer", threshold); |
| 645 | } |
| 646 | if (num_dex_methods_threshold < 0) { |
| 647 | Usage("--num-dex-methods passed a negative value %s", num_dex_methods_threshold); |
| 648 | } |
| 649 | } else if (option == "--host") { |
| 650 | is_host_ = true; |
| 651 | } else if (option == "--runtime-arg") { |
| 652 | if (++i >= argc) { |
| 653 | Usage("Missing required argument for --runtime-arg"); |
| 654 | } |
| 655 | if (log_options) { |
| 656 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 657 | } |
| 658 | runtime_args_.push_back(argv[i]); |
| 659 | } else if (option == "--dump-timing") { |
| 660 | dump_timing_ = true; |
| 661 | } else if (option == "--dump-passes") { |
| 662 | dump_passes_ = true; |
| 663 | } else if (option == "--dump-stats") { |
| 664 | dump_stats_ = true; |
| 665 | } else if (option == "--include-debug-symbols" || option == "--no-strip-symbols") { |
| 666 | include_debug_symbols = true; |
| 667 | } else if (option == "--no-include-debug-symbols" || option == "--strip-symbols") { |
| 668 | include_debug_symbols = false; |
| 669 | generate_gdb_information = false; // Depends on debug symbols, see above. |
| 670 | } else if (option.starts_with("--profile-file=")) { |
| 671 | profile_file_ = option.substr(strlen("--profile-file=")).data(); |
| 672 | VLOG(compiler) << "dex2oat: profile file is " << profile_file_; |
| 673 | } else if (option == "--no-profile-file") { |
| 674 | // No profile |
| 675 | } else if (option.starts_with("--top-k-profile-threshold=")) { |
| 676 | ParseDouble(option.data(), '=', 0.0, 100.0, &top_k_profile_threshold); |
| 677 | } else if (option == "--print-pass-names") { |
| 678 | PassDriverMEOpts::PrintPassNames(); |
| 679 | } else if (option.starts_with("--disable-passes=")) { |
| 680 | std::string disable_passes = option.substr(strlen("--disable-passes=")).data(); |
| 681 | PassDriverMEOpts::CreateDefaultPassList(disable_passes); |
| 682 | } else if (option.starts_with("--print-passes=")) { |
| 683 | std::string print_passes = option.substr(strlen("--print-passes=")).data(); |
| 684 | PassDriverMEOpts::SetPrintPassList(print_passes); |
| 685 | } else if (option == "--print-all-passes") { |
| 686 | PassDriverMEOpts::SetPrintAllPasses(); |
| 687 | } else if (option.starts_with("--dump-cfg-passes=")) { |
| 688 | std::string dump_passes_string = option.substr(strlen("--dump-cfg-passes=")).data(); |
| 689 | PassDriverMEOpts::SetDumpPassList(dump_passes_string); |
| 690 | } else if (option == "--print-pass-options") { |
| 691 | print_pass_options = true; |
| 692 | } else if (option.starts_with("--pass-options=")) { |
| 693 | std::string options = option.substr(strlen("--pass-options=")).data(); |
| 694 | PassDriverMEOpts::SetOverriddenPassOptions(options); |
| 695 | } else if (option == "--include-patch-information") { |
| 696 | include_patch_information = true; |
| 697 | } else if (option == "--no-include-patch-information") { |
| 698 | include_patch_information = false; |
| 699 | } else if (option.starts_with("--verbose-methods=")) { |
Brian Carlstrom | 95b033b | 2014-12-03 22:29:37 -0800 | [diff] [blame] | 700 | // TODO: rather than switch off compiler logging, make all VLOG(compiler) messages |
| 701 | // conditional on having verbost methods. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 702 | gLogVerbosity.compiler = false; |
| 703 | Split(option.substr(strlen("--verbose-methods=")).ToString(), ',', &verbose_methods_); |
Andreas Gampe | dbfe254 | 2014-11-25 22:21:42 -0800 | [diff] [blame] | 704 | } else if (option.starts_with("--dump-init-failures=")) { |
| 705 | std::string file_name = option.substr(strlen("--dump-init-failures=")).data(); |
| 706 | init_failure_output_.reset(new std::ofstream(file_name)); |
| 707 | if (init_failure_output_.get() == nullptr) { |
| 708 | LOG(ERROR) << "Failed to allocate ofstream"; |
| 709 | } else if (init_failure_output_->fail()) { |
| 710 | LOG(ERROR) << "Failed to open " << file_name << " for writing the initialization " |
| 711 | << "failures."; |
| 712 | init_failure_output_.reset(); |
| 713 | } |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 714 | } else if (option.starts_with("--swap-file=")) { |
| 715 | swap_file_name_ = option.substr(strlen("--swap-file=")).data(); |
| 716 | } else if (option.starts_with("--swap-fd=")) { |
| 717 | const char* swap_fd_str = option.substr(strlen("--swap-fd=")).data(); |
| 718 | if (!ParseInt(swap_fd_str, &swap_fd_)) { |
| 719 | Usage("Failed to parse --swap-fd argument '%s' as an integer", swap_fd_str); |
| 720 | } |
| 721 | if (swap_fd_ < 0) { |
| 722 | Usage("--swap-fd passed a negative value %d", swap_fd_); |
| 723 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 724 | } else { |
| 725 | Usage("Unknown argument %s", option.data()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 726 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Nicolas Geoffray | 9bb492a | 2014-11-25 23:42:00 +0000 | [diff] [blame] | 729 | if (compiler_kind_ == Compiler::kOptimizing) { |
| 730 | // Optimizing only supports PIC mode. |
| 731 | compile_pic = true; |
| 732 | } |
| 733 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 734 | if (oat_filename_.empty() && oat_fd_ == -1) { |
| 735 | Usage("Output must be supplied with either --oat-file or --oat-fd"); |
| 736 | } |
| 737 | |
| 738 | if (!oat_filename_.empty() && oat_fd_ != -1) { |
| 739 | Usage("--oat-file should not be used with --oat-fd"); |
| 740 | } |
| 741 | |
| 742 | if (!oat_symbols.empty() && oat_fd_ != -1) { |
| 743 | Usage("--oat-symbols should not be used with --oat-fd"); |
| 744 | } |
| 745 | |
| 746 | if (!oat_symbols.empty() && is_host_) { |
| 747 | Usage("--oat-symbols should not be used with --host"); |
| 748 | } |
| 749 | |
| 750 | if (oat_fd_ != -1 && !image_filename_.empty()) { |
| 751 | Usage("--oat-fd should not be used with --image"); |
| 752 | } |
| 753 | |
| 754 | if (android_root_.empty()) { |
| 755 | const char* android_root_env_var = getenv("ANDROID_ROOT"); |
| 756 | if (android_root_env_var == nullptr) { |
| 757 | Usage("--android-root unspecified and ANDROID_ROOT not set"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 758 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 759 | android_root_ += android_root_env_var; |
| 760 | } |
| 761 | |
| 762 | image_ = (!image_filename_.empty()); |
| 763 | if (!image_ && boot_image_filename.empty()) { |
| 764 | boot_image_filename += android_root_; |
| 765 | boot_image_filename += "/framework/boot.art"; |
| 766 | } |
| 767 | if (!boot_image_filename.empty()) { |
| 768 | boot_image_option_ += "-Ximage:"; |
| 769 | boot_image_option_ += boot_image_filename; |
| 770 | } |
| 771 | |
| 772 | if (image_classes_filename_ != nullptr && !image_) { |
| 773 | Usage("--image-classes should only be used with --image"); |
| 774 | } |
| 775 | |
| 776 | if (image_classes_filename_ != nullptr && !boot_image_option_.empty()) { |
| 777 | Usage("--image-classes should not be used with --boot-image"); |
| 778 | } |
| 779 | |
| 780 | if (image_classes_zip_filename_ != nullptr && image_classes_filename_ == nullptr) { |
| 781 | Usage("--image-classes-zip should be used with --image-classes"); |
| 782 | } |
| 783 | |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 784 | if (compiled_classes_filename_ != nullptr && !image_) { |
| 785 | Usage("--compiled-classes should only be used with --image"); |
| 786 | } |
| 787 | |
| 788 | if (compiled_classes_filename_ != nullptr && !boot_image_option_.empty()) { |
| 789 | Usage("--compiled-classes should not be used with --boot-image"); |
| 790 | } |
| 791 | |
| 792 | if (compiled_classes_zip_filename_ != nullptr && compiled_classes_filename_ == nullptr) { |
| 793 | Usage("--compiled-classes-zip should be used with --compiled-classes"); |
| 794 | } |
| 795 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 796 | if (dex_filenames_.empty() && zip_fd_ == -1) { |
| 797 | Usage("Input must be supplied with either --dex-file or --zip-fd"); |
| 798 | } |
| 799 | |
| 800 | if (!dex_filenames_.empty() && zip_fd_ != -1) { |
| 801 | Usage("--dex-file should not be used with --zip-fd"); |
| 802 | } |
| 803 | |
| 804 | if (!dex_filenames_.empty() && !zip_location_.empty()) { |
| 805 | Usage("--dex-file should not be used with --zip-location"); |
| 806 | } |
| 807 | |
| 808 | if (dex_locations_.empty()) { |
| 809 | for (const char* dex_file_name : dex_filenames_) { |
| 810 | dex_locations_.push_back(dex_file_name); |
| 811 | } |
| 812 | } else if (dex_locations_.size() != dex_filenames_.size()) { |
| 813 | Usage("--dex-location arguments do not match --dex-file arguments"); |
| 814 | } |
| 815 | |
| 816 | if (zip_fd_ != -1 && zip_location_.empty()) { |
| 817 | Usage("--zip-location should be supplied with --zip-fd"); |
| 818 | } |
| 819 | |
| 820 | if (boot_image_option_.empty()) { |
| 821 | if (image_base_ == 0) { |
| 822 | Usage("Non-zero --base not specified"); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | oat_stripped_ = oat_filename_; |
| 827 | if (!oat_symbols.empty()) { |
| 828 | oat_unstripped_ = oat_symbols; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 829 | } else { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 830 | oat_unstripped_ = oat_filename_; |
| 831 | } |
| 832 | |
| 833 | // If no instruction set feature was given, use the default one for the target |
| 834 | // instruction set. |
| 835 | if (instruction_set_features_.get() == nullptr) { |
| 836 | instruction_set_features_.reset( |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 837 | InstructionSetFeatures::FromVariant(instruction_set_, "default", &error_msg)); |
| 838 | if (instruction_set_features_.get() == nullptr) { |
| 839 | Usage("Problem initializing default instruction set features variant: %s", |
| 840 | error_msg.c_str()); |
| 841 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | if (instruction_set_ == kRuntimeISA) { |
| 845 | std::unique_ptr<const InstructionSetFeatures> runtime_features( |
| 846 | InstructionSetFeatures::FromCppDefines()); |
| 847 | if (!instruction_set_features_->Equals(runtime_features.get())) { |
| 848 | LOG(WARNING) << "Mismatch between dex2oat instruction set features (" |
| 849 | << *instruction_set_features_ << ") and those of dex2oat executable (" |
| 850 | << *runtime_features <<") for the command line:\n" |
| 851 | << CommandLine(); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | if (compiler_filter_string == nullptr) { |
Andreas Gampe | c5a3ea7 | 2015-01-13 16:41:53 -0800 | [diff] [blame^] | 856 | if (instruction_set_ == kMips && |
| 857 | reinterpret_cast<const MipsInstructionSetFeatures*>(instruction_set_features_.get())-> |
| 858 | IsR6()) { |
| 859 | // For R6, only interpreter mode is working. |
| 860 | // TODO: fix compiler for Mips32r6. |
| 861 | compiler_filter_string = "interpret-only"; |
| 862 | } else if (instruction_set_ == kMips64) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 863 | // TODO: fix compiler for Mips64. |
| 864 | compiler_filter_string = "interpret-only"; |
| 865 | } else if (image_) { |
| 866 | compiler_filter_string = "speed"; |
| 867 | } else { |
| 868 | // TODO: Migrate SMALL mode to command line option. |
| 869 | #if ART_SMALL_MODE |
| 870 | compiler_filter_string = "interpret-only"; |
| 871 | #else |
| 872 | compiler_filter_string = "speed"; |
| 873 | #endif |
| 874 | } |
| 875 | } |
| 876 | CHECK(compiler_filter_string != nullptr); |
| 877 | CompilerOptions::CompilerFilter compiler_filter = CompilerOptions::kDefaultCompilerFilter; |
| 878 | if (strcmp(compiler_filter_string, "verify-none") == 0) { |
| 879 | compiler_filter = CompilerOptions::kVerifyNone; |
| 880 | } else if (strcmp(compiler_filter_string, "interpret-only") == 0) { |
| 881 | compiler_filter = CompilerOptions::kInterpretOnly; |
| 882 | } else if (strcmp(compiler_filter_string, "space") == 0) { |
| 883 | compiler_filter = CompilerOptions::kSpace; |
| 884 | } else if (strcmp(compiler_filter_string, "balanced") == 0) { |
| 885 | compiler_filter = CompilerOptions::kBalanced; |
| 886 | } else if (strcmp(compiler_filter_string, "speed") == 0) { |
| 887 | compiler_filter = CompilerOptions::kSpeed; |
| 888 | } else if (strcmp(compiler_filter_string, "everything") == 0) { |
| 889 | compiler_filter = CompilerOptions::kEverything; |
| 890 | } else if (strcmp(compiler_filter_string, "time") == 0) { |
| 891 | compiler_filter = CompilerOptions::kTime; |
| 892 | } else { |
| 893 | Usage("Unknown --compiler-filter value %s", compiler_filter_string); |
| 894 | } |
| 895 | |
| 896 | // Checks are all explicit until we know the architecture. |
| 897 | bool implicit_null_checks = false; |
| 898 | bool implicit_so_checks = false; |
| 899 | bool implicit_suspend_checks = false; |
| 900 | // Set the compilation target's implicit checks options. |
| 901 | switch (instruction_set_) { |
| 902 | case kArm: |
| 903 | case kThumb2: |
| 904 | case kArm64: |
| 905 | case kX86: |
| 906 | case kX86_64: |
| 907 | implicit_null_checks = true; |
| 908 | implicit_so_checks = true; |
| 909 | break; |
| 910 | |
| 911 | default: |
| 912 | // Defaults are correct. |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | if (print_pass_options) { |
| 917 | PassDriverMEOpts::PrintPassOptions(); |
| 918 | } |
| 919 | |
| 920 | compiler_options_.reset(new CompilerOptions(compiler_filter, |
| 921 | huge_method_threshold, |
| 922 | large_method_threshold, |
| 923 | small_method_threshold, |
| 924 | tiny_method_threshold, |
| 925 | num_dex_methods_threshold, |
| 926 | generate_gdb_information, |
| 927 | include_patch_information, |
| 928 | top_k_profile_threshold, |
| 929 | include_debug_symbols, |
| 930 | implicit_null_checks, |
| 931 | implicit_so_checks, |
| 932 | implicit_suspend_checks, |
| 933 | compile_pic, |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 934 | verbose_methods_.empty() ? |
| 935 | nullptr : |
Andreas Gampe | dbfe254 | 2014-11-25 22:21:42 -0800 | [diff] [blame] | 936 | &verbose_methods_, |
| 937 | init_failure_output_.get())); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 938 | |
| 939 | // Done with usage checks, enable watchdog if requested |
| 940 | if (watch_dog_enabled) { |
| 941 | watchdog_.reset(new WatchDog(true)); |
| 942 | } |
| 943 | |
| 944 | // Fill some values into the key-value store for the oat header. |
| 945 | key_value_store_.reset(new SafeMap<std::string, std::string>()); |
| 946 | |
| 947 | // Insert some compiler things. |
| 948 | { |
| 949 | std::ostringstream oss; |
| 950 | for (int i = 0; i < argc; ++i) { |
| 951 | if (i > 0) { |
| 952 | oss << ' '; |
| 953 | } |
| 954 | oss << argv[i]; |
| 955 | } |
| 956 | key_value_store_->Put(OatHeader::kDex2OatCmdLineKey, oss.str()); |
| 957 | oss.str(""); // Reset. |
| 958 | oss << kRuntimeISA; |
| 959 | key_value_store_->Put(OatHeader::kDex2OatHostKey, oss.str()); |
| 960 | key_value_store_->Put(OatHeader::kPicKey, compile_pic ? "true" : "false"); |
| 961 | } |
| 962 | } |
| 963 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 964 | // Check whether the oat output file is writable, and open it for later. Also open a swap file, |
| 965 | // if a name is given. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 966 | bool OpenFile() { |
| 967 | bool create_file = !oat_unstripped_.empty(); // as opposed to using open file descriptor |
| 968 | if (create_file) { |
| 969 | oat_file_.reset(OS::CreateEmptyFile(oat_unstripped_.c_str())); |
| 970 | if (oat_location_.empty()) { |
| 971 | oat_location_ = oat_filename_; |
| 972 | } |
| 973 | } else { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 974 | oat_file_.reset(new File(oat_fd_, oat_location_, true)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 975 | oat_file_->DisableAutoClose(); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 976 | if (oat_file_->SetLength(0) != 0) { |
| 977 | PLOG(WARNING) << "Truncating oat file " << oat_location_ << " failed."; |
| 978 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 979 | } |
| 980 | if (oat_file_.get() == nullptr) { |
| 981 | PLOG(ERROR) << "Failed to create oat file: " << oat_location_; |
| 982 | return false; |
| 983 | } |
| 984 | if (create_file && fchmod(oat_file_->Fd(), 0644) != 0) { |
| 985 | PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location_; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 986 | oat_file_->Erase(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 987 | return false; |
| 988 | } |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 989 | |
| 990 | // Swap file handling. |
| 991 | // |
| 992 | // If the swap fd is not -1, we assume this is the file descriptor of an open but unlinked file |
| 993 | // that we can use for swap. |
| 994 | // |
| 995 | // If the swap fd is -1 and we have a swap-file string, open the given file as a swap file. We |
| 996 | // will immediately unlink to satisfy the swap fd assumption. |
| 997 | if (swap_fd_ == -1 && !swap_file_name_.empty()) { |
| 998 | std::unique_ptr<File> swap_file(OS::CreateEmptyFile(swap_file_name_.c_str())); |
| 999 | if (swap_file.get() == nullptr) { |
| 1000 | PLOG(ERROR) << "Failed to create swap file: " << swap_file_name_; |
| 1001 | return false; |
| 1002 | } |
| 1003 | swap_fd_ = swap_file->Fd(); |
| 1004 | swap_file->MarkUnchecked(); // We don't we to track this, it will be unlinked immediately. |
| 1005 | swap_file->DisableAutoClose(); // We'll handle it ourselves, the File object will be |
| 1006 | // released immediately. |
| 1007 | unlink(swap_file_name_.c_str()); |
| 1008 | } |
| 1009 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1010 | return true; |
| 1011 | } |
| 1012 | |
Andreas Gampe | a650e70 | 2014-12-03 14:28:02 -0800 | [diff] [blame] | 1013 | void EraseOatFile() { |
| 1014 | DCHECK(oat_file_.get() != nullptr); |
| 1015 | oat_file_->Erase(); |
| 1016 | oat_file_.reset(); |
| 1017 | } |
| 1018 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1019 | // Set up the environment for compilation. Includes starting the runtime and loading/opening the |
| 1020 | // boot class path. |
| 1021 | bool Setup() { |
| 1022 | TimingLogger::ScopedTiming t("dex2oat Setup", timings_); |
| 1023 | RuntimeOptions runtime_options; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1024 | art::MemMap::Init(); // For ZipEntry::ExtractToMemMap. |
| 1025 | if (boot_image_option_.empty()) { |
Richard Uhler | c275259 | 2015-01-02 13:28:22 -0800 | [diff] [blame] | 1026 | std::string boot_class_path = "-Xbootclasspath:"; |
| 1027 | boot_class_path += Join(dex_filenames_, ':'); |
| 1028 | runtime_options.push_back(std::make_pair(boot_class_path, nullptr)); |
| 1029 | std::string boot_class_path_locations = "-Xbootclasspath-locations:"; |
| 1030 | boot_class_path_locations += Join(dex_locations_, ':'); |
| 1031 | runtime_options.push_back(std::make_pair(boot_class_path_locations, nullptr)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1032 | } else { |
Richard Uhler | c275259 | 2015-01-02 13:28:22 -0800 | [diff] [blame] | 1033 | runtime_options.push_back(std::make_pair(boot_image_option_, nullptr)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1034 | } |
| 1035 | for (size_t i = 0; i < runtime_args_.size(); i++) { |
| 1036 | runtime_options.push_back(std::make_pair(runtime_args_[i], nullptr)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1037 | } |
Brian Carlstrom | d76e083 | 2013-08-29 15:17:42 -0700 | [diff] [blame] | 1038 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1039 | verification_results_.reset(new VerificationResults(compiler_options_.get())); |
| 1040 | callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(), &method_inliner_map_)); |
| 1041 | runtime_options.push_back(std::make_pair("compilercallbacks", callbacks_.get())); |
| 1042 | runtime_options.push_back( |
| 1043 | std::make_pair("imageinstructionset", GetInstructionSetString(instruction_set_))); |
| 1044 | |
| 1045 | if (!CreateRuntime(runtime_options)) { |
| 1046 | return false; |
| 1047 | } |
| 1048 | |
| 1049 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we |
| 1050 | // Runtime::Start, give it away now so that we don't starve GC. |
| 1051 | Thread* self = Thread::Current(); |
| 1052 | self->TransitionFromRunnableToSuspended(kNative); |
| 1053 | // If we're doing the image, override the compiler filter to force full compilation. Must be |
| 1054 | // done ahead of WellKnownClasses::Init that causes verification. Note: doesn't force |
| 1055 | // compilation of class initializers. |
| 1056 | // Whilst we're in native take the opportunity to initialize well known classes. |
| 1057 | WellKnownClasses::Init(self->GetJniEnv()); |
| 1058 | |
| 1059 | // If --image-classes was specified, calculate the full list of classes to include in the image |
| 1060 | if (image_classes_filename_ != nullptr) { |
| 1061 | std::string error_msg; |
| 1062 | if (image_classes_zip_filename_ != nullptr) { |
| 1063 | image_classes_.reset(ReadImageClassesFromZip(image_classes_zip_filename_, |
| 1064 | image_classes_filename_, |
| 1065 | &error_msg)); |
| 1066 | } else { |
| 1067 | image_classes_.reset(ReadImageClassesFromFile(image_classes_filename_)); |
| 1068 | } |
| 1069 | if (image_classes_.get() == nullptr) { |
| 1070 | LOG(ERROR) << "Failed to create list of image classes from '" << image_classes_filename_ << |
| 1071 | "': " << error_msg; |
| 1072 | return false; |
| 1073 | } |
| 1074 | } else if (image_) { |
| 1075 | image_classes_.reset(new std::set<std::string>); |
| 1076 | } |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 1077 | // If --compiled-classes was specified, calculate the full list of classes to compile in the |
| 1078 | // image. |
| 1079 | if (compiled_classes_filename_ != nullptr) { |
| 1080 | std::string error_msg; |
| 1081 | if (compiled_classes_zip_filename_ != nullptr) { |
| 1082 | compiled_classes_.reset(ReadImageClassesFromZip(compiled_classes_zip_filename_, |
| 1083 | compiled_classes_filename_, |
| 1084 | &error_msg)); |
| 1085 | } else { |
| 1086 | compiled_classes_.reset(ReadImageClassesFromFile(compiled_classes_filename_)); |
| 1087 | } |
| 1088 | if (compiled_classes_.get() == nullptr) { |
| 1089 | LOG(ERROR) << "Failed to create list of compiled classes from '" |
| 1090 | << compiled_classes_filename_ << "': " << error_msg; |
| 1091 | return false; |
| 1092 | } |
| 1093 | } else if (image_) { |
| 1094 | compiled_classes_.reset(nullptr); // By default compile everything. |
| 1095 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1096 | |
| 1097 | if (boot_image_option_.empty()) { |
| 1098 | dex_files_ = Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
| 1099 | } else { |
| 1100 | if (dex_filenames_.empty()) { |
| 1101 | ATRACE_BEGIN("Opening zip archive from file descriptor"); |
| 1102 | std::string error_msg; |
| 1103 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd_, |
| 1104 | zip_location_.c_str(), |
| 1105 | &error_msg)); |
| 1106 | if (zip_archive.get() == nullptr) { |
| 1107 | LOG(ERROR) << "Failed to open zip from file descriptor for '" << zip_location_ << "': " |
| 1108 | << error_msg; |
| 1109 | return false; |
Brian Carlstrom | f79fccb | 2014-02-20 08:55:10 -0800 | [diff] [blame] | 1110 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1111 | if (!DexFile::OpenFromZip(*zip_archive.get(), zip_location_, &error_msg, &dex_files_)) { |
| 1112 | LOG(ERROR) << "Failed to open dex from file descriptor for zip file '" << zip_location_ |
| 1113 | << "': " << error_msg; |
| 1114 | return false; |
| 1115 | } |
| 1116 | ATRACE_END(); |
| 1117 | } else { |
| 1118 | size_t failure_count = OpenDexFiles(dex_filenames_, dex_locations_, dex_files_); |
| 1119 | if (failure_count > 0) { |
| 1120 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 1121 | return false; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | constexpr bool kSaveDexInput = false; |
| 1126 | if (kSaveDexInput) { |
| 1127 | for (size_t i = 0; i < dex_files_.size(); ++i) { |
| 1128 | const DexFile* dex_file = dex_files_[i]; |
Brian Carlstrom | 95b033b | 2014-12-03 22:29:37 -0800 | [diff] [blame] | 1129 | std::string tmp_file_name(StringPrintf("/data/local/tmp/dex2oat.%d.%zd.dex", |
| 1130 | getpid(), i)); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1131 | std::unique_ptr<File> tmp_file(OS::CreateEmptyFile(tmp_file_name.c_str())); |
| 1132 | if (tmp_file.get() == nullptr) { |
| 1133 | PLOG(ERROR) << "Failed to open file " << tmp_file_name |
| 1134 | << ". Try: adb shell chmod 777 /data/local/tmp"; |
| 1135 | continue; |
| 1136 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1137 | // This is just dumping files for debugging. Ignore errors, and leave remnants. |
| 1138 | UNUSED(tmp_file->WriteFully(dex_file->Begin(), dex_file->Size())); |
| 1139 | UNUSED(tmp_file->Flush()); |
| 1140 | UNUSED(tmp_file->Close()); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1141 | LOG(INFO) << "Wrote input to " << tmp_file_name; |
| 1142 | } |
Brian Carlstrom | f79fccb | 2014-02-20 08:55:10 -0800 | [diff] [blame] | 1143 | } |
| 1144 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1145 | // Ensure opened dex files are writable for dex-to-dex transformations. |
| 1146 | for (const auto& dex_file : dex_files_) { |
| 1147 | if (!dex_file->EnableWrite()) { |
| 1148 | PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_file->GetLocation() << "'\n"; |
Andreas Gampe | 7ba6496 | 2014-10-23 11:37:40 -0700 | [diff] [blame] | 1149 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1150 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1151 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 1152 | // If we use a swap file, ensure we are above the threshold to make it necessary. |
| 1153 | if (swap_fd_ != -1) { |
| 1154 | if (!UseSwap(image_, dex_files_)) { |
| 1155 | close(swap_fd_); |
| 1156 | swap_fd_ = -1; |
| 1157 | LOG(INFO) << "Decided to run without swap."; |
| 1158 | } else { |
| 1159 | LOG(INFO) << "Accepted running with swap."; |
| 1160 | } |
| 1161 | } |
| 1162 | // Note that dex2oat won't close the swap_fd_. The compiler driver's swap space will do that. |
| 1163 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1164 | /* |
| 1165 | * If we're not in interpret-only or verify-none mode, go ahead and compile small applications. |
| 1166 | * Don't bother to check if we're doing the image. |
| 1167 | */ |
Brian Carlstrom | 95b033b | 2014-12-03 22:29:37 -0800 | [diff] [blame] | 1168 | if (!image_ && |
| 1169 | compiler_options_->IsCompilationEnabled() && |
| 1170 | compiler_kind_ == Compiler::kQuick) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1171 | size_t num_methods = 0; |
| 1172 | for (size_t i = 0; i != dex_files_.size(); ++i) { |
| 1173 | const DexFile* dex_file = dex_files_[i]; |
| 1174 | CHECK(dex_file != nullptr); |
| 1175 | num_methods += dex_file->NumMethodIds(); |
| 1176 | } |
| 1177 | if (num_methods <= compiler_options_->GetNumDexMethodsThreshold()) { |
| 1178 | compiler_options_->SetCompilerFilter(CompilerOptions::kSpeed); |
| 1179 | VLOG(compiler) << "Below method threshold, compiling anyways"; |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | return true; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1184 | } |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1185 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1186 | // Create and invoke the compiler driver. This will compile all the dex files. |
| 1187 | void Compile() { |
| 1188 | TimingLogger::ScopedTiming t("dex2oat Compile", timings_); |
| 1189 | compiler_phases_timings_.reset(new CumulativeLogger("compilation times")); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1190 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1191 | // Handle and ClassLoader creation needs to come after Runtime::Create |
| 1192 | jobject class_loader = nullptr; |
| 1193 | Thread* self = Thread::Current(); |
| 1194 | if (!boot_image_option_.empty()) { |
| 1195 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1196 | std::vector<const DexFile*> class_path_files(dex_files_); |
| 1197 | OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files); |
| 1198 | ScopedObjectAccess soa(self); |
| 1199 | for (size_t i = 0; i < class_path_files.size(); i++) { |
| 1200 | class_linker->RegisterDexFile(*class_path_files[i]); |
| 1201 | } |
| 1202 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader); |
| 1203 | ScopedLocalRef<jobject> class_loader_local(soa.Env(), |
| 1204 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 1205 | class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); |
| 1206 | Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files); |
| 1207 | } |
| 1208 | |
| 1209 | driver_.reset(new CompilerDriver(compiler_options_.get(), |
| 1210 | verification_results_.get(), |
| 1211 | &method_inliner_map_, |
| 1212 | compiler_kind_, |
| 1213 | instruction_set_, |
| 1214 | instruction_set_features_.get(), |
| 1215 | image_, |
| 1216 | image_classes_.release(), |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 1217 | compiled_classes_.release(), |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1218 | thread_count_, |
| 1219 | dump_stats_, |
| 1220 | dump_passes_, |
| 1221 | compiler_phases_timings_.get(), |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 1222 | swap_fd_, |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1223 | profile_file_)); |
| 1224 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1225 | driver_->CompileAll(class_loader, dex_files_, timings_); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1226 | } |
| 1227 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1228 | // Notes on the interleaving of creating the image and oat file to |
| 1229 | // ensure the references between the two are correct. |
| 1230 | // |
| 1231 | // Currently we have a memory layout that looks something like this: |
| 1232 | // |
| 1233 | // +--------------+ |
| 1234 | // | image | |
| 1235 | // +--------------+ |
| 1236 | // | boot oat | |
| 1237 | // +--------------+ |
| 1238 | // | alloc spaces | |
| 1239 | // +--------------+ |
| 1240 | // |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1241 | // There are several constraints on the loading of the image and boot.oat. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1242 | // |
| 1243 | // 1. The image is expected to be loaded at an absolute address and |
| 1244 | // contains Objects with absolute pointers within the image. |
| 1245 | // |
| 1246 | // 2. There are absolute pointers from Methods in the image to their |
| 1247 | // code in the oat. |
| 1248 | // |
| 1249 | // 3. There are absolute pointers from the code in the oat to Methods |
| 1250 | // in the image. |
| 1251 | // |
| 1252 | // 4. There are absolute pointers from code in the oat to other code |
| 1253 | // in the oat. |
| 1254 | // |
| 1255 | // To get this all correct, we go through several steps. |
| 1256 | // |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1257 | // 1. We prepare offsets for all data in the oat file and calculate |
| 1258 | // the oat data size and code size. During this stage, we also set |
| 1259 | // oat code offsets in methods for use by the image writer. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1260 | // |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1261 | // 2. We prepare offsets for the objects in the image and calculate |
| 1262 | // the image size. |
| 1263 | // |
| 1264 | // 3. We create the oat file. Originally this was just our own proprietary |
| 1265 | // file but now it is contained within an ELF dynamic object (aka an .so |
| 1266 | // file). Since we know the image size and oat data size and code size we |
| 1267 | // can prepare the ELF headers and we then know the ELF memory segment |
| 1268 | // layout and we can now resolve all references. The compiler provides |
| 1269 | // LinkerPatch information in each CompiledMethod and we resolve these, |
| 1270 | // using the layout information and image object locations provided by |
| 1271 | // image writer, as we're writing the method code. |
| 1272 | // |
| 1273 | // 4. We create the image file. It needs to know where the oat file |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1274 | // will be loaded after itself. Originally when oat file was simply |
| 1275 | // memory mapped so we could predict where its contents were based |
| 1276 | // on the file size. Now that it is an ELF file, we need to inspect |
| 1277 | // the ELF file to understand the in memory segment layout including |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1278 | // where the oat header is located within. |
| 1279 | // TODO: We could just remember this information from step 3. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1280 | // |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1281 | // 5. We fixup the ELF program headers so that dlopen will try to |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1282 | // load the .so at the desired location at runtime by offsetting the |
| 1283 | // Elf32_Phdr.p_vaddr values by the desired base address. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1284 | // TODO: Do this in step 3. We already know the layout there. |
| 1285 | // |
| 1286 | // Steps 1.-3. are done by the CreateOatFile() above, steps 4.-5. |
| 1287 | // are done by the CreateImageFile() below. |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1288 | |
| 1289 | |
| 1290 | // Write out the generated code part. Calls the OatWriter and ElfBuilder. Also prepares the |
| 1291 | // ImageWriter, if necessary. |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1292 | // Note: Flushing (and closing) the file is the caller's responsibility, except for the failure |
| 1293 | // case (when the file will be explicitly erased). |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1294 | bool CreateOatFile() { |
| 1295 | CHECK(key_value_store_.get() != nullptr); |
| 1296 | |
| 1297 | TimingLogger::ScopedTiming t("dex2oat Oat", timings_); |
| 1298 | |
| 1299 | std::unique_ptr<OatWriter> oat_writer; |
| 1300 | { |
| 1301 | TimingLogger::ScopedTiming t2("dex2oat OatWriter", timings_); |
| 1302 | std::string image_file_location; |
| 1303 | uint32_t image_file_location_oat_checksum = 0; |
| 1304 | uintptr_t image_file_location_oat_data_begin = 0; |
| 1305 | int32_t image_patch_delta = 0; |
| 1306 | if (image_) { |
| 1307 | PrepareImageWriter(image_base_); |
| 1308 | } else { |
| 1309 | TimingLogger::ScopedTiming t3("Loading image checksum", timings_); |
| 1310 | gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 1311 | image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum(); |
| 1312 | image_file_location_oat_data_begin = |
| 1313 | reinterpret_cast<uintptr_t>(image_space->GetImageHeader().GetOatDataBegin()); |
| 1314 | image_file_location = image_space->GetImageFilename(); |
| 1315 | image_patch_delta = image_space->GetImageHeader().GetPatchDelta(); |
| 1316 | } |
| 1317 | |
| 1318 | if (!image_file_location.empty()) { |
| 1319 | key_value_store_->Put(OatHeader::kImageLocationKey, image_file_location); |
| 1320 | } |
| 1321 | |
| 1322 | oat_writer.reset(new OatWriter(dex_files_, image_file_location_oat_checksum, |
| 1323 | image_file_location_oat_data_begin, |
| 1324 | image_patch_delta, |
| 1325 | driver_.get(), |
| 1326 | image_writer_.get(), |
| 1327 | timings_, |
| 1328 | key_value_store_.get())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1329 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1330 | |
| 1331 | if (image_) { |
| 1332 | // The OatWriter constructor has already updated offsets in methods and we need to |
| 1333 | // prepare method offsets in the image address space for direct method patching. |
| 1334 | TimingLogger::ScopedTiming t2("dex2oat Prepare image address space", timings_); |
| 1335 | if (!image_writer_->PrepareImageAddressSpace()) { |
| 1336 | LOG(ERROR) << "Failed to prepare image address space."; |
| 1337 | return false; |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | { |
| 1342 | TimingLogger::ScopedTiming t2("dex2oat Write ELF", timings_); |
| 1343 | if (!driver_->WriteElf(android_root_, is_host_, dex_files_, oat_writer.get(), |
| 1344 | oat_file_.get())) { |
| 1345 | LOG(ERROR) << "Failed to write ELF file " << oat_file_->GetPath(); |
| 1346 | return false; |
| 1347 | } |
| 1348 | } |
| 1349 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1350 | VLOG(compiler) << "Oat file written successfully (unstripped): " << oat_location_; |
| 1351 | return true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1354 | // If we are compiling an image, invoke the image creation routine. Else just skip. |
| 1355 | bool HandleImage() { |
| 1356 | if (image_) { |
| 1357 | TimingLogger::ScopedTiming t("dex2oat ImageWriter", timings_); |
| 1358 | if (!CreateImageFile()) { |
| 1359 | return false; |
| 1360 | } |
| 1361 | VLOG(compiler) << "Image written successfully: " << image_filename_; |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 1362 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1363 | return true; |
| 1364 | } |
| 1365 | |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1366 | // Create a copy from unstripped to stripped. |
| 1367 | bool CopyUnstrippedToStripped() { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1368 | // If we don't want to strip in place, copy from unstripped location to stripped location. |
| 1369 | // We need to strip after image creation because FixupElf needs to use .strtab. |
| 1370 | if (oat_unstripped_ != oat_stripped_) { |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1371 | // If the oat file is still open, flush it. |
| 1372 | if (oat_file_.get() != nullptr && oat_file_->IsOpened()) { |
| 1373 | if (!FlushCloseOatFile()) { |
| 1374 | return false; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1375 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1376 | } |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1377 | |
| 1378 | TimingLogger::ScopedTiming t("dex2oat OatFile copy", timings_); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1379 | std::unique_ptr<File> in(OS::OpenFileForReading(oat_unstripped_.c_str())); |
| 1380 | std::unique_ptr<File> out(OS::CreateEmptyFile(oat_stripped_.c_str())); |
| 1381 | size_t buffer_size = 8192; |
Dan Albert | 6fc59ab | 2014-12-11 14:09:51 -0800 | [diff] [blame] | 1382 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1383 | while (true) { |
| 1384 | int bytes_read = TEMP_FAILURE_RETRY(read(in->Fd(), buffer.get(), buffer_size)); |
| 1385 | if (bytes_read <= 0) { |
| 1386 | break; |
| 1387 | } |
| 1388 | bool write_ok = out->WriteFully(buffer.get(), bytes_read); |
| 1389 | CHECK(write_ok); |
| 1390 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1391 | if (out->FlushCloseOrErase() != 0) { |
| 1392 | PLOG(ERROR) << "Failed to flush and close copied oat file: " << oat_stripped_; |
| 1393 | return false; |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1394 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1395 | VLOG(compiler) << "Oat file copied successfully (stripped): " << oat_stripped_; |
Nicolas Geoffray | ea3fa0b | 2014-02-10 11:59:41 +0000 | [diff] [blame] | 1396 | } |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1397 | return true; |
| 1398 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1399 | |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1400 | bool FlushOatFile() { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1401 | if (oat_file_.get() != nullptr) { |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1402 | TimingLogger::ScopedTiming t2("dex2oat Flush ELF", timings_); |
| 1403 | if (oat_file_->Flush() != 0) { |
| 1404 | PLOG(ERROR) << "Failed to flush oat file: " << oat_location_ << " / " |
| 1405 | << oat_filename_; |
| 1406 | oat_file_->Erase(); |
| 1407 | return false; |
| 1408 | } |
| 1409 | } |
| 1410 | return true; |
| 1411 | } |
| 1412 | |
| 1413 | bool FlushCloseOatFile() { |
| 1414 | if (oat_file_.get() != nullptr) { |
| 1415 | std::unique_ptr<File> tmp(oat_file_.release()); |
| 1416 | if (tmp->FlushCloseOrErase() != 0) { |
| 1417 | PLOG(ERROR) << "Failed to flush and close oat file: " << oat_location_ << " / " |
| 1418 | << oat_filename_; |
| 1419 | return false; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1420 | } |
| 1421 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1422 | return true; |
| 1423 | } |
| 1424 | |
| 1425 | void DumpTiming() { |
| 1426 | if (dump_timing_ || (dump_slow_timing_ && timings_->GetTotalNs() > MsToNs(1000))) { |
| 1427 | LOG(INFO) << Dumpable<TimingLogger>(*timings_); |
| 1428 | } |
| 1429 | if (dump_passes_) { |
| 1430 | LOG(INFO) << Dumpable<CumulativeLogger>(*driver_->GetTimingsLogger()); |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | CompilerOptions* GetCompilerOptions() const { |
| 1435 | return compiler_options_.get(); |
| 1436 | } |
| 1437 | |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1438 | bool IsImage() const { |
| 1439 | return image_; |
| 1440 | } |
| 1441 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1442 | bool IsHost() const { |
| 1443 | return is_host_; |
| 1444 | } |
| 1445 | |
| 1446 | private: |
| 1447 | static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames, |
| 1448 | const std::vector<const char*>& dex_locations, |
| 1449 | std::vector<const DexFile*>& dex_files) { |
| 1450 | size_t failure_count = 0; |
| 1451 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 1452 | const char* dex_filename = dex_filenames[i]; |
| 1453 | const char* dex_location = dex_locations[i]; |
| 1454 | ATRACE_BEGIN(StringPrintf("Opening dex file '%s'", dex_filenames[i]).c_str()); |
| 1455 | std::string error_msg; |
| 1456 | if (!OS::FileExists(dex_filename)) { |
| 1457 | LOG(WARNING) << "Skipping non-existent dex file '" << dex_filename << "'"; |
| 1458 | continue; |
| 1459 | } |
| 1460 | if (!DexFile::Open(dex_filename, dex_location, &error_msg, &dex_files)) { |
| 1461 | LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "': " << error_msg; |
| 1462 | ++failure_count; |
| 1463 | } |
| 1464 | ATRACE_END(); |
| 1465 | } |
| 1466 | return failure_count; |
| 1467 | } |
| 1468 | |
| 1469 | // Returns true if dex_files has a dex with the named location. |
| 1470 | static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, |
| 1471 | const std::string& location) { |
| 1472 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 1473 | if (dex_files[i]->GetLocation() == location) { |
| 1474 | return true; |
| 1475 | } |
| 1476 | } |
| 1477 | return false; |
| 1478 | } |
| 1479 | |
| 1480 | // Appends to dex_files any elements of class_path that it doesn't already |
| 1481 | // contain. This will open those dex files as necessary. |
| 1482 | static void OpenClassPathFiles(const std::string& class_path, |
| 1483 | std::vector<const DexFile*>& dex_files) { |
| 1484 | std::vector<std::string> parsed; |
| 1485 | Split(class_path, ':', &parsed); |
| 1486 | // Take Locks::mutator_lock_ so that lock ordering on the ClassLinker::dex_lock_ is maintained. |
| 1487 | ScopedObjectAccess soa(Thread::Current()); |
| 1488 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 1489 | if (DexFilesContains(dex_files, parsed[i])) { |
| 1490 | continue; |
| 1491 | } |
| 1492 | std::string error_msg; |
| 1493 | if (!DexFile::Open(parsed[i].c_str(), parsed[i].c_str(), &error_msg, &dex_files)) { |
| 1494 | LOG(WARNING) << "Failed to open dex file '" << parsed[i] << "': " << error_msg; |
| 1495 | } |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | // Create a runtime necessary for compilation. |
| 1500 | bool CreateRuntime(const RuntimeOptions& runtime_options) |
| 1501 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
| 1502 | if (!Runtime::Create(runtime_options, false)) { |
| 1503 | LOG(ERROR) << "Failed to create runtime"; |
| 1504 | return false; |
| 1505 | } |
| 1506 | Runtime* runtime = Runtime::Current(); |
| 1507 | runtime->SetInstructionSet(instruction_set_); |
| 1508 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 1509 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 1510 | if (!runtime->HasCalleeSaveMethod(type)) { |
| 1511 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(), type); |
| 1512 | } |
| 1513 | } |
| 1514 | runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); |
| 1515 | runtime->GetClassLinker()->RunRootClinits(); |
| 1516 | runtime_ = runtime; |
| 1517 | return true; |
| 1518 | } |
| 1519 | |
| 1520 | void PrepareImageWriter(uintptr_t image_base) { |
| 1521 | image_writer_.reset(new ImageWriter(*driver_, image_base, compiler_options_->GetCompilePic())); |
| 1522 | } |
| 1523 | |
| 1524 | // Let the ImageWriter write the image file. If we do not compile PIC, also fix up the oat file. |
| 1525 | bool CreateImageFile() |
| 1526 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
| 1527 | CHECK(image_writer_ != nullptr); |
| 1528 | if (!image_writer_->Write(image_filename_, oat_unstripped_, oat_location_)) { |
| 1529 | LOG(ERROR) << "Failed to create image file " << image_filename_; |
| 1530 | return false; |
| 1531 | } |
| 1532 | uintptr_t oat_data_begin = image_writer_->GetOatDataBegin(); |
| 1533 | |
| 1534 | // Destroy ImageWriter before doing FixupElf. |
| 1535 | image_writer_.reset(); |
| 1536 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1537 | // Do not fix up the ELF file if we are --compile-pic |
| 1538 | if (!compiler_options_->GetCompilePic()) { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1539 | std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_unstripped_.c_str())); |
| 1540 | if (oat_file.get() == nullptr) { |
| 1541 | PLOG(ERROR) << "Failed to open ELF file: " << oat_unstripped_; |
| 1542 | return false; |
| 1543 | } |
| 1544 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1545 | if (!ElfWriter::Fixup(oat_file.get(), oat_data_begin)) { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1546 | oat_file->Erase(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1547 | LOG(ERROR) << "Failed to fixup ELF file " << oat_file->GetPath(); |
| 1548 | return false; |
| 1549 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 1550 | |
| 1551 | if (oat_file->FlushCloseOrErase()) { |
| 1552 | PLOG(ERROR) << "Failed to flush and close fixed ELF file " << oat_file->GetPath(); |
| 1553 | return false; |
| 1554 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | return true; |
| 1558 | } |
| 1559 | |
| 1560 | // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) |
| 1561 | static std::set<std::string>* ReadImageClassesFromFile(const char* image_classes_filename) { |
| 1562 | std::unique_ptr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, |
| 1563 | std::ifstream::in)); |
| 1564 | if (image_classes_file.get() == nullptr) { |
| 1565 | LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; |
| 1566 | return nullptr; |
| 1567 | } |
| 1568 | std::unique_ptr<std::set<std::string>> result(ReadImageClasses(*image_classes_file)); |
| 1569 | image_classes_file->close(); |
| 1570 | return result.release(); |
| 1571 | } |
| 1572 | |
| 1573 | static std::set<std::string>* ReadImageClasses(std::istream& image_classes_stream) { |
| 1574 | std::unique_ptr<std::set<std::string>> image_classes(new std::set<std::string>); |
| 1575 | while (image_classes_stream.good()) { |
| 1576 | std::string dot; |
| 1577 | std::getline(image_classes_stream, dot); |
| 1578 | if (StartsWith(dot, "#") || dot.empty()) { |
| 1579 | continue; |
| 1580 | } |
| 1581 | std::string descriptor(DotToDescriptor(dot.c_str())); |
| 1582 | image_classes->insert(descriptor); |
| 1583 | } |
| 1584 | return image_classes.release(); |
| 1585 | } |
| 1586 | |
| 1587 | // Reads the class names (java.lang.Object) and returns a set of descriptors (Ljava/lang/Object;) |
| 1588 | static std::set<std::string>* ReadImageClassesFromZip(const char* zip_filename, |
| 1589 | const char* image_classes_filename, |
| 1590 | std::string* error_msg) { |
| 1591 | std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(zip_filename, error_msg)); |
| 1592 | if (zip_archive.get() == nullptr) { |
| 1593 | return nullptr; |
| 1594 | } |
| 1595 | std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(image_classes_filename, error_msg)); |
| 1596 | if (zip_entry.get() == nullptr) { |
| 1597 | *error_msg = StringPrintf("Failed to find '%s' within '%s': %s", image_classes_filename, |
| 1598 | zip_filename, error_msg->c_str()); |
| 1599 | return nullptr; |
| 1600 | } |
| 1601 | std::unique_ptr<MemMap> image_classes_file(zip_entry->ExtractToMemMap(zip_filename, |
| 1602 | image_classes_filename, |
| 1603 | error_msg)); |
| 1604 | if (image_classes_file.get() == nullptr) { |
| 1605 | *error_msg = StringPrintf("Failed to extract '%s' from '%s': %s", image_classes_filename, |
| 1606 | zip_filename, error_msg->c_str()); |
| 1607 | return nullptr; |
| 1608 | } |
| 1609 | const std::string image_classes_string(reinterpret_cast<char*>(image_classes_file->Begin()), |
| 1610 | image_classes_file->Size()); |
| 1611 | std::istringstream image_classes_stream(image_classes_string); |
| 1612 | return ReadImageClasses(image_classes_stream); |
| 1613 | } |
| 1614 | |
Mathieu Chartier | 49285c5 | 2014-12-02 15:43:48 -0800 | [diff] [blame] | 1615 | void LogCompletionTime() { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1616 | LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) |
Mathieu Chartier | ab972ef | 2014-12-03 17:38:22 -0800 | [diff] [blame] | 1617 | << " (threads: " << thread_count_ << ") " |
| 1618 | << driver_->GetMemoryUsageString(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | std::unique_ptr<CompilerOptions> compiler_options_; |
| 1622 | Compiler::Kind compiler_kind_; |
| 1623 | |
| 1624 | InstructionSet instruction_set_; |
| 1625 | std::unique_ptr<const InstructionSetFeatures> instruction_set_features_; |
| 1626 | |
| 1627 | std::unique_ptr<SafeMap<std::string, std::string> > key_value_store_; |
| 1628 | |
| 1629 | std::unique_ptr<VerificationResults> verification_results_; |
| 1630 | DexFileToMethodInlinerMap method_inliner_map_; |
| 1631 | std::unique_ptr<QuickCompilerCallbacks> callbacks_; |
| 1632 | |
| 1633 | // Not a unique_ptr as we want to just exit on non-debug builds, not bringing the runtime down |
| 1634 | // in an orderly fashion. The destructor takes care of deleting this. |
| 1635 | Runtime* runtime_; |
| 1636 | |
| 1637 | size_t thread_count_; |
| 1638 | uint64_t start_ns_; |
| 1639 | std::unique_ptr<WatchDog> watchdog_; |
| 1640 | std::unique_ptr<File> oat_file_; |
| 1641 | std::string oat_stripped_; |
| 1642 | std::string oat_unstripped_; |
| 1643 | std::string oat_location_; |
| 1644 | std::string oat_filename_; |
| 1645 | int oat_fd_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1646 | std::vector<const char*> dex_filenames_; |
| 1647 | std::vector<const char*> dex_locations_; |
| 1648 | int zip_fd_; |
| 1649 | std::string zip_location_; |
| 1650 | std::string boot_image_option_; |
| 1651 | std::vector<const char*> runtime_args_; |
| 1652 | std::string image_filename_; |
| 1653 | uintptr_t image_base_; |
| 1654 | const char* image_classes_zip_filename_; |
| 1655 | const char* image_classes_filename_; |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 1656 | const char* compiled_classes_zip_filename_; |
| 1657 | const char* compiled_classes_filename_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1658 | std::unique_ptr<std::set<std::string>> image_classes_; |
Andreas Gampe | 4bf3ae9 | 2014-11-11 13:28:29 -0800 | [diff] [blame] | 1659 | std::unique_ptr<std::set<std::string>> compiled_classes_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1660 | bool image_; |
| 1661 | std::unique_ptr<ImageWriter> image_writer_; |
| 1662 | bool is_host_; |
| 1663 | std::string android_root_; |
| 1664 | std::vector<const DexFile*> dex_files_; |
| 1665 | std::unique_ptr<CompilerDriver> driver_; |
| 1666 | std::vector<std::string> verbose_methods_; |
| 1667 | bool dump_stats_; |
| 1668 | bool dump_passes_; |
| 1669 | bool dump_timing_; |
| 1670 | bool dump_slow_timing_; |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 1671 | std::string swap_file_name_; |
| 1672 | int swap_fd_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1673 | std::string profile_file_; // Profile file to use |
| 1674 | TimingLogger* timings_; |
| 1675 | std::unique_ptr<CumulativeLogger> compiler_phases_timings_; |
Andreas Gampe | dbfe254 | 2014-11-25 22:21:42 -0800 | [diff] [blame] | 1676 | std::unique_ptr<std::ostream> init_failure_output_; |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1677 | |
| 1678 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 1679 | }; |
| 1680 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1681 | const unsigned int WatchDog::kWatchDogTimeoutSeconds; |
| 1682 | |
| 1683 | static void b13564922() { |
| 1684 | #if defined(__linux__) && defined(__arm__) |
| 1685 | int major, minor; |
| 1686 | struct utsname uts; |
| 1687 | if (uname(&uts) != -1 && |
| 1688 | sscanf(uts.release, "%d.%d", &major, &minor) == 2 && |
| 1689 | ((major < 3) || ((major == 3) && (minor < 4)))) { |
| 1690 | // Kernels before 3.4 don't handle the ASLR well and we can run out of address |
| 1691 | // space (http://b/13564922). Work around the issue by inhibiting further mmap() randomization. |
| 1692 | int old_personality = personality(0xffffffff); |
| 1693 | if ((old_personality & ADDR_NO_RANDOMIZE) == 0) { |
| 1694 | int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); |
| 1695 | if (new_personality == -1) { |
| 1696 | LOG(WARNING) << "personality(. | ADDR_NO_RANDOMIZE) failed."; |
| 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | #endif |
| 1701 | } |
| 1702 | |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1703 | static int CompileImage(Dex2Oat& dex2oat) { |
| 1704 | dex2oat.Compile(); |
| 1705 | |
| 1706 | // Create the boot.oat. |
| 1707 | if (!dex2oat.CreateOatFile()) { |
Andreas Gampe | a650e70 | 2014-12-03 14:28:02 -0800 | [diff] [blame] | 1708 | dex2oat.EraseOatFile(); |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1709 | return EXIT_FAILURE; |
| 1710 | } |
| 1711 | |
| 1712 | // Flush and close the boot.oat. We always expect the output file by name, and it will be |
| 1713 | // re-opened from the unstripped name. |
| 1714 | if (!dex2oat.FlushCloseOatFile()) { |
| 1715 | return EXIT_FAILURE; |
| 1716 | } |
| 1717 | |
| 1718 | // Creates the boot.art and patches the boot.oat. |
| 1719 | if (!dex2oat.HandleImage()) { |
| 1720 | return EXIT_FAILURE; |
| 1721 | } |
| 1722 | |
| 1723 | // When given --host, finish early without stripping. |
| 1724 | if (dex2oat.IsHost()) { |
| 1725 | dex2oat.DumpTiming(); |
| 1726 | return EXIT_SUCCESS; |
| 1727 | } |
| 1728 | |
| 1729 | // Copy unstripped to stripped location, if necessary. |
| 1730 | if (!dex2oat.CopyUnstrippedToStripped()) { |
| 1731 | return EXIT_FAILURE; |
| 1732 | } |
| 1733 | |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1734 | // FlushClose again, as stripping might have re-opened the oat file. |
| 1735 | if (!dex2oat.FlushCloseOatFile()) { |
| 1736 | return EXIT_FAILURE; |
| 1737 | } |
| 1738 | |
| 1739 | dex2oat.DumpTiming(); |
| 1740 | return EXIT_SUCCESS; |
| 1741 | } |
| 1742 | |
| 1743 | static int CompileApp(Dex2Oat& dex2oat) { |
| 1744 | dex2oat.Compile(); |
| 1745 | |
| 1746 | // Create the app oat. |
| 1747 | if (!dex2oat.CreateOatFile()) { |
Andreas Gampe | a650e70 | 2014-12-03 14:28:02 -0800 | [diff] [blame] | 1748 | dex2oat.EraseOatFile(); |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1749 | return EXIT_FAILURE; |
| 1750 | } |
| 1751 | |
| 1752 | // Do not close the oat file here. We might haven gotten the output file by file descriptor, |
| 1753 | // which we would lose. |
| 1754 | if (!dex2oat.FlushOatFile()) { |
| 1755 | return EXIT_FAILURE; |
| 1756 | } |
| 1757 | |
| 1758 | // When given --host, finish early without stripping. |
| 1759 | if (dex2oat.IsHost()) { |
| 1760 | if (!dex2oat.FlushCloseOatFile()) { |
| 1761 | return EXIT_FAILURE; |
| 1762 | } |
| 1763 | |
| 1764 | dex2oat.DumpTiming(); |
| 1765 | return EXIT_SUCCESS; |
| 1766 | } |
| 1767 | |
| 1768 | // Copy unstripped to stripped location, if necessary. This will implicitly flush & close the |
| 1769 | // unstripped version. If this is given, we expect to be able to open writable files by name. |
| 1770 | if (!dex2oat.CopyUnstrippedToStripped()) { |
| 1771 | return EXIT_FAILURE; |
| 1772 | } |
| 1773 | |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1774 | // Flush and close the file. |
| 1775 | if (!dex2oat.FlushCloseOatFile()) { |
| 1776 | return EXIT_FAILURE; |
| 1777 | } |
| 1778 | |
| 1779 | dex2oat.DumpTiming(); |
| 1780 | return EXIT_SUCCESS; |
| 1781 | } |
| 1782 | |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1783 | static int dex2oat(int argc, char** argv) { |
| 1784 | b13564922(); |
| 1785 | |
| 1786 | TimingLogger timings("compiler", false, false); |
| 1787 | |
| 1788 | Dex2Oat dex2oat(&timings); |
| 1789 | |
| 1790 | // Parse arguments. Argument mistakes will lead to exit(EXIT_FAILURE) in UsageError. |
| 1791 | dex2oat.ParseArgs(argc, argv); |
| 1792 | |
| 1793 | // Check early that the result of compilation can be written |
| 1794 | if (!dex2oat.OpenFile()) { |
| 1795 | return EXIT_FAILURE; |
| 1796 | } |
| 1797 | |
| 1798 | LOG(INFO) << CommandLine(); |
| 1799 | |
| 1800 | if (!dex2oat.Setup()) { |
Andreas Gampe | a650e70 | 2014-12-03 14:28:02 -0800 | [diff] [blame] | 1801 | dex2oat.EraseOatFile(); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1802 | return EXIT_FAILURE; |
| 1803 | } |
| 1804 | |
Andreas Gampe | 10e477d | 2014-11-19 12:57:42 -0800 | [diff] [blame] | 1805 | if (dex2oat.IsImage()) { |
| 1806 | return CompileImage(dex2oat); |
| 1807 | } else { |
| 1808 | return CompileApp(dex2oat); |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1809 | } |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1810 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1811 | } // namespace art |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1812 | |
| 1813 | int main(int argc, char** argv) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 1814 | int result = art::dex2oat(argc, argv); |
| 1815 | // Everything was done, do an explicit exit here to avoid running Runtime destructors that take |
| 1816 | // time (bug 10645725) unless we're a debug build or running on valgrind. Note: The Dex2Oat class |
| 1817 | // should not destruct the runtime in this case. |
| 1818 | if (!art::kIsDebugBuild && (RUNNING_ON_VALGRIND == 0)) { |
| 1819 | exit(result); |
| 1820 | } |
| 1821 | return result; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1822 | } |