[Android][FWK]一个system_server 重启的案例分析

本文记录了一次Android设备因蓝牙Gatt组件出现NullPointerException导致的系统崩溃案例。通过对系统日志的解析,定位到BluetoothManagerService中的异常代码,并跟踪到具体的问题根源及解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android 8.1 上,报了一个手机重启。

查看android  event log, 查询关键字 am_crash, 发现问题出现的时间点上有个 NullPointerException

16:16:12.278 1000  3277  3456 I am_crash: [3277,0,system_server,-1,java.lang.NullPointerException,Attempt to invoke virtual method 'void com.android.bluetooth.gatt.AdvertiseManager.stopAdvertisingSets()' on a null object reference,Parcel.java,1949]

 

main log 上可以看到, system server 已经在垂死挣扎中。

16:16:12.446 10037 32271 32271 E AndroidRuntime: DeadSystemException: The system died; earlier logs will point to the root cause

以上打印log的代码位置如下:

frameworks/base/core/java/android/util/Log.java
/**
 * Helper function for long messages. Uses the LineBreakBufferedWriter to break
 * up long messages and stacktraces along newlines, but tries to write in large
 * chunks. This is to avoid truncation.
 * @hide
 */
public static int printlns(int bufID, int priority, String tag, String msg,
        Throwable tr) {
    ImmediateLogWriter logWriter = new ImmediateLogWriter(bufID, priority, tag);
    // Acceptable buffer size. Get the native buffer size, subtract two zero terminators,
    // and the length of the tag.
    // Note: we implicitly accept possible truncation for Modified-UTF8 differences. It
    //       is too expensive to compute that ahead of time.
    int bufferSize = PreloadHolder.LOGGER_ENTRY_MAX_PAYLOAD    // Base.
            - 2                                                // Two terminators.
            - (tag != null ? tag.length() : 0)                 // Tag length.
            - 32;                                              // Some slack.
    // At least assume you can print *some* characters (tag is not too large).
    bufferSize = Math.max(bufferSize, 100);

    LineBreakBufferedWriter lbbw = new LineBreakBufferedWriter(logWriter, bufferSize);

    lbbw.println(msg);

    if (tr != null) {
        // This is to reduce the amount of log spew that apps do in the non-error
        // condition of the network being unavailable.
        Throwable t = tr;
        while (t != null) {
            if (t instanceof UnknownHostException) {
                break;
            }
            if (t instanceof DeadSystemException) {
                lbbw.println("DeadSystemException: The system died; "
                        + "earlier logs will point to the root cause");
                break;
            }
            t = t.getCause();
        }
        if (t == null) {
            tr.printStackTrace(lbbw);
        }
    }

    lbbw.flush();

    return logWriter.getWritten();
}

以下是发生问题时的BackTrace,问题就是一个 NullPointerException,跟 BT 有关。

16:16:12.278  1000  3277  3456 E AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: android.io
16:16:12.278  1000  3277  3456 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.android.bluetooth.gatt.AdvertiseManager.stopAdvertisingSets()' on a null object reference
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at android.os.Parcel.readException(Parcel.java:1949)
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at android.os.Parcel.readException(Parcel.java:1889)
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at android.bluetooth.IBluetoothGatt$Stub$Proxy.unregAll(IBluetoothGatt.java:1855)
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at com.android.server.BluetoothManagerService.sendBrEdrDownCallback(BluetoothManagerService.java:794)
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at com.android.server.BluetoothManagerService.bluetoothStateChangeHandler(BluetoothManagerService.java:2076)
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at com.android.server.BluetoothManagerService.-wrap7(Unknown Source:0)
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at com.android.server.BluetoothManagerService$BluetoothHandler.handleMessage(BluetoothManagerService.java:1697)
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:105)
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:164)
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at android.os.HandlerThread.run(HandlerThread.java:65)
16:16:12.278  1000  3277  3456 E AndroidRuntime: 	at com.android.server.ServiceThread.run(ServiceThread.java:46)
16:16:12.280  1000  3277  3277 I chatty  : uid=1000 system_server expire 1 line

对应code:

frameworks/base/services/core/java/com/android/server/BluetoothManagerService.java


769    /**
770     * Inform BluetoothAdapter instances that BREDR part is down
771     * and turn off all service and stack if no LE app needs it
772     */
773    private void sendBrEdrDownCallback() {
774        if (DBG) Slog.d(TAG,"Calling sendBrEdrDownCallback callbacks");
775
776        if (mBluetooth == null) {
777            Slog.w(TAG, "Bluetooth handle is null");
778            return;
779        }
780
781        if (isBleAppPresent()) {
782            // Need to stay at BLE ON. Disconnect all Gatt connections
783            try {
784                mBluetoothGatt.unregAll();
785            } catch (RemoteException e) {
786                Slog.e(TAG, "Unable to disconnect all apps.", e);
787            }
788        } else {
789            try {
790                mBluetoothLock.readLock().lock();
791                if (mBluetooth != null) mBluetooth.onBrEdrDown();
792            } catch (RemoteException e) {
793                Slog.e(TAG, "Call to onBrEdrDown() failed.", e);
794            } finally {
795                mBluetoothLock.readLock().unlock();
796            }
797        }
798
799    }

BluetoothManagerService.java 794行已经在走异常流程。

省略分析...

属于android 原生问题,最后发现已经有patch,传送门

 https://android-review.googlesource.com/

 

 

end.

Android FWK(Framework)和APP(Application)领域,需要具备以下能力和技能: 1. Java编程能力:Android开发主要使用Java语言进行编程,因此需要熟练掌握Java语言的基础知识和编程技巧,包括面向对象编程、数据结构和算法等。 2. Android开发框架:需要熟悉Android开发框架的各个组件,如Activity、Service、BroadcastReceiver、ContentProvider等,并掌握它们之间的关系和交互方式。 3. Android UI开发:需要熟悉Android UI的各种控件和布局方式,并能够灵活运用它们进行界面设计和开发。 4. 网络编程:需要掌握Android中的网络编程技术,包括HTTP、TCP/IP、WebSocket等通信协议的使用和调试。 5. 数据库操作:需要了解Android中的数据库操作技术,包括SQLite数据库的使用和管理,以及ORM框架的应用。 6. 多线程编程:需要掌握Android中的多线程编程技术,包括Handler、Runnable、Thread等,以及线程同步和异步消息处理等。 7. 调试和测试工具:需要熟悉Android开发中的调试和测试工具,如Logcat、DDMS、Android Studio等,以便及时发现和解决问题。 8. 第三方库和框架:需要了解Android中常用的第三方库和框架,如OkHttp、Retrofit、Glide、ButterKnife、RxJava等,以便在开发中更加高效地使用它们。 9. 设计模式和架构:需要了解常用的设计模式和架构,如MVC、MVP、MVVM等,以便在开发中更好地组织代码和实现业务逻辑。 总之,Android FWK和APP开发涉及到的知识和技能非常广泛,需要具备全面的技术能力和实践经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值