HIDL学习总结--如何在由ICameraProvider.hal文件定义的HIDL接口实现中添加调试Log

最近调试问题涉及到了ICameraProvider类,该类文件是由ICameraProvider.hal–HIDL接口定义文件经hidl-gen编译工具自动生成的,于是上网学习了下HIDL相关文章1

为了方便调试,研究实现了在由ICameraProvider.hal接口定义文件自动生成的CameraProviderAll.cpp类中添加一些调试Log的功能。

首先讲解下hidl-gen工具

  • 名称:hidl-gen
  • 工具使用方法:
examples:
croot
hidl-gen -o output -L c++ -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport android.hardware.nfc@1.0::INfc.hal
hidl-gen -o output -L vts -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport android.hardware.nfc@1.0
hidl-gen -o test -L c++ -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport android.hardware.nfc@1.0
hidl-gen -L hash -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport android.hardware.nfc@1.0
  • 源码路径:
    system/tools/hidl

  • 工具入口

//system\tools\hidl\main.cpp

int main(int argc, char **argv) {
    
    
    std::string outputPath;
    std::string rootPath;
    std::vector<std::string> packageRootPaths;
    std::vector<std::string> packageRoots;
    ...
    Coordinator coordinator(packageRootPaths, packageRoots, rootPath);
    coordinator.addDefaultPackagePath("android.hardware", "hardware/interfaces");
    coordinator.addDefaultPackagePath("android.hidl", "system/libhidl/transport");
    coordinator.addDefaultPackagePath("android.frameworks", "frameworks/hardware/interfaces");
    coordinator.addDefaultPackagePath("android.system", "system/hardware/interfaces");

    for (int i = 0; i < argc; ++i) {
    
    
        FQName fqName(argv[i]);
....
       //开始生成
        status_t err =
            outputFormat->generate(fqName, me, &coordinator, outputPath);
...
    }

    return 0;
}

下面讲解ICameraProvider::registerAsService(…)函数声明和函数实现方法的生成过程:

  • 声明函数生成方法:
//system\tools\hidl\generateCpp.cpp
static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) {
    
    
    declareGetService(out, interfaceName, true /* isTry */);//声明tryGetService
    declareGetService(out, interfaceName, false /* isTry */);//声明getService
    //声明registerAsService
    out << "__attribute__ ((warn_unused_result))"
        << "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n";
   //声明registerForNotifications
    out << "static bool registerForNotifications(\n";
    out.indent(2, [&] {
    
    
        out << "const std::string &serviceName,\n"
            << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
            << "&notification);\n";
    });

}
  • 生成的函数声明:
//out\soong\.intermediates\hardware\interfaces\camera\provider\2.4\[email protected]_genc++_headers\gen\android\hardware\camera\provider\2.4\ICameraProvider.h
    __attribute__ ((warn_unused_result))::android::status_t registerAsService(const std::string &serviceName="default");
  • 实现函数生成方法:
static void implementServiceManagerInteractions(Formatter &out,
        const FQName &fqName, const std::string &package) {
    
    

    const std::string interfaceName = fqName.getInterfaceName();

    implementGetService(out, fqName, true /* isTry */);//实现tryGetService
    implementGetService(out, fqName, false /* isTry */);//实现getService
    //实现registerAsService
    out << "::android::status_t " << interfaceName << "::registerAsService("
        << "const std::string &serviceName) ";
    out.block([&] {
    
    
        out << "::android::hardware::details::onRegistration(\""
            << fqName.getPackageAndVersion().string() << "\", \""
            << interfaceName
            << "\", serviceName);\n\n";
        out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
        out.indent(2, [&] {
    
    
            out << "= ::android::hardware::defaultServiceManager();\n";
        });
        out.sIf("sm == nullptr", [&] {
    
    
            out << "return ::android::INVALID_OPERATION;\n";
        }).endl();
        out << "::android::hardware::Return<bool> ret = "
            << "sm->add(serviceName.c_str(), this);\n"
            << "return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;\n";
    }).endl().endl();


    //实现registerForNotifications
    out << "bool " << interfaceName << "::registerForNotifications(\n";
    out.indent(2, [&] {
    
    
        out << "const std::string &serviceName,\n"
            << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
            << "&notification) ";
    });
    out.block([&] {
    
    
        out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
        out.indent(2, [&] {
    
    
            out << "= ::android::hardware::defaultServiceManager();\n";
        });
        out.sIf("sm == nullptr", [&] {
    
    
            out << "return false;\n";
        }).endl();
        out << "::android::hardware::Return<bool> success =\n";
        out.indent(2, [&] {
    
    
            out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n";
            out.indent(2, [&] {
    
    
                out << "serviceName, notification);\n";
            });
        });
        out << "return success.isOk() && success;\n";
    }).endl().endl();
}
  • 生成的实现方法是:
