android 使用栈跟踪代码c/cpp

使用栈跟踪代码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

属性查找:
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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android Studio中编写C/C++代码,你需要进行以下步骤: 1. 创建一个新的Android Studio项目。 2. 在你的项目中创建一个新的C/C++源文件。你可以使用菜单File->New->C/C++ Source File来创建源文件。 3. 在你的源文件中编写C/C++代码。 4. 在你的项目中创建一个新的JNI头文件。你可以使用菜单File->New->JNI/C++ Header File来创建头文件。 5. 在你的JNI头文件中声明你的C/C++函数和变量。例如,如果你的C/C++源文件中有一个函数叫做myFunction,你可以在你的JNI头文件中这样声明它: ```c++ JNIEXPORT void JNICALL Java_com_example_myapp_MyClass_myFunction(JNIEnv *env, jobject obj); ``` 请注意,这里的Java_com_example_myapp_MyClass_myFunction是根据你的Java类和函数名称来自动生成的。你需要根据你的实际项目进行修改。 6. 在你的Java代码中,使用JNI接口调用你的C/C++函数。例如,如果你的Java类是MyClass,你可以在它的某个方法中这样调用你的C/C++函数: ```java public void myMethod() { myFunction(); } ``` 7. 在你的项目中配置NDK环境。你需要下载NDK并在你的项目中配置NDK路径。你可以使用菜单File->Project Structure->SDK Location来配置NDK路径。 8. 在你的项目中配置CMake。你需要创建一个CMakeLists.txt文件来告诉Android Studio如何编译你的C/C++代码。你可以使用以下模板来创建CMakeLists.txt文件: ``` cmake_minimum_required(VERSION 3.10.2) project(myproject C CXX) add_library(mylib SHARED mysource.cpp) target_link_libraries(mylib log) ``` 请注意,这里的mysource.cpp和mylib是根据你的实际项目进行修改。 9. 运行你的项目并测试你的C/C++代码。 请注意,以上步骤只是一个基本的示例,你需要根据你的实际项目需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值