blob: 60487670accf7e5441b9b27f9d78cf0aa54b077c [file] [log] [blame]
Vladimir Marko606adb32018-04-05 14:49:24 +01001/*
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
34namespace art {
35
36std::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
59std::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()) {
Vladimir Marko957e7082018-04-11 17:25:42 +010076 ClassTable* table = Runtime::Current()->GetClassLinker()->ClassTableForClassLoader(loader);
77 oss << loader_separator << loader->GetClass()->PrettyDescriptor()
78 << "/" << static_cast<const void*>(table);
Vladimir Marko606adb32018-04-05 14:49:24 +010079 loader_separator = ";";
80 // If we didn't find the class yet, try to find it in the current class loader.
81 if (!found_class) {
Vladimir Marko606adb32018-04-05 14:49:24 +010082 ObjPtr<mirror::Class> klass =
83 (table != nullptr) ? table->Lookup(class_descriptor, hash) : nullptr;
84 if (klass != nullptr) {
85 found_class = true;
86 oss << "[hit:" << DescribeSpace(klass) << "]";
87 }
88 }
89
90 // For PathClassLoader, DexClassLoader or DelegateLastClassLoader
91 // also dump the dex file locations.
92 if (loader->GetClass() == path_class_loader ||
93 loader->GetClass() == dex_class_loader ||
94 loader->GetClass() == delegate_last_class_loader) {
95 oss << "(";
96 ScopedObjectAccessUnchecked soa(Thread::Current());
97 StackHandleScope<1> hs(soa.Self());
98 Handle<mirror::ClassLoader> handle(hs.NewHandle(loader));
99 const char* path_separator = "";
100 VisitClassLoaderDexFiles(soa,
101 handle,
102 [&](const DexFile* dex_file) {
Vladimir Marko957e7082018-04-11 17:25:42 +0100103 oss << path_separator << dex_file->GetLocation()
104 << "/" << static_cast<const void*>(dex_file);
Vladimir Marko606adb32018-04-05 14:49:24 +0100105 path_separator = ":";
106 return true; // Continue with the next DexFile.
107 });
108 oss << ")";
109 }
110 }
111
112 return oss.str();
113}
114
Vladimir Markof5c537e2018-04-09 18:33:55 +0100115void DumpB77342775DebugData(ObjPtr<mirror::Class> target_class, ObjPtr<mirror::Class> src_class) {
116 std::string target_descriptor_storage;
117 const char* target_descriptor = target_class->GetDescriptor(&target_descriptor_storage);
118 const char kCheckedPrefix[] = "Lorg/apache/http/";
119 // Avoid spam for other packages. (That spam would break some ART run-tests for example.)
120 if (strncmp(target_descriptor, kCheckedPrefix, sizeof(kCheckedPrefix) - 1) != 0) {
121 return;
122 }
123 auto matcher = [target_descriptor, target_class](ObjPtr<mirror::Class> klass)
124 REQUIRES_SHARED(Locks::mutator_lock_) {
125 if (klass->DescriptorEquals(target_descriptor)) {
126 LOG(ERROR) << " descriptor match in "
127 << DescribeLoaders(klass->GetClassLoader(), target_descriptor)
128 << " match? " << std::boolalpha << (klass == target_class);
129 }
130 };
131
132 std::string source_descriptor_storage;
133 const char* source_descriptor = src_class->GetDescriptor(&source_descriptor_storage);
134
135 if (target_class->IsInterface()) {
136 ObjPtr<mirror::IfTable> iftable = src_class->GetIfTable();
137 CHECK(iftable != nullptr);
138 size_t ifcount = iftable->Count();
139 LOG(ERROR) << "Maybe bug 77342775, looking for " << target_descriptor
Vladimir Marko957e7082018-04-11 17:25:42 +0100140 << " with loader " << DescribeLoaders(target_class->GetClassLoader(), target_descriptor)
141 << " in interface table for " << source_descriptor << " ifcount=" << ifcount
142 << " with loader " << DescribeLoaders(src_class->GetClassLoader(), source_descriptor);
Vladimir Markof5c537e2018-04-09 18:33:55 +0100143 for (size_t i = 0; i != ifcount; ++i) {
144 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
145 CHECK(iface != nullptr);
146 LOG(ERROR) << " iface #" << i << ": " << iface->PrettyDescriptor();
147 matcher(iface);
148 }
149 } else {
150 LOG(ERROR) << "Maybe bug 77342775, looking for " << target_descriptor
Vladimir Marko957e7082018-04-11 17:25:42 +0100151 << " with loader " << DescribeLoaders(target_class->GetClassLoader(), target_descriptor)
152 << " in superclass chain for " << source_descriptor
153 << " with loader " << DescribeLoaders(src_class->GetClassLoader(), source_descriptor);
Vladimir Markof5c537e2018-04-09 18:33:55 +0100154 for (ObjPtr<mirror::Class> klass = src_class;
155 klass != nullptr;
156 klass = klass->GetSuperClass()) {
157 LOG(ERROR) << " - " << klass->PrettyDescriptor();
158 matcher(klass);
159 }
160 }
161}
162
Vladimir Marko606adb32018-04-05 14:49:24 +0100163} // namespace art