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