使用栈跟踪代码cpp

在你的模块的Android.mk添加libutils动态库:
LOCAL_SHARED_LIBRARIES :=

libutils
(2). 在你需要获取native调用栈的位置定义android::CallStack对象,即可将调用栈输出到main log里:

#include <utils/CallStack.h>
android::CallStack stack("zhongyukang");

In android 9.0, you should use libutilscallstack. Look “android/system/core/libutils/Android.bp” for more details.
在这里插入图片描述

LOCAL_SHARED_LIBRARIES:= \
   +libutilscallstack

/* add this code at necessary place /
注意带
android namespace要在前面加#include <utils/CallStack.h> / add this line */,否则不生效
如:

#define calc_time_diff(x,y) ((x.tv_sec - y.tv_sec )+ (double)( x.tv_nsec - y.tv_nsec ) / (double)1000000000)
#include <utils/CallStack.h> /* add this line */

namespace android {


在蓝牙协议栈中,也可用同样的方法来打印堆栈

diff --git a/system/bt/stack/rfcomm/rfc_port_fsm.cc b/system/bt/stack/rfcomm/rfc_port_fsm.cc
index 5157a75..8dec1e3 100644
--- a/system/bt/stack/rfcomm/rfc_port_fsm.cc
+++ b/system/bt/stack/rfcomm/rfc_port_fsm.cc
@@ -33,6 +33,7 @@
 #include "port_int.h"
 #include "rfc_int.h"
 #include "rfcdefs.h"
+#include <utils/CallStack.h>
@@ -335,6 +342,7 @@ void rfc_port_sm_term_wait_sec_check(tPORT* p_port, uint16_t event,
  ******************************************************************************/
 void rfc_port_sm_orig_wait_sec_check(tPORT* p_port, uint16_t event,
                                      void* p_data) {
+  android::CallStack stack("zhongyukang");
   switch (event) {
     case RFC_EVENT_SEC_COMPLETE:
       if (*((uint8_t*)p_data) != BTM_SUCCESS) {

但要带符号的so push到板子上重启复现打印后才有函数名(符号),
这里直接push带symbol的so到对应路径:

find out -iname bluetooth.default.so
out/soong/.intermediates/system/bt/main/bluetooth.default/android_arm_armv7-a-neon_cortex-a7_shared_core/bluetooth.default.so
out/target/product/gwi_dnyb/obj/SHARED_LIBRARIES/bluetooth.default_intermediates/LINKED/bluetooth.default.so
out/target/product/gwi_dnyb/obj/SHARED_LIBRARIES/bluetooth.default_intermediates/bluetooth.default.so
out/target/product/gwi_dnyb/obj/SHARED_LIBRARIES/bluetooth.default_intermediates/PACKED/bluetooth.default.so
out/target/product/gwi_dnyb/system/lib/hw/bluetooth.default.so
out/target/product/gwi_dnyb/symbols/system/lib/hw/bluetooth.default.so

adb push out/target/product/gwi_dnyb/symbols/system/lib/hw/bluetooth.default.so /system/lib/hw/
adb reboot

否则没有符号需要自己用addr2line转换
如:

01-01 01:16:07.884   966  1258 W bt_rfcomm: zhongyukang port event RFC_STATE_CLOSED 9
01-01 01:16:07.884   966  1258 W bt_rfcomm: rfc_port_sm_state_closed, zhongyukang RFC_EVENT_DM, index=8 event 9
01-01 01:16:07.944   966  1258 W bt_rfcomm: zhongyukang port event 15
01-01 01:16:08.028   966  1258 D zhongyukang: #00 pc 00126fed  /system/lib/hw/bluetooth.default.so (rfc_port_sm_execute(tPORT*, unsigned short, void*)+400)
01-01 01:16:08.028   966  1258 D zhongyukang: #01 pc 00129959  /system/lib/hw/bluetooth.default.so (rfc_sec_check_complete(RawAddress const*, unsigned char, void*, unsigned char)+44)
01-01 01:16:08.028   966  1258 D zhongyukang: #02 pc 000f0879  /system/lib/hw/bluetooth.default.so (btm_sec_auth_complete(unsigned short, unsigned char)+984)
01-01 01:16:08.028   966  1258 D zhongyukang: #03 pc 000f45d7  /system/lib/hw/bluetooth.default.so (btu_hci_msg_process(BT_HDR*)+38)
01-01 01:16:08.028   966  1258 D zhongyukang: #04 pc 00083677  /system/lib/libchrome.so (base::debug::TaskAnnotator::RunTask(char const*, base::PendingTask const&)+122)
01-01 01:16:08.028   966  1258 D zhongyukang: #05 pc 0009320f  /system/lib/libchrome.so (base::MessageLoop::RunTask(base::PendingTask const&)+162)
01-01 01:16:08.028   966  1258 D zhongyukang: #06 pc 000933fd  /system/lib/libchrome.so (base::MessageLoop::DeferOrRunPendingTask(base::PendingTask)+18)
01-01 01:16:08.028   966  1258 D zhongyukang: #07 pc 000935d7  /system/lib/libchrome.so (base::MessageLoop::DoWork()+190)
01-01 01:16:08.028   966  1258 D zhongyukang: #08 pc 0009445d  /system/lib/libchrome.so (base::MessagePumpDefault::Run(base::MessagePump::Delegate*)+156)
01-01 01:16:08.028   966  1258 D zhongyukang: #09 pc 000a3449  /system/lib/libchrome.so (base::RunLoop::Run()+40)
01-01 01:16:08.028   966  1258 D zhongyukang: #10 pc 000f46d1  /system/lib/hw/bluetooth.default.so (btu_message_loop_run(void*)+180)
01-01 01:16:08.028   966  1258 D zhongyukang: #11 pc 00142927  /system/lib/hw/bluetooth.default.so (work_queue_read_cb(void*)+50)
01-01 01:16:08.028   966  1258 D zhongyukang: #12 pc 0014125d  /system/lib/hw/bluetooth.default.so (run_reactor(reactor_t*, int)+216)
01-01 01:16:08.028   966  1258 D zhongyukang: #13 pc 00141159  /system/lib/hw/bluetooth.default.so (reactor_start(reactor_t*)+44)
01-01 01:16:08.028   966  1258 D zhongyukang: #14 pc 00142451  /system/lib/hw/bluetooth.default.so (run_thread(void*)+136)
01-01 01:16:08.028   966  1258 D zhongyukang: #15 pc 00047c53  /system/lib/libc.so (__pthread_start(void*)+22)
01-01 01:16:08.028   966  1258 D zhongyukang: #16 pc 0001b117  /system/lib/libc.so (__start_thread+32)
01-01 01:16:08.029   966  1258 E bt_rfcomm: rfc_port_sm_orig_wait_sec_check, RFC_EVENT_SEC_COMPLETE, index=15, result=8
01-01 01:16:08.029   966  1258 W bt_rfcomm: zhongyukang port event 12
01-01 01:16:08.127   966  1258 D zhongyukang: #00 pc 00126fed  /system/lib/hw/bluetooth.default.so (rfc_port_sm_execute(tPORT*, unsigned short, void*)+400)
01-01 01:16:08.127   966  1258 D zhongyukang: #01 pc 00122d07  /system/lib/hw/bluetooth.default.so (RFCOMM_RemoveServer(unsigned short)+138)
01-01 01:16:08.127   966  1258 D zhongyukang: #02 pc 0003de27  /system/lib/hw/bluetooth.default.so (bta_hf_client_mgmt_cback(unsigned int, unsigned short)+230)
01-01 01:16:08.127   966  1258 D zhongyukang: #03 pc 001249f7  /system/lib/hw/bluetooth.default.so (port_rfc_closed(tPORT*, unsigned char)+162)
01-01 01:16:08.127   966  1258 D zhongyukang: #04 pc 00127207  /system/lib/hw/bluetooth.default.so (rfc_port_sm_execute(tPORT*, unsigned short, void*)+938)
01-01 01:16:08.127   966  1258 D zhongyukang: #05 pc 00129959  /system/lib/hw/bluetooth.default.so (rfc_sec_check_complete(RawAddress const*, unsigned char, void*, unsigned char)+44)
01-01 01:16:08.127   966  1258 D zhongyukang: #06 pc 000f0879  /system/lib/hw/bluetooth.default.so (btm_sec_auth_complete(unsigned short, unsigned char)+984)
01-01 01:16:08.127   966  1258 D zhongyukang: #07 pc 000f45d7  /system/lib/hw/bluetooth.default.so (btu_hci_msg_process(BT_HDR*)+38)
01-01 01:16:08.127   966  1258 D zhongyukang: #08 pc 00083677  /system/lib/libchrome.so (base::debug::TaskAnnotator::RunTask(char const*, base::PendingTask const&)+122)
01-01 01:16:08.127   966  1258 D zhongyukang: #09 pc 0009320f  /system/lib/libchrome.so (base::MessageLoop::RunTask(base::PendingTask const&)+162)
01-01 01:16:08.127   966  1258 D zhongyukang: #10 pc 000933fd  /system/lib/libchrome.so (base::MessageLoop::DeferOrRunPendingTask(base::PendingTask)+18)
01-01 01:16:08.127   966  1258 D zhongyukang: #11 pc 000935d7  /system/lib/libchrome.so (base::MessageLoop::DoWork()+190)
01-01 01:16:08.127   966  1258 D zhongyukang: #12 pc 0009445d  /system/lib/libchrome.so (base::MessagePumpDefault::Run(base::MessagePump::Delegate*)+156)
01-01 01:16:08.127   966  1258 D zhongyukang: #13 pc 000a3449  /system/lib/libchrome.so (base::RunLoop::Run()+40)
01-01 01:16:08.127   966  1258 D zhongyukang: #14 pc 000f46d1  /system/lib/hw/bluetooth.default.so (btu_message_loop_run(void*)+180)
01-01 01:16:08.127   966  1258 D zhongyukang: #15 pc 00142927  /system/lib/hw/bluetooth.default.so (work_queue_read_cb(void*)+50)
01-01 01:16:08.127   966  1258 D zhongyukang: #16 pc 0014125d  /system/lib/hw/bluetooth.default.so (run_reactor(reactor_t*, int)+216)
01-01 01:16:08.127   966  1258 D zhongyukang: #17 pc 00141159  /system/lib/hw/bluetooth.default.so (reactor_start(reactor_t*)+44)
01-01 01:16:08.127   966  1258 D zhongyukang: #18 pc 00142451  /system/lib/hw/bluetooth.default.so (run_thread(void*)+136)
01-01 01:16:08.127   966  1258 D zhongyukang: #19 pc 00047c53  /system/lib/libc.so (__pthread_start(void*)+22)
01-01 01:16:08.127   966  1258 D zhongyukang: #20 pc 0001b117  /system/lib/libc.so (__start_thread+32)
01-01 01:16:08.128   966  1258 W bt_rfcomm: rfc_port_sm_orig_wait_sec_check, RFC_EVENT_CLOSE, index=8


且后面的+数字,不知道在哪,还可以用addr2line来查看:
arm-linux-androideabi-addr2line -C -f -e

arm-linux-androideabi-addr2line  -C -f -e  out/target/product/gwi_dnyb/symbols/system/lib/hw/bluetooth.default.so  00129959
rfc_alloc_multiplexer_channel(RawAddress const&, bool)
system/bt/stack/rfcomm/rfc_utils.cc:181

否则不带符号:如

01-01 01:08:54.234   966  1286 W bt_rfcomm: zhongyukang port event RFC_STATE_CLOSED 9
01-01 01:08:54.234   966  1286 W bt_rfcomm: rfc_port_sm_state_closed, zhongyukang RFC_EVENT_DM, index=9 event 9
01-01 01:08:54.293   966  1286 W bt_rfcomm: zhongyukang port event 15
01-01 01:08:54.348   966  1286 D zhongyukang: #00 pc 00126fed  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.348   966  1286 D zhongyukang: #01 pc 00129959  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.348   966  1286 D zhongyukang: #02 pc 000f0879  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.348   966  1286 D zhongyukang: #03 pc 000f45d7  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.348   966  1286 D zhongyukang: #04 pc 00083677  /system/lib/libchrome.so (base::debug::TaskAnnotator::RunTask(char const*, base::PendingTask const&)+122)
01-01 01:08:54.348   966  1286 D zhongyukang: #05 pc 0009320f  /system/lib/libchrome.so (base::MessageLoop::RunTask(base::PendingTask const&)+162)
01-01 01:08:54.348   966  1286 D zhongyukang: #06 pc 000933fd  /system/lib/libchrome.so (base::MessageLoop::DeferOrRunPendingTask(base::PendingTask)+18)
01-01 01:08:54.348   966  1286 D zhongyukang: #07 pc 000935d7  /system/lib/libchrome.so (base::MessageLoop::DoWork()+190)
01-01 01:08:54.348   966  1286 D zhongyukang: #08 pc 0009445d  /system/lib/libchrome.so (base::MessagePumpDefault::Run(base::MessagePump::Delegate*)+156)
01-01 01:08:54.348   966  1286 D zhongyukang: #09 pc 000a3449  /system/lib/libchrome.so (base::RunLoop::Run()+40)
01-01 01:08:54.348   966  1286 D zhongyukang: #10 pc 000f46d1  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.348   966  1286 D zhongyukang: #11 pc 00142927  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.348   966  1286 D zhongyukang: #12 pc 0014125d  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.348   966  1286 D zhongyukang: #13 pc 00141159  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.348   966  1286 D zhongyukang: #14 pc 00142451  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.348   966  1286 D zhongyukang: #15 pc 00047c53  /system/lib/libc.so (__pthread_start(void*)+22)
01-01 01:08:54.348   966  1286 D zhongyukang: #16 pc 0001b117  /system/lib/libc.so (__start_thread+32)
01-01 01:08:54.348   966  1286 E bt_rfcomm: rfc_port_sm_orig_wait_sec_check, RFC_EVENT_SEC_COMPLETE, index=15, result=9
01-01 01:08:54.349   966  1286 W bt_rfcomm: zhongyukang port event 12
01-01 01:08:54.413   966  1286 D zhongyukang: #00 pc 00126fed  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.413   966  1286 D zhongyukang: #01 pc 00122d07  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.413   966  1286 D zhongyukang: #02 pc 0003de27  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.413   966  1286 D zhongyukang: #03 pc 001249f7  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.413   966  1286 D zhongyukang: #04 pc 00127207  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.413   966  1286 D zhongyukang: #05 pc 00129959  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.413   966  1286 D zhongyukang: #06 pc 000f0879  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.414   966  1286 D zhongyukang: #07 pc 000f45d7  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.414   966  1286 D zhongyukang: #08 pc 00083677  /system/lib/libchrome.so (base::debug::TaskAnnotator::RunTask(char const*, base::PendingTask const&)+122)
01-01 01:08:54.414   966  1286 D zhongyukang: #09 pc 0009320f  /system/lib/libchrome.so (base::MessageLoop::RunTask(base::PendingTask const&)+162)
01-01 01:08:54.414   966  1286 D zhongyukang: #10 pc 000933fd  /system/lib/libchrome.so (base::MessageLoop::DeferOrRunPendingTask(base::PendingTask)+18)
01-01 01:08:54.414   966  1286 D zhongyukang: #11 pc 000935d7  /system/lib/libchrome.so (base::MessageLoop::DoWork()+190)
01-01 01:08:54.414   966  1286 D zhongyukang: #12 pc 0009445d  /system/lib/libchrome.so (base::MessagePumpDefault::Run(base::MessagePump::Delegate*)+156)
01-01 01:08:54.414   966  1286 D zhongyukang: #13 pc 000a3449  /system/lib/libchrome.so (base::RunLoop::Run()+40)
01-01 01:08:54.414   966  1286 D zhongyukang: #14 pc 000f46d1  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.414   966  1286 D zhongyukang: #15 pc 00142927  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.414   966  1286 D zhongyukang: #16 pc 0014125d  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.414   966  1286 D zhongyukang: #17 pc 00141159  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.414   966  1286 D zhongyukang: #18 pc 00142451  /system/lib/hw/bluetooth.default.so
01-01 01:08:54.414   966  1286 D zhongyukang: #19 pc 00047c53  /system/lib/libc.so (__pthread_start(void*)+22)
01-01 01:08:54.414   966  1286 D zhongyukang: #20 pc 0001b117  /system/lib/libc.so (__start_thread+32)
01-01 01:08:54.414   966  1286 W bt_rfcomm: rfc_port_sm_orig_wait_sec_check, RFC_EVENT_CLOSE, index=9
01-01 01:08:54.666   966  1286 W bt_rfcomm: zhongyukang port event 9
01-01 01:08:54.667   966  1286 W bt_rfcomm: zhongyukang port event RFC_STATE_CLOSED 9
01-01 01:08:54.667   966  1286 W bt_rfcomm: rfc_port_sm_state_closed, zhongyukang RFC_EVENT_DM, index=10 event 9
01-01 01:08:54.719   966  1286 W bt_rfcomm: zhongyukang port event 15

android 14:

android.bp

    shared_libs: [
        "android.hardware.broadcastradio-V1-ndk",
        "libbase",
        "libbinder_ndk",
        "liblog",
        "libcutils",
       +"libutilscallstack",
    ],

cpp
hardware/interfaces/broadcastradio/aidl/default/BroadcastRadio.cpp

+#include <utils/CallStack.h> /* add this line */
+using namespace android;//因为使用者与定义的库的命令空间不一样,需要指定一下,防止冲突,这是cpp语法要求
namespace aidl::android::hardware::broadcastradio {
+    CallStack stack("zhongyukang");

属性查找:
property_get

Android.mk

diff --git a/hardware/amlogic/camera/Android.mk b/hardware/amlogic/camera/Android.mk
index 822195f493..05fc39674b 100755
--- a/hardware/amlogic/camera/Android.mk
+++ b/hardware/amlogic/camera/Android.mk
@@ -106,7 +106,8 @@ LOCAL_SHARED_LIBRARIES:= \
     libjhead \
     libjpeg \
     libgui \
-    libion
+    libion \
+    libutilscallstack

 LOCAL_CFLAGS := -fno-short-enums -DCOPY_IMAGE_BUFFER

diff --git a/hardware/amlogic/camera/CameraHal.cpp b/hardware/amlogic/camera/CameraHal.cpp
index fe989dfcc7..286c3b3b14 100755
--- a/hardware/amlogic/camera/CameraHal.cpp
+++ b/hardware/amlogic/camera/CameraHal.cpp
@@ -33,6 +33,8 @@
 #include <poll.h>
 #include <math.h>

+#include <utils/CallStack.h>
+
 namespace android {

 static void write_sys_int(const char *path, int val)
@@ -306,6 +308,8 @@ int CameraHal::setParameters(const char* parameters)

     CameraParameters params;

+    android::CallStack stack("zhongyukang");
+
     String8 str_params(parameters);
     params.unflatten(str_params);
     if(mCameraAdapter->getState() != CameraAdapter::VIDEO_STATE&&params.get(ExCameraParameters::KEY_CAP_MODE)!=NULL)

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