Android debug小技巧

Android C++打印函数调用栈

1、在Androd.mk文件中增加链接库

LOCAL_SHARED_LIBRARIES := \
	libutilscallstack \

  或Android.bp的shared_libs中补上

shared_libs: [
    ....
    "libutilscallstack",
],

2、在对应cpp文件中修改代码

#include <utils/CallStack.h>
 ...
android::CallStack stack;  
stack.update();
stack.log(LOG_TAG, ANDROID_LOG_ERROR, "stackdump:"); 

 

Android 系统服务中打印调用者进程ID

#include <binder/IPCThreadState.h>

IPCThreadState* ipc = IPCThreadState::self();
const int pid = ipc->getCallingPid();
const int uid = ipc->getCallingUid();
ALOGD("pid=%d, uid=%d", pid, uid);

 

Android Java程序中打印堆栈调用信息

Thread.dumpStack();

 

Android  HIDL HAL 服务中打印调用者进程ID

直接引入头文件

#include <hwbinder/IPCThreadState.h>

 

然后

int pid = android::hardware::IPCThreadState::self()->getCallingPid();
int uid = android::hardware::IPCThreadState::self()->getCallingUid();
就可以获调用者的PID/UID

 

posted on 2021-11-24 10:00  二的次方  阅读(1198)  评论(1编辑  收藏  举报