blob: 94f6345bb0da9af12f6e15224ae9b816513007a0 [file] [log] [blame]
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "oat_file_manager.h"
18
19#include <memory>
20#include <queue>
21#include <vector>
22
23#include "base/logging.h"
24#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080025#include "base/systrace.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080026#include "class_linker.h"
Mathieu Chartier80b37b72015-10-12 18:13:39 -070027#include "dex_file-inl.h"
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -080028#include "gc/scoped_gc_critical_section.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070029#include "gc/space/image_space.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080030#include "handle_scope-inl.h"
31#include "mirror/class_loader.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070032#include "oat_file_assistant.h"
Mathieu Chartierfbc31082016-01-24 11:59:56 -080033#include "scoped_thread_state_change.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070034#include "thread-inl.h"
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -080035#include "thread_list.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070036
37namespace art {
38
39// For b/21333911.
Mathieu Chartier80b37b72015-10-12 18:13:39 -070040// Only enabled for debug builds to prevent bit rot. There are too many performance regressions for
41// normal builds.
42static constexpr bool kDuplicateClassesCheck = kIsDebugBuild;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070043
Mathieu Chartierfbc31082016-01-24 11:59:56 -080044// If true, then we attempt to load the application image if it exists.
45static constexpr bool kEnableAppImage = true;
46
Andreas Gampe29d38e72016-03-23 15:31:51 +000047CompilerFilter::Filter OatFileManager::filter_ = CompilerFilter::Filter::kSpeed;
48
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070049const OatFile* OatFileManager::RegisterOatFile(std::unique_ptr<const OatFile> oat_file) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070050 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070051 DCHECK(oat_file != nullptr);
52 if (kIsDebugBuild) {
Mathieu Chartiere58991b2015-10-13 07:59:34 -070053 CHECK(oat_files_.find(oat_file) == oat_files_.end());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070054 for (const std::unique_ptr<const OatFile>& existing : oat_files_) {
55 CHECK_NE(oat_file.get(), existing.get()) << oat_file->GetLocation();
56 // Check that we don't have an oat file with the same address. Copies of the same oat file
57 // should be loaded at different addresses.
58 CHECK_NE(oat_file->Begin(), existing->Begin()) << "Oat file already mapped at that location";
59 }
60 }
61 have_non_pic_oat_file_ = have_non_pic_oat_file_ || !oat_file->IsPic();
Mathieu Chartiere58991b2015-10-13 07:59:34 -070062 const OatFile* ret = oat_file.get();
63 oat_files_.insert(std::move(oat_file));
64 return ret;
65}
66
67void OatFileManager::UnRegisterAndDeleteOatFile(const OatFile* oat_file) {
68 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
69 DCHECK(oat_file != nullptr);
70 std::unique_ptr<const OatFile> compare(oat_file);
71 auto it = oat_files_.find(compare);
72 CHECK(it != oat_files_.end());
73 oat_files_.erase(it);
74 compare.release();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070075}
76
77const OatFile* OatFileManager::FindOpenedOatFileFromOatLocation(const std::string& oat_location)
78 const {
79 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070080 return FindOpenedOatFileFromOatLocationLocked(oat_location);
81}
82
83const OatFile* OatFileManager::FindOpenedOatFileFromOatLocationLocked(
84 const std::string& oat_location) const {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070085 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
86 if (oat_file->GetLocation() == oat_location) {
87 return oat_file.get();
88 }
89 }
90 return nullptr;
91}
92
Jeff Haodcdc85b2015-12-04 14:06:18 -080093std::vector<const OatFile*> OatFileManager::GetBootOatFiles() const {
94 std::vector<const OatFile*> oat_files;
95 std::vector<gc::space::ImageSpace*> image_spaces =
96 Runtime::Current()->GetHeap()->GetBootImageSpaces();
97 for (gc::space::ImageSpace* image_space : image_spaces) {
98 oat_files.push_back(image_space->GetOatFile());
99 }
100 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700101}
102
103const OatFile* OatFileManager::GetPrimaryOatFile() const {
104 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -0800105 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
106 if (!boot_oat_files.empty()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700107 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
Jeff Haodcdc85b2015-12-04 14:06:18 -0800108 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), oat_file.get()) ==
109 boot_oat_files.end()) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700110 return oat_file.get();
111 }
112 }
113 }
114 return nullptr;
115}
116
117OatFileManager::~OatFileManager() {
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700118 // Explicitly clear oat_files_ since the OatFile destructor calls back into OatFileManager for
119 // UnRegisterOatFileLocation.
120 oat_files_.clear();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700121}
122
Jeff Haodcdc85b2015-12-04 14:06:18 -0800123std::vector<const OatFile*> OatFileManager::RegisterImageOatFiles(
124 std::vector<gc::space::ImageSpace*> spaces) {
125 std::vector<const OatFile*> oat_files;
126 for (gc::space::ImageSpace* space : spaces) {
127 oat_files.push_back(RegisterOatFile(space->ReleaseOatFile()));
128 }
129 return oat_files;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700130}
131
132class DexFileAndClassPair : ValueObject {
133 public:
134 DexFileAndClassPair(const DexFile* dex_file, size_t current_class_index, bool from_loaded_oat)
135 : cached_descriptor_(GetClassDescriptor(dex_file, current_class_index)),
136 dex_file_(dex_file),
137 current_class_index_(current_class_index),
138 from_loaded_oat_(from_loaded_oat) {}
139
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700140 DexFileAndClassPair(const DexFileAndClassPair& rhs) = default;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700141
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700142 DexFileAndClassPair& operator=(const DexFileAndClassPair& rhs) = default;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700143
144 const char* GetCachedDescriptor() const {
145 return cached_descriptor_;
146 }
147
148 bool operator<(const DexFileAndClassPair& rhs) const {
149 const int cmp = strcmp(cached_descriptor_, rhs.cached_descriptor_);
150 if (cmp != 0) {
151 // Note that the order must be reversed. We want to iterate over the classes in dex files.
152 // They are sorted lexicographically. Thus, the priority-queue must be a min-queue.
153 return cmp > 0;
154 }
155 return dex_file_ < rhs.dex_file_;
156 }
157
158 bool DexFileHasMoreClasses() const {
159 return current_class_index_ + 1 < dex_file_->NumClassDefs();
160 }
161
162 void Next() {
163 ++current_class_index_;
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700164 cached_descriptor_ = GetClassDescriptor(dex_file_.get(), current_class_index_);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700165 }
166
167 size_t GetCurrentClassIndex() const {
168 return current_class_index_;
169 }
170
171 bool FromLoadedOat() const {
172 return from_loaded_oat_;
173 }
174
175 const DexFile* GetDexFile() const {
176 return dex_file_.get();
177 }
178
179 private:
180 static const char* GetClassDescriptor(const DexFile* dex_file, size_t index) {
181 DCHECK(IsUint<16>(index));
182 const DexFile::ClassDef& class_def = dex_file->GetClassDef(static_cast<uint16_t>(index));
183 return dex_file->StringByTypeIdx(class_def.class_idx_);
184 }
185
186 const char* cached_descriptor_;
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700187 std::shared_ptr<const DexFile> dex_file_;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700188 size_t current_class_index_;
189 bool from_loaded_oat_; // We only need to compare mismatches between what we load now
190 // and what was loaded before. Any old duplicates must have been
191 // OK, and any new "internal" duplicates are as well (they must
192 // be from multidex, which resolves correctly).
193};
194
195static void AddDexFilesFromOat(const OatFile* oat_file,
196 bool already_loaded,
197 /*out*/std::priority_queue<DexFileAndClassPair>* heap) {
198 for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
199 std::string error;
200 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error);
201 if (dex_file == nullptr) {
202 LOG(WARNING) << "Could not create dex file from oat file: " << error;
203 } else if (dex_file->NumClassDefs() > 0U) {
204 heap->emplace(dex_file.release(), /*current_class_index*/0U, already_loaded);
205 }
206 }
207}
208
209static void AddNext(/*inout*/DexFileAndClassPair* original,
210 /*inout*/std::priority_queue<DexFileAndClassPair>* heap) {
211 if (original->DexFileHasMoreClasses()) {
212 original->Next();
213 heap->push(std::move(*original));
214 }
215}
216
217// Check for class-def collisions in dex files.
218//
219// This works by maintaining a heap with one class from each dex file, sorted by the class
220// descriptor. Then a dex-file/class pair is continually removed from the heap and compared
221// against the following top element. If the descriptor is the same, it is now checked whether
222// the two elements agree on whether their dex file was from an already-loaded oat-file or the
223// new oat file. Any disagreement indicates a collision.
224bool OatFileManager::HasCollisions(const OatFile* oat_file,
225 std::string* error_msg /*out*/) const {
226 DCHECK(oat_file != nullptr);
227 DCHECK(error_msg != nullptr);
228 if (!kDuplicateClassesCheck) {
229 return false;
230 }
231
232 // Dex files are registered late - once a class is actually being loaded. We have to compare
233 // against the open oat files. Take the oat_file_manager_lock_ that protects oat_files_ accesses.
234 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
235
236 std::priority_queue<DexFileAndClassPair> queue;
237
238 // Add dex files from already loaded oat files, but skip boot.
Jeff Haodcdc85b2015-12-04 14:06:18 -0800239 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700240 // The same OatFile can be loaded multiple times at different addresses. In this case, we don't
241 // need to check both against each other since they would have resolved the same way at compile
242 // time.
243 std::unordered_set<std::string> unique_locations;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700244 for (const std::unique_ptr<const OatFile>& loaded_oat_file : oat_files_) {
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700245 DCHECK_NE(loaded_oat_file.get(), oat_file);
246 const std::string& location = loaded_oat_file->GetLocation();
Jeff Haodcdc85b2015-12-04 14:06:18 -0800247 if (std::find(boot_oat_files.begin(), boot_oat_files.end(), loaded_oat_file.get()) ==
248 boot_oat_files.end() && location != oat_file->GetLocation() &&
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700249 unique_locations.find(location) == unique_locations.end()) {
250 unique_locations.insert(location);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700251 AddDexFilesFromOat(loaded_oat_file.get(), /*already_loaded*/true, &queue);
252 }
253 }
254
255 if (queue.empty()) {
256 // No other oat files, return early.
257 return false;
258 }
259
260 // Add dex files from the oat file to check.
261 AddDexFilesFromOat(oat_file, /*already_loaded*/false, &queue);
262
263 // Now drain the queue.
264 while (!queue.empty()) {
265 // Modifying the top element is only safe if we pop right after.
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700266 DexFileAndClassPair compare_pop(queue.top());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700267 queue.pop();
268
269 // Compare against the following elements.
270 while (!queue.empty()) {
Mathieu Chartier80b37b72015-10-12 18:13:39 -0700271 DexFileAndClassPair top(queue.top());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700272
273 if (strcmp(compare_pop.GetCachedDescriptor(), top.GetCachedDescriptor()) == 0) {
274 // Same descriptor. Check whether it's crossing old-oat-files to new-oat-files.
275 if (compare_pop.FromLoadedOat() != top.FromLoadedOat()) {
276 *error_msg =
277 StringPrintf("Found duplicated class when checking oat files: '%s' in %s and %s",
278 compare_pop.GetCachedDescriptor(),
279 compare_pop.GetDexFile()->GetLocation().c_str(),
280 top.GetDexFile()->GetLocation().c_str());
281 return true;
282 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700283 queue.pop();
284 AddNext(&top, &queue);
285 } else {
286 // Something else. Done here.
287 break;
288 }
289 }
290 AddNext(&compare_pop, &queue);
291 }
292
293 return false;
294}
295
296std::vector<std::unique_ptr<const DexFile>> OatFileManager::OpenDexFilesFromOat(
297 const char* dex_location,
298 const char* oat_location,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800299 jobject class_loader,
300 jobjectArray dex_elements,
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700301 const OatFile** out_oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700302 std::vector<std::string>* error_msgs) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800303 ScopedTrace trace(__FUNCTION__);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700304 CHECK(dex_location != nullptr);
305 CHECK(error_msgs != nullptr);
306
307 // Verify we aren't holding the mutator lock, which could starve GC if we
308 // have to generate or relocate an oat file.
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800309 Thread* const self = Thread::Current();
310 Locks::mutator_lock_->AssertNotHeld(self);
311 Runtime* const runtime = Runtime::Current();
Calin Juravleb077e152016-02-18 18:47:37 +0000312
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700313 OatFileAssistant oat_file_assistant(dex_location,
314 oat_location,
315 kRuntimeISA,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000316 /*profile_changed*/false,
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800317 !runtime->IsAotCompiler());
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700318
319 // Lock the target oat location to avoid races generating and loading the
320 // oat file.
321 std::string error_msg;
322 if (!oat_file_assistant.Lock(/*out*/&error_msg)) {
323 // Don't worry too much if this fails. If it does fail, it's unlikely we
324 // can generate an oat file anyway.
325 VLOG(class_linker) << "OatFileAssistant::Lock: " << error_msg;
326 }
327
328 const OatFile* source_oat_file = nullptr;
329
Nicolas Geoffraye722d292015-12-15 11:51:37 +0000330 // Update the oat file on disk if we can. This may fail, but that's okay.
331 // Best effort is all that matters here.
Richard Uhler1e860612016-03-30 12:17:55 -0700332 switch (oat_file_assistant.MakeUpToDate(filter_, /*out*/ &error_msg)) {
333 case OatFileAssistant::kUpdateFailed:
334 LOG(WARNING) << error_msg;
335 break;
336
337 case OatFileAssistant::kUpdateNotAttempted:
338 // Avoid spamming the logs if we decided not to attempt making the oat
339 // file up to date.
340 VLOG(oat) << error_msg;
341 break;
342
343 case OatFileAssistant::kUpdateSucceeded:
344 // Nothing to do.
345 break;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700346 }
347
348 // Get the oat file on disk.
349 std::unique_ptr<const OatFile> oat_file(oat_file_assistant.GetBestOatFile().release());
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800350
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700351 if (oat_file != nullptr) {
352 // Take the file only if it has no collisions, or we must take it because of preopting.
353 bool accept_oat_file = !HasCollisions(oat_file.get(), /*out*/ &error_msg);
354 if (!accept_oat_file) {
355 // Failed the collision check. Print warning.
356 if (Runtime::Current()->IsDexFileFallbackEnabled()) {
357 LOG(WARNING) << "Found duplicate classes, falling back to interpreter mode for "
358 << dex_location;
359 } else {
360 LOG(WARNING) << "Found duplicate classes, dex-file-fallback disabled, will be failing to "
361 " load classes for " << dex_location;
362 }
363 LOG(WARNING) << error_msg;
364
365 // However, if the app was part of /system and preopted, there is no original dex file
366 // available. In that case grudgingly accept the oat file.
367 if (!DexFile::MaybeDex(dex_location)) {
368 accept_oat_file = true;
369 LOG(WARNING) << "Dex location " << dex_location << " does not seem to include dex file. "
370 << "Allow oat file use. This is potentially dangerous.";
371 }
372 }
373
374 if (accept_oat_file) {
375 VLOG(class_linker) << "Registering " << oat_file->GetLocation();
376 source_oat_file = RegisterOatFile(std::move(oat_file));
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700377 *out_oat_file = source_oat_file;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700378 }
379 }
380
381 std::vector<std::unique_ptr<const DexFile>> dex_files;
382
383 // Load the dex files from the oat file.
384 if (source_oat_file != nullptr) {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800385 bool added_image_space = false;
386 if (source_oat_file->IsExecutable()) {
387 std::unique_ptr<gc::space::ImageSpace> image_space(
388 kEnableAppImage ? oat_file_assistant.OpenImageSpace(source_oat_file) : nullptr);
389 if (image_space != nullptr) {
390 ScopedObjectAccess soa(self);
391 StackHandleScope<1> hs(self);
392 Handle<mirror::ClassLoader> h_loader(
393 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(class_loader)));
394 // Can not load app image without class loader.
395 if (h_loader.Get() != nullptr) {
396 std::string temp_error_msg;
397 // Add image space has a race condition since other threads could be reading from the
398 // spaces array.
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800399 {
400 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800401 gc::ScopedGCCriticalSection gcs(self,
402 gc::kGcCauseAddRemoveAppImageSpace,
403 gc::kCollectorTypeAddRemoveAppImageSpace);
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800404 ScopedSuspendAll ssa("Add image space");
405 runtime->GetHeap()->AddSpace(image_space.get());
406 }
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800407 {
408 ScopedTrace trace2(StringPrintf("Adding image space for location %s", dex_location));
409 added_image_space = runtime->GetClassLinker()->AddImageSpace(image_space.get(),
410 h_loader,
411 dex_elements,
412 dex_location,
413 /*out*/&dex_files,
414 /*out*/&temp_error_msg);
415 }
Mathieu Chartiera6e81ed2016-02-25 13:52:10 -0800416 if (added_image_space) {
Mathieu Chartierbd064ea2016-02-11 16:27:18 -0800417 // Successfully added image space to heap, release the map so that it does not get
418 // freed.
419 image_space.release();
420 } else {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800421 LOG(INFO) << "Failed to add image file " << temp_error_msg;
422 dex_files.clear();
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800423 {
424 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier61d2b2d2016-02-04 13:31:46 -0800425 gc::ScopedGCCriticalSection gcs(self,
426 gc::kGcCauseAddRemoveAppImageSpace,
427 gc::kCollectorTypeAddRemoveAppImageSpace);
Mathieu Chartiera9d82fe2016-01-25 20:06:11 -0800428 ScopedSuspendAll ssa("Remove image space");
429 runtime->GetHeap()->RemoveSpace(image_space.get());
430 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800431 // Non-fatal, don't update error_msg.
432 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800433 }
434 }
435 }
436 if (!added_image_space) {
437 DCHECK(dex_files.empty());
438 dex_files = oat_file_assistant.LoadDexFiles(*source_oat_file, dex_location);
439 }
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700440 if (dex_files.empty()) {
441 error_msgs->push_back("Failed to open dex files from " + source_oat_file->GetLocation());
442 }
443 }
444
445 // Fall back to running out of the original dex file if we couldn't load any
446 // dex_files from the oat file.
447 if (dex_files.empty()) {
448 if (oat_file_assistant.HasOriginalDexFiles()) {
449 if (Runtime::Current()->IsDexFileFallbackEnabled()) {
450 if (!DexFile::Open(dex_location, dex_location, /*out*/ &error_msg, &dex_files)) {
451 LOG(WARNING) << error_msg;
452 error_msgs->push_back("Failed to open dex files from " + std::string(dex_location));
453 }
454 } else {
455 error_msgs->push_back("Fallback mode disabled, skipping dex files.");
456 }
457 } else {
458 error_msgs->push_back("No original dex files found for dex location "
459 + std::string(dex_location));
460 }
461 }
Calin Juravlec90bc922016-02-24 10:13:09 +0000462
463 // TODO(calin): Consider optimizing this knowing that is useless to record the
464 // use of fully compiled apks.
465 Runtime::Current()->NotifyDexLoaded(dex_location);
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700466 return dex_files;
467}
468
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700469bool OatFileManager::RegisterOatFileLocation(const std::string& oat_location) {
470 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_count_lock_);
471 auto it = oat_file_count_.find(oat_location);
472 if (it != oat_file_count_.end()) {
473 ++it->second;
474 return false;
475 }
476 oat_file_count_.insert(std::pair<std::string, size_t>(oat_location, 1u));
477 return true;
478}
479
480void OatFileManager::UnRegisterOatFileLocation(const std::string& oat_location) {
481 WriterMutexLock mu(Thread::Current(), *Locks::oat_file_count_lock_);
482 auto it = oat_file_count_.find(oat_location);
483 if (it != oat_file_count_.end()) {
484 --it->second;
485 if (it->second == 0) {
486 oat_file_count_.erase(it);
487 }
488 }
489}
490
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000491void OatFileManager::DumpForSigQuit(std::ostream& os) {
492 ReaderMutexLock mu(Thread::Current(), *Locks::oat_file_manager_lock_);
493 std::vector<const OatFile*> boot_oat_files = GetBootOatFiles();
494 for (const std::unique_ptr<const OatFile>& oat_file : oat_files_) {
495 if (ContainsElement(boot_oat_files, oat_file.get())) {
496 continue;
497 }
Andreas Gampe29d38e72016-03-23 15:31:51 +0000498 os << oat_file->GetLocation() << ": " << oat_file->GetCompilerFilter() << "\n";
Nicolas Geoffray04680f32016-03-17 11:56:54 +0000499 }
500}
501
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -0700502} // namespace art