汽車看門狗

使用汽車看門狗幫助調試 VHAL。汽車看門狗監控並殺死不健康的進程。對於要被汽車看門狗監視的進程,該進程必須向汽車看門狗註冊。當汽車看門狗殺死不健康的進程時,汽車看門狗會將進程的狀態寫入data/anr ,就像其他應用程序無響應 (ANR) 轉儲一樣。這樣做有助於調試過程。

本文介紹供應商 HAL 和服務如何向汽車看門狗註冊進程。

供應商 HAL

通常,供應商 HAL 為hwbinder使用線程池。但是,汽車看門狗客戶端通過binder與汽車看門狗守護進程通信,這與hwbinder不同。因此,正在使用另一個用於binder的線程池。

在makefile中指定汽車看門狗aidl

  1. shared_libs中包含carwatchdog_aidl_interface-ndk_platform

    Android.bp

    cc_defaults {
        name: "vhal_v2_0_defaults",
        shared_libs: [
            "libbinder_ndk",
            "libhidlbase",
            "liblog",
            "libutils",
            "android.hardware.automotive.vehicle@2.0",
            "carwatchdog_aidl_interface-ndk_platform",
        ],
        cflags: [
            "-Wall",
            "-Wextra",
            "-Werror",
        ],
    }
    

添加 SELinux 策略

  1. 允許system_server殺死你的 HAL。如果您沒有system_server.te ,請創建一個。強烈建議您為每台設備添加 SELinux 策略。
  2. 允許供應商 HAL 使用 binder( binder_use宏)並將供應商 HAL 添加到carwatchdog客戶端域( carwatchdog_client_domain宏)。請參閱下面的systemserver.tevehicle_default.te代碼:

    系統服務器.te

    # Allow system_server to kill vehicle HAL
    allow system_server hal_vehicle_server:process sigkill;
    

    hal_vehicle_default.te

    # Configuration for register VHAL to car watchdog
    carwatchdog_client_domain(hal_vehicle_default)
    binder_use(hal_vehicle_default)
    

通過繼承 BnCarWatchdogClient 實現一個客戶端類

  1. checkIfAlive中,執行健康檢查。例如,發佈到線程循環處理程序。如果健康,請致電ICarWatchdog::tellClientAlive 。請參閱下面的WatchogClient.hWatchogClient.cpp代碼:

    WatchogClient.h

    class WatchdogClient : public aidl::android::automotive::watchdog::BnCarWatchdogClient {
      public:
        explicit WatchdogClient(const ::android::sp<::android::Looper>& handlerLooper, VehicleHalManager* vhalManager);
    
    ndk::ScopedAStatus checkIfAlive(int32_t sessionId, aidl::android::automotive::watchdog::TimeoutLength timeout) override; ndk::ScopedAStatus prepareProcessTermination() override; };

    WatchogClient.cpp

    ndk::ScopedAStatus WatchdogClient::checkIfAlive(int32_t sessionId, TimeoutLength /*timeout*/) {
        // Implement or call your health check logic here
        return ndk::ScopedAStatus::ok();
    }
    

