Android ANR日志实战分析指南

                                        Android ANR日志实战分析指南

.概述

     ANR(Application Not responding),是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间能未能得到有效响应或者响应时间过长,都会造成ANR。一般地,这时往往会弹出一个提示框,告知用户当前xxx未响应,用户可选择继续等待或者Force Close。

    触发ANR的过程可分为三个步骤: 埋炸弹, 拆炸弹, 引爆炸弹。对于原理性的东西,我想大家也不是非常愿意花时间去看,如果有兴趣的可以参与大神Gityuan的博客,里面有非常详细的梳理,奉上链接http://gityuan.com/2016/07/02/android-anr/

 

.ANR的类型

  • InputDispatchTimeout(常见)

 input事件在5S内没有处理完成发生了ANR。

 logcat日志关键字:Input event dispatching timed out

  • BroadcastTimeout

前台Broadcast:onReceiver在10S内没有处理完成发生ANR。

后台Broadcast:onReceiver在60s内没有处理完成发生ANR。

logcat日志关键字:Timeout of broadcast BroadcastRecord

  • ServiceTimeout

前台Service:onCreateonStartonBind等生命周期在20s内没有处理完成发生ANR。

后台Service:onCreateonStartonBind等生命周期在200s内没有处理完成发生ANR

logcat日志关键字:Timeout executing service

  • ContentProviderTimeout

ContentProvider 在10S内没有处理完成发生ANR。

logcat日志关键字:timeout publishing content providers

 

 

.ANR出现的原因

  1. 主线程频繁进行耗时的IO操作:如数据库读写
  2. 多线程操作的死锁,主线程被block;
  3. 主线程被Binder 对端block;
  4. System Server中WatchDog出现ANR;
  5. service binder的连接达到上线无法和和System Server通信
  6. 系统资源已耗尽(管道、CPU、IO)

 

.ANR案例分析过程

    下面我将带领大家一步一步分析ANR,这个过程更加理解如何找到问题、分析问题以及解决问题

1.查看events log

    当发生ANR时可以从events log(当然前提是你有将events日志保存了下来)日志中搜索关键字:am_anr,找到出现ANR的时间点、进程PID、ANR类型。日志如下

10-26 09:36:52.873  4514  4622 I am_anr  : [0,11059,com.xxx.anrproject,950582854,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: 8.  Wait queue head age: 5950.0ms.)]

从上面的log我们可以看出: 应用com.xxx.anrproject10-26 09:36:52.873时间,发生了一次InputDispatchTimeout类型的ANR,它的进程号是1480. 把关键的信息整理一下:
ANR时间:10-26 09:36:52.873
进程pid:11059

进程名com.xxx.anrproject
ANR类型InputDispatchTimeout

我们已经知道了发生InputDispatchTimeout的ANR是因为 input事件在5秒内没有处理完成。那么在这个时间10-26 09:36:52.873的前5秒,也就是(09:36:52 ~09:36:53)时间段左右程序到底做了什么事情?这个简单,因为我们已经知道pid了,再搜索一下pid = 11059的日志.这些日志表示该进程所运行的轨迹,关键的日志如下:

10-26 09:36:52.791  4514  4676 I InputDispatcher: Application is not responding: Window{85eac2d u0 com.xxx.anrproject/com.xxx.anrproject.MainActivity}.  It has been 5005.7ms since event, 5005.4ms since wait started.  Reason: 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: 8.  Wait queue head age: 5950.0ms.
10-26 09:36:52.805  4514  4676 I WindowManager: Input event dispatching timed out sending to com.xxx.anrproject/com.xxx.anrproject.MainActivity.  Reason: 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: 8.  Wait queue head age: 5950.0ms.
10-26 09:36:52.973  4514  4622 I Process : Sending signal. PID: 11059 SIG: 3

从上面我们可以知道,在时间 10-26 09:36:52.791程序发生了InputDispatcher: Application is not responding: Window{85eac2d u0 com.xxx.anrproject/com.xxx.anrproject.MainActivity}没有响应,5秒后发生了InputDispatchTimeout的ANR异常。

虽然知道了是怎么开始的,但是具体原因还没有找到,是不是当时CPU很紧张、各路APP再抢占资源? 我们再看看CPU的信息,。搜索关键字关键字: ANR IN

