我已经成功回答我的问题了。
据我从源代码中可以理解,java 调用有 3 个可能的入口点:
- ArtMethod::调用 (art/runtime/mirror/art_method.cc)
- 执行(art/runtime/interpreter/interpreter.cc)
- DoCall(艺术/运行时/解释器/interpreter_common.cc)
ArtMethod::Invoke 似乎用于反射并使用指向 OAT 代码部分的指针直接调用该方法。 (同样,没有文档,它可能不准确)。
一般执行最终都会调用DoCall。
ART 的一些优化使得 Java 调用的研究变得困难,例如方法内联和直接偏移地址调用。
第一步是禁用这些优化:
在 device/brand-name/model/device.mk 中(在我的例子中,nexus 5 的 device/lge/hammerhead/device.mk ):
将“仅解释”选项添加到 dex2oat。使用此选项,ART 仅编译引导类路径,因此应用程序不会在 OAT 中编译。
PRODUCT_PROPERTY_OVERRIDES := \
dalvik.vm.dex2oat-filter=interpret-only
第二步是在 art/compiler/dex/frontend.cc 中禁用内联:
取消注释“kSuppressMethodInlined”。
/* Default optimizer/debug setting for the compiler. */
static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
(1 << kLoadStoreElimination) |
// (1 << kLoadHoisting) |
// (1 << kSuppressLoads) |
// (1 << kNullCheckElimination) |
// (1 << kClassInitCheckElimination) |
(1 << kGlobalValueNumbering) |
// (1 << kPromoteRegs) |
// (1 << kTrackLiveTemps) |
// (1 << kSafeOptimizations) |
// (1 << kBBOpt) |
// (1 << kMatch) |
// (1 << kPromoteCompilerTemps) |
// (1 << kSuppressExceptionEdges) |
(1 << kSuppressMethodInlining) |
0;
最后一步是在 art/compiler/driver/compiler_driver.cc 中禁用直接代码偏移调用:
-bool use_dex_cache = GetCompilerOptions().GetCompilePic();
+bool use_dex_cache = true;
通过这些更改,所有不同的调用都将落入 DoCall 函数中,我们最终可以在其中添加目标日志记录例程。
在 art/runtime/interpreter/interpreter_common.h 中,在包含的开头添加:
#ifdef HAVE_ANDROID_OS
#include "cutils/properties.h"
#endif
在 art/runtime/interpreter/interpreter_common.cc 中,在 DoCall 函数的开头添加:
#ifdef HAVE_ANDROID_OS
char targetAppVar[92];
property_get("target.app.pid", targetAppVar, "0");
int targetAppPID = atoi(targetAppVar);
if(targetAppPID != 0 && targetAppPID == getpid())
LOG(INFO) << "DoCall - " << PrettyMethod(method, true);
#endif
为了定位应用程序,我使用一个设置目标 pid 的属性。
为此,我们需要 lib system/core/libcutils,并且该库仅在为真实手机编译 AOSP 时可用(不会干扰当前的 makefile)。
因此该解决方案不适用于模拟器。 (仅猜测,从未尝试过编辑:已确认,“cutils/properties.h”无法添加到模拟器的构建中)。
编译并刷新打补丁的AOSP后,启动一个应用程序,ps | grep 用于查找 PID 并在 root 中设置属性:
shell@android:/ # ps | grep contacts
u0_a2 4278 129 1234668 47356 ffffffff 401e8318 S com.android.contacts
shell@android:/ # setprop target.app.pid 4278
shell@android:/ # logcat
[...]
I/art ( 4278): DoCall - int android.view.View.getId()
I/art ( 4278): DoCall - void com.android.contacts.activities.PeopleActivity$ContactsUnavailableFragmentListener.onCreateNewContactAction()
I/art ( 4278): DoCall - void android.content.Intent.<init>(java.lang.String, android.net.Uri)
I/art ( 4278): DoCall - void android.app.Activity.startActivity(android.content.Intent)
I/ActivityManager( 498): START u0 {act=android.intent.action.INSERT dat=content://com.android.contacts/contacts cmp=com.android.contacts/.activities.ContactEditorActivity} from uid 10002 on display 0
V/WindowManager( 498): addAppToken: AppWindowToken{3a82282b token=Token{dc3f87a ActivityRecord{c0aaca5 u0 com.android.contacts/.activities.ContactEditorActivity t4}}} to stack=1 task=4 at 1
I/art ( 4278): DoCall - void android.app.Fragment.onPause()
I/art ( 4278): DoCall - void com.android.contacts.common.list.ContactEntryListFragment.removePendingDirectorySearchRequests()
I/art ( 4278): DoCall - void android.os.Handler.removeMessages(int)
I/art ( 4278): DoCall - void com.android.contacts.list.ProviderStatusWatcher.stop()
I/art ( 4278): DoCall - boolean com.android.contacts.list.ProviderStatusWatcher.isStarted()
I/art ( 4278): DoCall - void android.os.Handler.removeCallbacks(java.lang.Runnable)
I/art ( 4278): DoCall - android.content.ContentResolver com.android.contacts.ContactsActivity.getContentResolver()
I/art ( 4278): DoCall - void android.content.ContentResolver.unregisterContentObserver(android.database.ContentObserver)
I/art ( 4278): DoCall - void android.app.Activity.onPause()
I/art ( 4278): DoCall - void android.view.ViewGroup.drawableStateChanged()
I/art ( 4278): DoCall - void com.android.contacts.ContactsActivity.<init>()
I/art ( 4278): DoCall - void com.android.contacts.common.activity.TransactionSafeActivity.<init>()
I/art ( 4278): DoCall - void android.app.Activity.<init>()
I/art ( 4278): DoCall - void com.android.contacts.util.DialogManager.<init>(android.app.Activity)
I/art ( 4278): DoCall - void java.lang.Object.<init>()
[...]
当它结束时:
shell@android:/ # setprop target.app.pid 0
Voilà !
从用户的角度来看,过载并不明显,但 logcat 很快就会被填满。
PS:文件路径和名称与Android 5版本(Lollipop)匹配,它们可能与高级版本不同。
PS':如果有人想要打印方法的参数,我建议它查看 art/runtime/utils.cc 中的 PrettyArguments 方法,并在代码中的某处找到一些实际的实现。