啟動binder線程並註冊客戶端

  1. 為 binder 通信創建線程池。如果供應商 HAL 出於自身目的使用 hwbinder,則必須創建另一個線程池用於 car watchdog binder 通信)。
  2. 使用名稱搜索守護進程並調用ICarWatchdog::registerClient 。汽車看門狗守護進程接口名稱為android.automotive.watchdog.ICarWatchdog/default
  3. 根據服務響應能力,選擇汽車看門狗支持的以下三種超時類型之一,然後將超時傳遞給ICarWatchdog::registerClient調用:
    • 關鍵(3s)
    • 中等(5s)
    • 正常(10s)
    有關VehicleService.cppWatchogClient.cpp的代碼,請參見以下代碼:

    車輛服務.cpp

    int main(int /* argc */, char* /* argv */ []) {
        // Set up thread pool for hwbinder
        configureRpcThreadpool(4, false /* callerWillJoin */);
    
        ALOGI("Registering as service...");
        status_t status = service->registerAsService();
    
        if (status != OK) {
            ALOGE("Unable to register vehicle service (%d)", status);
            return 1;
        }
    
        // Setup a binder thread pool to be a car watchdog client.
        ABinderProcess_setThreadPoolMaxThreadCount(1);
        ABinderProcess_startThreadPool();
        sp<Looper> looper(Looper::prepare(0 /* opts */));
        std::shared_ptr<WatchdogClient> watchdogClient =
                ndk::SharedRefBase::make<WatchdogClient>(looper, service.get());
        // The current health check is done in the main thread, so it falls short of capturing the real
        // situation. Checking through HAL binder thread should be considered.
        if (!watchdogClient->initialize()) {
            ALOGE("Failed to initialize car watchdog client");
            return 1;
        }
        ALOGI("Ready");
        while (true) {
            looper->pollAll(-1 /* timeoutMillis */);
        }
    
        return 1;
    }
    

    WatchogClient.cpp

    bool WatchdogClient::initialize() {
        ndk::SpAIBinder binder(AServiceManager_getService("android.automotive.watchdog.ICarWatchdog/default"));
        if (binder.get() == nullptr) {
            ALOGE("Failed to get carwatchdog daemon");
            return false;
        }
        std::shared_ptr<ICarWatchdog> server = ICarWatchdog::fromBinder(binder);
        if (server == nullptr) {
            ALOGE("Failed to connect to carwatchdog daemon");
            return false;
        }
        mWatchdogServer = server;
    
        binder = this->asBinder();
        if (binder.get() == nullptr) {
            ALOGE("Failed to get car watchdog client binder object");
            return false;
        }
        std::shared_ptr<ICarWatchdogClient> client = ICarWatchdogClient::fromBinder(binder);
        if (client == nullptr) {
            ALOGE("Failed to get ICarWatchdogClient from binder");
            return false;
        }
        mTestClient = client;
        mWatchdogServer->registerClient(client, TimeoutLength::TIMEOUT_NORMAL);
        ALOGI("Successfully registered the client to car watchdog server");
        return true;
    }
    

供應商服務(本機)

指定汽車看門狗aidl makefile

  1. shared_libs中包含carwatchdog_aidl_interface-ndk_platform

    Android.bp

    cc_binary {
        name: "sample_native_client",
        srcs: [
            "src/*.cpp"
        ],
        shared_libs: [
            "carwatchdog_aidl_interface-ndk_platform",
            "libbinder_ndk",
        ],
        vendor: true,
    }
    

添加 SELinux 策略

  1. 要添加 SELinux 策略,請允許供應商服務域使用 binder( binder_use宏)並將供應商服務域添加到carwatchdog客戶端域( carwatchdog_client_domain宏)。有關sample_client.tefile_contexts的信息,請參見下面的代碼:

    sample_client.te

    type sample_client, domain;
    type sample_client_exec, exec_type, file_type, vendor_file_type;
    
    carwatchdog_client_domain(sample_client)
    
    init_daemon_domain(sample_client)
    binder_use(sample_client)
    

    文件上下文

    /vendor/bin/sample_native_client  u:object_r:sample_client_exec:s0
    

通過繼承 BnCarWatchdogClient 實現一個客戶端類

  1. checkIfAlive中,執行健康檢查。一種選擇是發佈到線程循環處理程序。如果健康,請致電ICarWatchdog::tellClientAlive 。請參閱下面的SampleNativeClient.hSampleNativeClient.cpp代碼:

    SampleNativeClient.h

    class SampleNativeClient : public BnCarWatchdogClient {
    public:
        ndk::ScopedAStatus checkIfAlive(int32_t sessionId, TimeoutLength
            timeout) override;
        ndk::ScopedAStatus prepareProcessTermination() override;
        void initialize();
    
    private:
        void respondToDaemon();
    private:
        ::android::sp<::android::Looper> mHandlerLooper;
        std::shared_ptr<ICarWatchdog> mWatchdogServer;
        std::shared_ptr<ICarWatchdogClient> mClient;
        int32_t mSessionId;
    };
    

    示例 NativeClient.cpp

    ndk::ScopedAStatus WatchdogClient::checkIfAlive(int32_t sessionId, TimeoutLength timeout) {
        mHandlerLooper->removeMessages(mMessageHandler,
            WHAT_CHECK_ALIVE);
        mSessionId = sessionId;
        mHandlerLooper->sendMessage(mMessageHandler,
            Message(WHAT_CHECK_ALIVE));
        return ndk::ScopedAStatus::ok();
    }
    // WHAT_CHECK_ALIVE triggers respondToDaemon from thread handler
    void WatchdogClient::respondToDaemon() {
      // your health checking method here
      ndk::ScopedAStatus status = mWatchdogServer->tellClientAlive(mClient,
            mSessionId);
    }
    

