案例:whatsapp输入框内一直输入表情符号导致ANR
1、在misc\anr文件中找的arn堆栈log信息
suspend all histogram: Sum: 180.110ms 99% C.I. 20.680us-8069.119us Avg: 874.320us Max: 140167us
DALVIK THREADS (69):
"main" prio=5 tid=1 Runnable
| group="main" sCount=0 dsCount=0 obj=0x73cfa448 self=0xb7660290
| sysTid=9653 nice=0 cgrp=default sched=0/0 handle=0xb6f2fc00
| state=R schedstat=( 0 0 0 ) utm=9323 stm=358 core=3 HZ=100
| stack=0xbe427000-0xbe429000 stackSize=8MB
| held mutexes= "mutator lock"(shared held)
at android.text.TextUtils.lastIndexOf(TextUtils.java:181)
at android.text.TextUtils.lastIndexOf(TextUtils.java:154)
at android.text.DynamicLayout.reflow(DynamicLayout.java:202)
at android.text.DynamicLayout.-wrap0(DynamicLayout.java:-1)
at android.text.DynamicLayout$ChangeWatcher.reflow(DynamicLayout.java:662)
at android.text.DynamicLayout$ChangeWatcher.onSpanAdded(DynamicLayout.java:681)
at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:1053)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:759)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:658)
at com.whatsapp.f.b.a(EmojiFormatter.java:303)
at com.whatsapp.util.ba.a(StringUtils.java:151)
at com.whatsapp.Conversation$30.afterTextChanged(Conversation.java:747)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8003)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10166)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1043)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:560)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:492)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:491)
at com.whatsapp.Conversation$20.a(Conversation.java:3081)
at com.whatsapp.EmojiPicker.a(EmojiPicker.java:603)
at com.whatsapp.nl.onClick(unavailable:1484)
at android.view.View.performClick(View.java:5209)
at android.view.View$PerformClick.run(View.java:21166)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5452)
at java.lang.reflect.Method.invoke!(Native method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)
其中tid=1表示主线程提示信息是Runnable,表示主线程没有阻塞
其中sysTid=9653是此应用的进程ID.
红色部分为应用导致ANR具体位置。
2、events.log中搜索9653或者am_anr找到ANR原因
05-10 11:28:18.454 1384 1448 I am_anr : [0,9653,com.whatsapp,954760772,Input dispatching timed out
3、system.log中查看CPU使用情况(关键字CUP usage from )
05-10 11:28:24.800 1384 1448 E ActivityManager:
ANR in com.whatsapp (com.whatsapp/.Conversation)
05-10 11:28:24.800 1384 1448 E ActivityManager: PID: 9653
05-10 11:28:24.800 1384 1448 E ActivityManager: Reason: Input dispatching timed out (Waiting to send non-key event because the touched window has not finished processing certain input events that were delivered to it over 500.0ms ago. Wait queue length: 2. Wait queue head age: 8027.7ms.)
05-10 11:28:24.800 1384 1448 E ActivityManager: Load: 6.74 / 6.39 / 5.29
CPU usage from 5586ms to 6137ms later:
05-10 11:28:24.800 1384 1448 E ActivityManager:
99% 9653/com.whatsapp: 99% user + 0% kernel / faults: 32 minor
05-10 11:28:24.800 1384 1448 E ActivityManager: 99% 9653/com.whatsapp: 99% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 14% 9308/com.duapps.recorder: 12% user + 1.8% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 5.4% 9640/gle.aac.encoder: 5.4% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 3.6% 9639/MediaCodec_loop: 3.6% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 9635/MediaCodec_loop: 0% user + 1.8% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 9643/ScreenCaptureTa: 1.8% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 9644/audioencode out: 1.8% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 9645/audioencode in : 1.8% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 9650/MPEG4Writer: 0% user + 1.8% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 12% 230/mediaserver: 12% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 7.2% 9619/AudioIn_20: 7.2% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 1507/AudioOut_2: 0% user + 1.8% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 9637/rd.h264.encoder: 1.8% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 9.1% 1384/system_server: 1.8% user + 7.2% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 7.2% 1448/ActivityManager: 0% user + 7.2% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 2296/Binder_C: 0% user + 1.8% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 4627/Binder_F: 1.8% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 7.2% 235/memsicp: 0% user + 7.2% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 5.4% 85/focal-wait-queu: 0% user + 5.4% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.1% 86/sensor_report_e: 0% user + 1.1% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.1% 88/watchdog_feeder: 0% user + 1.1% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 174/logd: 0% user + 1.8% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.8% 8159/logd.reader.per: 0% user + 1.8% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.2% 205/servicemanager: 0% user + 1.2% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.4% 1624/com.android.systemui: 1.4% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.4% 2186/sdcard: 0% user + 1.4% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.5% 8034/slog: 1.5% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 1.5% 8115/slog: 1.5% user + 0% kernel
05-10 11:28:24.800 1384 1448 E ActivityManager: 39% TOTAL: 33% user + 5.5% kernel
红色部分显示应用的耗时操作导致cpu使率用很高。
4、查看的log分析文档中表示要查看trach.txt中的堆栈信息(此文件信息和misc\anr中的堆栈信息log相同)
第一步,先pull出/data/anr/traces.txt文件。
第二步,查看app被杀死时的进程id,你这里我看是7889。
第三步,查看traces.txt里关于该进程id的信息。
5、我试着反编译whatsapp这个应用看看具体的代码,但是他的加密做的非常好,看不到log中打印类的具体内容。