10-26 09:36:52.791  4514  4676 I InputDispatcher: Application is not responding: Window{85eac2d u0 com.xxx.anrproject/com.xxx.anrproject.MainActivity}.  It has been 5005.7ms since event, 5005.4ms since wait started.  Reason: 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: 8.  Wait queue head age: 5950.0ms.
10-26 09:36:52.805  4514  4676 I WindowManager: Input event dispatching timed out sending to com.xxx.anrproject/com.xxx.anrproject.MainActivity.  Reason: 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: 8.  Wait queue head age: 5950.0ms.
10-26 09:36:52.973  4514  4622 I Process : Sending signal. PID: 11059 SIG: 3
10-26 09:36:52.974 11059 11065 I art     : Thread[3,tid=11065,WaitingInMainSignalCatcherLoop,Thread*=0xa1223d00,peer=0x12c2eee0,"Signal Catcher"]: reacting to signal 3
10-26 09:36:52.974 11059 11065 I art     :
10-26 09:36:53.124  4514  4622 I Process : Sending signal. PID: 4514 SIG: 3
10-26 09:36:53.125  4514  4522 I art     : Thread[2,tid=4522,WaitingInMainSignalCatcherLoop,Thread*=0xa1223800,peer=0x12c010d0,"Signal Catcher"]: reacting to signal 3
10-26 09:36:53.125  4514  4522 I art     :
10-26 09:36:53.131 11059 11065 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:53.149  4929  4929 D wpa_supplicant: wlan0: Control interface command 'SIGNAL_POLL'
10-26 09:36:53.163  4929  4929 D wpa_supplicant: CTRL-DEBUG: global_ctrl_sock-sendto: sock=8 sndbuf=163840 outq=0 send_len=48
10-26 09:36:53.411   584  4598 E NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'UID' not found
10-26 09:36:53.716  4514  4926 E WifiHAL : Error polling socket
10-26 09:36:53.935  4514  4622 I Process : Sending signal. PID: 5875 SIG: 3
10-26 09:36:53.936  5875  5885 I art     : Thread[3,tid=5885,WaitingInMainSignalCatcherLoop,Thread*=0xa1223d00,peer=0x12c23310,"Signal Catcher"]: reacting to signal 3
10-26 09:36:53.936  5875  5885 I art     :
10-26 09:36:53.937  4514  4522 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:54.051  4514  4622 I Process : Sending signal. PID: 5862 SIG: 3
10-26 09:36:54.052  5862  5871 I art     : Thread[3,tid=5871,WaitingInMainSignalCatcherLoop,Thread*=0xa1223d00,peer=0x12c240d0,"Signal Catcher"]: reacting to signal 3
10-26 09:36:54.052  5875  5885 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:54.052  5862  5871 I art     :
10-26 09:36:54.180  4514  4622 I Process : Sending signal. PID: 5846 SIG: 3
10-26 09:36:54.180  5846  5854 I art     : Thread[3,tid=5854,WaitingInMainSignalCatcherLoop,Thread*=0xa1223d00,peer=0x12c20dc0,"Signal Catcher"]: reacting to signal 3
10-26 09:36:54.180  5862  5871 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:54.180  5846  5854 I art     :
10-26 09:36:54.307  4514  4622 I Process : Sending signal. PID: 5832 SIG: 3
10-26 09:36:54.307  5846  5854 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:54.308  5832  5840 I art     : Thread[3,tid=5840,WaitingInMainSignalCatcherLoop,Thread*=0xa1223d00,peer=0x12c20b80,"Signal Catcher"]: reacting to signal 3
10-26 09:36:54.308  5832  5840 I art     :
10-26 09:36:54.449  4514  4622 I Process : Sending signal. PID: 5797 SIG: 3
10-26 09:36:54.450  5797  5820 I art     : Thread[3,tid=5820,WaitingInMainSignalCatcherLoop,Thread*=0xa1223d00,peer=0x12c20940,"Signal Catcher"]: reacting to signal 3
10-26 09:36:54.450  5797  5820 I art     :
10-26 09:36:54.450  5832  5840 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:54.588  4514  4622 I Process : Sending signal. PID: 5176 SIG: 3
10-26 09:36:54.588  5797  5820 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:54.588  5176  5208 I art     : Thread[3,tid=5208,WaitingInMainSignalCatcherLoop,Thread*=0xa1223d00,peer=0x12c00f70,"Signal Catcher"]: reacting to signal 3
10-26 09:36:54.589  5176  5208 I art     :
10-26 09:36:54.861  4514  4622 I Process : Sending signal. PID: 5149 SIG: 3
10-26 09:36:54.862  5176  5208 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:54.862  5149  5171 I art     : Thread[3,tid=5171,WaitingInMainSignalCatcherLoop,Thread*=0xa1223d00,peer=0x12c00d30,"Signal Catcher"]: reacting to signal 3
10-26 09:36:54.862  5149  5171 I art     :
10-26 09:36:55.043  4514  4622 I Process : Sending signal. PID: 4966 SIG: 3
10-26 09:36:55.043  5149  5171 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:55.043  4966  4983 I art     : Thread[3,tid=4983,WaitingInMainSignalCatcherLoop,Thread*=0xa1223d00,peer=0x12c00af0,"Signal Catcher"]: reacting to signal 3
10-26 09:36:55.044  4966  4983 I art     :
10-26 09:36:55.154  4966  4983 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:55.155  4514  4622 I Process : Sending signal. PID: 4754 SIG: 3
10-26 09:36:55.155  4754  4766 I art     : Thread[3,tid=4766,WaitingInMainSignalCatcherLoop,Thread*=0xa1223d00,peer=0x12c00670,"Signal Catcher"]: reacting to signal 3
10-26 09:36:55.155  4754  4766 I art     :
10-26 09:36:55.378  4754  4766 I art     : Wrote stack traces to '/data/anr/traces.txt'
10-26 09:36:55.400   250   250 W         : debuggerd: handling request: pid=424 uid=1000 gid=1003 tid=424
10-26 09:36:55.569   250   250 W         : debuggerd: resuming target 424
10-26 09:36:55.571   250   250 W         : debuggerd: handling request: pid=561 uid=1041 gid=1005 tid=561
10-26 09:36:55.716   250   250 W         : debuggerd: resuming target 561
10-26 09:36:55.718   250   250 W         : debuggerd: handling request: pid=562 uid=1047 gid=1005 tid=562
10-26 09:36:55.765   250   250 W         : debuggerd: resuming target 562
10-26 09:36:55.768   250   250 W         : debuggerd: handling request: pid=569 uid=1019 gid=1019 tid=569
10-26 09:36:55.805   250   250 W         : debuggerd: resuming target 569
10-26 09:36:55.807   250   250 W         : debuggerd: handling request: pid=579 uid=1046 gid=1006 tid=579
10-26 09:36:55.863   250   250 W         : debuggerd: resuming target 579
10-26 09:36:55.866   250   250 W         : debuggerd: handling request: pid=580 uid=1013 gid=1031 tid=580
10-26 09:36:55.898   250   250 W         : debuggerd: resuming target 580
10-26 09:36:55.900   250   250 W         : debuggerd: handling request: pid=581 uid=1040 gid=1026 tid=581
10-26 09:36:55.955   250   250 W         : debuggerd: resuming target 581
10-26 09:36:55.957   250   250 W         : debuggerd: handling request: pid=582 uid=1013 gid=1005 tid=582
10-26 09:36:56.019   250   250 W         : debuggerd: resuming target 582
10-26 09:36:56.022   250   250 W         : debuggerd: handling request: pid=4760 uid=1023 gid=1023 tid=4760
10-26 09:36:56.054   250   250 W         : debuggerd: resuming target 4760
10-26 09:36:56.184  4929  4929 D wpa_supplicant: wlan0: Control interface command 'SIGNAL_POLL'
10-26 09:36:56.201  4929  4929 D wpa_supplicant: CTRL-DEBUG: global_ctrl_sock-sendto: sock=8 sndbuf=163840 outq=0 send_len=48
10-26 09:36:57.002  4514  4622 E ActivityManager: ANR in com.xxx.anrproject (com.xxx.anrproject/.MainActivity) (进程名)
10-26 09:36:57.002  4514  4622 E ActivityManager: PID: 11059  (进程PID)
10-26 09:36:57.002  4514  4622 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: 8.  Wait queue head age: 5950.0ms.)
10-26 09:36:57.002  4514  4622 E ActivityManager: Load: 4.21 / 3.79 / 4.33 (Load表明是1分钟,5分钟,15分钟CPU的负载)
10-26 09:36:57.002  4514  4622 E ActivityManager: CPU usage from 87287ms to 0ms ago (2019-10-26 09:35:25.543 to 2019-10-26 09:36:52.830):
10-26 09:36:57.002  4514  4622 E ActivityManager:   7.2% 4514/system_server: 3.5% user + 3.6% kernel / faults: 7651 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   2.5% 424/surfaceflinger: 0.9% user + 1.6% kernel / faults: 635 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   2.2% 4754/com.android.systemui: 1.4% user + 0.8% kernel / faults: 520 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   1.5% 525/xxxservice: 0.2% user + 1.2% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.1% 4723/adbd: 0% user + 0% kernel / faults: 4879 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.5% 240/logd: 0.1% user + 0.3% kernel / faults: 25 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 5922/com.android.launcher3: 0% user + 0% kernel / faults: 3958 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.4% 10890/kworker/0:1: 0% user + 0.4% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.3% 9123/kworker/u8:5: 0% user + 0.3% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.3% 10273/kworker/0:3: 0% user + 0.3% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.2% 4884/VosMCThread: 0% user + 0.2% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.2% 4929/wpa_supplicant: 0.1% user + 0% kernel / faults: 335 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 561/audioserver: 0% user + 0% kernel / faults: 37 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.2% 8916/kworker/u8:2: 0% user + 0.2% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.2% 34/kworker/u9:0: 0% user + 0.2% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.1% 5832/com.xxxdroid.osmanager: 0.1% user + 0% kernel / faults: 425 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.1% 168/mmcqd/0: 0% user + 0.1% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 200/kworker/1:4: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.1% 10931/kworker/u8:1: 0% user + 0.1% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 5724/core_ctl/0: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.1% 6865/.client: 0% user + 0% kernel / faults: 958 minor 11 major
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 10041/kworker/3:3: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0.1% 10952/mdss_fb0: 0% user + 0.1% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 7/rcu_preempt: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 1024/lowi-server: 0% user + 0% kernel / faults: 102 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 4885/VosTXThread: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 3/ksoftirqd/0: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 672/xxx_logcat: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 492/ptt_socket_app: 0% user + 0% kernel / faults: 2036 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 548/xxxdroid_logservice: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 4886/VosRXThread: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 6746/com.android.defcontainer: 0% user + 0% kernel / faults: 392 minor 25 major
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 10/migration/0: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 11/migration/1: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 10947/kworker/u8:3: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 6/kworker/u8:0: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 15/migration/2: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 19/migration/3: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 64/system: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 104/kswapd0: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 160/cfinteractive: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 1026/xtwifi-client: 0% user + 0% kernel / faults: 26 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 5726/irq/215-408000.: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 5862/com.xxxdroid.verify: 0% user + 0% kernel / faults: 86 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 6723/kworker/2:3: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 1//init: 0% user + 0% kernel / faults: 3 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 16/ksoftirqd/2: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 135/hwrng: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 230/jbd2/mmcblk0p40: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 422/lmkd: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 423/servicemanager: 0% user + 0% kernel / faults: 52 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 524/zygote: 0% user + 0% kernel / faults: 150 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 570/installd: 0% user + 0% kernel / faults: 97 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 5149/.dataservices: 0% user + 0% kernel / faults: 11 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 5176/com.android.phone: 0% user + 0% kernel / faults: 28 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 5900/perfd: 0% user + 0% kernel / faults: 11 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 6788/org.codeaurora.gallery: 0% user + 0% kernel / faults: 146 minor 62 major
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 2/kthreadd: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 9/rcu_sched: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 12/ksoftirqd/1: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 20/ksoftirqd/3: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 22/kworker/3:0H: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 228/kworker/2:1H: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 489/cnd: 0% user + 0% kernel / faults: 7 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 509/thermal-engine: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 588/rild: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 593/cnss-daemon: 0% user + 0% kernel
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 594/loc_launcher: 0% user + 0% kernel / faults: 101 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 678/qmuxd: 0% user + 0% kernel / faults: 12 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 4760/sdcard: 0% user + 0% kernel / faults: 6 minor
10-26 09:36:57.002  4514  4622 E ActivityManager:   0% 4869/VosWDThrea

