(1)Java中添加
Java中添加callstack常见以下几种方式。
import android.util.Log;
Exception e = new Exception("this is a log");
e.printStackTrace();
Thread.currentThread().getStackTrace();
Log.e("dump_test" , Log.getStackTraceString(new Throwable()));
(2)C++中添加
(A)Mtk Camera Hal中有自定义的stack
namespace NSCam {
namespace Utils {
void dumpCallStack(char const* prefix = 0);
bool saveBufToFile(char const*const fname, unsigned char*const buf, unsigned int const size);
unsigned int loadFileToBuf(char const*const fname, unsigned char*const buf, unsigned int const capacity);
}
}
NSCam::Utils::dumpCallStack(__FUNCTION__);
NSCam::Utils::saveBufToFile(filename,
(unsigned char*)jpg.mOutputJpegBuffer->getBufVA(0),
jpg.mOutputJpegBuffer->getBufSizeInBytes(0));
打印示例
11-15 15:37:23.082 748 6053 I MtkCam/Utils/encodeMainJpeg: pid(748) tid(6053) Cam@Jpeg
11-15 15:37:23.082 748 6053 I MtkCam/Utils/encodeMainJpeg: #00 pc 0000000000003040 /vendor/lib64/libladder.so (UnwindCurThreadBT+120)
11-15 15:37:23.082 748 6053 I MtkCam/Utils/encodeMainJpeg: #01 pc 000000000000e4c4 /vendor/lib64/libmtkcam_stdutils.so (NSCam::Utils::dumpCallStack(char const*)+52)
11-15 15:37:23.082 748 6053 I MtkCam/Utils/encodeMainJpeg: #02 pc 00000000001702ec /vendor/lib64/libmtkcam_hwnode.so (JpegNodeImp::encodeMainJpeg(android::sp<encode_frame>&)+44)
11-15 15:37:23.082 748 6053 I MtkCam/Utils/encodeMainJpeg: #03 pc 000000000016ceb4 /vendor/lib64/libmtkcam_hwnode.so (onProcessRequest(JpegNodeImp&, Request&)+788)
11-15 15:37:23.082 748 6053 I MtkCam/Utils/encodeMainJpeg: #04 pc 000000000017f064 /vendor/lib64/libmtkcam_hwnode.so (void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void (*)(JpegNodeImp&, Request&), std::__1::reference_wrapper<JpegNodeImp>, std::__1::reference_wrapper<Request> > >(void*)+44)
11-15 15:37:23.082 748 6053 I MtkCam/Utils/encodeMainJpeg: #05 pc 00000000000b0bd8 /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+64)
11-15 15:37:23.082 748 6053 I MtkCam/Utils/encodeMainJpeg: #06 pc 00000000000505d0 /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64)
(B)C++中通用添加
(a)在Android.bp / Android.mk 中添加 libutilscallstack
shared_libs: [
"libutilscallstack",
]
LOCAL_SHARED_LIBRARIES:= libutilscallstack
LOCAL_JNI_SHARED_LIBRARIES := libutilscallstack
(b)在C++源码文件预编译 include 中添加
#include <utils/CallStack.h>
(c)添加代码实现
#include <utils/CallStack.h>
using android::CallStack;
CallStack stack;
stack.update();
stack.log(__FUNCTION__);
(d)系统库的源码位置
class CallStack {
public:
CallStack();
~CallStack();
void clear() { mFrameLines.clear(); }
void update(int32_t ignoreDepth = 1, pid_t tid = BACKTRACE_CURRENT_THREAD);
void log(const char* logtag,
android_LogPriority priority = ANDROID_LOG_DEBUG,
const char* prefix = nullptr) const;
void dump(int fd, int indent = 0, const char* prefix = nullptr) const;
String8 toString(const char* prefix = nullptr) const;
void print(Printer& printer) const;
}
(e)打印示例
11-15 15:37:23.394 748 6053 D encodeMainJpeg: #00 pc 00000000001703d4 /vendor/lib64/libmtkcam_hwnode.so (JpegNodeImp::encodeMainJpeg(android::sp<encode_frame>&)+276)
11-15 15:37:23.394 748 6053 D encodeMainJpeg: #01 pc 000000000016ceb4 /vendor/lib64/libmtkcam_hwnode.so (onProcessRequest(JpegNodeImp&, Request&)+788)
11-15 15:37:23.394 748 6053 D encodeMainJpeg: #02 pc 000000000017f064 /vendor/lib64/libmtkcam_hwnode.so (void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void (*)(JpegNodeImp&, Request&), std::__1::reference_wrapper<JpegNodeImp>, std::__1::reference_wrapper<Request> > >(void*)+44)
11-15 15:37:23.394 748 6053 D encodeMainJpeg: #03 pc 00000000000b0bd8 /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+64)
11-15 15:37:23.394 748 6053 D encodeMainJpeg: #04 pc 00000000000505d0 /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64)
(3)Kernel中添加
Kernel中较简单,直接有现成的函数可以使用
dump_stack()
WARN_ON(x)
|