12月 062019
本来一直是在源码中加Log的方法进行调试
但是有些函数很长,if分支也很多,而且还有一些是通过线程或者binder、sendMessage等异步的方式调用的,加上系统编译又很慢,调试起来真是费劲
后来想到程序报错时会输出java的函数栈,我们能不能利用这个呢?可以一直追踪到函数的前几个调用函数。
答案是可以的,我们可以在代码中加入下面的代码,让程序打印调用栈
new Exception().printStackTrace()
加上这个以后,会打印堆栈,但不是抛出异常,这样就很方便的知道函数的调用栈列表了
对于调用Aidl时客户端的堆栈跟踪
假如我们跟踪某段代码后,发现这段代码是通过aidl被调用的,那我们就跟踪不到调用端的代码了,那么我们可以通过throw一个Exception的方式还跟踪。调用端会触发RemoteException而打印出堆栈。
当然,如果客户端try catch了RemoteException这个方式可能就不起作用了
throw new IllegalStateException(“Throw an aidl remote exception.” );
客户端会报如下样式的错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
04-26 07:25:31.473 2072 2072 D AndroidRuntime: Shutting down VM --------- beginning of crash 04-26 07:25:31.474 2072 2072 E AndroidRuntime: FATAL EXCEPTION: main 04-26 07:25:31.474 2072 2072 E AndroidRuntime: Process: com.android.settings:CryptKeeper, PID: 2072 04-26 07:25:31.474 2072 2072 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.settings/com.android.settings.CryptKeeper}: java.lang.IllegalStateException: Throw an aidl remote exception. 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.app.ActivityThread.-wrap11(Unknown Source:0) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6494) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:867) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Throw an aidl remote exception. 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:2021) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1959) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.content.pm.IPackageManager$Stub$Proxy.setComponentEnabledSetting(IPackageManager.java:4732) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.app.ApplicationPackageManager.setComponentEnabledSetting(ApplicationPackageManager.java:2276) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at com.android.settings.CryptKeeper.disableCryptKeeperComponent(CryptKeeper.java:1034) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at com.android.settings.CryptKeeper.onCreate(CryptKeeper.java:405) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:7009) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:7000) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 04-26 07:25:31.474 2072 2072 E AndroidRuntime: ... 9 more |
Sorry, the comment form is closed at this time.