我已经在log 中标志了相关的含义。com.xxx.anrproject 占用了极少数的CPU,完全没有体现出来,其实这并不算多。现在的手机基本都是多核CPU。假如你的CPU是4核,那么上限是400%,以此类推。

既然不是CPU负载的原因,那么到底是什么原因呢? 这时就要看我们的终极大杀器——traces.txt。

 

2.traces.txt 日志分析

    当APP不响应、响应慢了、或者WatchDog的监视没有得到回应时,系统就会dump出一个traces.txt文件,存放在文件目录:/data/anr/traces.txt,通过traces文件,我们可以拿到线程名、堆栈信息、线程当前状态、binder call等信息。
通过adb命令拿到该文件:adb pull /data/anr/traces.txt
trace: Cmd line:com.xxx.anrproject

 

​
"main" prio=5 tid=1 Sleeping
  | group="main" sCount=1 dsCount=0 obj=0x7392e730 self=0xacf84400
  | sysTid=11059 nice=-10 cgrp=default sched=0/0 handle=0xb047752c
  | state=S schedstat=( 310040005 80960887 319 ) utm=20 stm=11 core=0 HZ=100
  | stack=0xbe61f000-0xbe621000 stackSize=8MB
  | held mutexes=
  at java.lang.Thread.sleep!(Native method)
  - sleeping on <0x0eaa6bed> (a java.lang.Object)
  at java.lang.Thread.sleep(Thread.java:371)
  - locked <0x0eaa6bed> (a java.lang.Object)
  at java.lang.Thread.sleep(Thread.java:313)
  at com.xxx.anrproject.MainActivity.FUN1(MainActivity.java:33)
  at com.xxx.anrproject.MainActivity.access$0(MainActivity.java:30)
  at com.xxx.anrproject.MainActivity$1.onClick(MainActivity.java:24)
  at android.view.View.performClick(View.java:5637)
  at android.view.View$PerformClick.run(View.java:22433)
  at android.os.Handler.handleCallback(Handler.java:751)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6121)
  at java.lang.reflect.Method.invoke!(Native method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

​

我详细解析一下traces.txt里面的一些字段,看看它到底能给我们提供什么信息.
main:main标识是主线程,如果是线程,那么命名成“Thread-X”的格式,x表示线程id,逐步递增。
prio:线程优先级,默认是5
tid:tid不是线程的id,是线程唯一标识ID
group:是线程组名称
sCount:该线程被挂起的次数
dsCount:是线程被调试器挂起的次数
obj:对象地址
self:该线程Native的地址
sysTid:是线程号(主线程的线程号和进程号相同)
nice:是线程的调度优先级
sched:分别标志了线程的调度策略和优先级
cgrp:调度归属组
handle:线程处理函数的地址。
state:是调度状态
schedstat:从 /proc/[pid]/task/[tid]/schedstat读出,三个值分别表示线程在cpu上执行的时间、线程的等待时间和线程执行的时间片长度,不支持这项信息的三个值都是0;
utm:是线程用户态下使用的时间值(单位是jiffies)
stm:是内核态下的调度时间值
core:是最后执行这个线程的cpu核的序号。

Java的堆栈信息是我们最关心的,它能够定位到具体位置。从上面的traces,我们可以判断at com.xxx.anrproject.MainActivity.FUN1(MainActivity.java:33) 导致了com.xxx.anrproject发生了ANR。这时候可以对着源码查看,找到出问题,并且解决它。

总结一下这分析流程:首先我们搜索am_anr,找到出现ANR的时间点、进程PID、ANR类型、然后再找搜索PID,找前5秒左右的日志。过滤ANR IN 查看CPU信息,接着查看traces.txt,找到java的堆栈信息定位代码位置,最后查看源码,分析与解决问题。这个过程基本能找到发生ANR的来龙去脉。

 

.ANR案例分析

1.主线程被其它线程lock,导致死锁

waiting on <0x1cd570> (a android.os.MessageQueue)

DALVIK THREADS:
"main" prio=5 tid=3 TIMED_WAIT
  | group="main" sCount=1 dsCount=0 s=0 obj=0x400143a8
  | sysTid=691 nice=0 sched=0/0 handle=-1091117924
  at java.lang.Object.wait(Native Method)
  - waiting on <0x1cd570> (a android.os.MessageQueue)
  at java.lang.Object.wait(Object.java:195)
  at android.os.MessageQueue.next(MessageQueue.java:144)
  at android.os.Looper.loop(Looper.java:110)
  at android.app.ActivityThread.main(ActivityThread.java:3742)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
  at dalvik.system.NativeStart.main(Native Method)

"Binder Thread #3" prio=5 tid=15 NATIVE
  | group="main" sCount=1 dsCount=0 s=0 obj=0x434e7758
  | sysTid=734 nice=0 sched=0/0 handle=1733632
  at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #2" prio=5 tid=13 NATIVE
  | group="main" sCount=1 dsCount=0 s=0 obj=0x1cd570
  | sysTid=696 nice=0 sched=0/0 handle=1369840
  at dalvik.system.NativeStart.run(Native Method)

"Binder Thread #1" prio=5 tid=11 NATIVE
  | group="main" sCount=1 dsCount=0 s=0 obj=0x433aca10
  | sysTid=695 nice=0 sched=0/0 handle=1367448
  at dalvik.system.NativeStart.run(Native Method)

----- end 691 -----

2.主线程做耗时的操作:比如数据库读写

"main" prio=5 tid=1 Native
held mutexes=
kernel: (couldn't read /proc/self/task/11003/stack)
native: #00 pc 000492a4 /system/lib/libc.so (nanosleep+12)
native: #01 pc 0002dc21 /system/lib/libc.so (usleep+52)
native: #02 pc 00009cab /system/lib/libsqlite.so (???)
native: #03 pc 00011119 /system/lib/libsqlite.so (???)
native: #04 pc 00016455 /system/lib/libsqlite.so (???)
native: #16 pc 0000fa29 /system/lib/libsqlite.so (???)
native: #17 pc 0000fad7 /system/lib/libsqlite.so (sqlite3_prepare16_v2+14)
native: #18 pc 0007f671 /system/lib/libandroid_runtime.so (???)
native: #19 pc 002b4721 /system/framework/arm/boot-framework.oat (Java_android_database_sqlite_SQLiteConnection_nativePrepareStatement__JLjava_lang_String_2+116)
at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:294)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
locked <0x0db193bf> (a java.lang.Object)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:690)
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:299)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
locked <0x045a4a8c> (a com.xxxx.video.common.data.DataBaseHelper)
at com.xxxx.video.common.data.DataBaseORM.<init>(DataBaseORM.java:46)
at com.xxxx.video.common.data.DataBaseORM.getInstance(DataBaseORM.java:53)
locked <0x017095d5> (a java.lang.Class<com.xxxx.video.common.data.DataBaseORM>)

 