啟動一個活頁夾線程並註冊客戶端

汽車看門狗守護進程接口名稱為android.automotive.watchdog.ICarWatchdog/default

  1. 使用名稱搜索守護進程並調用ICarWatchdog::registerClient 。請參閱下面的main.cppSampleNativeClient.cpp代碼:

    主文件

    int main(int argc, char** argv) {
        sp<Looper> looper(Looper::prepare(/*opts=*/0));
    
        ABinderProcess_setThreadPoolMaxThreadCount(1);
        ABinderProcess_startThreadPool();
        std::shared_ptr<SampleNativeClient> client =
            ndk::SharedRefBase::make<SampleNatvieClient>(looper);
    
        // The client is registered in initialize()
        client->initialize();
        ...
    }
    

    示例 NativeClient.cpp

    void SampleNativeClient::initialize() {
        ndk::SpAIBinder binder(AServiceManager_getService(
            "android.automotive.watchdog.ICarWatchdog/default"));
        std::shared_ptr<ICarWatchdog> server =
            ICarWatchdog::fromBinder(binder);
        mWatchdogServer = server;
        ndk::SpAIBinder binder = this->asBinder();
        std::shared_ptr<ICarWatchdogClient> client =
            ICarWatchdogClient::fromBinder(binder)
        mClient = client;
        server->registerClient(client, TimeoutLength::TIMEOUT_NORMAL);
    }
    

供應商服務 (Android)

通過繼承 CarWatchdogClientCallback 實現客戶端

  1. 編輯新文件如下:
    private final CarWatchdogClientCallback mClientCallback = new CarWatchdogClientCallback() {
        @Override
        public boolean onCheckHealthStatus(int sessionId, int timeout) {
            // Your health check logic here
            // Returning true implies the client is healthy
            // If false is returned, the client should call
            // CarWatchdogManager.tellClientAlive after health check is
            // completed
        }
    
        @Override
        public void onPrepareProcessTermination() {}
    };
    

註冊客戶端

  1. 調用CarWatchdogManager.registerClient() :
    private void startClient() {
        CarWatchdogManager manager =
            (CarWatchdogManager) car.getCarManager(
            Car.CAR_WATCHDOG_SERVICE);
        // Choose a proper executor according to your health check method
        ExecutorService executor = Executors.newFixedThreadPool(1);
        manager.registerClient(executor, mClientCallback,
            CarWatchdogManager.TIMEOUT_NORMAL);
    }
    

註銷客戶端

  1. 服務完成後調用CarWatchdogManager.unregisterClient()
    private void finishClient() {
        CarWatchdogManager manager =
            (CarWatchdogManager) car.getCarManager(
            Car.CAR_WATCHDOG_SERVICE);
        manager.unregisterClient(mClientCallback);
    }
    

檢測被汽車看門狗終止的進程

汽車看門狗轉儲/殺死進程(供應商 HAL、供應商本機服務、供應商 Android 服務),這些進程在卡住且無響應時註冊到汽車看門狗。通過檢查 logcats 來檢測這種轉儲。當有問題的進程被轉儲或終止時,汽車看門狗會輸出日誌carwatchdog killed process_name (pid:process_id) 。因此:

$ adb logcat -s CarServiceHelper | fgrep "carwatchdog killed"

相關日誌被捕獲。例如,如果 KitchenSink 應用程序(汽車看門狗客戶端)卡住,則會將如下一行寫入日誌:

05-01 09:50:19.683   578  5777 W CarServiceHelper: carwatchdog killed com.google.android.car.kitchensink (pid: 5574)

要確定 KitchenSink 應用程序卡住的原因或位置,請使用存儲在/data/anr的進程轉儲,就像使用 Activity ANR 案例一樣。

$ adb root
$ adb shell grep -Hn "pid process_pid" /data/anr/*

以下示例輸出特定於 KitchenSink 應用程序:

$ adb shell su root grep -Hn "pid 5574" /data/anr/*.
/data/anr/anr_2020-05-01-09-50-18-290:3:----- pid 5574 at 2020-05-01 09:50:18 -----
/data/anr/anr_2020-05-01-09-50-18-290:285:----- Waiting Channels: pid 5574 at 2020-05-01 09:50:18 -----

找到轉儲文件(例如,上例中的/data/anr/anr_2020-05-01-09-50-18-290 )並開始分析。