欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android系统下打印函数调用栈信息(c++部分)

程序员文章站 2022-06-24 10:02:46
一般需要添加3部分1、依赖的库libutilscallstack.soAndroid.mk下添加LOCAL_SHARED_LIBRARIES := libdl libdisplaydebug libsdmutils libutilscallstack如果不添加对libutilscallstack.so的引用,出现以下错误hardware/qcom/display/sdm/libs/core/display_base.cpp:41:10: fatal error: 'uti......

一、需要添加3部分

1、依赖的库

libutilscallstack.so

Android.mk下添加LOCAL_SHARED_LIBRARIES        := libdl libdisplaydebug libsdmutils libutilscallstack

如果不添加对libutilscallstack.so的引用,出现以下错误

hardware/qcom/display/sdm/libs/core/display_base.cpp:41:10: fatal error: 'utils/CallStack.h' file not found
#include <utils/CallStack.h>
         ^~~~~~~~~~~~~~~~~~~

ld.lld: error: undefined symbol: android::CallStack::CallStack()
>>> referenced by display_base.cpp:549 (hardware/qcom/display/sdm/libs/core/display_base.cpp:549)
>>>               out/target/product/trinket/obj/SHARED_LIBRARIES/libsdmcore_intermediates/display_base.o:(sdm::DisplayBase::SetDisplayState(sdm::DisplayState, bool, int*))

ld.lld: error: undefined symbol: android::CallStack::update(int, int)
>>> referenced by display_base.cpp:550 (hardware/qcom/display/sdm/libs/core/display_base.cpp:550)
>>>               out/target/product/trinket/obj/SHARED_LIBRARIES/libsdmcore_intermediates/display_base.o:(sdm::DisplayBase::SetDisplayState(sdm::DisplayState, bool, int*))

ld.lld: error: undefined symbol: android::CallStack::dump(int, int, char const*) const
>>> referenced by display_base.cpp:551 (hardware/qcom/display/sdm/libs/core/display_base.cpp:551)
>>>               out/target/product/trinket/obj/SHARED_LIBRARIES/libsdmcore_intermediates/display_base.o:(sdm::DisplayBase::SetDisplayState(sdm::DisplayState, bool, int*))

ld.lld: error: undefined symbol: android::CallStack::log(char const*, android_LogPriority, char const*) const
>>> referenced by display_base.cpp:552 (hardware/qcom/display/sdm/libs/core/display_base.cpp:552)
>>>               out/target/product/trinket/obj/SHARED_LIBRARIES/libsdmcore_intermediates/display_base.o:(sdm::DisplayBase::SetDisplayState(sdm::DisplayState, bool, int*))

ld.lld: error: undefined symbol: android::CallStack::~CallStack()
>>> referenced by display_base.cpp:658 (hardware/qcom/display/sdm/libs/core/display_base.cpp:658)
>>>               out/target/product/trinket/obj/SHARED_LIBRARIES/libsdmcore_intermediates/display_base.o:(sdm::DisplayBase::SetDisplayState(sdm::DisplayState, bool, int*))
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
14:33:25 ninja failed with: exit status 1

2、添加接口的头文件

#include <utils/CallStack.h>

如果不添加以上头文件,会出现以下错误

RED_LIBRARIES/libsdmcore_intermediates/display_base.o hardware/qcom/display/sdm/libs/core/display_base.cpp"
hardware/qcom/display/sdm/libs/core/display_base.cpp:549:3: error: use of undeclared identifier 'android'
  android::CallStack callstack;
  ^
hardware/qcom/display/sdm/libs/core/display_base.cpp:552:42: error: use of undeclared identifier 'ANDROID_LOG_DEBUG'
  callstack.log("callstack-tag-display", ANDROID_LOG_DEBUG, "  ");

 

3、调用的接口

  android::CallStack callstack;
  callstack.update();
  callstack.dump(1);//打印到输出
  callstack.log("callstack-tag-display", ANDROID_LOG_DEBUG, "  ");//logcat输出

