Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 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 <sstream> |
| 18 | |
| 19 | #include "debug_print.h" |
| 20 | |
| 21 | #include "class_linker.h" |
| 22 | #include "class_table.h" |
| 23 | #include "class_loader_utils.h" |
| 24 | #include "dex/utf.h" |
| 25 | #include "gc/heap.h" |
| 26 | #include "gc/space/space-inl.h" |
| 27 | #include "mirror/class.h" |
| 28 | #include "mirror/class_loader.h" |
| 29 | #include "runtime.h" |
| 30 | #include "scoped_thread_state_change-inl.h" |
| 31 | #include "thread-current-inl.h" |
| 32 | #include "well_known_classes.h" |
| 33 | |
| 34 | namespace art { |
| 35 | |
| 36 | std::string DescribeSpace(ObjPtr<mirror::Class> klass) { |
| 37 | std::ostringstream oss; |
| 38 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 39 | gc::space::ContinuousSpace* cs = |
| 40 | heap->FindContinuousSpaceFromObject(klass.Ptr(), /* fail_ok */ true); |
| 41 | if (cs != nullptr) { |
| 42 | if (cs->IsImageSpace()) { |
| 43 | oss << "image;" << cs->GetName() << ";" << cs->AsImageSpace()->GetImageFilename(); |
| 44 | } else { |
| 45 | oss << "continuous;" << cs->GetName(); |
| 46 | } |
| 47 | } else { |
| 48 | gc::space::DiscontinuousSpace* ds = |
| 49 | heap->FindDiscontinuousSpaceFromObject(klass, /* fail_ok */ true); |
| 50 | if (ds != nullptr) { |
| 51 | oss << "discontinuous;" << ds->GetName(); |
| 52 | } else { |
| 53 | oss << "invalid"; |
| 54 | } |
| 55 | } |
| 56 | return oss.str(); |
| 57 | } |
| 58 | |
| 59 | std::string DescribeLoaders(ObjPtr<mirror::ClassLoader> loader, const char* class_descriptor) { |
| 60 | std::ostringstream oss; |
| 61 | uint32_t hash = ComputeModifiedUtf8Hash(class_descriptor); |
| 62 | ObjPtr<mirror::Class> path_class_loader = |
| 63 | WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_PathClassLoader); |
| 64 | ObjPtr<mirror::Class> dex_class_loader = |
| 65 | WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_DexClassLoader); |
| 66 | ObjPtr<mirror::Class> delegate_last_class_loader = |
| 67 | WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_DelegateLastClassLoader); |
| 68 | |
| 69 | // Print the class loader chain. |
| 70 | bool found_class = false; |
| 71 | const char* loader_separator = ""; |
| 72 | if (loader == nullptr) { |
| 73 | oss << "BootClassLoader"; // This would be unexpected. |
| 74 | } |
| 75 | for (; loader != nullptr; loader = loader->GetParent()) { |
| 76 | oss << loader_separator << loader->GetClass()->PrettyDescriptor(); |
| 77 | loader_separator = ";"; |
| 78 | // If we didn't find the class yet, try to find it in the current class loader. |
| 79 | if (!found_class) { |
| 80 | ClassTable* table = Runtime::Current()->GetClassLinker()->ClassTableForClassLoader(loader); |
| 81 | ObjPtr<mirror::Class> klass = |
| 82 | (table != nullptr) ? table->Lookup(class_descriptor, hash) : nullptr; |
| 83 | if (klass != nullptr) { |
| 84 | found_class = true; |
| 85 | oss << "[hit:" << DescribeSpace(klass) << "]"; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // For PathClassLoader, DexClassLoader or DelegateLastClassLoader |
| 90 | // also dump the dex file locations. |
| 91 | if (loader->GetClass() == path_class_loader || |
| 92 | loader->GetClass() == dex_class_loader || |
| 93 | loader->GetClass() == delegate_last_class_loader) { |
| 94 | oss << "("; |
| 95 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 96 | StackHandleScope<1> hs(soa.Self()); |
| 97 | Handle<mirror::ClassLoader> handle(hs.NewHandle(loader)); |
| 98 | const char* path_separator = ""; |
| 99 | VisitClassLoaderDexFiles(soa, |
| 100 | handle, |
| 101 | [&](const DexFile* dex_file) { |
| 102 | oss << path_separator << dex_file->GetLocation(); |
| 103 | path_separator = ":"; |
| 104 | return true; // Continue with the next DexFile. |
| 105 | }); |
| 106 | oss << ")"; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return oss.str(); |
| 111 | } |
| 112 | |
| 113 | } // namespace art |