AppErrors.java revision b1cdb10e7c6c1c41c5a421d996f3bbee581dff36
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server.am;
18
19import com.android.internal.app.ProcessMap;
20import com.android.internal.logging.MetricsLogger;
21import com.android.internal.logging.nano.MetricsProto;
22import com.android.internal.os.ProcessCpuTracker;
23import com.android.server.RescueParty;
24import com.android.server.Watchdog;
25
26import android.app.ActivityManager;
27import android.app.ActivityOptions;
28import android.app.ActivityThread;
29import android.app.AppOpsManager;
30import android.app.ApplicationErrorReport;
31import android.app.Dialog;
32import android.content.ActivityNotFoundException;
33import android.content.Context;
34import android.content.Intent;
35import android.content.pm.ApplicationInfo;
36import android.os.Binder;
37import android.os.Message;
38import android.os.Process;
39import android.os.RemoteException;
40import android.os.SystemClock;
41import android.os.SystemProperties;
42import android.os.UserHandle;
43import android.provider.Settings;
44import android.util.ArrayMap;
45import android.util.ArraySet;
46import android.util.EventLog;
47import android.util.Log;
48import android.util.Slog;
49import android.util.SparseArray;
50import android.util.TimeUtils;
51
52import java.io.File;
53import java.io.FileDescriptor;
54import java.io.PrintWriter;
55import java.util.ArrayList;
56import java.util.Collections;
57import java.util.HashMap;
58import java.util.List;
59import java.util.Set;
60
61import static com.android.server.Watchdog.NATIVE_STACKS_OF_INTEREST;
62import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ANR;
63import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
64import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
65import static com.android.server.am.ActivityManagerService.MY_PID;
66import static com.android.server.am.ActivityManagerService.SYSTEM_DEBUGGABLE;
67
68/**
69 * Controls error conditions in applications.
70 */
71class AppErrors {
72
73    private static final String TAG = TAG_WITH_CLASS_NAME ? "AppErrors" : TAG_AM;
74
75    private final ActivityManagerService mService;
76    private final Context mContext;
77
78    private ArraySet<String> mAppsNotReportingCrashes;
79
80    /**
81     * The last time that various processes have crashed since they were last explicitly started.
82     */
83    private final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<>();
84
85    /**
86     * The last time that various processes have crashed (not reset even when explicitly started).
87     */
88    private final ProcessMap<Long> mProcessCrashTimesPersistent = new ProcessMap<>();
89
90    /**
91     * Set of applications that we consider to be bad, and will reject
92     * incoming broadcasts from (which the user has no control over).
93     * Processes are added to this set when they have crashed twice within
94     * a minimum amount of time; they are removed from it when they are
95     * later restarted (hopefully due to some user action).  The value is the
96     * time it was added to the list.
97     */
98    private final ProcessMap<BadProcessInfo> mBadProcesses = new ProcessMap<>();
99
100
101    AppErrors(Context context, ActivityManagerService service) {
102        context.assertRuntimeOverlayThemable();
103        mService = service;
104        mContext = context;
105    }
106
107    boolean dumpLocked(FileDescriptor fd, PrintWriter pw, boolean needSep,
108            String dumpPackage) {
109        if (!mProcessCrashTimes.getMap().isEmpty()) {
110            boolean printed = false;
111            final long now = SystemClock.uptimeMillis();
112            final ArrayMap<String, SparseArray<Long>> pmap = mProcessCrashTimes.getMap();
113            final int processCount = pmap.size();
114            for (int ip = 0; ip < processCount; ip++) {
115                final String pname = pmap.keyAt(ip);
116                final SparseArray<Long> uids = pmap.valueAt(ip);
117                final int uidCount = uids.size();
118                for (int i = 0; i < uidCount; i++) {
119                    final int puid = uids.keyAt(i);
120                    final ProcessRecord r = mService.mProcessNames.get(pname, puid);
121                    if (dumpPackage != null && (r == null
122                            || !r.pkgList.containsKey(dumpPackage))) {
123                        continue;
124                    }
125                    if (!printed) {
126                        if (needSep) pw.println();
127                        needSep = true;
128                        pw.println("  Time since processes crashed:");
129                        printed = true;
130                    }
131                    pw.print("    Process "); pw.print(pname);
132                    pw.print(" uid "); pw.print(puid);
133                    pw.print(": last crashed ");
134                    TimeUtils.formatDuration(now-uids.valueAt(i), pw);
135                    pw.println(" ago");
136                }
137            }
138        }
139
140        if (!mBadProcesses.getMap().isEmpty()) {
141            boolean printed = false;
142            final ArrayMap<String, SparseArray<BadProcessInfo>> pmap = mBadProcesses.getMap();
143            final int processCount = pmap.size();
144            for (int ip = 0; ip < processCount; ip++) {
145                final String pname = pmap.keyAt(ip);
146                final SparseArray<BadProcessInfo> uids = pmap.valueAt(ip);
147                final int uidCount = uids.size();
148                for (int i = 0; i < uidCount; i++) {
149                    final int puid = uids.keyAt(i);
150                    final ProcessRecord r = mService.mProcessNames.get(pname, puid);
151                    if (dumpPackage != null && (r == null
152                            || !r.pkgList.containsKey(dumpPackage))) {
153                        continue;
154                    }
155                    if (!printed) {
156                        if (needSep) pw.println();
157                        needSep = true;
158                        pw.println("  Bad processes:");
159                        printed = true;
160                    }
161                    final BadProcessInfo info = uids.valueAt(i);
162                    pw.print("    Bad process "); pw.print(pname);
163                    pw.print(" uid "); pw.print(puid);
164                    pw.print(": crashed at time "); pw.println(info.time);
165                    if (info.shortMsg != null) {
166                        pw.print("      Short msg: "); pw.println(info.shortMsg);
167                    }
168                    if (info.longMsg != null) {
169                        pw.print("      Long msg: "); pw.println(info.longMsg);
170                    }
171                    if (info.stack != null) {
172                        pw.println("      Stack:");
173                        int lastPos = 0;
174                        for (int pos = 0; pos < info.stack.length(); pos++) {
175                            if (info.stack.charAt(pos) == '\n') {
176                                pw.print("        ");
177                                pw.write(info.stack, lastPos, pos-lastPos);
178                                pw.println();
179                                lastPos = pos+1;
180                            }
181                        }
182                        if (lastPos < info.stack.length()) {
183                            pw.print("        ");
184                            pw.write(info.stack, lastPos, info.stack.length()-lastPos);
185                            pw.println();
186                        }
187                    }
188                }
189            }
190        }
191        return needSep;
192    }
193
194    boolean isBadProcessLocked(ApplicationInfo info) {
195        return mBadProcesses.get(info.processName, info.uid) != null;
196    }
197
198    void clearBadProcessLocked(ApplicationInfo info) {
199        mBadProcesses.remove(info.processName, info.uid);
200    }
201
202    void resetProcessCrashTimeLocked(ApplicationInfo info) {
203        mProcessCrashTimes.remove(info.processName, info.uid);
204    }
205
206    void resetProcessCrashTimeLocked(boolean resetEntireUser, int appId, int userId) {
207        final ArrayMap<String, SparseArray<Long>> pmap = mProcessCrashTimes.getMap();
208        for (int ip = pmap.size() - 1; ip >= 0; ip--) {
209            SparseArray<Long> ba = pmap.valueAt(ip);
210            for (int i = ba.size() - 1; i >= 0; i--) {
211                boolean remove = false;
212                final int entUid = ba.keyAt(i);
213                if (!resetEntireUser) {
214                    if (userId == UserHandle.USER_ALL) {
215                        if (UserHandle.getAppId(entUid) == appId) {
216                            remove = true;
217                        }
218                    } else {
219                        if (entUid == UserHandle.getUid(userId, appId)) {
220                            remove = true;
221                        }
222                    }
223                } else if (UserHandle.getUserId(entUid) == userId) {
224                    remove = true;
225                }
226                if (remove) {
227                    ba.removeAt(i);
228                }
229            }
230            if (ba.size() == 0) {
231                pmap.removeAt(ip);
232            }
233        }
234    }
235
236    void loadAppsNotReportingCrashesFromConfigLocked(String appsNotReportingCrashesConfig) {
237        if (appsNotReportingCrashesConfig != null) {
238            final String[] split = appsNotReportingCrashesConfig.split(",");
239            if (split.length > 0) {
240                mAppsNotReportingCrashes = new ArraySet<>();
241                Collections.addAll(mAppsNotReportingCrashes, split);
242            }
243        }
244    }
245
246    void killAppAtUserRequestLocked(ProcessRecord app, Dialog fromDialog) {
247        app.crashing = false;
248        app.crashingReport = null;
249        app.notResponding = false;
250        app.notRespondingReport = null;
251        if (app.anrDialog == fromDialog) {
252            app.anrDialog = null;
253        }
254        if (app.waitDialog == fromDialog) {
255            app.waitDialog = null;
256        }
257        if (app.pid > 0 && app.pid != MY_PID) {
258            handleAppCrashLocked(app, "user-terminated" /*reason*/,
259                    null /*shortMsg*/, null /*longMsg*/, null /*stackTrace*/, null /*data*/);
260            app.kill("user request after error", true);
261        }
262    }
263
264    /**
265     * Induce a crash in the given app.
266     *
267     * @param uid if nonnegative, the required matching uid of the target to crash
268     * @param initialPid fast-path match for the target to crash
269     * @param packageName fallback match if the stated pid is not found or doesn't match uid
270     * @param userId If nonnegative, required to identify a match by package name
271     * @param message
272     */
273    void scheduleAppCrashLocked(int uid, int initialPid, String packageName, int userId,
274            String message) {
275        ProcessRecord proc = null;
276
277        // Figure out which process to kill.  We don't trust that initialPid
278        // still has any relation to current pids, so must scan through the
279        // list.
280
281        synchronized (mService.mPidsSelfLocked) {
282            for (int i=0; i<mService.mPidsSelfLocked.size(); i++) {
283                ProcessRecord p = mService.mPidsSelfLocked.valueAt(i);
284                if (uid >= 0 && p.uid != uid) {
285                    continue;
286                }
287                if (p.pid == initialPid) {
288                    proc = p;
289                    break;
290                }
291                if (p.pkgList.containsKey(packageName)
292                        && (userId < 0 || p.userId == userId)) {
293                    proc = p;
294                }
295            }
296        }
297
298        if (proc == null) {
299            Slog.w(TAG, "crashApplication: nothing for uid=" + uid
300                    + " initialPid=" + initialPid
301                    + " packageName=" + packageName
302                    + " userId=" + userId);
303            return;
304        }
305
306        proc.scheduleCrash(message);
307    }
308
309    /**
310     * Bring up the "unexpected error" dialog box for a crashing app.
311     * Deal with edge cases (intercepts from instrumented applications,
312     * ActivityController, error intent receivers, that sort of thing).
313     * @param r the application crashing
314     * @param crashInfo describing the failure
315     */
316    void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) {
317        final int callingPid = Binder.getCallingPid();
318        final int callingUid = Binder.getCallingUid();
319
320        final long origId = Binder.clearCallingIdentity();
321        try {
322            crashApplicationInner(r, crashInfo, callingPid, callingUid);
323        } finally {
324            Binder.restoreCallingIdentity(origId);
325        }
326    }
327
328    void crashApplicationInner(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo,
329            int callingPid, int callingUid) {
330        long timeMillis = System.currentTimeMillis();
331        String shortMsg = crashInfo.exceptionClassName;
332        String longMsg = crashInfo.exceptionMessage;
333        String stackTrace = crashInfo.stackTrace;
334        if (shortMsg != null && longMsg != null) {
335            longMsg = shortMsg + ": " + longMsg;
336        } else if (shortMsg != null) {
337            longMsg = shortMsg;
338        }
339
340        // If a persistent app is stuck in a crash loop, the device isn't very
341        // usable, so we want to consider sending out a rescue party.
342        if (r != null && r.persistent) {
343            RescueParty.notePersistentAppCrash(mContext, r.uid);
344        }
345
346        AppErrorResult result = new AppErrorResult();
347        TaskRecord task;
348        synchronized (mService) {
349            /**
350             * If crash is handled by instance of {@link android.app.IActivityController},
351             * finish now and don't show the app error dialog.
352             */
353            if (handleAppCrashInActivityController(r, crashInfo, shortMsg, longMsg, stackTrace,
354                    timeMillis, callingPid, callingUid)) {
355                return;
356            }
357
358            /**
359             * If this process was running instrumentation, finish now - it will be handled in
360             * {@link ActivityManagerService#handleAppDiedLocked}.
361             */
362            if (r != null && r.instr != null) {
363                return;
364            }
365
366            // Log crash in battery stats.
367            if (r != null) {
368                mService.mBatteryStatsService.noteProcessCrash(r.processName, r.uid);
369            }
370
371            AppErrorDialog.Data data = new AppErrorDialog.Data();
372            data.result = result;
373            data.proc = r;
374
375            // If we can't identify the process or it's already exceeded its crash quota,
376            // quit right away without showing a crash dialog.
377            if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace, data)) {
378                return;
379            }
380
381            final Message msg = Message.obtain();
382            msg.what = ActivityManagerService.SHOW_ERROR_UI_MSG;
383
384            task = data.task;
385            msg.obj = data;
386            mService.mUiHandler.sendMessage(msg);
387        }
388
389        int res = result.get();
390
391        Intent appErrorIntent = null;
392        MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_CRASH, res);
393        if (res == AppErrorDialog.TIMEOUT || res == AppErrorDialog.CANCEL) {
394            res = AppErrorDialog.FORCE_QUIT;
395        }
396        synchronized (mService) {
397            if (res == AppErrorDialog.MUTE) {
398                stopReportingCrashesLocked(r);
399            }
400            if (res == AppErrorDialog.RESTART) {
401                mService.removeProcessLocked(r, false, true, "crash");
402                if (task != null) {
403                    try {
404                        mService.startActivityFromRecents(task.taskId,
405                                ActivityOptions.makeBasic().toBundle());
406                    } catch (IllegalArgumentException e) {
407                        // Hmm, that didn't work, app might have crashed before creating a
408                        // recents entry. Let's see if we have a safe-to-restart intent.
409                        final Set<String> cats = task.intent.getCategories();
410                        if (cats != null && cats.contains(Intent.CATEGORY_LAUNCHER)) {
411                            mService.startActivityInPackage(task.mCallingUid,
412                                    task.mCallingPackage, task.intent, null, null, null, 0, 0,
413                                    ActivityOptions.makeBasic().toBundle(), task.userId, null,
414                                    "AppErrors");
415                        }
416                    }
417                }
418            }
419            if (res == AppErrorDialog.FORCE_QUIT) {
420                long orig = Binder.clearCallingIdentity();
421                try {
422                    // Kill it with fire!
423                    mService.mStackSupervisor.handleAppCrashLocked(r);
424                    if (!r.persistent) {
425                        mService.removeProcessLocked(r, false, false, "crash");
426                        mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
427                    }
428                } finally {
429                    Binder.restoreCallingIdentity(orig);
430                }
431            }
432            if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) {
433                appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo);
434            }
435            if (r != null && !r.isolated && res != AppErrorDialog.RESTART) {
436                // XXX Can't keep track of crash time for isolated processes,
437                // since they don't have a persistent identity.
438                mProcessCrashTimes.put(r.info.processName, r.uid,
439                        SystemClock.uptimeMillis());
440            }
441        }
442
443        if (appErrorIntent != null) {
444            try {
445                mContext.startActivityAsUser(appErrorIntent, new UserHandle(r.userId));
446            } catch (ActivityNotFoundException e) {
447                Slog.w(TAG, "bug report receiver dissappeared", e);
448            }
449        }
450    }
451
452    private boolean handleAppCrashInActivityController(ProcessRecord r,
453                                                       ApplicationErrorReport.CrashInfo crashInfo,
454                                                       String shortMsg, String longMsg,
455                                                       String stackTrace, long timeMillis,
456                                                       int callingPid, int callingUid) {
457        if (mService.mController == null) {
458            return false;
459        }
460
461        try {
462            String name = r != null ? r.processName : null;
463            int pid = r != null ? r.pid : callingPid;
464            int uid = r != null ? r.info.uid : callingUid;
465            if (!mService.mController.appCrashed(name, pid,
466                    shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) {
467                if ("1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"))
468                        && "Native crash".equals(crashInfo.exceptionClassName)) {
469                    Slog.w(TAG, "Skip killing native crashed app " + name
470                            + "(" + pid + ") during testing");
471                } else {
472                    Slog.w(TAG, "Force-killing crashed app " + name
473                            + " at watcher's request");
474                    if (r != null) {
475                        if (!makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace, null))
476                        {
477                            r.kill("crash", true);
478                        }
479                    } else {
480                        // Huh.
481                        Process.killProcess(pid);
482                        ActivityManagerService.killProcessGroup(uid, pid);
483                    }
484                }
485                return true;
486            }
487        } catch (RemoteException e) {
488            mService.mController = null;
489            Watchdog.getInstance().setActivityController(null);
490        }
491        return false;
492    }
493
494    private boolean makeAppCrashingLocked(ProcessRecord app,
495            String shortMsg, String longMsg, String stackTrace, AppErrorDialog.Data data) {
496        app.crashing = true;
497        app.crashingReport = generateProcessError(app,
498                ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace);
499        startAppProblemLocked(app);
500        app.stopFreezingAllLocked();
501        return handleAppCrashLocked(app, "force-crash" /*reason*/, shortMsg, longMsg, stackTrace,
502                data);
503    }
504
505    void startAppProblemLocked(ProcessRecord app) {
506        // If this app is not running under the current user, then we
507        // can't give it a report button because that would require
508        // launching the report UI under a different user.
509        app.errorReportReceiver = null;
510
511        for (int userId : mService.mUserController.getCurrentProfileIdsLocked()) {
512            if (app.userId == userId) {
513                app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
514                        mContext, app.info.packageName, app.info.flags);
515            }
516        }
517        mService.skipCurrentReceiverLocked(app);
518    }
519
520    /**
521     * Generate a process error record, suitable for attachment to a ProcessRecord.
522     *
523     * @param app The ProcessRecord in which the error occurred.
524     * @param condition Crashing, Application Not Responding, etc.  Values are defined in
525     *                      ActivityManager.AppErrorStateInfo
526     * @param activity The activity associated with the crash, if known.
527     * @param shortMsg Short message describing the crash.
528     * @param longMsg Long message describing the crash.
529     * @param stackTrace Full crash stack trace, may be null.
530     *
531     * @return Returns a fully-formed AppErrorStateInfo record.
532     */
533    private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app,
534            int condition, String activity, String shortMsg, String longMsg, String stackTrace) {
535        ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo();
536
537        report.condition = condition;
538        report.processName = app.processName;
539        report.pid = app.pid;
540        report.uid = app.info.uid;
541        report.tag = activity;
542        report.shortMsg = shortMsg;
543        report.longMsg = longMsg;
544        report.stackTrace = stackTrace;
545
546        return report;
547    }
548
549    Intent createAppErrorIntentLocked(ProcessRecord r,
550            long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
551        ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo);
552        if (report == null) {
553            return null;
554        }
555        Intent result = new Intent(Intent.ACTION_APP_ERROR);
556        result.setComponent(r.errorReportReceiver);
557        result.putExtra(Intent.EXTRA_BUG_REPORT, report);
558        result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
559        return result;
560    }
561
562    private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r,
563            long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
564        if (r.errorReportReceiver == null) {
565            return null;
566        }
567
568        if (!r.crashing && !r.notResponding && !r.forceCrashReport) {
569            return null;
570        }
571
572        ApplicationErrorReport report = new ApplicationErrorReport();
573        report.packageName = r.info.packageName;
574        report.installerPackageName = r.errorReportReceiver.getPackageName();
575        report.processName = r.processName;
576        report.time = timeMillis;
577        report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
578
579        if (r.crashing || r.forceCrashReport) {
580            report.type = ApplicationErrorReport.TYPE_CRASH;
581            report.crashInfo = crashInfo;
582        } else if (r.notResponding) {
583            report.type = ApplicationErrorReport.TYPE_ANR;
584            report.anrInfo = new ApplicationErrorReport.AnrInfo();
585
586            report.anrInfo.activity = r.notRespondingReport.tag;
587            report.anrInfo.cause = r.notRespondingReport.shortMsg;
588            report.anrInfo.info = r.notRespondingReport.longMsg;
589        }
590
591        return report;
592    }
593
594    boolean handleAppCrashLocked(ProcessRecord app, String reason,
595            String shortMsg, String longMsg, String stackTrace, AppErrorDialog.Data data) {
596        long now = SystemClock.uptimeMillis();
597        boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
598                Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
599
600        Long crashTime;
601        Long crashTimePersistent;
602        if (!app.isolated) {
603            crashTime = mProcessCrashTimes.get(app.info.processName, app.uid);
604            crashTimePersistent = mProcessCrashTimesPersistent.get(app.info.processName, app.uid);
605        } else {
606            crashTime = crashTimePersistent = null;
607        }
608        if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) {
609            // This process loses!
610            Slog.w(TAG, "Process " + app.info.processName
611                    + " has crashed too many times: killing!");
612            EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH,
613                    app.userId, app.info.processName, app.uid);
614            mService.mStackSupervisor.handleAppCrashLocked(app);
615            if (!app.persistent) {
616                // We don't want to start this process again until the user
617                // explicitly does so...  but for persistent process, we really
618                // need to keep it running.  If a persistent process is actually
619                // repeatedly crashing, then badness for everyone.
620                EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.userId, app.uid,
621                        app.info.processName);
622                if (!app.isolated) {
623                    // XXX We don't have a way to mark isolated processes
624                    // as bad, since they don't have a peristent identity.
625                    mBadProcesses.put(app.info.processName, app.uid,
626                            new BadProcessInfo(now, shortMsg, longMsg, stackTrace));
627                    mProcessCrashTimes.remove(app.info.processName, app.uid);
628                }
629                app.bad = true;
630                app.removed = true;
631                // Don't let services in this process be restarted and potentially
632                // annoy the user repeatedly.  Unless it is persistent, since those
633                // processes run critical code.
634                mService.removeProcessLocked(app, false, false, "crash");
635                mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
636                if (!showBackground) {
637                    return false;
638                }
639            }
640            mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
641        } else {
642            TaskRecord affectedTask =
643                    mService.mStackSupervisor.finishTopRunningActivityLocked(app, reason);
644            if (data != null) {
645                data.task = affectedTask;
646            }
647            if (data != null && crashTimePersistent != null
648                    && now < crashTimePersistent + ProcessList.MIN_CRASH_INTERVAL) {
649                data.repeating = true;
650            }
651        }
652
653        boolean procIsBoundForeground =
654                (app.curProcState == ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE);
655        // Bump up the crash count of any services currently running in the proc.
656        for (int i=app.services.size()-1; i>=0; i--) {
657            // Any services running in the application need to be placed
658            // back in the pending list.
659            ServiceRecord sr = app.services.valueAt(i);
660            sr.crashCount++;
661
662            // Allow restarting for started or bound foreground services that are crashing the
663            // first time. This includes wallpapers.
664            if ((data != null) && (sr.crashCount <= 1)
665                    && (sr.isForeground || procIsBoundForeground)) {
666                data.isRestartableForService = true;
667            }
668        }
669
670        // If the crashing process is what we consider to be the "home process" and it has been
671        // replaced by a third-party app, clear the package preferred activities from packages
672        // with a home activity running in the process to prevent a repeatedly crashing app
673        // from blocking the user to manually clear the list.
674        final ArrayList<ActivityRecord> activities = app.activities;
675        if (app == mService.mHomeProcess && activities.size() > 0
676                && (mService.mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
677            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
678                final ActivityRecord r = activities.get(activityNdx);
679                if (r.isHomeActivity()) {
680                    Log.i(TAG, "Clearing package preferred activities from " + r.packageName);
681                    try {
682                        ActivityThread.getPackageManager()
683                                .clearPackagePreferredActivities(r.packageName);
684                    } catch (RemoteException c) {
685                        // pm is in same process, this will never happen.
686                    }
687                }
688            }
689        }
690
691        if (!app.isolated) {
692            // XXX Can't keep track of crash times for isolated processes,
693            // because they don't have a perisistent identity.
694            mProcessCrashTimes.put(app.info.processName, app.uid, now);
695            mProcessCrashTimesPersistent.put(app.info.processName, app.uid, now);
696        }
697
698        if (app.crashHandler != null) mService.mHandler.post(app.crashHandler);
699        return true;
700    }
701
702    void handleShowAppErrorUi(Message msg) {
703        AppErrorDialog.Data data = (AppErrorDialog.Data) msg.obj;
704        boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
705                Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
706        synchronized (mService) {
707            ProcessRecord proc = data.proc;
708            AppErrorResult res = data.result;
709            if (proc != null && proc.crashDialog != null) {
710                Slog.e(TAG, "App already has crash dialog: " + proc);
711                if (res != null) {
712                    res.set(AppErrorDialog.ALREADY_SHOWING);
713                }
714                return;
715            }
716            boolean isBackground = (UserHandle.getAppId(proc.uid)
717                    >= Process.FIRST_APPLICATION_UID
718                    && proc.pid != MY_PID);
719            for (int userId : mService.mUserController.getCurrentProfileIdsLocked()) {
720                isBackground &= (proc.userId != userId);
721            }
722            if (isBackground && !showBackground) {
723                Slog.w(TAG, "Skipping crash dialog of " + proc + ": background");
724                if (res != null) {
725                    res.set(AppErrorDialog.BACKGROUND_USER);
726                }
727                return;
728            }
729            final boolean crashSilenced = mAppsNotReportingCrashes != null &&
730                    mAppsNotReportingCrashes.contains(proc.info.packageName);
731            if ((mService.canShowErrorDialogs() || showBackground) && !crashSilenced) {
732                proc.crashDialog = new AppErrorDialog(mContext, mService, data);
733            } else {
734                // The device is asleep, so just pretend that the user
735                // saw a crash dialog and hit "force quit".
736                if (res != null) {
737                    res.set(AppErrorDialog.CANT_SHOW);
738                }
739            }
740        }
741        // If we've created a crash dialog, show it without the lock held
742        if(data.proc.crashDialog != null) {
743            Slog.i(TAG, "Showing crash dialog for package " + data.proc.info.packageName
744                    + " u" + data.proc.userId);
745            data.proc.crashDialog.show();
746        }
747    }
748
749    void stopReportingCrashesLocked(ProcessRecord proc) {
750        if (mAppsNotReportingCrashes == null) {
751            mAppsNotReportingCrashes = new ArraySet<>();
752        }
753        mAppsNotReportingCrashes.add(proc.info.packageName);
754    }
755
756    static boolean isInterestingForBackgroundTraces(ProcessRecord app) {
757        // The system_server is always considered interesting.
758        if (app.pid == MY_PID) {
759            return true;
760        }
761
762        // A package is considered interesting if any of the following is true :
763        //
764        // - It's displaying an activity.
765        // - It's the SystemUI.
766        // - It has an overlay or a top UI visible.
767        //
768        // NOTE: The check whether a given ProcessRecord belongs to the systemui
769        // process is a bit of a kludge, but the same pattern seems repeated at
770        // several places in the system server.
771        return app.isInterestingToUserLocked() ||
772            (app.info != null && "com.android.systemui".equals(app.info.packageName)) ||
773            (app.hasTopUi || app.hasOverlayUi);
774    }
775
776    final void appNotResponding(ProcessRecord app, ActivityRecord activity,
777            ActivityRecord parent, boolean aboveSystem, final String annotation) {
778        ArrayList<Integer> firstPids = new ArrayList<Integer>(5);
779        SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20);
780
781        if (mService.mController != null) {
782            try {
783                // 0 == continue, -1 = kill process immediately
784                int res = mService.mController.appEarlyNotResponding(
785                        app.processName, app.pid, annotation);
786                if (res < 0 && app.pid != MY_PID) {
787                    app.kill("anr", true);
788                }
789            } catch (RemoteException e) {
790                mService.mController = null;
791                Watchdog.getInstance().setActivityController(null);
792            }
793        }
794
795        long anrTime = SystemClock.uptimeMillis();
796        if (ActivityManagerService.MONITOR_CPU_USAGE) {
797            mService.updateCpuStatsNow();
798        }
799
800        // Unless configured otherwise, swallow ANRs in background processes & kill the process.
801        boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
802                Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
803
804        boolean isSilentANR;
805
806        synchronized (mService) {
807            // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
808            if (mService.mShuttingDown) {
809                Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation);
810                return;
811            } else if (app.notResponding) {
812                Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation);
813                return;
814            } else if (app.crashing) {
815                Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation);
816                return;
817            } else if (app.killedByAm) {
818                Slog.i(TAG, "App already killed by AM skipping ANR: " + app + " " + annotation);
819                return;
820            } else if (app.killed) {
821                Slog.i(TAG, "Skipping died app ANR: " + app + " " + annotation);
822                return;
823            }
824
825            // In case we come through here for the same app before completing
826            // this one, mark as anring now so we will bail out.
827            app.notResponding = true;
828
829            // Log the ANR to the event log.
830            EventLog.writeEvent(EventLogTags.AM_ANR, app.userId, app.pid,
831                    app.processName, app.info.flags, annotation);
832
833            // Dump thread traces as quickly as we can, starting with "interesting" processes.
834            firstPids.add(app.pid);
835
836            // Don't dump other PIDs if it's a background ANR
837            isSilentANR = !showBackground && !isInterestingForBackgroundTraces(app);
838            if (!isSilentANR) {
839                int parentPid = app.pid;
840                if (parent != null && parent.app != null && parent.app.pid > 0) {
841                    parentPid = parent.app.pid;
842                }
843                if (parentPid != app.pid) firstPids.add(parentPid);
844
845                if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID);
846
847                for (int i = mService.mLruProcesses.size() - 1; i >= 0; i--) {
848                    ProcessRecord r = mService.mLruProcesses.get(i);
849                    if (r != null && r.thread != null) {
850                        int pid = r.pid;
851                        if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) {
852                            if (r.persistent) {
853                                firstPids.add(pid);
854                                if (DEBUG_ANR) Slog.i(TAG, "Adding persistent proc: " + r);
855                            } else if (r.treatLikeActivity) {
856                                firstPids.add(pid);
857                                if (DEBUG_ANR) Slog.i(TAG, "Adding likely IME: " + r);
858                            } else {
859                                lastPids.put(pid, Boolean.TRUE);
860                                if (DEBUG_ANR) Slog.i(TAG, "Adding ANR proc: " + r);
861                            }
862                        }
863                    }
864                }
865            }
866        }
867
868        // Log the ANR to the main log.
869        StringBuilder info = new StringBuilder();
870        info.setLength(0);
871        info.append("ANR in ").append(app.processName);
872        if (activity != null && activity.shortComponentName != null) {
873            info.append(" (").append(activity.shortComponentName).append(")");
874        }
875        info.append("\n");
876        info.append("PID: ").append(app.pid).append("\n");
877        if (annotation != null) {
878            info.append("Reason: ").append(annotation).append("\n");
879        }
880        if (parent != null && parent != activity) {
881            info.append("Parent: ").append(parent.shortComponentName).append("\n");
882        }
883
884        ProcessCpuTracker processCpuTracker = new ProcessCpuTracker(true);
885
886        // don't dump native PIDs for background ANRs unless it is the process of interest
887        String[] nativeProcs = null;
888        if (isSilentANR) {
889            for (int i = 0; i < NATIVE_STACKS_OF_INTEREST.length; i++) {
890                if (NATIVE_STACKS_OF_INTEREST[i].equals(app.processName)) {
891                    nativeProcs = new String[] { app.processName };
892                    break;
893                }
894            }
895        } else {
896            nativeProcs = NATIVE_STACKS_OF_INTEREST;
897        }
898
899        int[] pids = nativeProcs == null ? null : Process.getPidsForCommands(nativeProcs);
900        ArrayList<Integer> nativePids = null;
901
902        if (pids != null) {
903            nativePids = new ArrayList<Integer>(pids.length);
904            for (int i : pids) {
905                nativePids.add(i);
906            }
907        }
908
909        // For background ANRs, don't pass the ProcessCpuTracker to
910        // avoid spending 1/2 second collecting stats to rank lastPids.
911        File tracesFile = ActivityManagerService.dumpStackTraces(
912                true, firstPids,
913                (isSilentANR) ? null : processCpuTracker,
914                (isSilentANR) ? null : lastPids,
915                nativePids);
916
917        String cpuInfo = null;
918        if (ActivityManagerService.MONITOR_CPU_USAGE) {
919            mService.updateCpuStatsNow();
920            synchronized (mService.mProcessCpuTracker) {
921                cpuInfo = mService.mProcessCpuTracker.printCurrentState(anrTime);
922            }
923            info.append(processCpuTracker.printCurrentLoad());
924            info.append(cpuInfo);
925        }
926
927        info.append(processCpuTracker.printCurrentState(anrTime));
928
929        Slog.e(TAG, info.toString());
930        if (tracesFile == null) {
931            // There is no trace file, so dump (only) the alleged culprit's threads to the log
932            Process.sendSignal(app.pid, Process.SIGNAL_QUIT);
933        }
934
935        mService.addErrorToDropBox("anr", app, app.processName, activity, parent, annotation,
936                cpuInfo, tracesFile, null);
937
938        if (mService.mController != null) {
939            try {
940                // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
941                int res = mService.mController.appNotResponding(
942                        app.processName, app.pid, info.toString());
943                if (res != 0) {
944                    if (res < 0 && app.pid != MY_PID) {
945                        app.kill("anr", true);
946                    } else {
947                        synchronized (mService) {
948                            mService.mServices.scheduleServiceTimeoutLocked(app);
949                        }
950                    }
951                    return;
952                }
953            } catch (RemoteException e) {
954                mService.mController = null;
955                Watchdog.getInstance().setActivityController(null);
956            }
957        }
958
959        synchronized (mService) {
960            mService.mBatteryStatsService.noteProcessAnr(app.processName, app.uid);
961
962            if (isSilentANR) {
963                app.kill("bg anr", true);
964                return;
965            }
966
967            // Set the app's notResponding state, and look up the errorReportReceiver
968            makeAppNotRespondingLocked(app,
969                    activity != null ? activity.shortComponentName : null,
970                    annotation != null ? "ANR " + annotation : "ANR",
971                    info.toString());
972
973            // Bring up the infamous App Not Responding dialog
974            Message msg = Message.obtain();
975            HashMap<String, Object> map = new HashMap<String, Object>();
976            msg.what = ActivityManagerService.SHOW_NOT_RESPONDING_UI_MSG;
977            msg.obj = map;
978            msg.arg1 = aboveSystem ? 1 : 0;
979            map.put("app", app);
980            if (activity != null) {
981                map.put("activity", activity);
982            }
983
984            mService.mUiHandler.sendMessage(msg);
985        }
986    }
987
988    private void makeAppNotRespondingLocked(ProcessRecord app,
989            String activity, String shortMsg, String longMsg) {
990        app.notResponding = true;
991        app.notRespondingReport = generateProcessError(app,
992                ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
993                activity, shortMsg, longMsg, null);
994        startAppProblemLocked(app);
995        app.stopFreezingAllLocked();
996    }
997
998    void handleShowAnrUi(Message msg) {
999        Dialog d = null;
1000        synchronized (mService) {
1001            HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
1002            ProcessRecord proc = (ProcessRecord)data.get("app");
1003            if (proc != null && proc.anrDialog != null) {
1004                Slog.e(TAG, "App already has anr dialog: " + proc);
1005                MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_ANR,
1006                        AppNotRespondingDialog.ALREADY_SHOWING);
1007                return;
1008            }
1009
1010            Intent intent = new Intent("android.intent.action.ANR");
1011            if (!mService.mProcessesReady) {
1012                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
1013                        | Intent.FLAG_RECEIVER_FOREGROUND);
1014            }
1015            mService.broadcastIntentLocked(null, null, intent,
1016                    null, null, 0, null, null, null, AppOpsManager.OP_NONE,
1017                    null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
1018
1019            boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
1020                    Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
1021            if (mService.canShowErrorDialogs() || showBackground) {
1022                d = new AppNotRespondingDialog(mService,
1023                        mContext, proc, (ActivityRecord)data.get("activity"),
1024                        msg.arg1 != 0);
1025                proc.anrDialog = d;
1026            } else {
1027                MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_ANR,
1028                        AppNotRespondingDialog.CANT_SHOW);
1029                // Just kill the app if there is no dialog to be shown.
1030                mService.killAppAtUsersRequest(proc, null);
1031            }
1032        }
1033        // If we've created a crash dialog, show it without the lock held
1034        if (d != null) {
1035            d.show();
1036        }
1037    }
1038
1039    /**
1040     * Information about a process that is currently marked as bad.
1041     */
1042    static final class BadProcessInfo {
1043        BadProcessInfo(long time, String shortMsg, String longMsg, String stack) {
1044            this.time = time;
1045            this.shortMsg = shortMsg;
1046            this.longMsg = longMsg;
1047            this.stack = stack;
1048        }
1049
1050        final long time;
1051        final String shortMsg;
1052        final String longMsg;
1053        final String stack;
1054    }
1055
1056}
1057