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