3.BroadcastTimeout,广播里面做耗时操作

"main" prio=5 tid=1 Sleeping
  | group="main" sCount=1 dsCount=0 obj=0x7392e730 self=0xacf84400
  | sysTid=12158 nice=-10 cgrp=default sched=0/0 handle=0xb047752c
  | state=S schedstat=( 278856989 47585413 287 ) utm=17 stm=10 core=2 HZ=100
  | stack=0xbe61f000-0xbe621000 stackSize=8MB
  | held mutexes=
  at java.lang.Thread.sleep!(Native method)
  - sleeping on <0x0eaa6bed> (a java.lang.Object)
  at java.lang.Thread.sleep(Thread.java:371)
  - locked <0x0eaa6bed> (a java.lang.Object)
  at java.lang.Thread.sleep(Thread.java:313)
  at com.xxx.anrproject.ANRBroadCast.FUN1(ANRBroadCast.java:17)
  at com.xxx.anrproject.ANRBroadCast.onReceive(ANRBroadCast.java:12)
  at android.app.ActivityThread.handleReceiver(ActivityThread.java:3041)
  at android.app.ActivityThread.-wrap18(ActivityThread.java:-1)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1562)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6121)
  at java.lang.reflect.Method.invoke!(Native method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

4.System Server中WatchDog出现ANR

    如果出现这种情况的ANR,这种一般在日志里面查找held by关键词。本案例可以见附件的文件,里面更加详细。

"Binder:1441_1D" prio=5 tid=122 Blocked
  | group="main" sCount=1 dsCount=0 obj=0x13f84e50 self=0x87a42900
  | sysTid=7317 nice=-2 cgrp=default sched=0/0 handle=0x849f6920
  | state=S schedstat=( 523254409041 294353783250 2408164 ) utm=38176 stm=14149 core=3 HZ=100
  | stack=0x848fa000-0x848fc000 stackSize=1014KB
  | held mutexes=
  at com.android.server.power.PowerManagerService.setScreenBrightnessOverrideFromWindowManagerInternal(PowerManagerService.java:2777)
  - waiting to lock <0x0af04ad6> (a java.lang.Object) held by thread 20
  at com.android.server.power.PowerManagerService.-wrap28(PowerManagerService.java:-1)
  at com.android.server.power.PowerManagerService$LocalService.setScreenBrightnessOverrideFromWindowManager(PowerManagerService.java:3879)
  at com.android.server.wm.WindowSurfacePlacer.performSurfacePlacementInner(WindowSurfacePlacer.java:492)
  at com.android.server.wm.WindowSurfacePlacer.performSurfacePlacementLoop(WindowSurfacePlacer.java:236)
  at com.android.server.wm.WindowSurfacePlacer.performSurfacePlacement(WindowSurfacePlacer.java:184)
  at com.android.server.wm.WindowManagerService.moveStackWindowsLocked(WindowManagerService.java:5067)
  at com.android.server.wm.WindowManagerService.moveTaskToTop(WindowManagerService.java:5096)
  - locked <0x0c05deae> (a java.util.HashMap)
  at com.android.server.am.ActivityStack.moveToFront(ActivityStack.java:729)
  at com.android.server.am.ActivityStackSupervisor.moveActivityStackToFront(ActivityStackSupervisor.java:1615)
  at com.android.server.am.ActivityManagerService.setFocusedActivityLocked(ActivityManagerService.java:3105)
  at com.android.server.am.ActivityStack.adjustFocusedActivityLocked(ActivityStack.java:3267)
  at com.android.server.am.ActivityStack.finishActivityLocked(ActivityStack.java:3540)
  at com.android.server.am.ActivityStack.requestFinishActivityLocked(ActivityStack.java:3364)
  at com.android.server.am.ActivityManagerService.finishActivity(ActivityManagerService.java:5023)
  - locked <0x00ed044f> (a com.android.server.am.ActivityManagerService)
  at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:382)
  at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2859)
  at android.os.Binder.execTransact(Binder.java:565)

