背景:
针对调试和分析某个模块的源码时候,经常需要对某个方法进行堆栈的打印,java相关的代码都没啥问题,大家都可以很正常的打印出来,但是一些native的模块就不是那么顺利了。
大家可能会说,马哥你不是介绍过native有CallStack可以搞定native层面的调用堆栈么?
哈哈,确实CallStack是可以实现对c++代码堆栈的打印,不过这个只是对一般的c++代码可能没问题,真正在实际开发过程中还是会遇到很多问题,比如下面这个同学的堆栈打印问题:
总结一下这个同学的问题:
学员朋友想要在BufferQueueProducer.cpp中的queueBuffer中加入堆栈打印,看看整个调用链路
也按套路加入了如下代码:
#include <utils/CallStack.h> android::CallStack cs("INTEL-MESA");cs.update();cs.log("INTEL-MESA");
而且对应的bp也引入了相关的库
但是编译的时候发现如下报错:
[ 1% 11/917] //frameworks/av/media/module/bqhelper:libstagefright_bufferqueue_helper link libstagefright_bufferqueue_helper.so
FAILED: out/soong/.intermediates/frameworks/av/media/module/bqhelper/libstagefright_bufferqueue_helper/android_vendor.34_x86_64_shared/unstripped/libstagefright_bufferqueue_helper.so
prebuilts/clang/host/linux-x86/clang-r487747c/bin/clang++ out/soong/.intermediates/bionic/libc/crtbegin_so/android_vendor.34_x86_64/crtbegin_so.o @out/soong/.intermediates/frameworks/av/media/module/bqhe
lper/libstagefright_bufferqueue_helper/android_vendor.34_x86_64_shared/unstripped/libstagefright_bufferqueue_helper.so.rsp out/soong/.intermediates/bionic/libc/crtend_so/android_vendor.34_x86_64/crtend_s
o.o -o out/soong/.intermediates/frameworks/av/media/module/bqhelper/libstagefright_bufferqueue_helper/android_vendor.34_x86_64_shared/unstripped/libstagefright_bufferqueue_helper.so -target x86_64-linux-
android29 -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--build-id=md5 -Wl,--fatal-warnings -Wl,--no-undefined-version -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_stripped.a -Wl,--exclude-libs
,libunwind_llvm.a -Wl,--exclude-libs,libunwind.a -fuse-ld=lld -Wl,--icf=safe -Wl,--pack-dyn-relocs=android+relr -Wl,--use-android-relr-tags -Wl,--no-undefined -Wl,--hash-style=gnu -m64 -nostdlib -Wl,--gc
-sections -shared -Wl,-soname,libstagefright_bufferqueue_helper.so -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fno-sanitize-link-runtime -Wl,--exclude-libs=libclang_rt.builtins-x86_64-a
ndroid.a -Wl,--exclude-libs=libclang_rt.ubsan_minimal-x86_64-android.a
ld.lld: error: undefined symbol: android::CallStack::CallStack(char const*, int)
>>> referenced by BufferQueueProducer.cpp:927 (frameworks/native/libs/gui/BufferQueueProducer.cpp:927)
>>> BufferQueueProducer.o:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*)) in
archive out/soong/.intermediates/frameworks/native/libs/gui/libgui_bufferqueue_static/android_vendor.34_x86_64_static/libgui_bufferqueue_static.ald.lld: error: undefined symbol: android::CallStack::update(int, int)
>>> referenced by BufferQueueProducer.cpp:928 (frameworks/native/libs/gui/BufferQueueProducer.cpp:928)
>>> BufferQueueProducer.o:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*)) in
archive out/soong/.intermediates/frameworks/native/libs/gui/libgui_bufferqueue_static/android_vendor.34_x86_64_static/libgui_bufferqueue_static.ald.lld: error: undefined symbol: android::CallStack::log(char const*, android_LogPriority, char const*) const
>>> referenced by BufferQueueProducer.cpp:929 (frameworks/native/libs/gui/BufferQueueProducer.cpp:929)
>>> BufferQueueProducer.o:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*)) in
archive out/soong/.intermediates/frameworks/native/libs/gui/libgui_bufferqueue_static/android_vendor.34_x86_64_static/libgui_bufferqueue_static.ald.lld: error: undefined symbol: android::CallStack::~CallStack()
>>> referenced by BufferQueueProducer.cpp:930 (frameworks/native/libs/gui/BufferQueueProducer.cpp:930)
>>> BufferQueueProducer.o:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*)) in
archive out/soong/.intermediates/frameworks/native/libs/gui/libgui_bufferqueue_static/android_vendor.34_x86_64_static/libgui_bufferqueue_static.a
clang-17: error: linker command failed with exit code 1 (use -v to see invocation)
[ 1% 12/917] //frameworks/av/media/module/bqhelper:libstagefright_bufferqueue_helper link libstagefright_bufferqueue_helper.so [x86]
FAILED: out/soong/.intermediates/frameworks/av/media/module/bqhelper/libstagefright_bufferqueue_helper/android_vendor.34_x86_x86_64_shared/unstripped/libstagefright_bufferqueue_helper.so
prebuilts/clang/host/linux-x86/clang-r487747c/bin/clang++ out/soong/.intermediates/bionic/libc/crtbegin_so/android_vendor.34_x86_x86_64/crtbegin_so.o @out/soong/.intermediates/frameworks/av/media/module/
bqhelper/libstagefright_bufferqueue_helper/android_vendor.34_x86_x86_64_shared/unstripped/libstagefright_bufferqueue_helper.so.rsp out/soong/.intermediates/bionic/libc/crtend_so/android_vendor.34_x86_x86
_64/crtend_so.o -o out/soong/.intermediates/frameworks/av/media/module/bqhelper/libstagefright_bufferqueue_helper/android_vendor.34_x86_x86_64_shared/unstripped/libstagefright_bufferqueue_helper.so -targ
et i686-linux-android29 -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--build-id=md5 -Wl,--fatal-warnings -Wl,--no-undefined-version -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_stripped.a -Wl,
--exclude-libs,libunwind_llvm.a -Wl,--exclude-libs,libunwind.a -fuse-ld=lld -Wl,--icf=safe -Wl,--pack-dyn-relocs=android+relr -Wl,--use-android-relr-tags -Wl,--no-undefined -Wl,--hash-style=gnu -m32 -nos
tdlib -Wl,--gc-sections -shared -Wl,-soname,libstagefright_bufferqueue_helper.so -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fno-sanitize-link-runtime -Wl,--exclude-libs=libclang_rt.bui
ltins-i686-android.a -Wl,--exclude-libs=libclang_rt.ubsan_minimal-i686-android.a
ld.lld: error: undefined symbol: android::CallStack::CallStack(char const*, int)
>>> referenced by BufferQueueProducer.cpp:927 (frameworks/native/libs/gui/BufferQueueProducer.cpp:927)
>>> BufferQueueProducer.o:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*)) in
archive out/soong/.intermediates/frameworks/native/libs/gui/libgui_bufferqueue_static/android_vendor.34_x86_x86_64_static/libgui_bufferqueue_static.ald.lld: error: undefined symbol: android::CallStack::update(int, int)
>>> referenced by BufferQueueProducer.cpp:928 (frameworks/native/libs/gui/BufferQueueProducer.cpp:928)
>>> BufferQueueProducer.o:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*)) in
archive out/soong/.intermediates/frameworks/native/libs/gui/libgui_bufferqueue_static/android_vendor.34_x86_x86_64_static/libgui_bufferqueue_static.ald.lld: error: undefined symbol: android::CallStack::log(char const*, android_LogPriority, char const*) const
>>> referenced by BufferQueueProducer.cpp:929 (frameworks/native/libs/gui/BufferQueueProducer.cpp:929)
>>> BufferQueueProducer.o:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*)) in
archive out/soong/.intermediates/frameworks/native/libs/gui/libgui_bufferqueue_static/android_vendor.34_x86_x86_64_static/libgui_bufferqueue_static.ald.lld: error: undefined symbol: android::CallStack::~CallStack()
>>> referenced by BufferQueueProducer.cpp:930 (frameworks/native/libs/gui/BufferQueueProducer.cpp:930)
>>> BufferQueueProducer.o:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*)) in
archive out/soong/.intermediates/frameworks/native/libs/gui/libgui_bufferqueue_static/android_vendor.34_x86_x86_64_static/libgui_bufferqueue_static.a
clang-17: error: linker command failed with exit code 1 (use -v to see invocation)
[ 1% 15/917] //frameworks/av/media/module/bqhelper:libstagefright_bufferqueue_helper_novndk link libstagefright_bufferqueue_helper_novndk.so [apex31]
FAILED: out/soong/.intermediates/frameworks/av/media/module/bqhelper/libstagefright_bufferqueue_helper_novndk/android_x86_64_shared_cfi_apex31/unstripped/libstagefright_bufferqueue_helper_novndk.so
prebuilts/clang/host/linux-x86/clang-r487747c/bin/clang++ out/soong/.intermediates/bionic/libc/crtbegin_so/android_x86_64_apex31/crtbegin_so.o @out/soong/.intermediates/frameworks/av/media/module/bqhelpe
r/libstagefright_bufferqueue_helper_novndk/android_x86_64_shared_cfi_apex31/unstripped/libstagefright_bufferqueue_helper_novndk.so.rsp out/soong/.intermediates/bionic/libc/crtend_so/android_x86_64_apex31
/crtend_so.o -o out/soong/.intermediates/frameworks/av/media/module/bqhelper/libstagefright_bufferqueue_helper_novndk/android_x86_64_shared_cfi_apex31/unstripped/libstagefright_bufferqueue_helper_novndk.
so -target x86_64-linux-android29 -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--build-id=md5 -Wl,--fatal-warnings -Wl,--no-undefined-version -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_strip
ped.a -Wl,--exclude-libs,libunwind_llvm.a -Wl,--exclude-libs,libunwind.a -fuse-ld=lld -Wl,--icf=safe -Wl,--pack-dyn-relocs=android+relr -Wl,--use-android-relr-tags -Wl,--no-undefined -Wl,--hash-style=gnu-m64 -nostdlib -Wl,--gc-sections -shared -Wl,-soname,libstagefright_bufferqueue_helper_novndk.so -flto -fsanitize-cfi-cross-dso -fsanitize=cfi -Wl,-plugin-opt,O1 -fsanitize=signed-integer-overflow,unsig
ned-integer-overflow,cfi -fno-sanitize-link-runtime -Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a -Wl,--exclude-libs=libclang_rt.ubsan_minimal-x86_64-android.a
ld.lld: error: undefined symbol: android::CallStack::CallStack(char const*, int)
>>> referenced by BufferQueueProducer.cpp:927 (frameworks/native/libs/gui/BufferQueueProducer.cpp:927)
>>> lto.tmp:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*))ld.lld: error: undefined symbol: android::CallStack::update(int, int)
>>> referenced by BufferQueueProducer.cpp:928 (frameworks/native/libs/gui/BufferQueueProducer.cpp:928)
>>> lto.tmp:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*))ld.lld: error: undefined symbol: android::CallStack::log(char const*, android_LogPriority, char const*) const
>>> referenced by BufferQueueProducer.cpp:929 (frameworks/native/libs/gui/BufferQueueProducer.cpp:929)
>>> lto.tmp:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*))ld.lld: error: undefined symbol: android::CallStack::~CallStack()
>>> referenced by BufferQueueProducer.cpp:930 (frameworks/native/libs/gui/BufferQueueProducer.cpp:930)
>>> lto.tmp:(android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*))
clang-17: error: linker command failed with exit code 1 (use -v to see invocation)
11:21:00 ninja failed with: exit status 1
大面积报ld.lld: error: undefined symbol: android::CallStack错误,明明已经引入了相关库,
一看到这些报其实大家可能都一脸懵,不知道为啥会有这种错误。。。。。
分析报错大概原因
仔细看看对应的报错
明显都不是编译libgui时候的报错,都是报错在编译
frameworks/av/media/module/bqhelper:libstagefright_bufferqueue_helper
等库的时候有问题,简单说就是这些库其实也对我们修改的BufferQueueProducer.cpp有依赖,从bp中也可以发现:
BufferQueueProducer.cpp是在libgui_bufferqueue_sources中
libgui_bufferqueue_sources由被2个地方使用
libgui_bufferqueue_static静态库
libgui的动态库
但是在libstagefright_bufferqueue_helper编译时候有依赖这个libgui_bufferqueue_static库
这个也就是为啥会报错的原因,那么清楚了原来libstagefright_bufferqueue_helper,libstagefright_bufferqueue_helper_novndk对这个有依赖,那么是否可以直接在这个bp加入 libutilscallstack呢?
尝试一下:
突然发现又报错如下:
Consider adding "com.android.media.swcodec" to 'apex_available' property of "liblzma"
error: frameworks/av/apex/Android.bp:248:1: module "com.android.media.swcodec" variant "android_common_cfi_com.android.media.swcodec_image": "liblzma" requires "liblzma" that doesn't list the APEX under
'apex_available'.Dependency path:via tag apex.dependencyTag{"executable"}-> mediaswcodec{os:android,image:,arch:x86_64,sdk:,apex:apex31}via tag cc.libraryDependencyTag: { Kind:sharedLibraryDependency Order:normalLibraryDependency wholeStatic:false reexportFlags:false explicitlyVersioned:false dataLib:false ndk:false staticUnwi
nder:false makeSuffix: skipApexAllowedDependenciesCheck:false excludeInApex:false unexportedSymbols:false}-> libmedia_codecserviceregistrant{os:android,image:,arch:x86_64,sdk:,link:shared,version:,apex:apex31}via tag cc.libraryDependencyTag: { Kind:sharedLibraryDependency Order:normalLibraryDependency wholeStatic:false reexportFlags:false explicitlyVersioned:false dataLib:false ndk:false staticUnwi
nder:false makeSuffix: skipApexAllowedDependenciesCheck:false excludeInApex:false unexportedSymbols:false}-> libcodec2_hidl@1.1{os:android,image:,arch:x86_64,sdk:,link:shared,version:,apex:apex31}via tag cc.libraryDependencyTag: { Kind:sharedLibraryDependency Order:normalLibraryDependency wholeStatic:false reexportFlags:true explicitlyVersioned:false dataLib:false ndk:false staticUnwin
der:false makeSuffix: skipApexAllowedDependenciesCheck:false excludeInApex:false unexportedSymbols:false}-> libcodec2_hidl@1.0{os:android,image:,arch:x86_64,sdk:,link:shared,version:,apex:apex31}via tag cc.libraryDependencyTag: { Kind:sharedLibraryDependency Order:normalLibraryDependency wholeStatic:false reexportFlags:false explicitlyVersioned:false dataLib:false ndk:false staticUnwi
nder:false makeSuffix: skipApexAllowedDependenciesCheck:false excludeInApex:false unexportedSymbols:false}-> libstagefright_bufferqueue_helper_novndk{os:android,image:,arch:x86_64,sdk:,link:shared,version:,apex:apex31}via tag cc.libraryDependencyTag: { Kind:sharedLibraryDependency Order:normalLibraryDependency wholeStatic:false reexportFlags:false explicitlyVersioned:false dataLib:false ndk:false staticUnwi
nder:false makeSuffix: skipApexAllowedDependenciesCheck:false excludeInApex:false unexportedSymbols:false}-> libutilscallstack{os:android,image:,arch:x86_64,sdk:,link:shared,version:,apex:apex31}via tag cc.libraryDependencyTag: { Kind:sharedLibraryDependency Order:normalLibraryDependency wholeStatic:false reexportFlags:false explicitlyVersioned:false dataLib:false ndk:false staticUnwi
nder:false makeSuffix: skipApexAllowedDependenciesCheck:false excludeInApex:false unexportedSymbols:false}-> libunwindstack{os:android,image:,arch:x86_64,sdk:,link:shared,version:,apex:apex31}via tag cc.libraryDependencyTag: { Kind:sharedLibraryDependency Order:normalLibraryDependency wholeStatic:false reexportFlags:false explicitlyVersioned:false dataLib:false ndk:false staticUnwi
nder:false makeSuffix: skipApexAllowedDependenciesCheck:false excludeInApex:false unexportedSymbols:false}-> liblzma{os:android,image:,arch:x86_64,sdk:,link:shared,version:,apex:apex31}via tag cc.dependencyTag: { name:reuse objects}-> liblzma{os:android,image:,arch:x86_64,sdk:,link:static,afdo:,lto:,apex:apex31}Consider adding "com.android.media.swcodec" to 'apex_available' property of "liblzma"
error: frameworks/av/apex/Android.bp:248:1: module “com.android.media.swcodec” variant “android_common_cfi_com.android.media.swcodec_image”: “libunwindstack” requires “liblzma” that doesn’t list the APEX
under ‘apex_available’.
有点感觉进入了无底洞,涉及的依赖有点多,要把"libunwindstack" requires “liblzma” 也加入相关的apex_available,对这块不是那么深入了解,所以也不敢在继续改了
问题如何解决思路:
因为上面有编译报错,所以对queueBuffer的堆栈打印暂时没有成功
1、继续按照编译提示正面进行修改解决,这个可能需要更加深入系统这些库依赖关系等,感觉影响波及较大,这里如果有网友知道正面修改如何解决的请留言联系哈
2、是否可以考虑其他方法可以实现对这个queueBuffer的堆栈打印呢?
比如我们产生anr时候有对应的trace文件,trace文件里面既可以有详细的堆栈CallStack,那么是否可以考虑从这个角度修改呢?
更多framework技术干货,请关注下面“千里马学框架”