//out\soong\.intermediates\hardware\interfaces\camera\provider\2.4\[email protected]_genc++\gen\android\hardware\camera\provider\2.4\CameraProviderAll.cpp
::android::status_t ICameraProvider::registerAsService(const std::string &serviceName) {
    
    
    ::android::hardware::details::onRegistration("[email protected]", "ICameraProvider", serviceName);

    const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm
            = ::android::hardware::defaultServiceManager();
    if (sm == nullptr) {
    
    
        return ::android::INVALID_OPERATION;
    }
    ::android::hardware::Return<bool> ret = sm->add(serviceName.c_str(), this);
    return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;
}

至此讲解完成了ICameraProvider::registerAsService(…)的方法的生成过程

下边讲解下如何在ICameraProvider::registerAsService实现函数中添加一些调试log

方法如下:

diff --git a/generateCpp.cpp b/generateCpp.cpp
old mode 100644
new mode 100755
index 158402f..5632958
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -367,6 +367,9 @@ static void implementServiceManagerInteractions(Formatter &out,
         out.sIf("sm == nullptr", [&] {
    
    
             out << "return ::android::INVALID_OPERATION;\n";
         }).endl();
         //添加注册服务时服务名serviceName的打印
+        out << "ALOGE(\"mytest 11 registerAsService:serviceName %s\", "
+            << "serviceName.c_str());\n";
+
         out << "::android::hardware::Return<bool> ret = "
             << "sm->add(serviceName.c_str(), this);\n"
             << "return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;\n";
@@ -1091,6 +1094,7 @@ status_t AST::generateCppSources(const std::string &outputPath) const {
    
    
     out << "#include <android/log.h>\n";
     out << "#include <cutils/trace.h>\n";
     out << "#include <hidl/HidlTransportSupport.h>\n\n";
     //添加头文件
+    out << "#include <dlfcn.h>\n";
     if (iface) {
    
    
         // This is a no-op for IServiceManager itself.
         out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n";
@@ -1584,6 +1588,19 @@ status_t AST::generateStubSource(
         << interfaceName
         << "\") { \n";
     out.indent();
     //添加调用堆栈打印
+
+    out << "ALOGE(\"mytest  interfaceName %s\", "<< "_hidl_impl->descriptor);\n";
+    out << "if(!strcmp(_hidl_impl->descriptor,\"[email protected]::ICameraProvider\")){\n";
+    out << "void(*prt_stack_fun)(const char* );\n";
+    out << "void * prt_stack_lib = dlopen(\"/system/lib/libutilscallstack.so\",RTLD_LAZY);\n";
+    out << "prt_stack_fun = (void (*)(const char *))dlsym(prt_stack_lib,\"call_stack_fun\");\n";
+    out << "if(prt_stack_lib!=NULL){\n";
+    out << "prt_stack_fun(\"Create\");\n";
+    out << "}\n";
+    out << "dlclose(prt_stack_lib);\n";
+    out << "}\n";
+
+
     out << "_hidl_mImpl = _hidl_impl;\n";
     out << "auto prio = ::android::hardware::details::gServicePrioMap.get("
         << "_hidl_impl, {SCHED_NORMAL, 0});\n";

编译后的到的结果如下:

//out\soong\.intermediates\hardware\interfaces\camera\provider\2.4\[email protected]_genc++\gen\android\hardware\camera\provider\2.4\CameraProviderAll.cpp
#define LOG_TAG "[email protected]::CameraProvider"

#include <android/log.h>
#include <cutils/trace.h>
#include <hidl/HidlTransportSupport.h>
//添加的头文件
#include <dlfcn.h>
...
::android::status_t ICameraProvider::registerAsService(const std::string &serviceName) {
    
    
    ::android::hardware::details::onRegistration("[email protected]", "ICameraProvider", serviceName);

    const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm
            = ::android::hardware::defaultServiceManager();
    if (sm == nullptr) {
    
    
        return ::android::INVALID_OPERATION;
    }
    //添加的log,打印注册服务名
    ALOGE("mytest 11 registerAsService:serviceName %s", serviceName.c_str());
    ::android::hardware::Return<bool> ret = sm->add(serviceName.c_str(), this);
    return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;
}

BnHwCameraProvider::BnHwCameraProvider(const ::android::sp<ICameraProvider> &_hidl_impl)
        : ::android::hidl::base::V1_0::BnHwBase(_hidl_impl, "[email protected]", "ICameraProvider") {
    
     
           //添加的log,打印接口名
            ALOGE("mytest  interfaceName %s", _hidl_impl->descriptor);
            //添加的堆栈打印
            if(!strcmp(_hidl_impl->descriptor,"[email protected]::ICameraProvider")){
    
    
            void(*prt_stack_fun)(const char* );
            void * prt_stack_lib = dlopen("/system/lib/libutilscallstack.so",RTLD_LAZY);
            prt_stack_fun = (void (*)(const char *))dlsym(prt_stack_lib,"call_stack_fun");
            if(prt_stack_lib!=NULL){
    
    
            prt_stack_fun("Create");
            }
            dlclose(prt_stack_lib);
            }
            _hidl_mImpl = _hidl_impl;
            auto prio = ::android::hardware::details::gServicePrioMap.get(_hidl_impl, {
    
    SCHED_NORMAL, 0});
            mSchedPolicy = prio.sched_policy;
            mSchedPriority = prio.prio;
}

重新编译CameraProvider,将得到的[email protected] push到system/lib下
得到的输出log如下:

02-16 01:43:45.225   613   613 E android.hardware.camera.provider@2.4::CameraProvider: mytest 11 registerAsService:serviceName legacy/0
02-16 01:43:45.225   613   613 E android.hardware.camera.provider@2.4::CameraProvider: mytest  interfaceName android.hardware.camera.provider@2.4::ICameraProvider
02-16 01:43:45.248   613   613 D SGJ_Create: #00 pc 00009e0f  /system/lib/libutilscallstack.so (call_stack_fun+42)
02-16 01:43:45.248   613   613 D SGJ_Create: #01 pc 000152cb  /system/lib/android.hardware.camera.provider@2.4.so (android::hardware::camera::provider::V2_4::BnHwCameraProvider::BnHwCameraProvider(android::sp<android::hardware::camera::provider::V2_4::ICameraProvider> const&)+278)
02-16 01:43:45.248   613   613 D SGJ_Create: #02 pc 000194fb  /system/lib/android.hardware.camera.provider@2.4.so
02-16 01:43:45.248   613   613 D SGJ_Create: #03 pc 000377d3  /system/lib/vndk-sp/libhidltransport.so (std::__1::function<android::sp<android::hidl::base::V1_0::IBase> (void*)>::operator()(void*) const+26)
02-16 01:43:45.248   613   613 D SGJ_Create: #04 pc 00031d6b  /system/lib/vndk-sp/libhidltransport.so (_ZN7android8hardware8toBinderINS_4hidl7manager4V1_015IServiceManagerEEENS_2spINS0_7IBinderEEENS6_IT_EE+218)
02-16 01:43:45.248   613   613 D SGJ_Create: #05 pc 00031b03  /system/lib/vndk-sp/libhidltransport.so (android::hidl::manager::V1_0::BpHwServiceManager::_hidl_add(android::hardware::IInterface*, android::hardware::details::HidlInstrumentor*, android::hardware::hidl_string const&, android::sp<android::hidl::base::V1_0::IBase> const&)+174)
02-16 01:43:45.248   613   613 D SGJ_Create: #06 pc 0003c7e9  /system/lib/vndk-sp/libhidltransport.so (android::hidl::manager::V1_1::BpHwServiceManager::add(android::hardware::hidl_string const&, android::sp<android::hidl::base::V1_0::IBase> const&)+18)
02-16 01:43:45.248   613   613 D SGJ_Create: #07 pc 00016fb3  /system/lib/android.hardware.camera.provider@2.4.so (android::hardware::camera::provider::V2_4::ICameraProvider::registerAsService(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&)+222)
02-16 01:43:45.248   613   613 D SGJ_Create: #08 pc 00000ee5  /vendor/bin/hw/android.hardware.camera.provider@2.4-service
02-16 01:43:45.248   613   613 D SGJ_Create: #09 pc 00000e5d  /vendor/bin/hw/android.hardware.camera.provider@2.4-service
02-16 01:43:45.248   613   613 D SGJ_Create: #10 pc 00000de7  /vendor/bin/hw/android.hardware.camera.provider@2.4-service
02-16 01:43:45.248   613   613 D SGJ_Create: #11 pc 00080b45  /system/lib/libc.so (__libc_init+48)
02-16 01:43:45.248   613   613 D SGJ_Create: #12 pc 00000ca4  /vendor/bin/hw/android.hardware.camera.provider@2.4-service

至此,研究实现了在由ICameraProvider.hal接口定义文件自动生成的CameraProviderAll.cpp类中添加一些调试Log的功能。


  1. 查阅的资料连接地址https://blog.csdn.net/yangwen123/article/details/79840569 ↩︎

猜你喜欢

转载自blog.csdn.net/u010116586/article/details/92813278
今日推荐