Android Trace

代码调试方法

Posted by LXG on January 17, 2020

Android打印Trace堆栈-Gityuan

当前线程Trace

Java


Thread.currentThread().dumpStack();   //方法1

android.util.Log.d(TAG,"Gityuan", new RuntimeException("Gityuan")); //方法2

android.util.Log.d(TAG, android.util.Log.getStackTraceString(new Throwable())); // 方法3

new RuntimeException("Gityuan").printStackTrace(); //方法4

Native


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

目标进程Trace

Java


adb shell kill -3 [pid]     //方法1
Process.sendSignal(pid, Process.SIGNAL_QUIT)  //方法2

Native


adb shell debuggerd -b [tid] //方法1
Debug.dumpNativeBacktraceToFile(pid, tracesPath) //方法2

Kernel


adb shell cat /proc/[tid]/stack  //方法1
WatchDog.dumpKernelStackTraces() //方法2

小结

以下分别列举输出Java, Native, Kernel的调用栈方式:

类别 函数式 命令式
Java Process.sendSignal(pid, Process.SIGNAL_QUIT) kill -3 [pid]
Native Debug.dumpNativeBacktraceToFile(pid, tracesPath) debuggerd -b [pid]
Kernel WD.dumpKernelStackTraces() cat /proc/[tid]/stack