2018-9-18 9-19
为了观察binder的生命周期,添加了log
vi frameworks/base/core/java/android/os/Binder.java
vi frameworks/base/core/jni/android_util_Binder.cpp
vi frameworks/native/libs/binder/BpBinder.cpp
在Binder.java中添加
public Binder() {
init();
Log.d(TAG, "====binder, Binder() = " + this.getClass().getName() + ", this=" + this);
在android_util_Binder.cpp中打开log开关
55#define DEBUG_DEATH 0
56#if DEBUG_DEATH
57#define LOGDEATH ALOGD
58#else
59#define LOGDEATH ALOGV
60#endif
把#define DEBUG_DEATH 0修改为
#define DEBUG_DEATH 1
但是却导致系统不能开机,加了个log就导致不能开机?
这个log有毒?
使用user_debug版本抓到log
--------- beginning of crash
01-01 03:51:14.218 937 937 E AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: main
01-01 03:51:14.218 937 937 E AndroidRuntime: java.lang.RuntimeException: Failed to boot service com.android.server.wifi.scanner.WifiScanningService: onBootPhase threw an exception during phase 500
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.server.SystemServiceManager.startBootPhase(SystemServiceManager.java:174)
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.server.SystemServer.startOtherServices(SystemServer.java:1738)
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.server.SystemServer.run(SystemServer.java:452)
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.server.SystemServer.main(SystemServer.java:313)
01-01 03:51:14.218 937 937 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:845)
01-01 03:51:14.218 937 937 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.server.am.BroadcastRecord.toString(BroadcastRecord.java:303)
01-01 03:51:14.218 937 937 E AndroidRuntime: at java.lang.String.valueOf(String.java:2827)
01-01 03:51:14.218 937 937 E AndroidRuntime: at java.lang.StringBuilder.append(StringBuilder.java:132)
01-01 03:51:14.218 937 937 E AndroidRuntime: at android.os.Binder.<init>(Binder.java:363)
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.server.am.BroadcastRecord.<init>(BroadcastRecord.java:231)
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.server.am.ActivityManagerService.registerReceiver(ActivityManagerService.java:19471)
01-01 03:51:14.218 937 937 E AndroidRuntime: at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1417)
01-01 03:51:14.218 937 937 E AndroidRuntime: at android.app.ContextImpl.registerReceiver(ContextImpl.java:1378)
01-01 03:51:14.218 937 937 E AndroidRuntime: at android.app.ContextImpl.registerReceiver(ContextImpl.java:1366)
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.server.wifi.scanner.WifiScanningServiceImpl.startService(WifiScanningServiceImpl.java:310)
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.server.wifi.scanner.WifiScanningService.onBootPhase(WifiScanningService.java:53)
01-01 03:51:14.218 937 937 E AndroidRuntime: at com.android.server.SystemServiceManager.startBootPhase(SystemServiceManager.java:165)
01-01 03:51:14.218 937 937 E AndroidRuntime: ... 6 more
原来BroadcastRecord继承于Binder
final class BroadcastRecord extends Binder {
当构造BroadcastRecord对象的时候,就调用到了这里
public Binder() {
init();
Log.d(TAG, "====binder, Binder() = " + this.getClass().getName() + ", this=" + this);
但是BroadcastRecord重写了toString方法,这里的 + this会调用到toString方法
public String toString() {
return "BroadcastRecord{"
+ Integer.toHexString(System.identityHashCode(this))
+ " u" + userId + " " + intent.getAction() + "}";
}
这里的intent为null,就导致了空指针异常
为什么+ this会调用到toString方法呢?因为String的+操作会调用到String的valueOf方法
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
里面调用了对象的toString方法