1. 程式人生 > >binder新增log引起的不能開機問題分析

binder新增log引起的不能開機問題分析

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方法