二、举例

1、在hardware/qcom/display/sdm/libs/core/display_base.cpp添加栈信息

对应patch

diff --git a/sdm/libs/core/Android.mk b/sdm/libs/core/Android.mk
index c934de06..43d09992 100644
--- a/sdm/libs/core/Android.mk
+++ b/sdm/libs/core/Android.mk
@@ -10,7 +10,7 @@ LOCAL_HEADER_LIBRARIES        := display_headers
 LOCAL_CFLAGS                  := -fno-operator-names -Wno-unused-parameter -DLOG_TAG=\"SDM\" \
                                  $(common_flags)
 LOCAL_HW_INTF_PATH_1          := fb
-LOCAL_SHARED_LIBRARIES        := libdl libdisplaydebug libsdmutils
+LOCAL_SHARED_LIBRARIES        := libdl libdisplaydebug libsdmutils libutilscallstack
 
 ifneq ($(TARGET_IS_HEADLESS), true)
     LOCAL_CFLAGS              += -isystem external/libdrm
diff --git a/sdm/libs/core/display_base.cpp b/sdm/libs/core/display_base.cpp
index f1b15c3e..ffc94af5 100755
--- a/sdm/libs/core/display_base.cpp
+++ b/sdm/libs/core/display_base.cpp
@@ -38,6 +38,7 @@
 
 #include "display_base.h"
 #include "hw_info_interface.h"
+#include <utils/CallStack.h>
 
 #define __CLASS__ "DisplayBase"
 
