AppErrors.java revision f6690100be5c5cd75c64d9a6a0345acff7b754d1
120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos/*
220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos * Copyright (C) 2016 The Android Open Source Project
320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos *
420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos * Licensed under the Apache License, Version 2.0 (the "License");
520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos * you may not use this file except in compliance with the License.
620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos * You may obtain a copy of the License at
720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos *
820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos *      http://www.apache.org/licenses/LICENSE-2.0
920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos *
1020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos * Unless required by applicable law or agreed to in writing, software
1120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos * distributed under the License is distributed on an "AS IS" BASIS,
1220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos * See the License for the specific language governing permissions and
1420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos * limitations under the License
1520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos */
1620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
1720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roospackage com.android.server.am;
1820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
1920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport com.android.internal.app.ProcessMap;
209046222cb2b1bd57278ddbf71d9f628f8dd254aeAdrian Roosimport com.android.internal.logging.MetricsLogger;
21383db5ebcc3a4a615faf249bf4f126f42e80b82eTamas Berghammerimport com.android.internal.logging.nano.MetricsProto;
2220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport com.android.internal.os.ProcessCpuTracker;
23fe6f85cac9e823fd33a134f7129fdf7310703293Jeff Sharkeyimport com.android.server.RescueParty;
2420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport com.android.server.Watchdog;
2520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
2620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.app.ActivityManager;
2720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.app.ActivityOptions;
2820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.app.ActivityThread;
2920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.app.AppOpsManager;
3020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.app.ApplicationErrorReport;
3120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.app.Dialog;
3220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.content.ActivityNotFoundException;
3320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.content.Context;
3420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.content.Intent;
3520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.content.pm.ApplicationInfo;
3620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.os.Binder;
3720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.os.Message;
3820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.os.Process;
3920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.os.RemoteException;
4020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.os.SystemClock;
4120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.os.SystemProperties;
4220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.os.UserHandle;
4320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.provider.Settings;
4420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.util.ArrayMap;
4520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.util.ArraySet;
4620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.util.EventLog;
4720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.util.Log;
4820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.util.Slog;
4920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.util.SparseArray;
5020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport android.util.TimeUtils;
5120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
5220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport java.io.File;
5320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport java.io.FileDescriptor;
5420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport java.io.PrintWriter;
5520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport java.util.ArrayList;
5620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport java.util.Collections;
5720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport java.util.HashMap;
584d8959489d6b4123340dd2113044548b37d1b370Dianne Hackbornimport java.util.Set;
5920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
6020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport static com.android.server.Watchdog.NATIVE_STACKS_OF_INTEREST;
619369efdf6a43d8fa0f82dcae651c76b85a5ea0adDianne Hackbornimport static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ANR;
6220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
6320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
6420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport static com.android.server.am.ActivityManagerService.MY_PID;
6520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosimport static com.android.server.am.ActivityManagerService.SYSTEM_DEBUGGABLE;
6620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
6720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos/**
6820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos * Controls error conditions in applications.
6920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos */
7020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roosclass AppErrors {
7120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
7220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private static final String TAG = TAG_WITH_CLASS_NAME ? "AppErrors" : TAG_AM;
7320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
7420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private final ActivityManagerService mService;
7520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private final Context mContext;
7620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
7720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private ArraySet<String> mAppsNotReportingCrashes;
7820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
7920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    /**
8020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * The last time that various processes have crashed since they were last explicitly started.
8120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     */
8220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<>();
8320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
8420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    /**
8520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * The last time that various processes have crashed (not reset even when explicitly started).
8620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     */
8720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private final ProcessMap<Long> mProcessCrashTimesPersistent = new ProcessMap<>();
8820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
8920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    /**
9020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * Set of applications that we consider to be bad, and will reject
9120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * incoming broadcasts from (which the user has no control over).
9220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * Processes are added to this set when they have crashed twice within
9320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * a minimum amount of time; they are removed from it when they are
9420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * later restarted (hopefully due to some user action).  The value is the
9520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * time it was added to the list.
9620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     */
9720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private final ProcessMap<BadProcessInfo> mBadProcesses = new ProcessMap<>();
9820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
9920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
10020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    AppErrors(Context context, ActivityManagerService service) {
101a82b62678a0e1eaba50ec5adce93862683dac065Adam Lesinski        context.assertRuntimeOverlayThemable();
10220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        mService = service;
10320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        mContext = context;
10420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
10520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
10620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    boolean dumpLocked(FileDescriptor fd, PrintWriter pw, boolean needSep,
10720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            String dumpPackage) {
10820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (!mProcessCrashTimes.getMap().isEmpty()) {
10920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            boolean printed = false;
11020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            final long now = SystemClock.uptimeMillis();
11120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            final ArrayMap<String, SparseArray<Long>> pmap = mProcessCrashTimes.getMap();
11220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            final int processCount = pmap.size();
11320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            for (int ip = 0; ip < processCount; ip++) {
11420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                final String pname = pmap.keyAt(ip);
11520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                final SparseArray<Long> uids = pmap.valueAt(ip);
11620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                final int uidCount = uids.size();
11720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                for (int i = 0; i < uidCount; i++) {
11820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    final int puid = uids.keyAt(i);
11920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    final ProcessRecord r = mService.mProcessNames.get(pname, puid);
12020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    if (dumpPackage != null && (r == null
12120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            || !r.pkgList.containsKey(dumpPackage))) {
12220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        continue;
12320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
12420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    if (!printed) {
12520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        if (needSep) pw.println();
12620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        needSep = true;
12720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        pw.println("  Time since processes crashed:");
12820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        printed = true;
12920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
13020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    pw.print("    Process "); pw.print(pname);
13120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    pw.print(" uid "); pw.print(puid);
13220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    pw.print(": last crashed ");
13320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    TimeUtils.formatDuration(now-uids.valueAt(i), pw);
13420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    pw.println(" ago");
13520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
13620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
13720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
13820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
13920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (!mBadProcesses.getMap().isEmpty()) {
14020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            boolean printed = false;
14120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            final ArrayMap<String, SparseArray<BadProcessInfo>> pmap = mBadProcesses.getMap();
14220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            final int processCount = pmap.size();
14320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            for (int ip = 0; ip < processCount; ip++) {
14420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                final String pname = pmap.keyAt(ip);
14520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                final SparseArray<BadProcessInfo> uids = pmap.valueAt(ip);
14620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                final int uidCount = uids.size();
14720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                for (int i = 0; i < uidCount; i++) {
14820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    final int puid = uids.keyAt(i);
14920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    final ProcessRecord r = mService.mProcessNames.get(pname, puid);
15020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    if (dumpPackage != null && (r == null
15120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            || !r.pkgList.containsKey(dumpPackage))) {
15220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        continue;
15320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
15420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    if (!printed) {
15520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        if (needSep) pw.println();
15620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        needSep = true;
15720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        pw.println("  Bad processes:");
15820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        printed = true;
15920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
16020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    final BadProcessInfo info = uids.valueAt(i);
16120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    pw.print("    Bad process "); pw.print(pname);
16220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    pw.print(" uid "); pw.print(puid);
16320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    pw.print(": crashed at time "); pw.println(info.time);
16420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    if (info.shortMsg != null) {
16520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        pw.print("      Short msg: "); pw.println(info.shortMsg);
16620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
16720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    if (info.longMsg != null) {
16820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        pw.print("      Long msg: "); pw.println(info.longMsg);
16920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
17020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    if (info.stack != null) {
17120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        pw.println("      Stack:");
17220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        int lastPos = 0;
17320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        for (int pos = 0; pos < info.stack.length(); pos++) {
17420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            if (info.stack.charAt(pos) == '\n') {
17520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                                pw.print("        ");
17620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                                pw.write(info.stack, lastPos, pos-lastPos);
17720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                                pw.println();
17820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                                lastPos = pos+1;
17920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            }
18020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        }
18120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        if (lastPos < info.stack.length()) {
18220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            pw.print("        ");
18320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            pw.write(info.stack, lastPos, info.stack.length()-lastPos);
18420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            pw.println();
18520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        }
18620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
18720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
18820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
18920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
19020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        return needSep;
19120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
19220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
19320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    boolean isBadProcessLocked(ApplicationInfo info) {
19420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        return mBadProcesses.get(info.processName, info.uid) != null;
19520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
19620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
19720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    void clearBadProcessLocked(ApplicationInfo info) {
19820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        mBadProcesses.remove(info.processName, info.uid);
19920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
20020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
20120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    void resetProcessCrashTimeLocked(ApplicationInfo info) {
20220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        mProcessCrashTimes.remove(info.processName, info.uid);
20320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
20420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
20520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    void resetProcessCrashTimeLocked(boolean resetEntireUser, int appId, int userId) {
20620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        final ArrayMap<String, SparseArray<Long>> pmap = mProcessCrashTimes.getMap();
20720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        for (int ip = pmap.size() - 1; ip >= 0; ip--) {
20820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            SparseArray<Long> ba = pmap.valueAt(ip);
20920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            for (int i = ba.size() - 1; i >= 0; i--) {
21020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                boolean remove = false;
21120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                final int entUid = ba.keyAt(i);
21220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                if (!resetEntireUser) {
21320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    if (userId == UserHandle.USER_ALL) {
21420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        if (UserHandle.getAppId(entUid) == appId) {
21520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            remove = true;
21620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        }
21720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    } else {
21820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        if (entUid == UserHandle.getUid(userId, appId)) {
21920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            remove = true;
22020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        }
22120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
22220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                } else if (UserHandle.getUserId(entUid) == userId) {
22320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    remove = true;
22420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
22520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                if (remove) {
22620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    ba.removeAt(i);
22720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
22820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
22920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (ba.size() == 0) {
23020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                pmap.removeAt(ip);
23120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
23220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
23320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
23420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
23520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    void loadAppsNotReportingCrashesFromConfigLocked(String appsNotReportingCrashesConfig) {
23620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (appsNotReportingCrashesConfig != null) {
23720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            final String[] split = appsNotReportingCrashesConfig.split(",");
23820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (split.length > 0) {
23920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                mAppsNotReportingCrashes = new ArraySet<>();
24020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Collections.addAll(mAppsNotReportingCrashes, split);
24120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
24220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
24320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
24420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
24520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    void killAppAtUserRequestLocked(ProcessRecord app, Dialog fromDialog) {
24620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.crashing = false;
24720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.crashingReport = null;
24820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.notResponding = false;
24920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.notRespondingReport = null;
25020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (app.anrDialog == fromDialog) {
25120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            app.anrDialog = null;
25220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
25320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (app.waitDialog == fromDialog) {
25420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            app.waitDialog = null;
25520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
25620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (app.pid > 0 && app.pid != MY_PID) {
25720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            handleAppCrashLocked(app, "user-terminated" /*reason*/,
25820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    null /*shortMsg*/, null /*longMsg*/, null /*stackTrace*/, null /*data*/);
25920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            app.kill("user request after error", true);
26020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
26120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
26220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
2638aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate    /**
2648aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate     * Induce a crash in the given app.
2658aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate     *
2668aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate     * @param uid if nonnegative, the required matching uid of the target to crash
2678aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate     * @param initialPid fast-path match for the target to crash
2688aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate     * @param packageName fallback match if the stated pid is not found or doesn't match uid
2698aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate     * @param userId If nonnegative, required to identify a match by package name
2708aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate     * @param message
2718aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate     */
2728aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate    void scheduleAppCrashLocked(int uid, int initialPid, String packageName, int userId,
27320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            String message) {
27420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        ProcessRecord proc = null;
27520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
27620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // Figure out which process to kill.  We don't trust that initialPid
27720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // still has any relation to current pids, so must scan through the
27820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // list.
27920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
28020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        synchronized (mService.mPidsSelfLocked) {
28120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            for (int i=0; i<mService.mPidsSelfLocked.size(); i++) {
28220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                ProcessRecord p = mService.mPidsSelfLocked.valueAt(i);
2838aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate                if (uid >= 0 && p.uid != uid) {
28420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    continue;
28520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
28620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                if (p.pid == initialPid) {
28720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    proc = p;
28820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    break;
28920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
2908aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate                if (p.pkgList.containsKey(packageName)
2918aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate                        && (userId < 0 || p.userId == userId)) {
29220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    proc = p;
29320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
29420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
29520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
29620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
29720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (proc == null) {
29820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            Slog.w(TAG, "crashApplication: nothing for uid=" + uid
29920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    + " initialPid=" + initialPid
3008aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate                    + " packageName=" + packageName
3018aa8fe128992f7e47ecbc8588027eaec82012f3aChristopher Tate                    + " userId=" + userId);
30220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            return;
30320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
30420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
3055719028766f51512beffa623db7ee682851570a0Joe Onorato        proc.scheduleCrash(message);
30620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
30720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
30820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    /**
30920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * Bring up the "unexpected error" dialog box for a crashing app.
31020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * Deal with edge cases (intercepts from instrumented applications,
31120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * ActivityController, error intent receivers, that sort of thing).
31220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * @param r the application crashing
31320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * @param crashInfo describing the failure
31420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     */
31520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) {
316b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu        final int callingPid = Binder.getCallingPid();
317b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu        final int callingUid = Binder.getCallingUid();
318b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu
319fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        final long origId = Binder.clearCallingIdentity();
320fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        try {
321b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu            crashApplicationInner(r, crashInfo, callingPid, callingUid);
322fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        } finally {
323fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            Binder.restoreCallingIdentity(origId);
324fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        }
325fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian    }
326fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian
327b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu    void crashApplicationInner(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo,
328b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu            int callingPid, int callingUid) {
32920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        long timeMillis = System.currentTimeMillis();
33020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        String shortMsg = crashInfo.exceptionClassName;
33120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        String longMsg = crashInfo.exceptionMessage;
33220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        String stackTrace = crashInfo.stackTrace;
33320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (shortMsg != null && longMsg != null) {
33420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            longMsg = shortMsg + ": " + longMsg;
33520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        } else if (shortMsg != null) {
33620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            longMsg = shortMsg;
33720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
33820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
339fe6f85cac9e823fd33a134f7129fdf7310703293Jeff Sharkey        // If a persistent app is stuck in a crash loop, the device isn't very
340fe6f85cac9e823fd33a134f7129fdf7310703293Jeff Sharkey        // usable, so we want to consider sending out a rescue party.
341fe6f85cac9e823fd33a134f7129fdf7310703293Jeff Sharkey        if (r != null && r.persistent) {
342fe6f85cac9e823fd33a134f7129fdf7310703293Jeff Sharkey            RescueParty.notePersistentAppCrash(mContext, r.uid);
343fe6f85cac9e823fd33a134f7129fdf7310703293Jeff Sharkey        }
344fe6f85cac9e823fd33a134f7129fdf7310703293Jeff Sharkey
34520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        AppErrorResult result = new AppErrorResult();
34620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        TaskRecord task;
34720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        synchronized (mService) {
348fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            /**
349fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian             * If crash is handled by instance of {@link android.app.IActivityController},
350fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian             * finish now and don't show the app error dialog.
351fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian             */
352fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            if (handleAppCrashInActivityController(r, crashInfo, shortMsg, longMsg, stackTrace,
353b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu                    timeMillis, callingPid, callingUid)) {
354fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                return;
35520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
35620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
357fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            /**
358fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian             * If this process was running instrumentation, finish now - it will be handled in
359fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian             * {@link ActivityManagerService#handleAppDiedLocked}.
360fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian             */
361340417356d92d0db71d0692344e66886ca795dfdDianne Hackborn            if (r != null && r.instr != null) {
36220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                return;
36320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
36420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
36520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // Log crash in battery stats.
36620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (r != null) {
36720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                mService.mBatteryStatsService.noteProcessCrash(r.processName, r.uid);
36820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
36920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
37020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            AppErrorDialog.Data data = new AppErrorDialog.Data();
37120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            data.result = result;
37220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            data.proc = r;
37320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
37420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // If we can't identify the process or it's already exceeded its crash quota,
37520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // quit right away without showing a crash dialog.
37620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace, data)) {
37720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                return;
37820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
37920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
3809645b0ffda8d328fd563ae9ad611c18a44102930Wale Ogunwale            final Message msg = Message.obtain();
38120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            msg.what = ActivityManagerService.SHOW_ERROR_UI_MSG;
38220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
38320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            task = data.task;
38420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            msg.obj = data;
38520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mService.mUiHandler.sendMessage(msg);
38620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
38720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
38820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        int res = result.get();
38920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
39020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        Intent appErrorIntent = null;
391fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_CRASH, res);
392ad028c1616be016e6bef0d9a664d3a0054804e01Adrian Roos        if (res == AppErrorDialog.TIMEOUT || res == AppErrorDialog.CANCEL) {
393fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            res = AppErrorDialog.FORCE_QUIT;
394fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        }
395fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        synchronized (mService) {
396fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            if (res == AppErrorDialog.MUTE) {
397fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                stopReportingCrashesLocked(r);
398fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            }
399fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            if (res == AppErrorDialog.RESTART) {
400fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                mService.removeProcessLocked(r, false, true, "crash");
401fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                if (task != null) {
40220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    try {
403fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        mService.startActivityFromRecents(task.taskId,
404fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                                ActivityOptions.makeBasic().toBundle());
405fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    } catch (IllegalArgumentException e) {
406fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        // Hmm, that didn't work, app might have crashed before creating a
407fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        // recents entry. Let's see if we have a safe-to-restart intent.
4084d8959489d6b4123340dd2113044548b37d1b370Dianne Hackborn                        final Set<String> cats = task.intent.getCategories();
4094d8959489d6b4123340dd2113044548b37d1b370Dianne Hackborn                        if (cats != null && cats.contains(Intent.CATEGORY_LAUNCHER)) {
410d3624e10392e20158eb2ec1c1cd8ec5bb0914a47Bryce Lee                            mService.getActivityStartController().startActivityInPackage(
411d3624e10392e20158eb2ec1c1cd8ec5bb0914a47Bryce Lee                                    task.mCallingUid, task.mCallingPackage, task.intent, null, null,
412d3624e10392e20158eb2ec1c1cd8ec5bb0914a47Bryce Lee                                    null, 0, 0, ActivityOptions.makeBasic().toBundle(), task.userId,
413d3624e10392e20158eb2ec1c1cd8ec5bb0914a47Bryce Lee                                    null, "AppErrors");
41420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        }
41520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
41620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
417fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            }
418fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            if (res == AppErrorDialog.FORCE_QUIT) {
419fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                long orig = Binder.clearCallingIdentity();
420fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                try {
421fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    // Kill it with fire!
422fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    mService.mStackSupervisor.handleAppCrashLocked(r);
423fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    if (!r.persistent) {
424fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        mService.removeProcessLocked(r, false, false, "crash");
425fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
426fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    }
427fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                } finally {
428fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    Binder.restoreCallingIdentity(orig);
42920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
43020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
431fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) {
432fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo);
433fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            }
434fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            if (r != null && !r.isolated && res != AppErrorDialog.RESTART) {
435fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                // XXX Can't keep track of crash time for isolated processes,
436fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                // since they don't have a persistent identity.
437fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                mProcessCrashTimes.put(r.info.processName, r.uid,
438fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        SystemClock.uptimeMillis());
439fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            }
44020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
44120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
44220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (appErrorIntent != null) {
44320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            try {
44420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                mContext.startActivityAsUser(appErrorIntent, new UserHandle(r.userId));
44520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            } catch (ActivityNotFoundException e) {
44620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Slog.w(TAG, "bug report receiver dissappeared", e);
44720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
44820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
44920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
45020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
451fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian    private boolean handleAppCrashInActivityController(ProcessRecord r,
452fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                                                       ApplicationErrorReport.CrashInfo crashInfo,
453fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                                                       String shortMsg, String longMsg,
454b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu                                                       String stackTrace, long timeMillis,
455b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu                                                       int callingPid, int callingUid) {
456fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        if (mService.mController == null) {
457fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            return false;
458fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        }
459fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian
460fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        try {
461fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            String name = r != null ? r.processName : null;
462b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu            int pid = r != null ? r.pid : callingPid;
463b5e249963141b2ab00d1a9b5e4b76d5689d64f2eMark Lu            int uid = r != null ? r.info.uid : callingUid;
464fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            if (!mService.mController.appCrashed(name, pid,
465fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) {
466fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                if ("1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"))
467fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        && "Native crash".equals(crashInfo.exceptionClassName)) {
468fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    Slog.w(TAG, "Skip killing native crashed app " + name
469fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                            + "(" + pid + ") during testing");
470fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                } else {
471fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    Slog.w(TAG, "Force-killing crashed app " + name
472fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                            + " at watcher's request");
473fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    if (r != null) {
474fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        if (!makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace, null))
475fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        {
476fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                            r.kill("crash", true);
477fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        }
478fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    } else {
479fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        // Huh.
480fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        Process.killProcess(pid);
481fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                        ActivityManagerService.killProcessGroup(uid, pid);
482fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                    }
483fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                }
484fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian                return true;
485fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            }
486fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        } catch (RemoteException e) {
487fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            mService.mController = null;
488fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian            Watchdog.getInstance().setActivityController(null);
489fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        }
490fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian        return false;
491fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian    }
492fa3991aae02676d5c619c86adf514def09802d78Andrii Kulian
49320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private boolean makeAppCrashingLocked(ProcessRecord app,
49420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            String shortMsg, String longMsg, String stackTrace, AppErrorDialog.Data data) {
49520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.crashing = true;
49620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.crashingReport = generateProcessError(app,
49720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace);
49820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        startAppProblemLocked(app);
49920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.stopFreezingAllLocked();
50020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        return handleAppCrashLocked(app, "force-crash" /*reason*/, shortMsg, longMsg, stackTrace,
50120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                data);
50220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
50320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
50420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    void startAppProblemLocked(ProcessRecord app) {
50520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // If this app is not running under the current user, then we
50620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // can't give it a report button because that would require
50720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // launching the report UI under a different user.
50820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.errorReportReceiver = null;
50920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
5101b3edacd6a381ae02e6b82cb9a4750ea8cb9ec84Fyodor Kupolov        for (int userId : mService.mUserController.getCurrentProfileIds()) {
51120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (app.userId == userId) {
51220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
51320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        mContext, app.info.packageName, app.info.flags);
51420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
51520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
51620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        mService.skipCurrentReceiverLocked(app);
51720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
51820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
51920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    /**
52020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * Generate a process error record, suitable for attachment to a ProcessRecord.
52120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     *
52220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * @param app The ProcessRecord in which the error occurred.
52320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * @param condition Crashing, Application Not Responding, etc.  Values are defined in
524f6690100be5c5cd75c64d9a6a0345acff7b754d1Sudheer Shanka     *                      ActivityManager.ProcessErrorStateInfo
52520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * @param activity The activity associated with the crash, if known.
52620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * @param shortMsg Short message describing the crash.
52720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * @param longMsg Long message describing the crash.
52820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * @param stackTrace Full crash stack trace, may be null.
52920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     *
530f6690100be5c5cd75c64d9a6a0345acff7b754d1Sudheer Shanka     * @return Returns a fully-formed ProcessErrorStateInfo record.
53120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     */
53220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app,
53320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            int condition, String activity, String shortMsg, String longMsg, String stackTrace) {
53420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo();
53520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
53620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.condition = condition;
53720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.processName = app.processName;
53820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.pid = app.pid;
53920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.uid = app.info.uid;
54020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.tag = activity;
54120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.shortMsg = shortMsg;
54220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.longMsg = longMsg;
54320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.stackTrace = stackTrace;
54420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
54520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        return report;
54620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
54720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
54820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    Intent createAppErrorIntentLocked(ProcessRecord r,
54920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
55020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo);
55120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (report == null) {
55220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            return null;
55320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
55420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        Intent result = new Intent(Intent.ACTION_APP_ERROR);
55520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        result.setComponent(r.errorReportReceiver);
55620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        result.putExtra(Intent.EXTRA_BUG_REPORT, report);
55720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
55820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        return result;
55920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
56020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
56120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r,
56220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
56320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (r.errorReportReceiver == null) {
56420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            return null;
56520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
56620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
56720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (!r.crashing && !r.notResponding && !r.forceCrashReport) {
56820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            return null;
56920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
57020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
57120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        ApplicationErrorReport report = new ApplicationErrorReport();
57220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.packageName = r.info.packageName;
57320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.installerPackageName = r.errorReportReceiver.getPackageName();
57420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.processName = r.processName;
57520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.time = timeMillis;
57620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
57720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
57820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (r.crashing || r.forceCrashReport) {
57920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            report.type = ApplicationErrorReport.TYPE_CRASH;
58020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            report.crashInfo = crashInfo;
58120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        } else if (r.notResponding) {
58220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            report.type = ApplicationErrorReport.TYPE_ANR;
58320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            report.anrInfo = new ApplicationErrorReport.AnrInfo();
58420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
58520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            report.anrInfo.activity = r.notRespondingReport.tag;
58620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            report.anrInfo.cause = r.notRespondingReport.shortMsg;
58720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            report.anrInfo.info = r.notRespondingReport.longMsg;
58820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
58920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
59020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        return report;
59120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
59220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
59320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    boolean handleAppCrashLocked(ProcessRecord app, String reason,
59420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            String shortMsg, String longMsg, String stackTrace, AppErrorDialog.Data data) {
595b0c8a887299a7f462c41408edda6043202143233Amith Yamasani        final long now = SystemClock.uptimeMillis();
596b0c8a887299a7f462c41408edda6043202143233Amith Yamasani        final boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
5976a7e08920c6c7d88352f66df6ba923b9156feb36Adrian Roos                Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
59820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
599b0c8a887299a7f462c41408edda6043202143233Amith Yamasani        final boolean procIsBoundForeground =
600b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            (app.curProcState == ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE);
601b0c8a887299a7f462c41408edda6043202143233Amith Yamasani
60220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        Long crashTime;
60320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        Long crashTimePersistent;
604b0c8a887299a7f462c41408edda6043202143233Amith Yamasani        boolean tryAgain = false;
605b0c8a887299a7f462c41408edda6043202143233Amith Yamasani
60620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (!app.isolated) {
60720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            crashTime = mProcessCrashTimes.get(app.info.processName, app.uid);
60820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            crashTimePersistent = mProcessCrashTimesPersistent.get(app.info.processName, app.uid);
60920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        } else {
61020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            crashTime = crashTimePersistent = null;
61120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
612b0c8a887299a7f462c41408edda6043202143233Amith Yamasani
613b0c8a887299a7f462c41408edda6043202143233Amith Yamasani        // Bump up the crash count of any services currently running in the proc.
614b0c8a887299a7f462c41408edda6043202143233Amith Yamasani        for (int i = app.services.size() - 1; i >= 0; i--) {
615b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            // Any services running in the application need to be placed
616b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            // back in the pending list.
617b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            ServiceRecord sr = app.services.valueAt(i);
618b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            // If the service was restarted a while ago, then reset crash count, else increment it.
619b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            if (now > sr.restartTime + ProcessList.MIN_CRASH_INTERVAL) {
620b0c8a887299a7f462c41408edda6043202143233Amith Yamasani                sr.crashCount = 1;
621b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            } else {
622b0c8a887299a7f462c41408edda6043202143233Amith Yamasani                sr.crashCount++;
623b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            }
624b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            // Allow restarting for started or bound foreground services that are crashing.
625b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            // This includes wallpapers.
626b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            if (sr.crashCount < mService.mConstants.BOUND_SERVICE_MAX_CRASH_RETRY
627b0c8a887299a7f462c41408edda6043202143233Amith Yamasani                    && (sr.isForeground || procIsBoundForeground)) {
628b0c8a887299a7f462c41408edda6043202143233Amith Yamasani                tryAgain = true;
629b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            }
630b0c8a887299a7f462c41408edda6043202143233Amith Yamasani        }
631b0c8a887299a7f462c41408edda6043202143233Amith Yamasani
632b0c8a887299a7f462c41408edda6043202143233Amith Yamasani        if (crashTime != null && now < crashTime + ProcessList.MIN_CRASH_INTERVAL) {
633b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            // The process crashed again very quickly. If it was a bound foreground service, let's
634b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            // try to restart again in a while, otherwise the process loses!
63520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            Slog.w(TAG, "Process " + app.info.processName
63620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    + " has crashed too many times: killing!");
63720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH,
63820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    app.userId, app.info.processName, app.uid);
63920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mService.mStackSupervisor.handleAppCrashLocked(app);
64020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (!app.persistent) {
64120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // We don't want to start this process again until the user
64220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // explicitly does so...  but for persistent process, we really
64320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // need to keep it running.  If a persistent process is actually
64420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // repeatedly crashing, then badness for everyone.
64520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.userId, app.uid,
64620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        app.info.processName);
64720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                if (!app.isolated) {
64820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    // XXX We don't have a way to mark isolated processes
64920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    // as bad, since they don't have a peristent identity.
65020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    mBadProcesses.put(app.info.processName, app.uid,
65120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            new BadProcessInfo(now, shortMsg, longMsg, stackTrace));
65220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    mProcessCrashTimes.remove(app.info.processName, app.uid);
65320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
65420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                app.bad = true;
65520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                app.removed = true;
65620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // Don't let services in this process be restarted and potentially
65720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // annoy the user repeatedly.  Unless it is persistent, since those
65820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // processes run critical code.
659b0c8a887299a7f462c41408edda6043202143233Amith Yamasani                mService.removeProcessLocked(app, false, tryAgain, "crash");
66020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
6616a7e08920c6c7d88352f66df6ba923b9156feb36Adrian Roos                if (!showBackground) {
6626a7e08920c6c7d88352f66df6ba923b9156feb36Adrian Roos                    return false;
6636a7e08920c6c7d88352f66df6ba923b9156feb36Adrian Roos                }
66420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
66520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
66620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        } else {
66720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            TaskRecord affectedTask =
66820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    mService.mStackSupervisor.finishTopRunningActivityLocked(app, reason);
66920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (data != null) {
67020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                data.task = affectedTask;
67120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
67220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (data != null && crashTimePersistent != null
67320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    && now < crashTimePersistent + ProcessList.MIN_CRASH_INTERVAL) {
67420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                data.repeating = true;
67520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
67620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
67720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
678b0c8a887299a7f462c41408edda6043202143233Amith Yamasani        if (data != null && tryAgain) {
679b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            data.isRestartableForService = true;
68020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
68120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
68220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // If the crashing process is what we consider to be the "home process" and it has been
68320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // replaced by a third-party app, clear the package preferred activities from packages
68420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // with a home activity running in the process to prevent a repeatedly crashing app
68520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // from blocking the user to manually clear the list.
68620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        final ArrayList<ActivityRecord> activities = app.activities;
68720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (app == mService.mHomeProcess && activities.size() > 0
68820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                && (mService.mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
68920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
69020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                final ActivityRecord r = activities.get(activityNdx);
6916fbde9fccb90c2715730a5a0ac9425d48b939558Wale Ogunwale                if (r.isActivityTypeHome()) {
69220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    Log.i(TAG, "Clearing package preferred activities from " + r.packageName);
69320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    try {
69420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        ActivityThread.getPackageManager()
69520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                                .clearPackagePreferredActivities(r.packageName);
69620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    } catch (RemoteException c) {
69720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        // pm is in same process, this will never happen.
69820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
69920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
70020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
70120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
70220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
70320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (!app.isolated) {
70420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // XXX Can't keep track of crash times for isolated processes,
705b0c8a887299a7f462c41408edda6043202143233Amith Yamasani            // because they don't have a persistent identity.
70620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mProcessCrashTimes.put(app.info.processName, app.uid, now);
70720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mProcessCrashTimesPersistent.put(app.info.processName, app.uid, now);
70820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
70920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
71020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (app.crashHandler != null) mService.mHandler.post(app.crashHandler);
71120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        return true;
71220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
71320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
71420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    void handleShowAppErrorUi(Message msg) {
71520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        AppErrorDialog.Data data = (AppErrorDialog.Data) msg.obj;
71620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
71720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
71820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        synchronized (mService) {
71920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            ProcessRecord proc = data.proc;
72020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            AppErrorResult res = data.result;
72120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (proc != null && proc.crashDialog != null) {
72220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Slog.e(TAG, "App already has crash dialog: " + proc);
72320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                if (res != null) {
7249046222cb2b1bd57278ddbf71d9f628f8dd254aeAdrian Roos                    res.set(AppErrorDialog.ALREADY_SHOWING);
72520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
72620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                return;
72720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
72820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            boolean isBackground = (UserHandle.getAppId(proc.uid)
72920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    >= Process.FIRST_APPLICATION_UID
73020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    && proc.pid != MY_PID);
7311b3edacd6a381ae02e6b82cb9a4750ea8cb9ec84Fyodor Kupolov            for (int userId : mService.mUserController.getCurrentProfileIds()) {
73220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                isBackground &= (proc.userId != userId);
73320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
73420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (isBackground && !showBackground) {
73520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Slog.w(TAG, "Skipping crash dialog of " + proc + ": background");
73620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                if (res != null) {
7379046222cb2b1bd57278ddbf71d9f628f8dd254aeAdrian Roos                    res.set(AppErrorDialog.BACKGROUND_USER);
73820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
73920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                return;
74020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
74120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            final boolean crashSilenced = mAppsNotReportingCrashes != null &&
74220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    mAppsNotReportingCrashes.contains(proc.info.packageName);
7436a7e08920c6c7d88352f66df6ba923b9156feb36Adrian Roos            if ((mService.canShowErrorDialogs() || showBackground) && !crashSilenced) {
744445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver                proc.crashDialog = new AppErrorDialog(mContext, mService, data);
74520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            } else {
74620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // The device is asleep, so just pretend that the user
74720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // saw a crash dialog and hit "force quit".
74820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                if (res != null) {
7499046222cb2b1bd57278ddbf71d9f628f8dd254aeAdrian Roos                    res.set(AppErrorDialog.CANT_SHOW);
75020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
75120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
75220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
753445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver        // If we've created a crash dialog, show it without the lock held
754445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver        if(data.proc.crashDialog != null) {
7555ddcca789172da68b7505fb301f6dc7cf173b4dcFyodor Kupolov            Slog.i(TAG, "Showing crash dialog for package " + data.proc.info.packageName
7565ddcca789172da68b7505fb301f6dc7cf173b4dcFyodor Kupolov                    + " u" + data.proc.userId);
757445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver            data.proc.crashDialog.show();
758445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver        }
75920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
76020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
76120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    void stopReportingCrashesLocked(ProcessRecord proc) {
76220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (mAppsNotReportingCrashes == null) {
76320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mAppsNotReportingCrashes = new ArraySet<>();
76420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
76520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        mAppsNotReportingCrashes.add(proc.info.packageName);
76620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
76720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
768a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath    static boolean isInterestingForBackgroundTraces(ProcessRecord app) {
769a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        // The system_server is always considered interesting.
770a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        if (app.pid == MY_PID) {
771a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath            return true;
772a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        }
773a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath
774a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        // A package is considered interesting if any of the following is true :
775a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        //
776a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        // - It's displaying an activity.
777a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        // - It's the SystemUI.
778a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        // - It has an overlay or a top UI visible.
779a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        //
780a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        // NOTE: The check whether a given ProcessRecord belongs to the systemui
781a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        // process is a bit of a kludge, but the same pattern seems repeated at
782a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        // several places in the system server.
783a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath        return app.isInterestingToUserLocked() ||
784a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath            (app.info != null && "com.android.systemui".equals(app.info.packageName)) ||
785a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath            (app.hasTopUi || app.hasOverlayUi);
786a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath    }
787a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath
78820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    final void appNotResponding(ProcessRecord app, ActivityRecord activity,
78920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            ActivityRecord parent, boolean aboveSystem, final String annotation) {
79020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        ArrayList<Integer> firstPids = new ArrayList<Integer>(5);
79120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20);
79220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
79320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (mService.mController != null) {
79420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            try {
79520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // 0 == continue, -1 = kill process immediately
796a85a2c63f191ae8c567ba6845c063cbe3dd750fcAdrian Roos                int res = mService.mController.appEarlyNotResponding(
797a85a2c63f191ae8c567ba6845c063cbe3dd750fcAdrian Roos                        app.processName, app.pid, annotation);
79820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                if (res < 0 && app.pid != MY_PID) {
79920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    app.kill("anr", true);
80020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
80120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            } catch (RemoteException e) {
80220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                mService.mController = null;
80320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Watchdog.getInstance().setActivityController(null);
80420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
80520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
80620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
80720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        long anrTime = SystemClock.uptimeMillis();
80820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (ActivityManagerService.MONITOR_CPU_USAGE) {
80920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mService.updateCpuStatsNow();
81020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
81120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
812f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray        // Unless configured otherwise, swallow ANRs in background processes & kill the process.
813f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray        boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
814f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
815f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray
816f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray        boolean isSilentANR;
817f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray
81820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        synchronized (mService) {
81920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
82020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (mService.mShuttingDown) {
82120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation);
82220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                return;
82320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            } else if (app.notResponding) {
82420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation);
82520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                return;
82620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            } else if (app.crashing) {
82720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation);
82820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                return;
829313177dccacaf5fefd2954d90c065e35e5b03253Tobias Lindskog            } else if (app.killedByAm) {
830313177dccacaf5fefd2954d90c065e35e5b03253Tobias Lindskog                Slog.i(TAG, "App already killed by AM skipping ANR: " + app + " " + annotation);
831313177dccacaf5fefd2954d90c065e35e5b03253Tobias Lindskog                return;
83241d65f28c202bd405f60f7739fd2cd775bb35213Mark Lu            } else if (app.killed) {
83341d65f28c202bd405f60f7739fd2cd775bb35213Mark Lu                Slog.i(TAG, "Skipping died app ANR: " + app + " " + annotation);
83441d65f28c202bd405f60f7739fd2cd775bb35213Mark Lu                return;
83520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
83620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
83720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // In case we come through here for the same app before completing
83820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // this one, mark as anring now so we will bail out.
83920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            app.notResponding = true;
84020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
84120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // Log the ANR to the event log.
84220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            EventLog.writeEvent(EventLogTags.AM_ANR, app.userId, app.pid,
84320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    app.processName, app.info.flags, annotation);
84420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
84520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // Dump thread traces as quickly as we can, starting with "interesting" processes.
84620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            firstPids.add(app.pid);
84720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
848f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray            // Don't dump other PIDs if it's a background ANR
849a22d9fbe8c1cf3cb47db0bfecbd4fd84c59b6ec0Narayan Kamath            isSilentANR = !showBackground && !isInterestingForBackgroundTraces(app);
850f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray            if (!isSilentANR) {
851f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                int parentPid = app.pid;
852f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                if (parent != null && parent.app != null && parent.app.pid > 0) {
853f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                    parentPid = parent.app.pid;
854f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                }
855f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                if (parentPid != app.pid) firstPids.add(parentPid);
856f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray
857f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID);
858f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray
859f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                for (int i = mService.mLruProcesses.size() - 1; i >= 0; i--) {
860f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                    ProcessRecord r = mService.mLruProcesses.get(i);
861f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                    if (r != null && r.thread != null) {
862f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                        int pid = r.pid;
863f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                        if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) {
864f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                            if (r.persistent) {
865f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                                firstPids.add(pid);
866f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                                if (DEBUG_ANR) Slog.i(TAG, "Adding persistent proc: " + r);
86790d1e363ed4956e3454b943407a7ebee9ae6e351Brian Carlstrom                            } else if (r.treatLikeActivity) {
86890d1e363ed4956e3454b943407a7ebee9ae6e351Brian Carlstrom                                firstPids.add(pid);
86990d1e363ed4956e3454b943407a7ebee9ae6e351Brian Carlstrom                                if (DEBUG_ANR) Slog.i(TAG, "Adding likely IME: " + r);
870f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                            } else {
871f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                                lastPids.put(pid, Boolean.TRUE);
872f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                                if (DEBUG_ANR) Slog.i(TAG, "Adding ANR proc: " + r);
873f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray                            }
87420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        }
87520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
87620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
87720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
87820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
87920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
88020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        // Log the ANR to the main log.
88120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        StringBuilder info = new StringBuilder();
88220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        info.setLength(0);
88320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        info.append("ANR in ").append(app.processName);
88420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (activity != null && activity.shortComponentName != null) {
88520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            info.append(" (").append(activity.shortComponentName).append(")");
88620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
88720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        info.append("\n");
88820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        info.append("PID: ").append(app.pid).append("\n");
88920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (annotation != null) {
89020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            info.append("Reason: ").append(annotation).append("\n");
89120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
89220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (parent != null && parent != activity) {
89320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            info.append("Parent: ").append(parent.shortComponentName).append("\n");
89420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
89520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
896f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray        ProcessCpuTracker processCpuTracker = new ProcessCpuTracker(true);
89720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
89866524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom        // don't dump native PIDs for background ANRs unless it is the process of interest
89966524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom        String[] nativeProcs = null;
900f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray        if (isSilentANR) {
90166524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom            for (int i = 0; i < NATIVE_STACKS_OF_INTEREST.length; i++) {
90266524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom                if (NATIVE_STACKS_OF_INTEREST[i].equals(app.processName)) {
90366524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom                    nativeProcs = new String[] { app.processName };
90466524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom                    break;
90566524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom                }
90666524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom            }
907f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray        } else {
90866524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom            nativeProcs = NATIVE_STACKS_OF_INTEREST;
909f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray        }
91020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
91194a0dcdde12192ad0815ec88f8ac62152f6a58b4Martijn Coenen        int[] pids = nativeProcs == null ? null : Process.getPidsForCommands(nativeProcs);
9126b47c54a65d8c0aca9a6523a926a386ecfaec069Steven Moreland        ArrayList<Integer> nativePids = null;
9136b47c54a65d8c0aca9a6523a926a386ecfaec069Steven Moreland
9146b47c54a65d8c0aca9a6523a926a386ecfaec069Steven Moreland        if (pids != null) {
9156b47c54a65d8c0aca9a6523a926a386ecfaec069Steven Moreland            nativePids = new ArrayList<Integer>(pids.length);
9166b47c54a65d8c0aca9a6523a926a386ecfaec069Steven Moreland            for (int i : pids) {
9176b47c54a65d8c0aca9a6523a926a386ecfaec069Steven Moreland                nativePids.add(i);
9186b47c54a65d8c0aca9a6523a926a386ecfaec069Steven Moreland            }
9196b47c54a65d8c0aca9a6523a926a386ecfaec069Steven Moreland        }
9206b47c54a65d8c0aca9a6523a926a386ecfaec069Steven Moreland
92166524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom        // For background ANRs, don't pass the ProcessCpuTracker to
92266524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom        // avoid spending 1/2 second collecting stats to rank lastPids.
923f013daa3ac7718c983cf850784f5cf29ee027487Narayan Kamath        File tracesFile = ActivityManagerService.dumpStackTraces(
924f013daa3ac7718c983cf850784f5cf29ee027487Narayan Kamath                true, firstPids,
925f013daa3ac7718c983cf850784f5cf29ee027487Narayan Kamath                (isSilentANR) ? null : processCpuTracker,
926f013daa3ac7718c983cf850784f5cf29ee027487Narayan Kamath                (isSilentANR) ? null : lastPids,
927f013daa3ac7718c983cf850784f5cf29ee027487Narayan Kamath                nativePids);
92866524247d4d5f21a86624a6ccdd5f014bf5aa8b6Brian Carlstrom
92920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        String cpuInfo = null;
93020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (ActivityManagerService.MONITOR_CPU_USAGE) {
93120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mService.updateCpuStatsNow();
93220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            synchronized (mService.mProcessCpuTracker) {
93320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                cpuInfo = mService.mProcessCpuTracker.printCurrentState(anrTime);
93420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
93520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            info.append(processCpuTracker.printCurrentLoad());
93620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            info.append(cpuInfo);
93720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
93820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
93920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        info.append(processCpuTracker.printCurrentState(anrTime));
94020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
94120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        Slog.e(TAG, info.toString());
94220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (tracesFile == null) {
94320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // There is no trace file, so dump (only) the alleged culprit's threads to the log
94420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            Process.sendSignal(app.pid, Process.SIGNAL_QUIT);
94520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
94620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
94720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        mService.addErrorToDropBox("anr", app, app.processName, activity, parent, annotation,
94820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                cpuInfo, tracesFile, null);
94920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
95020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        if (mService.mController != null) {
95120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            try {
95220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
95320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                int res = mService.mController.appNotResponding(
95420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        app.processName, app.pid, info.toString());
95520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                if (res != 0) {
95620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    if (res < 0 && app.pid != MY_PID) {
95720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        app.kill("anr", true);
95820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    } else {
95920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        synchronized (mService) {
96020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                            mService.mServices.scheduleServiceTimeoutLocked(app);
96120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        }
96220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    }
96320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    return;
96420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                }
96520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            } catch (RemoteException e) {
96620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                mService.mController = null;
96720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Watchdog.getInstance().setActivityController(null);
96820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
96920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
97020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
97120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        synchronized (mService) {
97220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mService.mBatteryStatsService.noteProcessAnr(app.processName, app.uid);
97320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
974f0f9a829818a98236c81b96f9fe0f9ac3125eb1dTim Murray            if (isSilentANR) {
97520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                app.kill("bg anr", true);
97620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                return;
97720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
97820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
97920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // Set the app's notResponding state, and look up the errorReportReceiver
98020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            makeAppNotRespondingLocked(app,
98120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    activity != null ? activity.shortComponentName : null,
98220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    annotation != null ? "ANR " + annotation : "ANR",
98320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    info.toString());
98420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
98520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            // Bring up the infamous App Not Responding dialog
98620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            Message msg = Message.obtain();
98720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            HashMap<String, Object> map = new HashMap<String, Object>();
98820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            msg.what = ActivityManagerService.SHOW_NOT_RESPONDING_UI_MSG;
98920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            msg.obj = map;
99020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            msg.arg1 = aboveSystem ? 1 : 0;
99120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            map.put("app", app);
99220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (activity != null) {
99320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                map.put("activity", activity);
99420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
99520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
99620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mService.mUiHandler.sendMessage(msg);
99720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
99820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
99920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
100020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    private void makeAppNotRespondingLocked(ProcessRecord app,
100120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            String activity, String shortMsg, String longMsg) {
100220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.notResponding = true;
100320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.notRespondingReport = generateProcessError(app,
100420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
100520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                activity, shortMsg, longMsg, null);
100620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        startAppProblemLocked(app);
100720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        app.stopFreezingAllLocked();
100820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
100920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
101020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    void handleShowAnrUi(Message msg) {
1011445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver        Dialog d = null;
101220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        synchronized (mService) {
101320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
101420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            ProcessRecord proc = (ProcessRecord)data.get("app");
101520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (proc != null && proc.anrDialog != null) {
101620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                Slog.e(TAG, "App already has anr dialog: " + proc);
10179046222cb2b1bd57278ddbf71d9f628f8dd254aeAdrian Roos                MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_ANR,
10189046222cb2b1bd57278ddbf71d9f628f8dd254aeAdrian Roos                        AppNotRespondingDialog.ALREADY_SHOWING);
101920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                return;
102020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
102120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
102220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            Intent intent = new Intent("android.intent.action.ANR");
102320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            if (!mService.mProcessesReady) {
102420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
102520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        | Intent.FLAG_RECEIVER_FOREGROUND);
102620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
102720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            mService.broadcastIntentLocked(null, null, intent,
102820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    null, null, 0, null, null, null, AppOpsManager.OP_NONE,
102920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                    null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
103020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
10316a7e08920c6c7d88352f66df6ba923b9156feb36Adrian Roos            boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
10326a7e08920c6c7d88352f66df6ba923b9156feb36Adrian Roos                    Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
10336a7e08920c6c7d88352f66df6ba923b9156feb36Adrian Roos            if (mService.canShowErrorDialogs() || showBackground) {
1034445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver                d = new AppNotRespondingDialog(mService,
103520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        mContext, proc, (ActivityRecord)data.get("activity"),
103620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                        msg.arg1 != 0);
103720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                proc.anrDialog = d;
103820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            } else {
10399046222cb2b1bd57278ddbf71d9f628f8dd254aeAdrian Roos                MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_ANR,
10409046222cb2b1bd57278ddbf71d9f628f8dd254aeAdrian Roos                        AppNotRespondingDialog.CANT_SHOW);
104120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                // Just kill the app if there is no dialog to be shown.
104220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos                mService.killAppAtUsersRequest(proc, null);
104320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            }
104420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
1045445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver        // If we've created a crash dialog, show it without the lock held
1046445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver        if (d != null) {
1047445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver            d.show();
1048445fd2afe9336cecf76e41d78aa0b6f3b7053700Phil Weaver        }
104920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
105020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
105120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    /**
105220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     * Information about a process that is currently marked as bad.
105320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos     */
105420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    static final class BadProcessInfo {
105520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        BadProcessInfo(long time, String shortMsg, String longMsg, String stack) {
105620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            this.time = time;
105720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            this.shortMsg = shortMsg;
105820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            this.longMsg = longMsg;
105920d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos            this.stack = stack;
106020d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        }
106120d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
106220d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        final long time;
106320d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        final String shortMsg;
106420d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        final String longMsg;
106520d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos        final String stack;
106620d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos    }
106720d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos
106820d7df3c3ff0000678a208b25fcf7ddf90c5abe4Adrian Roos}
1069