Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [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 | */ |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 19 | #include <sys/stat.h> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 20 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 21 | #include <iostream> |
| 22 | #include <fstream> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame^] | 26 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 27 | #include "base/unix_file/fd_file.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 28 | #include "class_linker.h" |
| 29 | #include "class_loader.h" |
| 30 | #include "compiler.h" |
| 31 | #include "image_writer.h" |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 32 | #include "leb128.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 33 | #include "oat_writer.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 34 | #include "object_utils.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 35 | #include "os.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 36 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 37 | #include "ScopedLocalRef.h" |
| 38 | #include "scoped_thread_state_change.h" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 39 | #include "sirt_ref.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 40 | #include "stringpiece.h" |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 41 | #include "timing_logger.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 42 | #include "well_known_classes.h" |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 43 | #include "zip_archive.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 44 | |
| 45 | namespace art { |
| 46 | |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 47 | static void UsageErrorV(const char* fmt, va_list ap) { |
| 48 | std::string error; |
| 49 | StringAppendV(&error, fmt, ap); |
| 50 | LOG(ERROR) << error; |
| 51 | } |
| 52 | |
| 53 | static void UsageError(const char* fmt, ...) { |
| 54 | va_list ap; |
| 55 | va_start(ap, fmt); |
| 56 | UsageErrorV(fmt, ap); |
| 57 | va_end(ap); |
| 58 | } |
| 59 | |
| 60 | static void Usage(const char* fmt, ...) { |
| 61 | va_list ap; |
| 62 | va_start(ap, fmt); |
| 63 | UsageErrorV(fmt, ap); |
| 64 | va_end(ap); |
| 65 | |
| 66 | UsageError("Usage: dex2oat [options]..."); |
| 67 | UsageError(""); |
| 68 | UsageError(" --dex-file=<dex-file>: specifies a .dex file to compile."); |
| 69 | UsageError(" Example: --dex-file=/system/framework/core.jar"); |
| 70 | UsageError(""); |
| 71 | UsageError(" --zip-fd=<file-descriptor>: specifies a file descriptor of a zip file"); |
| 72 | UsageError(" containing a classes.dex file to compile."); |
| 73 | UsageError(" Example: --zip-fd=5"); |
| 74 | UsageError(""); |
| 75 | UsageError(" --zip-location=<zip-location>: specifies a symbolic name for the file corresponding"); |
| 76 | UsageError(" to the file descriptor specified by --zip-fd."); |
| 77 | UsageError(" Example: --zip-location=/system/app/Calculator.apk"); |
| 78 | UsageError(""); |
| 79 | UsageError(" --oat-file=<file.oat>: specifies the required oat filename."); |
| 80 | UsageError(" Example: --oat-file=/system/framework/boot.oat"); |
| 81 | UsageError(""); |
| 82 | UsageError(" --oat-location=<oat-name>: specifies a symbolic name for the file corresponding"); |
| 83 | UsageError(" to the file descriptor specified by --oat-fd."); |
| 84 | UsageError(" Example: --oat-location=/data/art-cache/system@app@Calculator.apk.oat"); |
| 85 | UsageError(""); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 86 | UsageError(" --bitcode=<file.bc>: specifies the optional bitcode filename."); |
| 87 | UsageError(" Example: --bitcode=/system/framework/boot.bc"); |
| 88 | UsageError(""); |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 89 | UsageError(" --image=<file.art>: specifies the output image filename."); |
| 90 | UsageError(" Example: --image=/system/framework/boot.art"); |
| 91 | UsageError(""); |
| 92 | UsageError(" --image-classes=<classname-file>: specifies classes to include in an image."); |
| 93 | UsageError(" Example: --image=frameworks/base/preloaded-classes"); |
| 94 | UsageError(""); |
| 95 | UsageError(" --base=<hex-address>: specifies the base address when creating a boot image."); |
| 96 | UsageError(" Example: --base=0x50000000"); |
| 97 | UsageError(""); |
| 98 | UsageError(" --boot-image=<file.art>: provide the image file for the boot class path."); |
| 99 | UsageError(" Example: --boot-image=/system/framework/boot.art"); |
| 100 | UsageError(" Default: <host-prefix>/system/framework/boot.art"); |
| 101 | UsageError(""); |
| 102 | UsageError(" --host-prefix may be used to translate host paths to target paths during"); |
| 103 | UsageError(" cross compilation."); |
| 104 | UsageError(" Example: --host-prefix=out/target/product/crespo"); |
| 105 | UsageError(" Default: $ANDROID_PRODUCT_OUT"); |
| 106 | UsageError(""); |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 107 | UsageError(" --instruction-set=(arm|mips|x86): compile for a particular instruction"); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 108 | UsageError(" set."); |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 109 | UsageError(" Example: --instruction-set=x86"); |
| 110 | UsageError(" Default: arm"); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 111 | UsageError(""); |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 112 | UsageError(" --compiler-backend=(Quick|QuickGBC|Portable): select compiler backend"); |
| 113 | UsageError(" set."); |
| 114 | UsageError(" Example: --instruction-set=Portable"); |
| 115 | UsageError(" Default: Quick"); |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 116 | UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,"); |
| 117 | UsageError(" such as initial heap size, maximum heap size, and verbose output."); |
| 118 | UsageError(" Use a separate --runtime-arg switch for each argument."); |
| 119 | UsageError(" Example: --runtime-arg -Xms256m"); |
| 120 | UsageError(""); |
| 121 | std::cerr << "See log for usage error information\n"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 122 | exit(EXIT_FAILURE); |
| 123 | } |
| 124 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 125 | class Dex2Oat { |
| 126 | public: |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 127 | static bool Create(Dex2Oat** p_dex2oat, Runtime::Options& options, CompilerBackend compiler_backend, |
| 128 | InstructionSet instruction_set, size_t thread_count, bool support_debugging) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 129 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 130 | if (!CreateRuntime(options, instruction_set)) { |
| 131 | *p_dex2oat = NULL; |
| 132 | return false; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 133 | } |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 134 | *p_dex2oat = new Dex2Oat(Runtime::Current(), compiler_backend, instruction_set, thread_count, |
| 135 | support_debugging); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 136 | return true; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | ~Dex2Oat() { |
| 140 | delete runtime_; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 141 | LOG(INFO) << "dex2oat took " << PrettyDuration(NanoTime() - start_ns_) << " (threads: " << thread_count_ << ")"; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Make a list of descriptors for classes to include in the image |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 145 | const std::set<std::string>* GetImageClassDescriptors(const char* image_classes_filename) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 146 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 147 | UniquePtr<std::ifstream> image_classes_file(new std::ifstream(image_classes_filename, std::ifstream::in)); |
| 148 | if (image_classes_file.get() == NULL) { |
| 149 | LOG(ERROR) << "Failed to open image classes file " << image_classes_filename; |
| 150 | return NULL; |
| 151 | } |
| 152 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 153 | // Load all the classes specified in the file |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 154 | ClassLinker* class_linker = runtime_->GetClassLinker(); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 155 | Thread* self = Thread::Current(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 156 | while (image_classes_file->good()) { |
| 157 | std::string dot; |
| 158 | std::getline(*image_classes_file.get(), dot); |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 159 | if (StartsWith(dot, "#") || dot.empty()) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 160 | continue; |
| 161 | } |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 162 | std::string descriptor(DotToDescriptor(dot.c_str())); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 163 | SirtRef<Class> klass(self, class_linker->FindSystemClass(descriptor.c_str())); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 164 | if (klass.get() == NULL) { |
| 165 | LOG(WARNING) << "Failed to find class " << descriptor; |
| 166 | Thread::Current()->ClearException(); |
| 167 | } |
| 168 | } |
| 169 | image_classes_file->close(); |
| 170 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 171 | // Resolve exception classes referenced by the loaded classes. The catch logic assumes |
| 172 | // exceptions are resolved by the verifier when there is a catch block in an interested method. |
| 173 | // Do this here so that exception classes appear to have been specified image classes. |
| 174 | std::set<std::pair<uint16_t, const DexFile*> > unresolved_exception_types; |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 175 | SirtRef<Class> java_lang_Throwable(self, |
| 176 | class_linker->FindSystemClass("Ljava/lang/Throwable;")); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 177 | do { |
| 178 | unresolved_exception_types.clear(); |
| 179 | class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor, |
| 180 | &unresolved_exception_types); |
| 181 | typedef std::set<std::pair<uint16_t, const DexFile*> >::const_iterator It; // TODO: C++0x auto |
| 182 | for (It it = unresolved_exception_types.begin(), |
| 183 | end = unresolved_exception_types.end(); |
| 184 | it != end; ++it) { |
| 185 | uint16_t exception_type_idx = it->first; |
| 186 | const DexFile* dex_file = it->second; |
| 187 | DexCache* dex_cache = class_linker->FindDexCache(*dex_file); |
| 188 | ClassLoader* class_loader = NULL; |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 189 | SirtRef<Class> klass(self, class_linker->ResolveType(*dex_file, exception_type_idx, |
| 190 | dex_cache, class_loader)); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 191 | if (klass.get() == NULL) { |
| 192 | const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx); |
| 193 | const char* descriptor = dex_file->GetTypeDescriptor(type_id); |
| 194 | LOG(FATAL) << "Failed to resolve class " << descriptor; |
| 195 | } |
Elliott Hughes | 35be9b1 | 2012-05-29 17:59:26 -0700 | [diff] [blame] | 196 | DCHECK(java_lang_Throwable->IsAssignableFrom(klass.get())); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 197 | } |
| 198 | // Resolving exceptions may load classes that reference more exceptions, iterate until no |
| 199 | // more are found |
| 200 | } while (!unresolved_exception_types.empty()); |
| 201 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 202 | // We walk the roots looking for classes so that we'll pick up the |
| 203 | // above classes plus any classes them depend on such super |
| 204 | // classes, interfaces, and the required ClassLinker roots. |
| 205 | UniquePtr<std::set<std::string> > image_classes(new std::set<std::string>()); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 206 | class_linker->VisitClasses(RecordImageClassesVisitor, image_classes.get()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 207 | CHECK_NE(image_classes->size(), 0U); |
| 208 | return image_classes.release(); |
| 209 | } |
| 210 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 211 | const Compiler* CreateOatFile(const std::string& boot_image_option, |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 212 | const std::string* host_prefix, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 213 | const std::vector<const DexFile*>& dex_files, |
| 214 | File* oat_file, |
Logan Chien | de08e84 | 2012-03-21 00:34:12 +0800 | [diff] [blame] | 215 | const std::string& bitcode_filename, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 216 | bool image, |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 217 | const std::set<std::string>* image_classes, |
| 218 | bool dump_stats, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 219 | bool dump_timings) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 220 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 221 | // SirtRef and ClassLoader creation needs to come after Runtime::Create |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 222 | jobject class_loader = NULL; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 223 | if (!boot_image_option.empty()) { |
| 224 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 225 | std::vector<const DexFile*> class_path_files(dex_files); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 226 | OpenClassPathFiles(runtime_->GetClassPathString(), class_path_files); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 227 | for (size_t i = 0; i < class_path_files.size(); i++) { |
| 228 | class_linker->RegisterDexFile(*class_path_files[i]); |
| 229 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 230 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 231 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader); |
| 232 | ScopedLocalRef<jobject> class_loader_local(soa.Env(), |
| 233 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 234 | class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); |
| 235 | Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path_files); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 236 | } |
| 237 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 238 | UniquePtr<Compiler> compiler(new Compiler(compiler_backend_, |
| 239 | instruction_set_, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 240 | image, |
| 241 | thread_count_, |
| 242 | support_debugging_, |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 243 | image_classes, |
| 244 | dump_stats, |
| 245 | dump_timings)); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 246 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 247 | if ((compiler_backend_ == kPortable) || (compiler_backend_ == kIceland)) { |
| 248 | compiler->SetBitcodeFileName(bitcode_filename); |
| 249 | } |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 250 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 251 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 252 | |
| 253 | compiler->CompileAll(class_loader, dex_files); |
| 254 | |
| 255 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 256 | |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 257 | std::string image_file_location; |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 258 | uint32_t image_file_location_oat_checksum = 0; |
| 259 | uint32_t image_file_location_oat_begin = 0; |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 260 | Heap* heap = Runtime::Current()->GetHeap(); |
| 261 | if (heap->GetSpaces().size() > 1) { |
| 262 | ImageSpace* image_space = heap->GetImageSpace(); |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 263 | image_file_location_oat_checksum = image_space->GetImageHeader().GetOatChecksum(); |
| 264 | image_file_location_oat_begin = reinterpret_cast<uint32_t>(image_space->GetImageHeader().GetOatBegin()); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 265 | image_file_location = image_space->GetImageFilename(); |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 266 | if (host_prefix != NULL && StartsWith(image_file_location, host_prefix->c_str())) { |
| 267 | image_file_location = image_file_location.substr(host_prefix->size()); |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | if (!OatWriter::Create(oat_file, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 272 | dex_files, |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 273 | image_file_location_oat_checksum, |
| 274 | image_file_location_oat_begin, |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 275 | image_file_location, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 276 | *compiler.get())) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 277 | LOG(ERROR) << "Failed to create oat file " << oat_file->GetPath(); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 278 | return NULL; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 279 | } |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 280 | return compiler.release(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 283 | bool CreateImageFile(const std::string& image_filename, |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 284 | uintptr_t image_base, |
| 285 | const std::set<std::string>* image_classes, |
| 286 | const std::string& oat_filename, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 287 | const std::string& oat_location, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 288 | const Compiler& compiler) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 289 | LOCKS_EXCLUDED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 290 | ImageWriter image_writer(image_classes); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 291 | if (!image_writer.Write(image_filename, image_base, oat_filename, oat_location, compiler)) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 292 | LOG(ERROR) << "Failed to create image file " << image_filename; |
| 293 | return false; |
| 294 | } |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | private: |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 299 | explicit Dex2Oat(Runtime* runtime, CompilerBackend compiler_backend, InstructionSet instruction_set, |
| 300 | size_t thread_count, bool support_debugging) |
| 301 | : compiler_backend_(compiler_backend), |
| 302 | instruction_set_(instruction_set), |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 303 | runtime_(runtime), |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 304 | thread_count_(thread_count), |
| 305 | support_debugging_(support_debugging), |
| 306 | start_ns_(NanoTime()) { |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 307 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 308 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 309 | static bool CreateRuntime(Runtime::Options& options, InstructionSet instruction_set) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 310 | SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 311 | if (!Runtime::Create(options, false)) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 312 | LOG(ERROR) << "Failed to create runtime"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 313 | return false; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 314 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 315 | Runtime* runtime = Runtime::Current(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 316 | // if we loaded an existing image, we will reuse values from the image roots. |
| 317 | if (!runtime->HasJniDlsymLookupStub()) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 318 | runtime->SetJniDlsymLookupStub(Compiler::CreateJniDlsymLookupStub(instruction_set)); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 319 | } |
| 320 | if (!runtime->HasAbstractMethodErrorStubArray()) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 321 | runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(instruction_set)); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 322 | } |
| 323 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
| 324 | Runtime::TrampolineType type = Runtime::TrampolineType(i); |
| 325 | if (!runtime->HasResolutionStubArray(type)) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 326 | runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(instruction_set, type), type); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 327 | } |
| 328 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 329 | if (!runtime->HasResolutionMethod()) { |
| 330 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 331 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 332 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 333 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 334 | if (!runtime->HasCalleeSaveMethod(type)) { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 335 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(instruction_set, type), type); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 336 | } |
| 337 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 338 | runtime->GetClassLinker()->FixupDexCaches(runtime->GetResolutionMethod()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 339 | return true; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 342 | static void ResolveExceptionsForMethod(MethodHelper* mh, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 343 | std::set<std::pair<uint16_t, const DexFile*> >& exceptions_to_resolve) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 344 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 345 | const DexFile::CodeItem* code_item = mh->GetCodeItem(); |
| 346 | if (code_item == NULL) { |
| 347 | return; // native or abstract method |
| 348 | } |
| 349 | if (code_item->tries_size_ == 0) { |
| 350 | return; // nothing to process |
| 351 | } |
| 352 | const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0); |
| 353 | size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 354 | for (size_t i = 0; i < num_encoded_catch_handlers; i++) { |
| 355 | int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list); |
| 356 | bool has_catch_all = false; |
| 357 | if (encoded_catch_handler_size <= 0) { |
| 358 | encoded_catch_handler_size = -encoded_catch_handler_size; |
| 359 | has_catch_all = true; |
| 360 | } |
| 361 | for (int32_t j = 0; j < encoded_catch_handler_size; j++) { |
| 362 | uint16_t encoded_catch_handler_handlers_type_idx = |
| 363 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 364 | // Add to set of types to resolve if not already in the dex cache resolved types |
| 365 | if (!mh->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) { |
| 366 | exceptions_to_resolve.insert( |
| 367 | std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx, |
| 368 | &mh->GetDexFile())); |
| 369 | } |
| 370 | // ignore address associated with catch handler |
| 371 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 372 | } |
| 373 | if (has_catch_all) { |
| 374 | // ignore catch all address |
| 375 | DecodeUnsignedLeb128(&encoded_catch_handler_list); |
| 376 | } |
| 377 | } |
| 378 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 379 | |
| 380 | static bool ResolveCatchBlockExceptionsClassVisitor(Class* c, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 381 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 382 | std::set<std::pair<uint16_t, const DexFile*> >* exceptions_to_resolve = |
| 383 | reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*> >*>(arg); |
| 384 | MethodHelper mh; |
| 385 | for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 386 | AbstractMethod* m = c->GetVirtualMethod(i); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 387 | mh.ChangeMethod(m); |
| 388 | ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); |
| 389 | } |
| 390 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 391 | AbstractMethod* m = c->GetDirectMethod(i); |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 392 | mh.ChangeMethod(m); |
| 393 | ResolveExceptionsForMethod(&mh, *exceptions_to_resolve); |
| 394 | } |
| 395 | return true; |
| 396 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 397 | |
| 398 | static bool RecordImageClassesVisitor(Class* klass, void* arg) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 399 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 400 | std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg); |
| 401 | if (klass->IsArrayClass() || klass->IsPrimitive()) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 402 | return true; |
| 403 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 404 | image_classes->insert(ClassHelper(klass).GetDescriptor()); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 405 | return true; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 406 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 407 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 408 | // Appends to dex_files any elements of class_path that it doesn't already |
| 409 | // contain. This will open those dex files as necessary. |
| 410 | static void OpenClassPathFiles(const std::string& class_path, std::vector<const DexFile*>& dex_files) { |
| 411 | std::vector<std::string> parsed; |
| 412 | Split(class_path, ':', parsed); |
| 413 | for (size_t i = 0; i < parsed.size(); ++i) { |
| 414 | if (DexFilesContains(dex_files, parsed[i])) { |
| 415 | continue; |
| 416 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 417 | const DexFile* dex_file = DexFile::Open(parsed[i], parsed[i]); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 418 | if (dex_file == NULL) { |
| 419 | LOG(WARNING) << "Failed to open dex file " << parsed[i]; |
| 420 | } else { |
| 421 | dex_files.push_back(dex_file); |
| 422 | } |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 423 | } |
| 424 | } |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 425 | |
| 426 | // Returns true if dex_files has a dex with the named location. |
| 427 | static bool DexFilesContains(const std::vector<const DexFile*>& dex_files, const std::string& location) { |
| 428 | for (size_t i = 0; i < dex_files.size(); ++i) { |
| 429 | if (dex_files[i]->GetLocation() == location) { |
| 430 | return true; |
| 431 | } |
| 432 | } |
| 433 | return false; |
| 434 | } |
| 435 | |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 436 | const CompilerBackend compiler_backend_; |
| 437 | |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 438 | const InstructionSet instruction_set_; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 439 | |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 440 | Runtime* runtime_; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 441 | size_t thread_count_; |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 442 | bool support_debugging_; |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 443 | uint64_t start_ns_; |
| 444 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 445 | DISALLOW_IMPLICIT_CONSTRUCTORS(Dex2Oat); |
| 446 | }; |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 447 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 448 | static bool ParseInt(const char* in, int* out) { |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 449 | char* end; |
| 450 | int result = strtol(in, &end, 10); |
| 451 | if (in == end || *end != '\0') { |
| 452 | return false; |
| 453 | } |
| 454 | *out = result; |
| 455 | return true; |
| 456 | } |
| 457 | |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 458 | static size_t OpenDexFiles(const std::vector<const char*>& dex_filenames, |
| 459 | const std::vector<const char*>& dex_locations, |
| 460 | std::vector<const DexFile*>& dex_files) { |
| 461 | size_t failure_count = 0; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 462 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 463 | const char* dex_filename = dex_filenames[i]; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 464 | const char* dex_location = dex_locations[i]; |
| 465 | const DexFile* dex_file = DexFile::Open(dex_filename, dex_location); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 466 | if (dex_file == NULL) { |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 467 | LOG(WARNING) << "could not open .dex from file " << dex_filename; |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 468 | ++failure_count; |
jeffhao | 60f83e3 | 2012-02-13 17:16:30 -0800 | [diff] [blame] | 469 | } else { |
| 470 | dex_files.push_back(dex_file); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 471 | } |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 472 | } |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 473 | return failure_count; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 476 | // The primary goal of the watchdog is to prevent stuck build servers |
| 477 | // during development when fatal aborts lead to a cascade of failures |
| 478 | // that result in a deadlock. |
| 479 | class WatchDog { |
| 480 | |
| 481 | // WatchDog defines its own CHECK_PTHREAD_CALL to avoid using Log which uses locks |
| 482 | #undef CHECK_PTHREAD_CALL |
| 483 | #define CHECK_WATCH_DOG_PTHREAD_CALL(call, args, what) \ |
| 484 | do { \ |
| 485 | int rc = call args; \ |
| 486 | if (rc != 0) { \ |
| 487 | errno = rc; \ |
| 488 | std::string message(# call); \ |
| 489 | message += " failed for "; \ |
| 490 | message += reason; \ |
| 491 | Die(message); \ |
| 492 | } \ |
| 493 | } while (false) |
| 494 | |
| 495 | public: |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 496 | WatchDog(bool is_watch_dog_enabled) { |
| 497 | is_watch_dog_enabled_ = is_watch_dog_enabled; |
| 498 | if (!is_watch_dog_enabled_) { |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | shutting_down_ = false; |
| 502 | const char* reason = "dex2oat watch dog thread startup"; |
| 503 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_init, (&mutex_, NULL), reason); |
| 504 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_init, (&cond_, NULL), reason); |
| 505 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_init, (&attr_), reason); |
| 506 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_create, (&pthread_, &attr_, &CallBack, this), reason); |
| 507 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_attr_destroy, (&attr_), reason); |
| 508 | } |
| 509 | ~WatchDog() { |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 510 | if (!is_watch_dog_enabled_) { |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 511 | return; |
| 512 | } |
| 513 | const char* reason = "dex2oat watch dog thread shutdown"; |
| 514 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 515 | shutting_down_ = true; |
| 516 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_signal, (&cond_), reason); |
| 517 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 518 | |
| 519 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_join, (pthread_, NULL), reason); |
| 520 | |
| 521 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_cond_destroy, (&cond_), reason); |
| 522 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_destroy, (&mutex_), reason); |
| 523 | } |
| 524 | |
| 525 | private: |
| 526 | static void* CallBack(void* arg) { |
| 527 | WatchDog* self = reinterpret_cast<WatchDog*>(arg); |
| 528 | self->Wait(); |
| 529 | return NULL; |
| 530 | } |
| 531 | |
| 532 | static void Die(const std::string& message) { |
| 533 | // TODO: Switch to LOG(FATAL) when we can guarantee it won't prevent shutdown in error cases |
| 534 | fprintf(stderr, "%s\n", message.c_str()); |
| 535 | exit(1); |
| 536 | } |
| 537 | |
| 538 | void Wait() { |
| 539 | int64_t ms = kWatchDogTimeoutSeconds * 1000; |
| 540 | int32_t ns = 0; |
| 541 | timespec ts; |
| 542 | InitTimeSpec(true, CLOCK_REALTIME, ms, ns, &ts); |
| 543 | const char* reason = "dex2oat watch dog thread waiting"; |
| 544 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_lock, (&mutex_), reason); |
| 545 | while (!shutting_down_) { |
| 546 | int rc = TEMP_FAILURE_RETRY(pthread_cond_timedwait(&cond_, &mutex_, &ts)); |
| 547 | if (rc == ETIMEDOUT) { |
| 548 | std::string message(StringPrintf("dex2oat did not finish after %d seconds", |
| 549 | kWatchDogTimeoutSeconds)); |
| 550 | Die(message.c_str()); |
| 551 | } |
| 552 | if (rc != 0) { |
| 553 | std::string message(StringPrintf("pthread_cond_timedwait failed: %s", |
| 554 | strerror(errno))); |
| 555 | Die(message.c_str()); |
| 556 | } |
| 557 | } |
| 558 | CHECK_WATCH_DOG_PTHREAD_CALL(pthread_mutex_unlock, (&mutex_), reason); |
| 559 | } |
| 560 | |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 561 | #ifdef ART_USE_LLVM_COMPILER |
| 562 | static const unsigned int kWatchDogTimeoutSeconds = 20 * 60; // 15 minutes + buffer |
| 563 | #else |
| 564 | static const unsigned int kWatchDogTimeoutSeconds = 2 * 60; // 1 minute + buffer |
| 565 | #endif |
| 566 | |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 567 | bool is_watch_dog_enabled_; |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 568 | bool shutting_down_; |
| 569 | // TODO: Switch to Mutex when we can guarantee it won't prevent shutdown in error cases |
| 570 | pthread_mutex_t mutex_; |
| 571 | pthread_cond_t cond_; |
| 572 | pthread_attr_t attr_; |
| 573 | pthread_t pthread_; |
| 574 | }; |
| 575 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 576 | static int dex2oat(int argc, char** argv) { |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 577 | InitLogging(argv); |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 578 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 579 | // Skip over argv[0]. |
| 580 | argv++; |
| 581 | argc--; |
| 582 | |
| 583 | if (argc == 0) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 584 | Usage("no arguments specified"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | std::vector<const char*> dex_filenames; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 588 | std::vector<const char*> dex_locations; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 589 | int zip_fd = -1; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 590 | std::string zip_location; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 591 | std::string oat_filename; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 592 | std::string oat_location; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 593 | int oat_fd = -1; |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 594 | std::string bitcode_filename; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 595 | const char* image_classes_filename = NULL; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 596 | std::string image_filename; |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 597 | std::string boot_image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 598 | uintptr_t image_base = 0; |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 599 | UniquePtr<std::string> host_prefix; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 600 | std::vector<const char*> runtime_args; |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 601 | int thread_count = sysconf(_SC_NPROCESSORS_CONF); |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 602 | bool support_debugging = false; |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 603 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 604 | CompilerBackend compiler_backend = kPortable; |
| 605 | #elif defined(ART_USE_LLVM_COMPILER) |
| 606 | CompilerBackend compiler_backend = kIceland; |
| 607 | #else |
| 608 | CompilerBackend compiler_backend = kQuick; |
| 609 | #endif |
jeffhao | 9ad4f22 | 2012-05-30 18:51:19 -0700 | [diff] [blame] | 610 | #if defined(__arm__) |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 611 | InstructionSet instruction_set = kThumb2; |
jeffhao | 9ad4f22 | 2012-05-30 18:51:19 -0700 | [diff] [blame] | 612 | #elif defined(__i386__) |
| 613 | InstructionSet instruction_set = kX86; |
| 614 | #elif defined(__mips__) |
| 615 | InstructionSet instruction_set = kMips; |
| 616 | #else |
| 617 | #error "Unsupported architecture" |
| 618 | #endif |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 619 | bool dump_stats = kIsDebugBuild; |
| 620 | bool dump_timings = kIsDebugBuild; |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 621 | bool watch_dog_enabled = !kIsTargetBuild; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 622 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 623 | for (int i = 0; i < argc; i++) { |
| 624 | const StringPiece option(argv[i]); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 625 | bool log_options = false; |
| 626 | if (log_options) { |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 627 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 628 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 629 | if (option.starts_with("--dex-file=")) { |
| 630 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 631 | } else if (option.starts_with("--dex-location=")) { |
| 632 | dex_locations.push_back(option.substr(strlen("--dex-location=")).data()); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 633 | } else if (option.starts_with("--zip-fd=")) { |
| 634 | const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data(); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 635 | if (!ParseInt(zip_fd_str, &zip_fd)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 636 | Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 637 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 638 | } else if (option.starts_with("--zip-location=")) { |
| 639 | zip_location = option.substr(strlen("--zip-location=")).data(); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 640 | } else if (option.starts_with("--oat-file=")) { |
| 641 | oat_filename = option.substr(strlen("--oat-file=")).data(); |
| 642 | } else if (option.starts_with("--oat-fd=")) { |
| 643 | const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data(); |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 644 | if (!ParseInt(oat_fd_str, &oat_fd)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 645 | Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 646 | } |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 647 | } else if (option == "-g") { |
Elliott Hughes | de6e4cf | 2012-02-27 14:46:06 -0800 | [diff] [blame] | 648 | support_debugging = true; |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 649 | } else if (option == "--watch-dog") { |
| 650 | watch_dog_enabled = true; |
| 651 | } else if (option == "--no-watch-dog") { |
| 652 | watch_dog_enabled = false; |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 653 | } else if (option.starts_with("-j")) { |
Brian Carlstrom | b12552a | 2012-02-04 17:17:31 -0800 | [diff] [blame] | 654 | const char* thread_count_str = option.substr(strlen("-j")).data(); |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 655 | if (!ParseInt(thread_count_str, &thread_count)) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 656 | Usage("could not parse -j argument '%s' as an integer", thread_count_str); |
Elliott Hughes | 5523ee0 | 2012-02-03 18:18:34 -0800 | [diff] [blame] | 657 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 658 | } else if (option.starts_with("--oat-location=")) { |
| 659 | oat_location = option.substr(strlen("--oat-location=")).data(); |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 660 | } else if (option.starts_with("--bitcode=")) { |
| 661 | bitcode_filename = option.substr(strlen("--bitcode=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 662 | } else if (option.starts_with("--image=")) { |
| 663 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 664 | } else if (option.starts_with("--image-classes=")) { |
| 665 | image_classes_filename = option.substr(strlen("--image-classes=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 666 | } else if (option.starts_with("--base=")) { |
| 667 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 668 | char* end; |
| 669 | image_base = strtoul(image_base_str, &end, 16); |
| 670 | if (end == image_base_str || *end != '\0') { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 671 | Usage("Failed to parse hexadecimal value for option %s", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 672 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 673 | } else if (option.starts_with("--boot-image=")) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 674 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 675 | } else if (option.starts_with("--host-prefix=")) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 676 | host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data())); |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 677 | } else if (option.starts_with("--instruction-set=")) { |
| 678 | StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data(); |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 679 | if (instruction_set_str == "arm") { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 680 | instruction_set = kThumb2; |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 681 | } else if (instruction_set_str == "mips") { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 682 | instruction_set = kMips; |
jeffhao | 1f71ae8 | 2012-05-24 16:08:24 -0700 | [diff] [blame] | 683 | } else if (instruction_set_str == "x86") { |
Ian Rogers | 49c4894 | 2012-03-11 15:15:37 -0700 | [diff] [blame] | 684 | instruction_set = kX86; |
| 685 | } |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 686 | } else if (option.starts_with("--compiler-backend=")) { |
| 687 | StringPiece backend_str = option.substr(strlen("--compiler-backend=")).data(); |
| 688 | if (backend_str == "Quick") { |
| 689 | compiler_backend = kQuick; |
| 690 | } else if (backend_str == "QuickGBC") { |
| 691 | compiler_backend = kQuickGBC; |
| 692 | } else if (backend_str == "Iceland") { |
| 693 | // TODO: remove this when Portable/Iceland merge complete |
| 694 | compiler_backend = kIceland; |
| 695 | } else if (backend_str == "Portable") { |
| 696 | compiler_backend = kPortable; |
| 697 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 698 | } else if (option == "--runtime-arg") { |
| 699 | if (++i >= argc) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 700 | Usage("Missing required argument for --runtime-arg"); |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 701 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 702 | if (log_options) { |
| 703 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 704 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 705 | runtime_args.push_back(argv[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 706 | } else { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 707 | Usage("unknown argument %s", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 711 | if (oat_filename.empty() && oat_fd == -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 712 | Usage("Output must be supplied with either --oat-file or --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | if (!oat_filename.empty() && oat_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 716 | Usage("--oat-file should not be used with --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | if (!oat_filename.empty() && oat_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 720 | Usage("--oat-file should not be used with --oat-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 723 | if (oat_fd != -1 && !image_filename.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 724 | Usage("--oat-fd should not be used with --image"); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 727 | if (host_prefix.get() == NULL) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 728 | const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); |
| 729 | if (android_product_out != NULL) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 730 | host_prefix.reset(new std::string(android_product_out)); |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 734 | bool image = (!image_filename.empty()); |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 735 | if (!image && boot_image_filename.empty()) { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 736 | if (host_prefix.get() == NULL) { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 737 | boot_image_filename += GetAndroidRoot(); |
| 738 | } else { |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 739 | boot_image_filename += *host_prefix.get(); |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 740 | boot_image_filename += "/system"; |
| 741 | } |
| 742 | boot_image_filename += "/framework/boot.art"; |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 743 | } |
| 744 | std::string boot_image_option; |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 745 | if (!boot_image_filename.empty()) { |
Brian Carlstrom | b001126 | 2011-12-09 12:17:24 -0800 | [diff] [blame] | 746 | boot_image_option += "-Ximage:"; |
| 747 | boot_image_option += boot_image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 750 | if (image_classes_filename != NULL && !image) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 751 | Usage("--image-classes should only be used with --image"); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | if (image_classes_filename != NULL && !boot_image_option.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 755 | Usage("--image-classes should not be used with --boot-image"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 758 | if (dex_filenames.empty() && zip_fd == -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 759 | Usage("Input must be supplied with either --dex-file or --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | if (!dex_filenames.empty() && zip_fd != -1) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 763 | Usage("--dex-file should not be used with --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 764 | } |
| 765 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 766 | if (!dex_filenames.empty() && !zip_location.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 767 | Usage("--dex-file should not be used with --zip-location"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 770 | if (dex_locations.empty()) { |
| 771 | for (size_t i = 0; i < dex_filenames.size(); i++) { |
| 772 | dex_locations.push_back(dex_filenames[i]); |
| 773 | } |
| 774 | } else if (dex_locations.size() != dex_filenames.size()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 775 | Usage("--dex-location arguments do not match --dex-file arguments"); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | if (zip_fd != -1 && zip_location.empty()) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 779 | Usage("--zip-location should be supplied with --zip-fd"); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 782 | if (boot_image_option.empty()) { |
| 783 | if (image_base == 0) { |
Brian Carlstrom | cbfe6fe | 2012-02-17 11:13:36 -0800 | [diff] [blame] | 784 | Usage("non-zero --base not specified"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 785 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Brian Carlstrom | 994d62a | 2012-11-05 20:43:45 -0800 | [diff] [blame] | 788 | // Done with usage checks, enable watchdog if requested |
| 789 | WatchDog watch_dog(watch_dog_enabled); |
| 790 | |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 791 | // Check early that the result of compilation can be written |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 792 | UniquePtr<File> oat_file; |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 793 | bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor |
| 794 | if (create_file) { |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 795 | oat_file.reset(OS::OpenFile(oat_filename.c_str(), true)); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 796 | if (oat_location.empty()) { |
| 797 | oat_location = oat_filename; |
| 798 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 799 | } else { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 800 | oat_file.reset(new File(oat_fd, oat_location)); |
| 801 | oat_file->DisableAutoClose(); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 802 | } |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 803 | if (oat_file.get() == NULL) { |
Brian Carlstrom | 6cd40e5 | 2012-05-03 14:15:11 -0700 | [diff] [blame] | 804 | PLOG(ERROR) << "Failed to create oat file: " << oat_location; |
| 805 | return EXIT_FAILURE; |
| 806 | } |
| 807 | if (create_file && fchmod(oat_file->Fd(), 0644) != 0) { |
| 808 | PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location; |
Brian Carlstrom | 6ef827a | 2011-12-11 14:57:47 -0800 | [diff] [blame] | 809 | return EXIT_FAILURE; |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 810 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 811 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 812 | LOG(INFO) << "dex2oat: " << oat_location; |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 813 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 814 | Runtime::Options options; |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 815 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 816 | std::vector<const DexFile*> boot_class_path; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 817 | if (boot_image_option.empty()) { |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 818 | size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, boot_class_path); |
| 819 | if (failure_count > 0) { |
| 820 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 821 | return EXIT_FAILURE; |
| 822 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 823 | options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 824 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 825 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 826 | } |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 827 | if (host_prefix.get() != NULL) { |
| 828 | options.push_back(std::make_pair("host-prefix", host_prefix->c_str())); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 829 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 830 | for (size_t i = 0; i < runtime_args.size(); i++) { |
| 831 | options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); |
| 832 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 833 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 834 | Dex2Oat* p_dex2oat; |
buzbee | c531cef | 2012-10-18 07:09:20 -0700 | [diff] [blame] | 835 | if (!Dex2Oat::Create(&p_dex2oat, options, compiler_backend, instruction_set, thread_count, support_debugging)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 836 | LOG(ERROR) << "Failed to create dex2oat"; |
| 837 | return EXIT_FAILURE; |
| 838 | } |
| 839 | UniquePtr<Dex2Oat> dex2oat(p_dex2oat); |
| 840 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 841 | // give it away now and then switch to a more managable ScopedObjectAccess. |
| 842 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 843 | // Whilst we're in native take the opportunity to initialize well known classes. |
| 844 | WellKnownClasses::InitClasses(Thread::Current()->GetJniEnv()); |
| 845 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 846 | |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 847 | // If --image-classes was specified, calculate the full list of classes to include in the image |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 848 | UniquePtr<const std::set<std::string> > image_classes(NULL); |
| 849 | if (image_classes_filename != NULL) { |
| 850 | image_classes.reset(dex2oat->GetImageClassDescriptors(image_classes_filename)); |
| 851 | if (image_classes.get() == NULL) { |
| 852 | LOG(ERROR) << "Failed to create list of image classes from " << image_classes_filename; |
| 853 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 857 | std::vector<const DexFile*> dex_files; |
| 858 | if (boot_image_option.empty()) { |
| 859 | dex_files = Runtime::Current()->GetClassLinker()->GetBootClassPath(); |
| 860 | } else { |
| 861 | if (dex_filenames.empty()) { |
| 862 | UniquePtr<ZipArchive> zip_archive(ZipArchive::OpenFromFd(zip_fd)); |
| 863 | if (zip_archive.get() == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 864 | LOG(ERROR) << "Failed to zip from file descriptor for " << zip_location; |
Brian Carlstrom | 2e3d1b2 | 2012-01-09 18:01:56 -0800 | [diff] [blame] | 865 | return EXIT_FAILURE; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 866 | } |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 867 | const DexFile* dex_file = DexFile::Open(*zip_archive.get(), zip_location); |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 868 | if (dex_file == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 869 | LOG(ERROR) << "Failed to open dex from file descriptor for zip file: " << zip_location; |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 870 | return EXIT_FAILURE; |
| 871 | } |
| 872 | dex_files.push_back(dex_file); |
| 873 | } else { |
Elliott Hughes | bf1b457 | 2012-05-24 19:11:39 -0700 | [diff] [blame] | 874 | size_t failure_count = OpenDexFiles(dex_filenames, dex_locations, dex_files); |
| 875 | if (failure_count > 0) { |
| 876 | LOG(ERROR) << "Failed to open some dex files: " << failure_count; |
| 877 | return EXIT_FAILURE; |
| 878 | } |
Brian Carlstrom | a6cc893 | 2012-01-04 14:44:07 -0800 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 882 | UniquePtr<const Compiler> compiler(dex2oat->CreateOatFile(boot_image_option, |
Brian Carlstrom | 34f1fa4 | 2012-04-05 18:28:12 -0700 | [diff] [blame] | 883 | host_prefix.get(), |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 884 | dex_files, |
| 885 | oat_file.get(), |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 886 | bitcode_filename, |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 887 | image, |
Brian Carlstrom | ba0668e | 2012-03-26 13:14:07 -0700 | [diff] [blame] | 888 | image_classes.get(), |
| 889 | dump_stats, |
| 890 | dump_timings)); |
Brian Carlstrom | f582258 | 2012-03-19 22:34:31 -0700 | [diff] [blame] | 891 | |
| 892 | if (compiler.get() == NULL) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 893 | LOG(ERROR) << "Failed to create oat file: " << oat_location; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 894 | return EXIT_FAILURE; |
| 895 | } |
| 896 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 897 | if (!image) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 898 | LOG(INFO) << "Oat file written successfully: " << oat_location; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 899 | return EXIT_SUCCESS; |
| 900 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 901 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 902 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 903 | bool image_creation_success = dex2oat->CreateImageFile(image_filename, |
| 904 | image_base, |
| 905 | image_classes.get(), |
| 906 | oat_filename, |
| 907 | oat_location, |
| 908 | *compiler.get()); |
| 909 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 910 | if (!image_creation_success) { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 911 | return EXIT_FAILURE; |
| 912 | } |
| 913 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 914 | // We wrote the oat file successfully, and want to keep it. |
Elliott Hughes | bb551fa | 2012-01-25 16:35:29 -0800 | [diff] [blame] | 915 | LOG(INFO) << "Oat file written successfully: " << oat_filename; |
| 916 | LOG(INFO) << "Image written successfully: " << image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 917 | return EXIT_SUCCESS; |
| 918 | } |
| 919 | |
| 920 | } // namespace art |
| 921 | |
| 922 | int main(int argc, char** argv) { |
| 923 | return art::dex2oat(argc, argv); |
| 924 | } |