Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
Wale Ogunwale | 6425836 | 2018-10-16 15:13:37 -0700 | [diff] [blame] | 19 | import static android.app.ActivityTaskManager.INVALID_TASK_ID; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 20 | import static android.content.pm.ApplicationInfo.FLAG_SYSTEM; |
Adrian Roos | 86cb639 | 2018-12-13 16:42:43 +0100 | [diff] [blame] | 21 | |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 22 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 23 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 24 | import static com.android.server.am.ActivityManagerService.MY_PID; |
| 25 | import static com.android.server.am.ActivityManagerService.SYSTEM_DEBUGGABLE; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 26 | import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_FREE_RESIZE; |
| 27 | import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_NONE; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 28 | |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 29 | import android.app.ActivityManager; |
| 30 | import android.app.ActivityOptions; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 31 | import android.app.ApplicationErrorReport; |
| 32 | import android.app.Dialog; |
| 33 | import android.content.ActivityNotFoundException; |
| 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
| 36 | import android.content.pm.ApplicationInfo; |
Andrew Sapperstein | 5b679c4 | 2018-01-16 11:13:40 -0800 | [diff] [blame] | 37 | import android.net.Uri; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 38 | import android.os.Binder; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 39 | import android.os.Message; |
| 40 | import android.os.Process; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 41 | import android.os.SystemClock; |
| 42 | import android.os.SystemProperties; |
| 43 | import android.os.UserHandle; |
| 44 | import android.provider.Settings; |
| 45 | import android.util.ArrayMap; |
| 46 | import android.util.ArraySet; |
| 47 | import android.util.EventLog; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 48 | import android.util.Slog; |
| 49 | import android.util.SparseArray; |
| 50 | import android.util.TimeUtils; |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 51 | import android.util.proto.ProtoOutputStream; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 52 | |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 53 | import com.android.internal.app.ProcessMap; |
| 54 | import com.android.internal.logging.MetricsLogger; |
| 55 | import com.android.internal.logging.nano.MetricsProto; |
Zimuzo | 6efba54 | 2018-11-29 12:47:58 +0000 | [diff] [blame] | 56 | import com.android.server.PackageWatchdog; |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 57 | import com.android.server.RescueParty; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 58 | import com.android.server.wm.WindowProcessController; |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 59 | |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 60 | import java.io.FileDescriptor; |
| 61 | import java.io.PrintWriter; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 62 | import java.util.Collections; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 63 | |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 64 | /** |
| 65 | * Controls error conditions in applications. |
| 66 | */ |
| 67 | class AppErrors { |
| 68 | |
| 69 | private static final String TAG = TAG_WITH_CLASS_NAME ? "AppErrors" : TAG_AM; |
| 70 | |
| 71 | private final ActivityManagerService mService; |
| 72 | private final Context mContext; |
Zimuzo | 6efba54 | 2018-11-29 12:47:58 +0000 | [diff] [blame] | 73 | private final PackageWatchdog mPackageWatchdog; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 74 | |
| 75 | private ArraySet<String> mAppsNotReportingCrashes; |
| 76 | |
| 77 | /** |
| 78 | * The last time that various processes have crashed since they were last explicitly started. |
| 79 | */ |
| 80 | private final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<>(); |
| 81 | |
| 82 | /** |
| 83 | * The last time that various processes have crashed (not reset even when explicitly started). |
| 84 | */ |
| 85 | private final ProcessMap<Long> mProcessCrashTimesPersistent = new ProcessMap<>(); |
| 86 | |
| 87 | /** |
| 88 | * Set of applications that we consider to be bad, and will reject |
| 89 | * incoming broadcasts from (which the user has no control over). |
| 90 | * Processes are added to this set when they have crashed twice within |
| 91 | * a minimum amount of time; they are removed from it when they are |
| 92 | * later restarted (hopefully due to some user action). The value is the |
| 93 | * time it was added to the list. |
| 94 | */ |
| 95 | private final ProcessMap<BadProcessInfo> mBadProcesses = new ProcessMap<>(); |
| 96 | |
| 97 | |
Zimuzo | 6efba54 | 2018-11-29 12:47:58 +0000 | [diff] [blame] | 98 | AppErrors(Context context, ActivityManagerService service, PackageWatchdog watchdog) { |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 99 | context.assertRuntimeOverlayThemable(); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 100 | mService = service; |
| 101 | mContext = context; |
Zimuzo | 6efba54 | 2018-11-29 12:47:58 +0000 | [diff] [blame] | 102 | mPackageWatchdog = watchdog; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 105 | void writeToProto(ProtoOutputStream proto, long fieldId, String dumpPackage) { |
| 106 | if (mProcessCrashTimes.getMap().isEmpty() && mBadProcesses.getMap().isEmpty()) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | final long token = proto.start(fieldId); |
| 111 | final long now = SystemClock.uptimeMillis(); |
| 112 | proto.write(AppErrorsProto.NOW_UPTIME_MS, now); |
| 113 | |
| 114 | if (!mProcessCrashTimes.getMap().isEmpty()) { |
| 115 | final ArrayMap<String, SparseArray<Long>> pmap = mProcessCrashTimes.getMap(); |
| 116 | final int procCount = pmap.size(); |
| 117 | for (int ip = 0; ip < procCount; ip++) { |
| 118 | final long ctoken = proto.start(AppErrorsProto.PROCESS_CRASH_TIMES); |
| 119 | final String pname = pmap.keyAt(ip); |
| 120 | final SparseArray<Long> uids = pmap.valueAt(ip); |
| 121 | final int uidCount = uids.size(); |
| 122 | |
| 123 | proto.write(AppErrorsProto.ProcessCrashTime.PROCESS_NAME, pname); |
| 124 | for (int i = 0; i < uidCount; i++) { |
| 125 | final int puid = uids.keyAt(i); |
Amith Yamasani | 98a0092 | 2018-08-21 12:50:30 -0400 | [diff] [blame] | 126 | final ProcessRecord r = mService.getProcessNames().get(pname, puid); |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 127 | if (dumpPackage != null && (r == null || !r.pkgList.containsKey(dumpPackage))) { |
| 128 | continue; |
| 129 | } |
| 130 | final long etoken = proto.start(AppErrorsProto.ProcessCrashTime.ENTRIES); |
| 131 | proto.write(AppErrorsProto.ProcessCrashTime.Entry.UID, puid); |
| 132 | proto.write(AppErrorsProto.ProcessCrashTime.Entry.LAST_CRASHED_AT_MS, |
| 133 | uids.valueAt(i)); |
| 134 | proto.end(etoken); |
| 135 | } |
| 136 | proto.end(ctoken); |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | |
| 141 | if (!mBadProcesses.getMap().isEmpty()) { |
| 142 | final ArrayMap<String, SparseArray<BadProcessInfo>> pmap = mBadProcesses.getMap(); |
| 143 | final int processCount = pmap.size(); |
| 144 | for (int ip = 0; ip < processCount; ip++) { |
| 145 | final long btoken = proto.start(AppErrorsProto.BAD_PROCESSES); |
| 146 | final String pname = pmap.keyAt(ip); |
| 147 | final SparseArray<BadProcessInfo> uids = pmap.valueAt(ip); |
| 148 | final int uidCount = uids.size(); |
| 149 | |
| 150 | proto.write(AppErrorsProto.BadProcess.PROCESS_NAME, pname); |
| 151 | for (int i = 0; i < uidCount; i++) { |
| 152 | final int puid = uids.keyAt(i); |
Amith Yamasani | 98a0092 | 2018-08-21 12:50:30 -0400 | [diff] [blame] | 153 | final ProcessRecord r = mService.getProcessNames().get(pname, puid); |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 154 | if (dumpPackage != null && (r == null |
| 155 | || !r.pkgList.containsKey(dumpPackage))) { |
| 156 | continue; |
| 157 | } |
| 158 | final BadProcessInfo info = uids.valueAt(i); |
| 159 | final long etoken = proto.start(AppErrorsProto.BadProcess.ENTRIES); |
| 160 | proto.write(AppErrorsProto.BadProcess.Entry.UID, puid); |
| 161 | proto.write(AppErrorsProto.BadProcess.Entry.CRASHED_AT_MS, info.time); |
| 162 | proto.write(AppErrorsProto.BadProcess.Entry.SHORT_MSG, info.shortMsg); |
| 163 | proto.write(AppErrorsProto.BadProcess.Entry.LONG_MSG, info.longMsg); |
| 164 | proto.write(AppErrorsProto.BadProcess.Entry.STACK, info.stack); |
| 165 | proto.end(etoken); |
| 166 | } |
| 167 | proto.end(btoken); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | proto.end(token); |
| 172 | } |
| 173 | |
| 174 | boolean dumpLocked(FileDescriptor fd, PrintWriter pw, boolean needSep, String dumpPackage) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 175 | if (!mProcessCrashTimes.getMap().isEmpty()) { |
| 176 | boolean printed = false; |
| 177 | final long now = SystemClock.uptimeMillis(); |
| 178 | final ArrayMap<String, SparseArray<Long>> pmap = mProcessCrashTimes.getMap(); |
| 179 | final int processCount = pmap.size(); |
| 180 | for (int ip = 0; ip < processCount; ip++) { |
| 181 | final String pname = pmap.keyAt(ip); |
| 182 | final SparseArray<Long> uids = pmap.valueAt(ip); |
| 183 | final int uidCount = uids.size(); |
| 184 | for (int i = 0; i < uidCount; i++) { |
| 185 | final int puid = uids.keyAt(i); |
Amith Yamasani | 98a0092 | 2018-08-21 12:50:30 -0400 | [diff] [blame] | 186 | final ProcessRecord r = mService.getProcessNames().get(pname, puid); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 187 | if (dumpPackage != null && (r == null |
| 188 | || !r.pkgList.containsKey(dumpPackage))) { |
| 189 | continue; |
| 190 | } |
| 191 | if (!printed) { |
| 192 | if (needSep) pw.println(); |
| 193 | needSep = true; |
| 194 | pw.println(" Time since processes crashed:"); |
| 195 | printed = true; |
| 196 | } |
| 197 | pw.print(" Process "); pw.print(pname); |
| 198 | pw.print(" uid "); pw.print(puid); |
| 199 | pw.print(": last crashed "); |
| 200 | TimeUtils.formatDuration(now-uids.valueAt(i), pw); |
| 201 | pw.println(" ago"); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (!mBadProcesses.getMap().isEmpty()) { |
| 207 | boolean printed = false; |
| 208 | final ArrayMap<String, SparseArray<BadProcessInfo>> pmap = mBadProcesses.getMap(); |
| 209 | final int processCount = pmap.size(); |
| 210 | for (int ip = 0; ip < processCount; ip++) { |
| 211 | final String pname = pmap.keyAt(ip); |
| 212 | final SparseArray<BadProcessInfo> uids = pmap.valueAt(ip); |
| 213 | final int uidCount = uids.size(); |
| 214 | for (int i = 0; i < uidCount; i++) { |
| 215 | final int puid = uids.keyAt(i); |
Amith Yamasani | 98a0092 | 2018-08-21 12:50:30 -0400 | [diff] [blame] | 216 | final ProcessRecord r = mService.getProcessNames().get(pname, puid); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 217 | if (dumpPackage != null && (r == null |
| 218 | || !r.pkgList.containsKey(dumpPackage))) { |
| 219 | continue; |
| 220 | } |
| 221 | if (!printed) { |
| 222 | if (needSep) pw.println(); |
| 223 | needSep = true; |
| 224 | pw.println(" Bad processes:"); |
| 225 | printed = true; |
| 226 | } |
| 227 | final BadProcessInfo info = uids.valueAt(i); |
| 228 | pw.print(" Bad process "); pw.print(pname); |
| 229 | pw.print(" uid "); pw.print(puid); |
| 230 | pw.print(": crashed at time "); pw.println(info.time); |
| 231 | if (info.shortMsg != null) { |
| 232 | pw.print(" Short msg: "); pw.println(info.shortMsg); |
| 233 | } |
| 234 | if (info.longMsg != null) { |
| 235 | pw.print(" Long msg: "); pw.println(info.longMsg); |
| 236 | } |
| 237 | if (info.stack != null) { |
| 238 | pw.println(" Stack:"); |
| 239 | int lastPos = 0; |
| 240 | for (int pos = 0; pos < info.stack.length(); pos++) { |
| 241 | if (info.stack.charAt(pos) == '\n') { |
| 242 | pw.print(" "); |
| 243 | pw.write(info.stack, lastPos, pos-lastPos); |
| 244 | pw.println(); |
| 245 | lastPos = pos+1; |
| 246 | } |
| 247 | } |
| 248 | if (lastPos < info.stack.length()) { |
| 249 | pw.print(" "); |
| 250 | pw.write(info.stack, lastPos, info.stack.length()-lastPos); |
| 251 | pw.println(); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | return needSep; |
| 258 | } |
| 259 | |
| 260 | boolean isBadProcessLocked(ApplicationInfo info) { |
| 261 | return mBadProcesses.get(info.processName, info.uid) != null; |
| 262 | } |
| 263 | |
| 264 | void clearBadProcessLocked(ApplicationInfo info) { |
| 265 | mBadProcesses.remove(info.processName, info.uid); |
| 266 | } |
| 267 | |
| 268 | void resetProcessCrashTimeLocked(ApplicationInfo info) { |
| 269 | mProcessCrashTimes.remove(info.processName, info.uid); |
| 270 | } |
| 271 | |
| 272 | void resetProcessCrashTimeLocked(boolean resetEntireUser, int appId, int userId) { |
| 273 | final ArrayMap<String, SparseArray<Long>> pmap = mProcessCrashTimes.getMap(); |
| 274 | for (int ip = pmap.size() - 1; ip >= 0; ip--) { |
| 275 | SparseArray<Long> ba = pmap.valueAt(ip); |
| 276 | for (int i = ba.size() - 1; i >= 0; i--) { |
| 277 | boolean remove = false; |
| 278 | final int entUid = ba.keyAt(i); |
| 279 | if (!resetEntireUser) { |
| 280 | if (userId == UserHandle.USER_ALL) { |
| 281 | if (UserHandle.getAppId(entUid) == appId) { |
| 282 | remove = true; |
| 283 | } |
| 284 | } else { |
| 285 | if (entUid == UserHandle.getUid(userId, appId)) { |
| 286 | remove = true; |
| 287 | } |
| 288 | } |
| 289 | } else if (UserHandle.getUserId(entUid) == userId) { |
| 290 | remove = true; |
| 291 | } |
| 292 | if (remove) { |
| 293 | ba.removeAt(i); |
| 294 | } |
| 295 | } |
| 296 | if (ba.size() == 0) { |
| 297 | pmap.removeAt(ip); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void loadAppsNotReportingCrashesFromConfigLocked(String appsNotReportingCrashesConfig) { |
| 303 | if (appsNotReportingCrashesConfig != null) { |
| 304 | final String[] split = appsNotReportingCrashesConfig.split(","); |
| 305 | if (split.length > 0) { |
| 306 | mAppsNotReportingCrashes = new ArraySet<>(); |
| 307 | Collections.addAll(mAppsNotReportingCrashes, split); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void killAppAtUserRequestLocked(ProcessRecord app, Dialog fromDialog) { |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 313 | app.setCrashing(false); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 314 | app.crashingReport = null; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 315 | app.setNotResponding(false); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 316 | app.notRespondingReport = null; |
| 317 | if (app.anrDialog == fromDialog) { |
| 318 | app.anrDialog = null; |
| 319 | } |
| 320 | if (app.waitDialog == fromDialog) { |
| 321 | app.waitDialog = null; |
| 322 | } |
| 323 | if (app.pid > 0 && app.pid != MY_PID) { |
| 324 | handleAppCrashLocked(app, "user-terminated" /*reason*/, |
| 325 | null /*shortMsg*/, null /*longMsg*/, null /*stackTrace*/, null /*data*/); |
| 326 | app.kill("user request after error", true); |
| 327 | } |
| 328 | } |
| 329 | |
Christopher Tate | 8aa8fe1 | 2017-01-20 17:50:32 -0800 | [diff] [blame] | 330 | /** |
| 331 | * Induce a crash in the given app. |
| 332 | * |
| 333 | * @param uid if nonnegative, the required matching uid of the target to crash |
| 334 | * @param initialPid fast-path match for the target to crash |
| 335 | * @param packageName fallback match if the stated pid is not found or doesn't match uid |
| 336 | * @param userId If nonnegative, required to identify a match by package name |
| 337 | * @param message |
| 338 | */ |
| 339 | void scheduleAppCrashLocked(int uid, int initialPid, String packageName, int userId, |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 340 | String message) { |
| 341 | ProcessRecord proc = null; |
| 342 | |
| 343 | // Figure out which process to kill. We don't trust that initialPid |
| 344 | // still has any relation to current pids, so must scan through the |
| 345 | // list. |
| 346 | |
| 347 | synchronized (mService.mPidsSelfLocked) { |
| 348 | for (int i=0; i<mService.mPidsSelfLocked.size(); i++) { |
| 349 | ProcessRecord p = mService.mPidsSelfLocked.valueAt(i); |
Christopher Tate | 8aa8fe1 | 2017-01-20 17:50:32 -0800 | [diff] [blame] | 350 | if (uid >= 0 && p.uid != uid) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 351 | continue; |
| 352 | } |
| 353 | if (p.pid == initialPid) { |
| 354 | proc = p; |
| 355 | break; |
| 356 | } |
Christopher Tate | 8aa8fe1 | 2017-01-20 17:50:32 -0800 | [diff] [blame] | 357 | if (p.pkgList.containsKey(packageName) |
| 358 | && (userId < 0 || p.userId == userId)) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 359 | proc = p; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (proc == null) { |
| 365 | Slog.w(TAG, "crashApplication: nothing for uid=" + uid |
| 366 | + " initialPid=" + initialPid |
Christopher Tate | 8aa8fe1 | 2017-01-20 17:50:32 -0800 | [diff] [blame] | 367 | + " packageName=" + packageName |
| 368 | + " userId=" + userId); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 369 | return; |
| 370 | } |
| 371 | |
Joe Onorato | 5719028 | 2016-04-20 15:37:49 -0700 | [diff] [blame] | 372 | proc.scheduleCrash(message); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Bring up the "unexpected error" dialog box for a crashing app. |
| 377 | * Deal with edge cases (intercepts from instrumented applications, |
| 378 | * ActivityController, error intent receivers, that sort of thing). |
| 379 | * @param r the application crashing |
| 380 | * @param crashInfo describing the failure |
| 381 | */ |
| 382 | void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) { |
Mark Lu | b5e2499 | 2016-11-21 15:38:13 +0800 | [diff] [blame] | 383 | final int callingPid = Binder.getCallingPid(); |
| 384 | final int callingUid = Binder.getCallingUid(); |
| 385 | |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 386 | final long origId = Binder.clearCallingIdentity(); |
| 387 | try { |
Mark Lu | b5e2499 | 2016-11-21 15:38:13 +0800 | [diff] [blame] | 388 | crashApplicationInner(r, crashInfo, callingPid, callingUid); |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 389 | } finally { |
| 390 | Binder.restoreCallingIdentity(origId); |
| 391 | } |
| 392 | } |
| 393 | |
Mark Lu | b5e2499 | 2016-11-21 15:38:13 +0800 | [diff] [blame] | 394 | void crashApplicationInner(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo, |
| 395 | int callingPid, int callingUid) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 396 | long timeMillis = System.currentTimeMillis(); |
| 397 | String shortMsg = crashInfo.exceptionClassName; |
| 398 | String longMsg = crashInfo.exceptionMessage; |
| 399 | String stackTrace = crashInfo.stackTrace; |
| 400 | if (shortMsg != null && longMsg != null) { |
| 401 | longMsg = shortMsg + ": " + longMsg; |
| 402 | } else if (shortMsg != null) { |
| 403 | longMsg = shortMsg; |
| 404 | } |
| 405 | |
Zimuzo | 6efba54 | 2018-11-29 12:47:58 +0000 | [diff] [blame] | 406 | if (r != null) { |
| 407 | if (r.isPersistent()) { |
| 408 | // If a persistent app is stuck in a crash loop, the device isn't very |
| 409 | // usable, so we want to consider sending out a rescue party. |
| 410 | RescueParty.notePersistentAppCrash(mContext, r.uid); |
| 411 | } else { |
| 412 | // If a non-persistent app is stuck in crash loop, we want to inform |
| 413 | // the package watchdog, maybe an update or experiment can be rolled back. |
| 414 | mPackageWatchdog.onPackageFailure(r.getPackageList()); |
| 415 | } |
Jeff Sharkey | fe6f85c | 2017-01-20 10:42:57 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Garfield Tan | 2746ab5 | 2018-07-25 12:33:01 -0700 | [diff] [blame] | 418 | final int relaunchReason = r != null |
Wale Ogunwale | 6425836 | 2018-10-16 15:13:37 -0700 | [diff] [blame] | 419 | ? r.getWindowProcessController().computeRelaunchReason() : RELAUNCH_REASON_NONE; |
Garfield Tan | 2746ab5 | 2018-07-25 12:33:01 -0700 | [diff] [blame] | 420 | |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 421 | AppErrorResult result = new AppErrorResult(); |
Wale Ogunwale | 6425836 | 2018-10-16 15:13:37 -0700 | [diff] [blame] | 422 | int taskId; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 423 | synchronized (mService) { |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 424 | /** |
| 425 | * If crash is handled by instance of {@link android.app.IActivityController}, |
| 426 | * finish now and don't show the app error dialog. |
| 427 | */ |
| 428 | if (handleAppCrashInActivityController(r, crashInfo, shortMsg, longMsg, stackTrace, |
Mark Lu | b5e2499 | 2016-11-21 15:38:13 +0800 | [diff] [blame] | 429 | timeMillis, callingPid, callingUid)) { |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 430 | return; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 431 | } |
| 432 | |
Garfield Tan | 2746ab5 | 2018-07-25 12:33:01 -0700 | [diff] [blame] | 433 | // Suppress crash dialog if the process is being relaunched due to a crash during a free |
| 434 | // resize. |
Wale Ogunwale | 6425836 | 2018-10-16 15:13:37 -0700 | [diff] [blame] | 435 | if (relaunchReason == RELAUNCH_REASON_FREE_RESIZE) { |
Garfield Tan | 2746ab5 | 2018-07-25 12:33:01 -0700 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 439 | /** |
| 440 | * If this process was running instrumentation, finish now - it will be handled in |
| 441 | * {@link ActivityManagerService#handleAppDiedLocked}. |
| 442 | */ |
Wale Ogunwale | 906f9c6 | 2018-07-23 11:23:44 -0700 | [diff] [blame] | 443 | if (r != null && r.getActiveInstrumentation() != null) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 444 | return; |
| 445 | } |
| 446 | |
| 447 | // Log crash in battery stats. |
| 448 | if (r != null) { |
| 449 | mService.mBatteryStatsService.noteProcessCrash(r.processName, r.uid); |
| 450 | } |
| 451 | |
| 452 | AppErrorDialog.Data data = new AppErrorDialog.Data(); |
| 453 | data.result = result; |
| 454 | data.proc = r; |
| 455 | |
| 456 | // If we can't identify the process or it's already exceeded its crash quota, |
| 457 | // quit right away without showing a crash dialog. |
| 458 | if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace, data)) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
Wale Ogunwale | 9645b0f | 2016-09-12 10:49:35 -0700 | [diff] [blame] | 462 | final Message msg = Message.obtain(); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 463 | msg.what = ActivityManagerService.SHOW_ERROR_UI_MSG; |
| 464 | |
Wale Ogunwale | 6425836 | 2018-10-16 15:13:37 -0700 | [diff] [blame] | 465 | taskId = data.taskId; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 466 | msg.obj = data; |
| 467 | mService.mUiHandler.sendMessage(msg); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | int res = result.get(); |
| 471 | |
| 472 | Intent appErrorIntent = null; |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 473 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_CRASH, res); |
Adrian Roos | ad028c1 | 2016-05-17 14:30:42 -0700 | [diff] [blame] | 474 | if (res == AppErrorDialog.TIMEOUT || res == AppErrorDialog.CANCEL) { |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 475 | res = AppErrorDialog.FORCE_QUIT; |
| 476 | } |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 477 | synchronized (mService) { |
| 478 | if (res == AppErrorDialog.MUTE) { |
| 479 | stopReportingCrashesLocked(r); |
| 480 | } |
| 481 | if (res == AppErrorDialog.RESTART) { |
Amith Yamasani | 98a0092 | 2018-08-21 12:50:30 -0400 | [diff] [blame] | 482 | mService.mProcessList.removeProcessLocked(r, false, true, "crash"); |
Wale Ogunwale | 6425836 | 2018-10-16 15:13:37 -0700 | [diff] [blame] | 483 | if (taskId != INVALID_TASK_ID) { |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 484 | try { |
Wale Ogunwale | 387b34c | 2018-10-25 19:59:40 -0700 | [diff] [blame] | 485 | mService.startActivityFromRecents(taskId, |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 486 | ActivityOptions.makeBasic().toBundle()); |
| 487 | } catch (IllegalArgumentException e) { |
Wale Ogunwale | 6425836 | 2018-10-16 15:13:37 -0700 | [diff] [blame] | 488 | // Hmm...that didn't work. Task should either be in recents or associated |
| 489 | // with a stack. |
| 490 | Slog.e(TAG, "Could not restart taskId=" + taskId, e); |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | } |
| 494 | if (res == AppErrorDialog.FORCE_QUIT) { |
| 495 | long orig = Binder.clearCallingIdentity(); |
| 496 | try { |
| 497 | // Kill it with fire! |
Wale Ogunwale | 31913b5 | 2018-10-13 08:29:31 -0700 | [diff] [blame] | 498 | mService.mAtmInternal.onHandleAppCrash(r.getWindowProcessController()); |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 499 | if (!r.isPersistent()) { |
Amith Yamasani | 98a0092 | 2018-08-21 12:50:30 -0400 | [diff] [blame] | 500 | mService.mProcessList.removeProcessLocked(r, false, false, "crash"); |
Wale Ogunwale | 31913b5 | 2018-10-13 08:29:31 -0700 | [diff] [blame] | 501 | mService.mAtmInternal.resumeTopActivities(false /* scheduleIdle */); |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 502 | } |
| 503 | } finally { |
| 504 | Binder.restoreCallingIdentity(orig); |
| 505 | } |
| 506 | } |
Andrew Sapperstein | 5b679c4 | 2018-01-16 11:13:40 -0800 | [diff] [blame] | 507 | if (res == AppErrorDialog.APP_INFO) { |
| 508 | appErrorIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 509 | appErrorIntent.setData(Uri.parse("package:" + r.info.packageName)); |
| 510 | appErrorIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 511 | } |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 512 | if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) { |
| 513 | appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo); |
| 514 | } |
| 515 | if (r != null && !r.isolated && res != AppErrorDialog.RESTART) { |
| 516 | // XXX Can't keep track of crash time for isolated processes, |
| 517 | // since they don't have a persistent identity. |
| 518 | mProcessCrashTimes.put(r.info.processName, r.uid, |
| 519 | SystemClock.uptimeMillis()); |
| 520 | } |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | if (appErrorIntent != null) { |
| 524 | try { |
| 525 | mContext.startActivityAsUser(appErrorIntent, new UserHandle(r.userId)); |
| 526 | } catch (ActivityNotFoundException e) { |
| 527 | Slog.w(TAG, "bug report receiver dissappeared", e); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 532 | private boolean handleAppCrashInActivityController(ProcessRecord r, |
| 533 | ApplicationErrorReport.CrashInfo crashInfo, |
| 534 | String shortMsg, String longMsg, |
Mark Lu | b5e2499 | 2016-11-21 15:38:13 +0800 | [diff] [blame] | 535 | String stackTrace, long timeMillis, |
| 536 | int callingPid, int callingUid) { |
Wale Ogunwale | e217229 | 2018-10-25 10:11:10 -0700 | [diff] [blame] | 537 | String name = r != null ? r.processName : null; |
| 538 | int pid = r != null ? r.pid : callingPid; |
| 539 | int uid = r != null ? r.info.uid : callingUid; |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 540 | |
Wale Ogunwale | e217229 | 2018-10-25 10:11:10 -0700 | [diff] [blame] | 541 | return mService.mAtmInternal.handleAppCrashInActivityController( |
| 542 | name, pid, shortMsg, longMsg, timeMillis, crashInfo.stackTrace, () -> { |
| 543 | if ("1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0")) |
| 544 | && "Native crash".equals(crashInfo.exceptionClassName)) { |
| 545 | Slog.w(TAG, "Skip killing native crashed app " + name |
| 546 | + "(" + pid + ") during testing"); |
| 547 | } else { |
| 548 | Slog.w(TAG, "Force-killing crashed app " + name + " at watcher's request"); |
| 549 | if (r != null) { |
| 550 | if (!makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace, null)) { |
| 551 | r.kill("crash", true); |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 552 | } |
Wale Ogunwale | e217229 | 2018-10-25 10:11:10 -0700 | [diff] [blame] | 553 | } else { |
| 554 | // Huh. |
| 555 | Process.killProcess(pid); |
| 556 | ProcessList.killProcessGroup(uid, pid); |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 557 | } |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 558 | } |
Wale Ogunwale | e217229 | 2018-10-25 10:11:10 -0700 | [diff] [blame] | 559 | }); |
Andrii Kulian | fa3991a | 2016-04-28 14:18:37 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 562 | private boolean makeAppCrashingLocked(ProcessRecord app, |
| 563 | String shortMsg, String longMsg, String stackTrace, AppErrorDialog.Data data) { |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 564 | app.setCrashing(true); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 565 | app.crashingReport = generateProcessError(app, |
| 566 | ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace); |
Wale Ogunwale | 51cc98a | 2018-10-15 10:41:05 -0700 | [diff] [blame] | 567 | app.startAppProblemLocked(); |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 568 | app.getWindowProcessController().stopFreezingActivities(); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 569 | return handleAppCrashLocked(app, "force-crash" /*reason*/, shortMsg, longMsg, stackTrace, |
| 570 | data); |
| 571 | } |
| 572 | |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 573 | /** |
| 574 | * Generate a process error record, suitable for attachment to a ProcessRecord. |
| 575 | * |
| 576 | * @param app The ProcessRecord in which the error occurred. |
| 577 | * @param condition Crashing, Application Not Responding, etc. Values are defined in |
Sudheer Shanka | f669010 | 2017-10-16 10:20:32 -0700 | [diff] [blame] | 578 | * ActivityManager.ProcessErrorStateInfo |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 579 | * @param activity The activity associated with the crash, if known. |
| 580 | * @param shortMsg Short message describing the crash. |
| 581 | * @param longMsg Long message describing the crash. |
| 582 | * @param stackTrace Full crash stack trace, may be null. |
| 583 | * |
Sudheer Shanka | f669010 | 2017-10-16 10:20:32 -0700 | [diff] [blame] | 584 | * @return Returns a fully-formed ProcessErrorStateInfo record. |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 585 | */ |
Wale Ogunwale | 51cc98a | 2018-10-15 10:41:05 -0700 | [diff] [blame] | 586 | ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app, |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 587 | int condition, String activity, String shortMsg, String longMsg, String stackTrace) { |
| 588 | ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo(); |
| 589 | |
| 590 | report.condition = condition; |
| 591 | report.processName = app.processName; |
| 592 | report.pid = app.pid; |
| 593 | report.uid = app.info.uid; |
| 594 | report.tag = activity; |
| 595 | report.shortMsg = shortMsg; |
| 596 | report.longMsg = longMsg; |
| 597 | report.stackTrace = stackTrace; |
| 598 | |
| 599 | return report; |
| 600 | } |
| 601 | |
| 602 | Intent createAppErrorIntentLocked(ProcessRecord r, |
| 603 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 604 | ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo); |
| 605 | if (report == null) { |
| 606 | return null; |
| 607 | } |
| 608 | Intent result = new Intent(Intent.ACTION_APP_ERROR); |
| 609 | result.setComponent(r.errorReportReceiver); |
| 610 | result.putExtra(Intent.EXTRA_BUG_REPORT, report); |
| 611 | result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 612 | return result; |
| 613 | } |
| 614 | |
| 615 | private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r, |
| 616 | long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) { |
| 617 | if (r.errorReportReceiver == null) { |
| 618 | return null; |
| 619 | } |
| 620 | |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 621 | if (!r.isCrashing() && !r.isNotResponding() && !r.forceCrashReport) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 622 | return null; |
| 623 | } |
| 624 | |
| 625 | ApplicationErrorReport report = new ApplicationErrorReport(); |
| 626 | report.packageName = r.info.packageName; |
| 627 | report.installerPackageName = r.errorReportReceiver.getPackageName(); |
| 628 | report.processName = r.processName; |
| 629 | report.time = timeMillis; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 630 | report.systemApp = (r.info.flags & FLAG_SYSTEM) != 0; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 631 | |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 632 | if (r.isCrashing() || r.forceCrashReport) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 633 | report.type = ApplicationErrorReport.TYPE_CRASH; |
| 634 | report.crashInfo = crashInfo; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 635 | } else if (r.isNotResponding()) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 636 | report.type = ApplicationErrorReport.TYPE_ANR; |
| 637 | report.anrInfo = new ApplicationErrorReport.AnrInfo(); |
| 638 | |
| 639 | report.anrInfo.activity = r.notRespondingReport.tag; |
| 640 | report.anrInfo.cause = r.notRespondingReport.shortMsg; |
| 641 | report.anrInfo.info = r.notRespondingReport.longMsg; |
| 642 | } |
| 643 | |
| 644 | return report; |
| 645 | } |
| 646 | |
| 647 | boolean handleAppCrashLocked(ProcessRecord app, String reason, |
| 648 | String shortMsg, String longMsg, String stackTrace, AppErrorDialog.Data data) { |
Amith Yamasani | b0c8a88 | 2017-08-28 09:36:42 -0700 | [diff] [blame] | 649 | final long now = SystemClock.uptimeMillis(); |
| 650 | final boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
Adrian Roos | 6a7e089 | 2016-08-23 14:26:39 +0200 | [diff] [blame] | 651 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 652 | |
Amith Yamasani | b0c8a88 | 2017-08-28 09:36:42 -0700 | [diff] [blame] | 653 | final boolean procIsBoundForeground = |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 654 | (app.getCurProcState() == ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE); |
Amith Yamasani | b0c8a88 | 2017-08-28 09:36:42 -0700 | [diff] [blame] | 655 | |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 656 | Long crashTime; |
| 657 | Long crashTimePersistent; |
Amith Yamasani | b0c8a88 | 2017-08-28 09:36:42 -0700 | [diff] [blame] | 658 | boolean tryAgain = false; |
| 659 | |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 660 | if (!app.isolated) { |
| 661 | crashTime = mProcessCrashTimes.get(app.info.processName, app.uid); |
| 662 | crashTimePersistent = mProcessCrashTimesPersistent.get(app.info.processName, app.uid); |
| 663 | } else { |
| 664 | crashTime = crashTimePersistent = null; |
| 665 | } |
Amith Yamasani | b0c8a88 | 2017-08-28 09:36:42 -0700 | [diff] [blame] | 666 | |
| 667 | // Bump up the crash count of any services currently running in the proc. |
| 668 | for (int i = app.services.size() - 1; i >= 0; i--) { |
| 669 | // Any services running in the application need to be placed |
| 670 | // back in the pending list. |
| 671 | ServiceRecord sr = app.services.valueAt(i); |
| 672 | // If the service was restarted a while ago, then reset crash count, else increment it. |
| 673 | if (now > sr.restartTime + ProcessList.MIN_CRASH_INTERVAL) { |
| 674 | sr.crashCount = 1; |
| 675 | } else { |
| 676 | sr.crashCount++; |
| 677 | } |
| 678 | // Allow restarting for started or bound foreground services that are crashing. |
| 679 | // This includes wallpapers. |
| 680 | if (sr.crashCount < mService.mConstants.BOUND_SERVICE_MAX_CRASH_RETRY |
| 681 | && (sr.isForeground || procIsBoundForeground)) { |
| 682 | tryAgain = true; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | if (crashTime != null && now < crashTime + ProcessList.MIN_CRASH_INTERVAL) { |
| 687 | // The process crashed again very quickly. If it was a bound foreground service, let's |
| 688 | // try to restart again in a while, otherwise the process loses! |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 689 | Slog.w(TAG, "Process " + app.info.processName |
| 690 | + " has crashed too many times: killing!"); |
| 691 | EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH, |
| 692 | app.userId, app.info.processName, app.uid); |
Wale Ogunwale | 31913b5 | 2018-10-13 08:29:31 -0700 | [diff] [blame] | 693 | mService.mAtmInternal.onHandleAppCrash(app.getWindowProcessController()); |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 694 | if (!app.isPersistent()) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 695 | // We don't want to start this process again until the user |
| 696 | // explicitly does so... but for persistent process, we really |
| 697 | // need to keep it running. If a persistent process is actually |
| 698 | // repeatedly crashing, then badness for everyone. |
| 699 | EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.userId, app.uid, |
| 700 | app.info.processName); |
| 701 | if (!app.isolated) { |
| 702 | // XXX We don't have a way to mark isolated processes |
| 703 | // as bad, since they don't have a peristent identity. |
| 704 | mBadProcesses.put(app.info.processName, app.uid, |
| 705 | new BadProcessInfo(now, shortMsg, longMsg, stackTrace)); |
| 706 | mProcessCrashTimes.remove(app.info.processName, app.uid); |
| 707 | } |
| 708 | app.bad = true; |
| 709 | app.removed = true; |
| 710 | // Don't let services in this process be restarted and potentially |
| 711 | // annoy the user repeatedly. Unless it is persistent, since those |
| 712 | // processes run critical code. |
Amith Yamasani | 98a0092 | 2018-08-21 12:50:30 -0400 | [diff] [blame] | 713 | mService.mProcessList.removeProcessLocked(app, false, tryAgain, "crash"); |
Wale Ogunwale | 31913b5 | 2018-10-13 08:29:31 -0700 | [diff] [blame] | 714 | mService.mAtmInternal.resumeTopActivities(false /* scheduleIdle */); |
Adrian Roos | 6a7e089 | 2016-08-23 14:26:39 +0200 | [diff] [blame] | 715 | if (!showBackground) { |
| 716 | return false; |
| 717 | } |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 718 | } |
Wale Ogunwale | 31913b5 | 2018-10-13 08:29:31 -0700 | [diff] [blame] | 719 | mService.mAtmInternal.resumeTopActivities(false /* scheduleIdle */); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 720 | } else { |
Wale Ogunwale | 6425836 | 2018-10-16 15:13:37 -0700 | [diff] [blame] | 721 | final int affectedTaskId = mService.mAtmInternal.finishTopCrashedActivities( |
Wale Ogunwale | 31913b5 | 2018-10-13 08:29:31 -0700 | [diff] [blame] | 722 | app.getWindowProcessController(), reason); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 723 | if (data != null) { |
Wale Ogunwale | 6425836 | 2018-10-16 15:13:37 -0700 | [diff] [blame] | 724 | data.taskId = affectedTaskId; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 725 | } |
| 726 | if (data != null && crashTimePersistent != null |
| 727 | && now < crashTimePersistent + ProcessList.MIN_CRASH_INTERVAL) { |
| 728 | data.repeating = true; |
| 729 | } |
| 730 | } |
| 731 | |
Amith Yamasani | b0c8a88 | 2017-08-28 09:36:42 -0700 | [diff] [blame] | 732 | if (data != null && tryAgain) { |
| 733 | data.isRestartableForService = true; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | // If the crashing process is what we consider to be the "home process" and it has been |
| 737 | // replaced by a third-party app, clear the package preferred activities from packages |
| 738 | // with a home activity running in the process to prevent a repeatedly crashing app |
| 739 | // from blocking the user to manually clear the list. |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 740 | final WindowProcessController proc = app.getWindowProcessController(); |
Wale Ogunwale | d4d67d0 | 2018-10-25 18:09:39 -0700 | [diff] [blame] | 741 | final WindowProcessController homeProc = mService.mAtmInternal.getHomeProcess(); |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 742 | if (proc == homeProc && proc.hasActivities() |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 743 | && (((ProcessRecord) homeProc.mOwner).info.flags & FLAG_SYSTEM) == 0) { |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 744 | proc.clearPackagePreferredForHomeActivities(); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | if (!app.isolated) { |
| 748 | // XXX Can't keep track of crash times for isolated processes, |
Amith Yamasani | b0c8a88 | 2017-08-28 09:36:42 -0700 | [diff] [blame] | 749 | // because they don't have a persistent identity. |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 750 | mProcessCrashTimes.put(app.info.processName, app.uid, now); |
| 751 | mProcessCrashTimesPersistent.put(app.info.processName, app.uid, now); |
| 752 | } |
| 753 | |
| 754 | if (app.crashHandler != null) mService.mHandler.post(app.crashHandler); |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | void handleShowAppErrorUi(Message msg) { |
| 759 | AppErrorDialog.Data data = (AppErrorDialog.Data) msg.obj; |
| 760 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 761 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Adrian Roos | c3a06e5 | 2018-02-16 18:33:17 +0100 | [diff] [blame] | 762 | |
| 763 | AppErrorDialog dialogToShow = null; |
| 764 | final String packageName; |
| 765 | final int userId; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 766 | synchronized (mService) { |
Adrian Roos | c3a06e5 | 2018-02-16 18:33:17 +0100 | [diff] [blame] | 767 | final ProcessRecord proc = data.proc; |
| 768 | final AppErrorResult res = data.result; |
| 769 | if (proc == null) { |
| 770 | Slog.e(TAG, "handleShowAppErrorUi: proc is null"); |
| 771 | return; |
| 772 | } |
| 773 | packageName = proc.info.packageName; |
| 774 | userId = proc.userId; |
| 775 | if (proc.crashDialog != null) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 776 | Slog.e(TAG, "App already has crash dialog: " + proc); |
| 777 | if (res != null) { |
Adrian Roos | 9046222 | 2016-02-17 15:45:09 -0800 | [diff] [blame] | 778 | res.set(AppErrorDialog.ALREADY_SHOWING); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 779 | } |
| 780 | return; |
| 781 | } |
| 782 | boolean isBackground = (UserHandle.getAppId(proc.uid) |
| 783 | >= Process.FIRST_APPLICATION_UID |
| 784 | && proc.pid != MY_PID); |
Adrian Roos | c3a06e5 | 2018-02-16 18:33:17 +0100 | [diff] [blame] | 785 | for (int profileId : mService.mUserController.getCurrentProfileIds()) { |
| 786 | isBackground &= (userId != profileId); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 787 | } |
| 788 | if (isBackground && !showBackground) { |
| 789 | Slog.w(TAG, "Skipping crash dialog of " + proc + ": background"); |
| 790 | if (res != null) { |
Adrian Roos | 9046222 | 2016-02-17 15:45:09 -0800 | [diff] [blame] | 791 | res.set(AppErrorDialog.BACKGROUND_USER); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 792 | } |
| 793 | return; |
| 794 | } |
Andrew Sapperstein | 43643ae | 2017-12-20 15:17:33 -0800 | [diff] [blame] | 795 | final boolean showFirstCrash = Settings.Global.getInt( |
| 796 | mContext.getContentResolver(), |
| 797 | Settings.Global.SHOW_FIRST_CRASH_DIALOG, 0) != 0; |
| 798 | final boolean showFirstCrashDevOption = Settings.Secure.getIntForUser( |
| 799 | mContext.getContentResolver(), |
| 800 | Settings.Secure.SHOW_FIRST_CRASH_DIALOG_DEV_OPTION, |
| 801 | 0, |
Andrew Sapperstein | fd5238c | 2018-01-21 09:45:05 -0800 | [diff] [blame] | 802 | mService.mUserController.getCurrentUserId()) != 0; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 803 | final boolean crashSilenced = mAppsNotReportingCrashes != null && |
| 804 | mAppsNotReportingCrashes.contains(proc.info.packageName); |
Wale Ogunwale | 387b34c | 2018-10-25 19:59:40 -0700 | [diff] [blame] | 805 | if ((mService.mAtmInternal.canShowErrorDialogs() || showBackground) |
Wale Ogunwale | f673393 | 2018-06-27 05:14:34 -0700 | [diff] [blame] | 806 | && !crashSilenced |
Andrew Sapperstein | 43643ae | 2017-12-20 15:17:33 -0800 | [diff] [blame] | 807 | && (showFirstCrash || showFirstCrashDevOption || data.repeating)) { |
Adrian Roos | c3a06e5 | 2018-02-16 18:33:17 +0100 | [diff] [blame] | 808 | proc.crashDialog = dialogToShow = new AppErrorDialog(mContext, mService, data); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 809 | } else { |
| 810 | // The device is asleep, so just pretend that the user |
| 811 | // saw a crash dialog and hit "force quit". |
| 812 | if (res != null) { |
Adrian Roos | 9046222 | 2016-02-17 15:45:09 -0800 | [diff] [blame] | 813 | res.set(AppErrorDialog.CANT_SHOW); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | } |
Phil Weaver | 445fd2a | 2016-03-17 17:26:24 -0700 | [diff] [blame] | 817 | // If we've created a crash dialog, show it without the lock held |
Adrian Roos | c3a06e5 | 2018-02-16 18:33:17 +0100 | [diff] [blame] | 818 | if (dialogToShow != null) { |
| 819 | Slog.i(TAG, "Showing crash dialog for package " + packageName + " u" + userId); |
| 820 | dialogToShow.show(); |
Phil Weaver | 445fd2a | 2016-03-17 17:26:24 -0700 | [diff] [blame] | 821 | } |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Wale Ogunwale | 51cc98a | 2018-10-15 10:41:05 -0700 | [diff] [blame] | 824 | private void stopReportingCrashesLocked(ProcessRecord proc) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 825 | if (mAppsNotReportingCrashes == null) { |
| 826 | mAppsNotReportingCrashes = new ArraySet<>(); |
| 827 | } |
| 828 | mAppsNotReportingCrashes.add(proc.info.packageName); |
| 829 | } |
| 830 | |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 831 | void handleShowAnrUi(Message msg) { |
Adrian Roos | c3a06e5 | 2018-02-16 18:33:17 +0100 | [diff] [blame] | 832 | Dialog dialogToShow = null; |
Zimuzo | 6efba54 | 2018-11-29 12:47:58 +0000 | [diff] [blame] | 833 | String[] packageList = null; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 834 | synchronized (mService) { |
Adrian Roos | c3a06e5 | 2018-02-16 18:33:17 +0100 | [diff] [blame] | 835 | AppNotRespondingDialog.Data data = (AppNotRespondingDialog.Data) msg.obj; |
| 836 | final ProcessRecord proc = data.proc; |
| 837 | if (proc == null) { |
| 838 | Slog.e(TAG, "handleShowAnrUi: proc is null"); |
| 839 | return; |
| 840 | } |
Zimuzo | 6efba54 | 2018-11-29 12:47:58 +0000 | [diff] [blame] | 841 | if (!proc.isPersistent()) { |
| 842 | packageList = proc.getPackageList(); |
| 843 | } |
Adrian Roos | c3a06e5 | 2018-02-16 18:33:17 +0100 | [diff] [blame] | 844 | if (proc.anrDialog != null) { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 845 | Slog.e(TAG, "App already has anr dialog: " + proc); |
Adrian Roos | 9046222 | 2016-02-17 15:45:09 -0800 | [diff] [blame] | 846 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_ANR, |
| 847 | AppNotRespondingDialog.ALREADY_SHOWING); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 848 | return; |
| 849 | } |
| 850 | |
Adrian Roos | 6a7e089 | 2016-08-23 14:26:39 +0200 | [diff] [blame] | 851 | boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(), |
| 852 | Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0; |
Wale Ogunwale | 387b34c | 2018-10-25 19:59:40 -0700 | [diff] [blame] | 853 | if (mService.mAtmInternal.canShowErrorDialogs() || showBackground) { |
Adrian Roos | c3a06e5 | 2018-02-16 18:33:17 +0100 | [diff] [blame] | 854 | dialogToShow = new AppNotRespondingDialog(mService, mContext, data); |
| 855 | proc.anrDialog = dialogToShow; |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 856 | } else { |
Adrian Roos | 9046222 | 2016-02-17 15:45:09 -0800 | [diff] [blame] | 857 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_APP_ANR, |
| 858 | AppNotRespondingDialog.CANT_SHOW); |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 859 | // Just kill the app if there is no dialog to be shown. |
| 860 | mService.killAppAtUsersRequest(proc, null); |
| 861 | } |
| 862 | } |
Phil Weaver | 445fd2a | 2016-03-17 17:26:24 -0700 | [diff] [blame] | 863 | // If we've created a crash dialog, show it without the lock held |
Adrian Roos | c3a06e5 | 2018-02-16 18:33:17 +0100 | [diff] [blame] | 864 | if (dialogToShow != null) { |
| 865 | dialogToShow.show(); |
Phil Weaver | 445fd2a | 2016-03-17 17:26:24 -0700 | [diff] [blame] | 866 | } |
Zimuzo | 6efba54 | 2018-11-29 12:47:58 +0000 | [diff] [blame] | 867 | // Notify PackageWatchdog without the lock held |
| 868 | if (packageList != null) { |
| 869 | mPackageWatchdog.onPackageFailure(packageList); |
| 870 | } |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /** |
| 874 | * Information about a process that is currently marked as bad. |
| 875 | */ |
| 876 | static final class BadProcessInfo { |
| 877 | BadProcessInfo(long time, String shortMsg, String longMsg, String stack) { |
| 878 | this.time = time; |
| 879 | this.shortMsg = shortMsg; |
| 880 | this.longMsg = longMsg; |
| 881 | this.stack = stack; |
| 882 | } |
| 883 | |
| 884 | final long time; |
| 885 | final String shortMsg; |
| 886 | final String longMsg; |
| 887 | final String stack; |
| 888 | } |
| 889 | |
| 890 | } |