@@ -545,6 +546,11 @@ DisplayError DisplayBase::SetDisplayState(DisplayState state, bool teardown,
   DLOGI("Set state = %d, display %d-%d, teardown = %d", state, display_id_,
         display_type_, teardown);
 
+  android::CallStack callstack;
+  callstack.update();
+  callstack.dump(1);
+  callstack.log("callstack-tag-display", ANDROID_LOG_DEBUG, "  ");
+
   if (state == state_) {
     DLOGI("Same state transition is requested.");
     return kErrorNone;

 

2、编译 make libsdmcore (Android.mk文件中LOCAL_MODULE                  := libsdmcore)(所用编译工具链为vendor/qcom/proprietary/llvm-arm-toolchain-ship/10.0/bin/clang++)

3、替换库

adb root;adb disable-verity;adb reboot

adb root;adb remount

adb push ./trinket/vendor/lib64/libsdmcore.so vendor/lib64/; adb push ./trinket/vendor/lib/libsdmcore.so vendor/lib

4、重启设备adb reboot,重启后单击电源键,让手机从唤醒进入睡眠状态,程序就能走到栈调用的位置

以下为效果


12-21 00:31:05.474   723  3217 D callstack-tag-display:   #00 pc 0000000000036944  /vendor/lib64/libsdmcore.so (sdm::DisplayBase::SetDisplayState(sdm::DisplayState, bool, int*)+156)
12-21 00:31:05.474   723  3217 D callstack-tag-display:   #01 pc 00000000000414b4  /vendor/lib64/libsdmcore.so (sdm::DisplayBuiltIn::SetDisplayState(sdm::DisplayState, bool, int*)+152)
12-21 00:31:05.474   723  3217 D callstack-tag-display:   #02 pc 000000000004082c  /vendor/lib64/hw/hwcomposer.trinket.so (sdm::HWCDisplay::SetPowerMode(HWC2::PowerMode, bool)+488)
12-21 00:31:05.474   723  3217 D callstack-tag-display:   #03 pc 000000000004a48c  /vendor/lib64/hw/hwcomposer.trinket.so (sdm::HWCDisplayBuiltIn::SetPowerMode(HWC2::PowerMode, bool)+96)
12-21 00:31:05.474   723  3217 D callstack-tag-display:   #04 pc 000000000002cb08  /vendor/lib64/hw/hwcomposer.trinket.so (int sdm::HWCSession::CallDisplayFunction<HWC2::PowerMode, bool>(hwc2_device*, unsigned long, HWC2::Error (sdm::HWCDisplay::*)(HWC2::PowerMode, bool), HWC2::PowerMode, bool)+248)
12-21 00:31:05.474   723  3217 D callstack-tag-display:   #05 pc 000000000002c82c  /vendor/lib64/hw/hwcomposer.trinket.so (sdm::HWCSession::SetPowerMode(hwc2_device*, unsigned long, int)+480)
12-21 00:31:05.474   723  3217 D callstack-tag-display:   #06 pc 00000000000139ac  /vendor/bin/hw/android.hardware.graphics.composer@2.4-service (android::hardware::graphics::composer::V2_2::hal::detail::ComposerClientImpl<android::hardware::graphics::composer::V2_3::IComposerClient, android::hardware::graphics::composer::V2_3::hal::ComposerHal>::setPowerMode_2_2(unsigned long, android::hardware::graphics::composer::V2_2::IComposerClient::PowerMode)+28)
12-21 00:31:05.474   723  3217 D callstack-tag-display:   #07 pc 0000000000026df0  /apex/com.android.vndk.v30/lib64/android.hardware.graphics.composer@2.2.so (android::hardware::graphics::composer::V2_2::BnHwComposerClient::_hidl_setPowerMode_2_2(android::hidl::base::V1_0::BnHwBase*, android::hardware::Parcel const&, android::hardware::Parcel*, std::__1::function<void (android::hardware::Parcel&)>)+208)
12-21 00:31:05.474   723  3217 D callstack-tag-display:   #08 pc 00000000000422c8  /apex/com.android.vndk.v30/lib64/android.hardware.graphics.composer@2.4.so (android::hardware::graphics::composer::V2_4::BnHwComposerClient::onTransact(unsigned int, android::hardware::Parcel const&, android::hardware::Parcel*, unsigned int, std::__1::function<void (android::hardware::Parcel&)>)+2272)
12-21 00:31:05.475   723  3217 D callstack-tag-display:   #09 pc 00000000000941d8  /apex/com.android.vndk.v30/lib64/libhidlbase.so (android::hardware::BHwBinder::transact(unsigned int, android::hardware::Parcel const&, android::hardware::Parcel*, unsigned int, std::__1::function<void (android::hardware::Parcel&)>)+68)
12-21 00:31:05.475   723  3217 D callstack-tag-display:   #10 pc 0000000000098170  /apex/com.android.vndk.v30/lib64/libhidlbase.so (android::hardware::IPCThreadState::getAndExecuteCommand()+1076)
12-21 00:31:05.475   723  3217 D callstack-tag-display:   #11 pc 00000000000993b8  /apex/com.android.vndk.v30/lib64/libhidlbase.so (android::hardware::IPCThreadState::joinThreadPool(bool)+96)
12-21 00:31:05.475   723  3217 D callstack-tag-display:   #12 pc 00000000000a845c  /apex/com.android.vndk.v30/lib64/libhidlbase.so (android::hardware::PoolThread::threadLoop()+24)
12-21 00:31:05.475   723  3217 D callstack-tag-display:   #13 pc 00000000000154cc  /apex/com.android.vndk.v30/lib64/libutils.so (android::Thread::_threadLoop(void*)+260)
12-21 00:31:05.475   723  3217 D callstack-tag-display:   #14 pc 0000000000014d90  /apex/com.android.vndk.v30/lib64/libutils.so (thread_data_t::trampoline(thread_data_t const*)+412)
12-21 00:31:05.475   723  3217 D callstack-tag-display:   #15 pc 00000000000b0048  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+64)
12-21 00:31:05.475   723  3217 D callstack-tag-display:   #16 pc 00000000000503c8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64)
12-21 00:31:05.475     0     0 I         : [<0>][3217, HwBinder:723_3][drm:sde_connector_atomic_set_property:1160] sde connector set power mode = SDE_MODE_DPMS_OFF

 

 

本文地址:https://blog.csdn.net/chen245250566/article/details/111909525