Activitymanagerservice.appnotresponding () is called when the program is unresponsive and ANR, and analyzing this function helps to better understand the information printed in the log.
final void Appnotresponding (Processrecord app, Activityrecord activity, Activityrecord parent, Boolean Abov Esystem, final String annotation) {arraylist<integer> firstpids = new arraylist<integer> (5); sparsearray<boolean> lastpids = new sparsearray<boolean> (20); if (Mcontroller! = null) {try {//0 = = Continue,-1 = kill process immediately int res = mcontroller.appearlynotresponding (app.processname, app.pid, annotation); if (Res < 0 && app.pid! = my_pid) process.killprocess (app.pid); } catch (RemoteException e) {mcontroller = null; Watchdog.getinstance (). Setactivitycontroller (NULL); }} Long Anrtime = Systemclock.uptimemillis (); if (monitor_cpu_usage) {updatecpustatsnow (); } synchronized (this) {//Powermanager.reboot () can block for a long time, so Ignore Anrs while shutting down. if (Mshuttingdown) {//If the system is shutting down, ANR does not process, return; SLOG.I (TA G, "During shutdown skipping ANR:" + app + "+ annotation); Return } else if (app.notresponding) {//If the process has been previously reported to be ANR, then the following logic is no longer executed, printing directly "<span style= "Font-family:arial, Helvetica, Sans-serif;" >skipping duplicate ANR ", then Return;</span>" SLOG.I (TAG, "Skipping duplicate ANR:" + app + "" + An notation); Return } else if (app.crashing) {//If the application occurs crash, this time the ANR ignores, return; Slog . I (TAG, "Crashing app skipping ANR:" + app + "+ annotation); Return }//In case we come through-the same app before completing//This one, mark as anring now So we'll bail out. App.notresponding = true; Will <span style= "font-family:arial, Helvetica, Sans-serif;" >notresponding set to True indicates that the ANR is already processing the process, and the ANR direct return;</span>//log the ANR to the event log will be reported next time. Eventlog.writeevent (Eventlogtags.am_anr, App.userid, app.pid,//write AM_ANR information to the event log, search globally , only this one place prints the event ANR log, so if there is this log entry in the event log, it means that the function logic has been entered; App.processname, app.info.flags, annotation) ; Dump thread traces as quickly as we can, starting with "interesting" processes. Firstpids.add (APP.PID); will be out of the ANR process to join <span style= "font-family:arial, Helvetica, Sans-serif;" >firstpids list, subsequent printing of the process trace;</span> int parentpid = app.pid; if (parent! = NULL && Parent.app! = null && parent.app.pid > 0) parentpid = parent.app.pid; The parent process is also added to <span style= "font-family:arial, Helvetica, Sans-serif;" >firsTpids list;</span> if (parentpid! = app.pid) Firstpids.add (parentpid); if (my_pid! = App.pid && my_pid! = parentpid) Firstpids.add (my_pid); Add the System_server process to <span style= "font-family:arial, Helvetica, Sans-serif;" >firstpids list, stating that each ANR trace must include the System_server process trace;</span> for (int i = Mlruprocesses.size ()-1; I >= 0; i--) {Processrecord r = mlruprocesses.get (i); if (r! = NULL && R.thread! = null) {int PID = R.pid; if (PID > 0 && pid! = app.pid && pid! = parentpid && pid! = my_pid) {if (r.persistent) {firstpids.add (PID); The permanent process is also added to <span style= "font-family:arial, Helvetica, Sans-serif;" >firstpids List;</span>} else {lastpids.put (PID, boolean.true); }}}}}//Log the ANR to the main Log. StringBuilder info = new StringBuilder (); Info.setlength (0); Info.append ("ANR in"). Append (App.processname); "ANR in" is also a critical log for analyzing ANR; if (activity! = NULL && Activity.shortcomponentname! = null) {Info.appe nd ("("). Append (Activity.shortcomponentname). Append (")"); } info.append ("\ n"); Info.append ("PID:"). Append (App.pid). Append ("\ n"); if (annotation! = null) {info.append ("Reason:"). Append (annotation). Append ("\ n"); if (parent! = NULL && Parent! = activity) {info.append ("Parent:"). Append (Parent.shortcompone ntname). Append ("\ n"); } final Processcputracker Processcputracker = new Processcputracker (true); File Tracesfile = Dumpstacktraces (True, Firstpids, Processcputracker, Lastpids, native_stacks_of_interest); String cpuInfo = null; if (monitor_cpu_usage) {updatecpustatsnow (); Synchronized (mprocesscputhread) {cpuInfo = Mprocesscputracker.printcurrentstate (anrtime); } info.append (Processcputracker.printcurrentload ()); Info.append (CpuInfo); } info.append (Processcputracker.printcurrentstate (anrtime)); SLOG.E (TAG, info.tostring ()); if (Tracesfile = = null) {//if called <span style= "font-family:arial, Helvetica, Sans-serif; ">dumpstacktraces does not have a survival trace, then re-print only the ANR process trace;</span>//There is no trace file, so dump (just) the all eged culprit ' s threads to the log process.sendsignal (App.pid, process.signal_quit); } adderrortodropbox ("ANR", app, App.processname, activity, parent, annotation,//Add trace files to Dropbox, so only To occur ANR, and in the log there is "ANR in", then there must be a trace log in Dropbox, named "Anr" form; CpuInfo, TRACESFILe, NULL); if (Mcontroller! = null) {try {//0 = = Show Dialog, 1 = Keep waiting,-1 = Kill process Immedi ately int res = mcontroller.appnotresponding (App.processname, App.pid, info.tostring ()); if (res! = 0) {if (res < 0 && app.pid! = my_pid) {Process.killproce SS (App.pid); } else {synchronized (this) {mservices.scheduleservicetimeoutlocked (AP p); }} return; }} catch (RemoteException e) {mcontroller = null; Watchdog.getinstance (). Setactivitycontroller (NULL); }}//Unless configured otherwise, Swallow Anrs in background processes & kill the process. Boolean showbackground = Settings.Secure.getInt (Mcontext.getcontentresolver (), SettiNgs. Secure.anr_show_background, 0)! = 0; Synchronized (this) {if (!showbackground &&!app.isinterestingtouserlocked () && app.pid! = My_ PID) {killunneededprocesslocked (app, "background ANR"); Return }//Set the app ' s notresponding state, and look up the Errorreportreceiver Makeappnotrespondinglock Ed (app, Activity! = null? activity.shortComponentName:null, annotation! = null? "ANR" + Annotation: "ANR", info.tostring ()); Bring up the infamous APP is not responding dialog Message msg = Message.obtain (); hashmap<string, object> map = new hashmap<string, object> (); Msg.what = show_not_responding_msg; Msg.obj = map; Msg.arg1 = Abovesystem? 1:0; Map.put ("App", app); if (activity! = null) {Map.put ("activity", Activity); } mhandler.sendmessage (msg); } }
Appnotresponding () analysis