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