非android平台的用户空间C 调用栈打印,因为android 平台使用的是bionic,不是glibc,所以android 平台不能使用:
#include <execinfo.h>
int MAX_CALLSTACK_DEPTH = 32;
void *traceback[MAX_CALLSTACK_DEPTH];
int depth = backtrace(traceback, MAX_CALLSTACK_DEPTH);
int i = 0;
char **stackinfo_string = backtrace_symbols(traceback,depth);
for (i = 0; i < depth; i++){
printf("cyx %p %s \n\r", traceback[i],stackinfo_string[i]);
}
下面转载自 https://blog.csdn.net/u010164190/article/details/78674905
android平台
- 1.Java中:
- Log.e(“dump_test”,Log.getStackTraceString(new Throwable()));
- Log.d("dump","xxxxxxxxxxxxxxxx",new Throwable());
- 2.C++中:
- <1>.test.cpp
- #include <utils/Log.h>
- #include <utils/CallStack.h>
- void dumping_callstack(){
- android::CallStack stack;
- //getpid()和gettid()效果一样
- //stack.update(2,getpid());
- //stack.update(2,gettid());
- stack.update();
- //输出到printf
- stack.dump(1);
- //输出到logcat
- stack.log("dump_test");
- //可以设置第2、3个参数
- //stack.log("Dumping Stack",ANDROID_LOG_ERROR ,"123 ");
- }
- void func1(){
- dumping_callstack();
- }
- void func2(){
- func1();
- }
- void func3(){
- func2();
- }
- int main(){
- ALOGE("main_test------------------>");
- func3();
- }
- <2>.Android.mk
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_SRC_FILES := test.cpp
- LOCAL_MODULE_TAGS := optional
- LOCAL_MODULE := test
- LOCAL_SHARED_LIBRARIES += libcutils libutils
- include $(BUILD_EXECUTABLE)
- 3.C中:
- <1>.创建callstack.cpp 或者直接在system/core/libutils/CallStack.cpp 里添加定义
- #include <utils/CallStack.h>
- extern "C" void dumping_callstack();
- void dumping_callstack(){
- android::CallStack stack;
- stack.update();
- stack.log(“dump_test“);
- }
- namespace android { 。。。。
- <2>.创建callstack.h //这一步可省略, 调用的地方直接 extern void dumping_callstack();即可
- void dumping_callstack();
- <3>.测试test.c
- #include "callstack.h"
- static ssize_t out_write(){
- dumping_callstack();
- }
- <4>.Anroid.mk中添加到编译选项:callstack.cpp及库
- LOCAL_SHARED_LIBRARIES := libcutils libutils
- LOCAL_SRC_FILES := callstack.cpp
- 4.Kernel中:
- #include <asm/ptrace.h>
- printk(KERN_ERR "dump_stack start: %s() %d \n",__FUNCTION__,__LINE__);
- dump_stack();
- .......
- printk(KERN_ERR "dump_stack stop: %s() %d \n",__FUNCTION__,__LINE__);
- 根据dump stack的log位置加printk()。
- 测试:
- # adb logcat | grep dump_test