
FART正餐前甜點:ART下幾個通用簡單高效的dump內存中dex方法

const?uint8_t* class_data,
Handle<mirror::Class> klass,
const?OatFile::OatClass* oat_class) {
{
// Note:?We cannot have thread suspension until the field and method arrays are setup or else
// Class::VisitFieldRoots may miss some fields or methods.
ScopedAssertNoThreadSuspension nts(self, __FUNCTION__);
// Load static fields.
//遍歷dex中的相關field
ClassDataItemIterator it(dex_file, class_data);
const?size_t num_sfields = it.NumStaticFields();
ArtField* sfields = num_sfields != 0?? AllocArtFieldArray(self, num_sfields) : nullptr;
for?(size_t i = 0; it.HasNextStaticField(); i++, it.Next()) {
CHECK_LT(i, num_sfields);
LoadField(it, klass, &sfields[i]);
}
klass->SetSFields(sfields);
klass->SetNumStaticFields(num_sfields);
DCHECK_EQ(klass->NumStaticFields(), num_sfields);
// Load instance fields.
const?size_t num_ifields = it.NumInstanceFields();
ArtField* ifields = num_ifields != 0?? AllocArtFieldArray(self, num_ifields) : nullptr;
for?(size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) {
CHECK_LT(i, num_ifields);
LoadField(it, klass, &ifields[i]);
}
klass->SetIFields(ifields);
klass->SetNumInstanceFields(num_ifields);
DCHECK_EQ(klass->NumInstanceFields(), num_ifields);
// Load methods.
//遍歷dex中的相關Method并初始化
if?(it.NumDirectMethods() != 0) {
klass->SetDirectMethodsPtr(AllocArtMethodArray(self, it.NumDirectMethods()));
}
klass->SetNumDirectMethods(it.NumDirectMethods());
if?(it.NumVirtualMethods() != 0) {
klass->SetVirtualMethodsPtr(AllocArtMethodArray(self, it.NumVirtualMethods()));
}
klass->SetNumVirtualMethods(it.NumVirtualMethods());
size_t class_def_method_index = 0;
uint32_t last_dex_method_index = DexFile::kDexNoIndex;
size_t last_class_def_method_index = 0;
//首先遍歷初始化DirectMethod
for?(size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) {
ArtMethod* method = klass->GetDirectMethodUnchecked(i, image_pointer_size_);
LoadMethod(self, dex_file, it, klass, method);
LinkCode(method, oat_class, class_def_method_index);
uint32_t it_method_index = it.GetMemberIndex();
if?(last_dex_method_index == it_method_index) {
// duplicate case
method->SetMethodIndex(last_class_def_method_index);
} else?{
method->SetMethodIndex(class_def_method_index);
last_dex_method_index = it_method_index;
last_class_def_method_index = class_def_method_index;
}
class_def_method_index++;
}
//然后遍歷初始化VirtualMethod
for?(size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) {
ArtMethod* method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_);
LoadMethod(self, dex_file, it, klass, method);
DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i);
LinkCode(method, oat_class, class_def_method_index);
class_def_method_index++;
}
DCHECK(!it.HasNext());
}
self->AllowThreadSuspension();
}
Handle<mirror::Class> klass, ArtMethod* dst) {
uint32_t dex_method_idx = it.GetMemberIndex();
const?DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
const?char* method_name = dex_file.StringDataByIdx(method_id.name_idx_);
ScopedAssertNoThreadSuspension ants(self, “LoadMethod”);
//初始化相關變量
dst->SetDexMethodIndex(dex_method_idx);
dst->SetDeclaringClass(klass.Get());
//初始化CodeItem指針
dst->SetCodeItemOffset(it.GetMethodCodeItemOffset());
dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods());
dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes());
uint32_t access_flags = it.GetMethodAccessFlags();
if?(UNLIKELY(strcmp(“finalize”, method_name) == 0)) {
// Set finalizable flag on declaring class.
if?(strcmp(“V”, dex_file.GetShorty(method_id.proto_idx_)) == 0) {
// Void return type.
if?(klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged.
klass->SetFinalizable();
} else?{
std::string temp;
const?char* klass_descriptor = klass->GetDescriptor(&temp);
// The Enum class declares a “final” finalize() method to prevent subclasses from
// introducing a finalizer. We don’t want to set the finalizable flag for Enum or its
// subclasses, so we exclude it here.
// We also want to avoid setting the flag on Object, where we know that finalize() is
// empty.
if?(strcmp(klass_descriptor, “Ljava/lang/Object;”) != 0?&&
strcmp(klass_descriptor, “Ljava/lang/Enum;”) != 0) {
klass->SetFinalizable();
}
}
}
} else?if?(method_name[0] == ‘<‘) {
// Fix broken access flags for initializers. Bug 11157540.
bool is_init = (strcmp(“<init>”, method_name) == 0);
bool is_clinit = !is_init && (strcmp(“<clinit>”, method_name) == 0);
if?(UNLIKELY(!is_init && !is_clinit)) {
LOG(WARNING) << “Unexpected ‘<‘ at start of method name “?<< method_name;
} else?{
if?(UNLIKELY((access_flags & kAccConstructor) == 0)) {
LOG(WARNING) << method_name << ” didn’t have expected constructor access flag in class “
<< PrettyDescriptor(klass.Get()) << ” in dex file “?<< dex_file.GetLocation();
access_flags |= kAccConstructor;
}
}
}
dst->SetAccessFlags(access_flags);
}
??dst->SetDeclaringClass(klass.Get());
??dst->SetCodeItemOffset(it.GetMethodCodeItemOffset());
??dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods());
??dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes());
uint32_t class_def_method_index) {
Runtime* const?runtime = Runtime::Current();
if?(runtime->IsAotCompiler()) {
// The following code only applies to a non-compiler runtime.
return;
}
// Method shouldn’t have already been linked.
DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr);
if?(oat_class != nullptr) {
// Every kind of method should at least get an invoke stub from the oat_method.
// non-abstract methods also get their code pointers.
const?OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index);
oat_method.LinkMethod(method);
}
// Install entry point from interpreter.
bool enter_interpreter = NeedsInterpreter(method, method->GetEntryPointFromQuickCompiledCode());
if?(enter_interpreter && !method->IsNative()) {
method->SetEntryPointFromInterpreter(artInterpreterToInterpreterBridge);
} else?{
method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge);
}
if?(method->IsAbstract()) {
method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
return;
}
if?(method->IsStatic() && !method->IsConstructor()) {
// For static methods excluding the class initializer, install the trampoline.
// It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines
// after initializing class (see ClassLinker::InitializeClass method).
method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub());
} else?if?(enter_interpreter) {
if?(!method->IsNative()) {
// Set entry point from compiled code if there’s no code or in interpreter only mode.
method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge());
} else?{
method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub());
}
}
if?(method->IsNative()) {
// Unregistering restores the dlsym lookup stub.
method->UnregisterNative();
if?(enter_interpreter) {
// We have a native method here without code. Then it should have either the generic JNI
// trampoline as entrypoint (non-static), or the resolution trampoline (static).
// TODO:?this doesn’t handle all the cases where trampolines may be installed.
const?void* entry_point = method->GetEntryPointFromQuickCompiledCode();
DCHECK(IsQuickGenericJniStub(entry_point) || IsQuickResolutionStub(entry_point));
}
}
}<span style=“color:rgb(0, 0, 0); font-family:none; font-size:15px;”>
</span>