"ActivityManager" prio=5 tid=11 Blocked
  | group="main" sCount=1 dsCount=0 obj=0x12c1aec0 self=0xa8f05800
  | sysTid=1480 nice=-2 cgrp=default sched=0/0 handle=0xa1879920
  | state=S schedstat=( 554591757112 206716799738 965453 ) utm=25815 stm=29644 core=1 HZ=100
  | stack=0xa1777000-0xa1779000 stackSize=1038KB
  | held mutexes=
  at com.android.server.am.ActivityManagerService$MainHandler.handleMessage(ActivityManagerService.java:1986)
  - waiting to lock <0x00ed044f> (a com.android.server.am.ActivityManagerService) held by thread 122
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.os.HandlerThread.run(HandlerThread.java:61)
  at com.android.server.ServiceThread.run(ServiceThread.java:46)

    从日志可以看出7317 进程持有锁0x0af04ad6,然后其它进程在等待进程7317释放然后导致的死锁ANR错误

 

总结

     如上的文章借鉴了博客https://juejin.im/post/5be698d4e51d452acb74ea4c的一些信息,并加上了一些实际开发中遇到的ANR问题,希望可以给各位遇到ANR问题的解决提供一定的思路。如果大家想要更加一步的了解,那么强烈推荐Gityuan的博客,虽然人家已经远离Android步入了Fluter的海洋。

     最后老规矩,奉上全部ANR日志的汇总ANR各种日志大汇总