最近調試問題涉及到了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> "
<< "¬ification);\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> "
<< "¬ification) ";
});
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的功能。
查閱的資料連接地址https://blog.csdn.net/yangwen123/article/details/79840569 ↩︎