boolShouldStayInSwitchInterpreter(ArtMethod* method)REQUIRES_SHARED(Locks::mutator_lock_) {
if (!Runtime::Current()->IsStarted()) {
// For unstarted runtimes, always use the interpreter entrypoint. This fixes the case where// we are doing cross compilation. Note that GetEntryPointFromQuickCompiledCode doesn't use// the image pointer size here and this may case an overflow if it is called from the// compiler. b/62402160returntrue;
}
if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) {
returnfalse;
}
if (Thread::Current()->IsForceInterpreter()) {
// Force the use of interpreter when it is required by the debugger.returntrue;
}
if (Thread::Current()->IsAsyncExceptionPending()) {
// Force use of interpreter to handle async-exceptionsreturntrue;
}
constvoid* code = method->GetEntryPointFromQuickCompiledCode();
return Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(code);
}
NO_STACK_PROTECTOR
voidArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
constchar* shorty) {
......
Runtime* runtime = Runtime::Current();
// Call the invoke stub, passing everything as arguments.// If the runtime is not yet started or it is required by the debugger, then perform the// Invocation by the interpreter, explicitly forcing interpretation over JIT to prevent// cycling around the various JIT/Interpreter methods that handle method invocation.////////////////////////////////////////////////////////////////////////////////////// 判断是否是有个有效的Java方法bool isValidJavaMethod = !IsNative() && !IsProxyMethod() && IsInvokable();
auto isInterpreter = UNLIKELY(!runtime->IsStarted() ||
(self->IsForceInterpreter() && isValidJavaMethod));
if (这个APP是否对部分方法添加了跟踪) {
// 这个APP对部分方法添加了跟踪/*
为什么先判断跟踪再判断是否要解释执行?
是因为对于在Java层注册的Native方法,也需要判断是否要被跟踪
*/// 检查这个方法是否需要被跟踪// 如果需要,那么将isInterpreter设置为true继续执行,它会执行到Execute函数内,其中会判断是否需要代理类并且做相关的逻辑
}
////////////////////////////////////////////////////////////////////////////////////if (isInterpreter) {
if (IsStatic()) {
art::interpreter::EnterInterpreterFromInvoke(
self, this, nullptr, args, result, /*stay_in_interpreter=*/true);
} else {
mirror::Object* receiver =
reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
art::interpreter::EnterInterpreterFromInvoke(
self, this, receiver, args + 1, result, /*stay_in_interpreter=*/true);
}
} else {
......
////////////////////////////////////////////////////////////////////////////////////bool isUseNewValue = false;
if (IsNative()) {
// 是Natvie方法,而不是Java方法执行本地编译好的机器码if (是否需要被跟踪,上面判断过,直接复用) {
JValue newValue;
bool useNewValue = false;
// 检查需要被跟踪的方法是否有代理类// 被跟踪方法有代理类的情况下,调用解析方法入参,将入参转换成jvalue数组,通过CallObjectMethodV调用到代理类,然后能获得返回对象。
处理跟踪(被跟踪方法相关信息, &newValue, useNewValue);
if (useNewValue) {
// 需要被拦截,设置新的值
*result = newValue;
isUseNewValue = true;
}
}
}
if (!isUseNewValue) {
// 使用新值的情况下,不调用原方法if (!IsStatic()) {
(*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
} else {
(*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
}
......
}
// 也可以在Native方法执行完成后调用代理方法做拦截
} else {
LOG(INFO) << "Not invoking '" << PrettyMethod() << "' code=null";
if (result != nullptr) {
result->SetJ(0);
}
}
}
// Pop transition.
self->PopManagedStackFragment(fragment);
}