The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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; |
| 18 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 19 | import android.app.IActivityController; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 24 | import android.hidl.manager.V1_0.IServiceManager; |
Makoto Onuki | 9902954 | 2018-08-27 17:23:09 -0700 | [diff] [blame] | 25 | import android.os.Binder; |
| 26 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.os.Debug; |
| 28 | import android.os.Handler; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 29 | import android.os.IPowerManager; |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 30 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.Process; |
Makoto Onuki | 9902954 | 2018-08-27 17:23:09 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 33 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.SystemClock; |
Makoto Onuki | 9902954 | 2018-08-27 17:23:09 -0700 | [diff] [blame] | 35 | import android.system.ErrnoException; |
| 36 | import android.system.Os; |
| 37 | import android.system.OsConstants; |
| 38 | import android.system.StructRlimit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.util.EventLog; |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 40 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 41 | import android.util.Slog; |
Simon MacMullen | dfc56be | 2020-01-07 18:31:43 +0000 | [diff] [blame] | 42 | import android.util.SparseArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
Simon MacMullen | dfc56be | 2020-01-07 18:31:43 +0000 | [diff] [blame] | 44 | import com.android.internal.os.ProcessCpuTracker; |
Makoto Onuki | 9902954 | 2018-08-27 17:23:09 -0700 | [diff] [blame] | 45 | import com.android.internal.os.ZygoteConnectionConstants; |
Muhammad Qureshi | e2b2432 | 2020-01-28 10:54:17 -0800 | [diff] [blame] | 46 | import com.android.internal.util.FrameworkStatsLog; |
Makoto Onuki | 9902954 | 2018-08-27 17:23:09 -0700 | [diff] [blame] | 47 | import com.android.server.am.ActivityManagerService; |
Wale Ogunwale | 1f5e53d | 2018-11-05 05:12:46 -0800 | [diff] [blame] | 48 | import com.android.server.wm.SurfaceAnimationThread; |
Makoto Onuki | 9902954 | 2018-08-27 17:23:09 -0700 | [diff] [blame] | 49 | |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 50 | import java.io.File; |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 51 | import java.io.FileWriter; |
| 52 | import java.io.IOException; |
Simon MacMullen | 46755e2 | 2020-02-04 14:03:37 +0000 | [diff] [blame] | 53 | import java.io.StringWriter; |
Nandana Dutt | 6647ef5 | 2018-07-12 17:02:57 +0100 | [diff] [blame] | 54 | import java.nio.charset.StandardCharsets; |
| 55 | import java.nio.file.Files; |
| 56 | import java.nio.file.Path; |
| 57 | import java.nio.file.Paths; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import java.util.ArrayList; |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 59 | import java.util.Arrays; |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 60 | import java.util.Collections; |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 61 | import java.util.HashSet; |
| 62 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
| 64 | /** This class calls its monitor every minute. Killing this process if they don't return **/ |
| 65 | public class Watchdog extends Thread { |
| 66 | static final String TAG = "Watchdog"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 68 | /** Debug flag. */ |
Makoto Onuki | 729c41e | 2019-05-30 09:47:10 -0700 | [diff] [blame] | 69 | public static final boolean DEBUG = false; |
Makoto Onuki | 9902954 | 2018-08-27 17:23:09 -0700 | [diff] [blame] | 70 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | // Set this to true to use debug default values. |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 72 | private static final boolean DB = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 74 | // Note 1: Do not lower this value below thirty seconds without tightening the invoke-with |
| 75 | // timeout in com.android.internal.os.ZygoteConnection, or wrapped applications |
| 76 | // can trigger the watchdog. |
| 77 | // Note 2: The debug value is already below the wait time in ZygoteConnection. Wrapped |
| 78 | // applications may not work with a debug build. CTS will fail. |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 79 | private static final long DEFAULT_TIMEOUT = DB ? 10 * 1000 : 60 * 1000; |
| 80 | private static final long CHECK_INTERVAL = DEFAULT_TIMEOUT / 2; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 81 | |
| 82 | // These are temporally ordered: larger values as lateness increases |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 83 | private static final int COMPLETED = 0; |
| 84 | private static final int WAITING = 1; |
| 85 | private static final int WAITED_HALF = 2; |
| 86 | private static final int OVERDUE = 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
Igor Murashkin | 44d04aa | 2013-10-23 10:56:02 -0700 | [diff] [blame] | 88 | // Which native processes to dump into dropbox's stack traces |
| 89 | public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] { |
Andy Hung | 3a64ecb | 2016-03-09 13:55:58 -0800 | [diff] [blame] | 90 | "/system/bin/audioserver", |
Andy Hung | 689574a | 2016-04-13 19:27:43 -0700 | [diff] [blame] | 91 | "/system/bin/cameraserver", |
| 92 | "/system/bin/drmserver", |
| 93 | "/system/bin/mediadrmserver", |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 94 | "/system/bin/mediaserver", |
Mike Yu | c985595 | 2019-11-19 14:36:02 +0800 | [diff] [blame] | 95 | "/system/bin/netd", |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 96 | "/system/bin/sdcard", |
Eric Laurent | 05d4e35 | 2016-03-14 18:49:08 -0700 | [diff] [blame] | 97 | "/system/bin/surfaceflinger", |
Jeff Sharkey | 3e40afb | 2019-01-02 09:21:24 -0700 | [diff] [blame] | 98 | "/system/bin/vold", |
Andy Hung | 689574a | 2016-04-13 19:27:43 -0700 | [diff] [blame] | 99 | "media.extractor", // system/bin/mediaextractor |
Andy Hung | b1c4c93 | 2018-01-04 12:05:43 -0800 | [diff] [blame] | 100 | "media.metrics", // system/bin/mediametrics |
Chong Zhang | e55e6e0 | 2017-06-02 10:52:04 -0700 | [diff] [blame] | 101 | "media.codec", // vendor/bin/hw/android.hardware.media.omx@1.0-service |
Chong Zhang | f1f7632 | 2019-03-27 16:41:51 -0700 | [diff] [blame] | 102 | "media.swcodec", // /apex/com.android.media.swcodec/bin/mediaswcodec |
Andreas Gampe | cf9e79b | 2016-05-11 18:41:25 -0700 | [diff] [blame] | 103 | "com.android.bluetooth", // Bluetooth service |
Howard Ro | adc510f | 2020-02-20 04:57:00 +0000 | [diff] [blame] | 104 | "/apex/com.android.os.statsd/bin/statsd", // Stats daemon |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 107 | public static final List<String> HAL_INTERFACES_OF_INTEREST = Arrays.asList( |
Hui Yu | 7e65c3f | 2019-04-16 17:03:16 -0700 | [diff] [blame] | 108 | "android.hardware.audio@2.0::IDevicesFactory", |
| 109 | "android.hardware.audio@4.0::IDevicesFactory", |
Mikhail Naganov | 708ea28 | 2019-07-10 10:16:32 -0700 | [diff] [blame] | 110 | "android.hardware.audio@5.0::IDevicesFactory", |
Kevin Rocard | df6e4d5 | 2019-09-30 19:54:55 +0100 | [diff] [blame] | 111 | "android.hardware.audio@6.0::IDevicesFactory", |
Kevin Chyn | 7733b75 | 2019-06-19 13:48:18 -0700 | [diff] [blame] | 112 | "android.hardware.biometrics.face@1.0::IBiometricsFace", |
Kris Chen | f8dc483 | 2020-03-03 15:45:59 +0800 | [diff] [blame] | 113 | "android.hardware.biometrics.fingerprint@2.1::IBiometricsFingerprint", |
Hui Yu | 7e65c3f | 2019-04-16 17:03:16 -0700 | [diff] [blame] | 114 | "android.hardware.bluetooth@1.0::IBluetoothHci", |
| 115 | "android.hardware.camera.provider@2.4::ICameraProvider", |
Sasha Kuznetsov | 11ed7d9 | 2020-02-28 12:55:09 -0800 | [diff] [blame] | 116 | "android.hardware.gnss@1.0::IGnss", |
Hui Yu | 7e65c3f | 2019-04-16 17:03:16 -0700 | [diff] [blame] | 117 | "android.hardware.graphics.allocator@2.0::IAllocator", |
| 118 | "android.hardware.graphics.composer@2.1::IComposer", |
| 119 | "android.hardware.health@2.0::IHealth", |
Chong Zhang | f1f7632 | 2019-03-27 16:41:51 -0700 | [diff] [blame] | 120 | "android.hardware.media.c2@1.0::IComponentStore", |
Hui Yu | 7e65c3f | 2019-04-16 17:03:16 -0700 | [diff] [blame] | 121 | "android.hardware.media.omx@1.0::IOmx", |
| 122 | "android.hardware.media.omx@1.0::IOmxStore", |
Stefano Galarraga | 219f1e5 | 2020-03-18 17:01:42 +0000 | [diff] [blame] | 123 | "android.hardware.neuralnetworks@1.0::IDevice", |
Benjamin Schwartz | f0a81f6 | 2019-06-18 17:58:39 -0700 | [diff] [blame] | 124 | "android.hardware.power.stats@1.0::IPowerStats", |
Hui Yu | 7e65c3f | 2019-04-16 17:03:16 -0700 | [diff] [blame] | 125 | "android.hardware.sensors@1.0::ISensors", |
Kalesh Singh | 5fe631d | 2019-07-02 13:51:53 -0700 | [diff] [blame] | 126 | "android.hardware.vr@1.0::IVr", |
| 127 | "android.system.suspend@1.0::ISystemSuspend" |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 128 | ); |
| 129 | |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 130 | private static Watchdog sWatchdog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
| 132 | /* This handler will be used to post message back onto the main thread */ |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 133 | private final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>(); |
| 134 | private final HandlerChecker mMonitorChecker; |
| 135 | private ActivityManagerService mActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 137 | private IActivityController mController; |
| 138 | private boolean mAllowRestart = true; |
| 139 | private final OpenFdMonitor mOpenFdMonitor; |
| 140 | private final List<Integer> mInterestingJavaPids = new ArrayList<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | /** |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 143 | * Used for checking status of handle threads and scheduling monitor callbacks. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | */ |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 145 | public final class HandlerChecker implements Runnable { |
| 146 | private final Handler mHandler; |
| 147 | private final String mName; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 148 | private final long mWaitMax; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 149 | private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>(); |
Zimuzo | b065b15 | 2019-04-16 13:33:01 +0100 | [diff] [blame] | 150 | private final ArrayList<Monitor> mMonitorQueue = new ArrayList<Monitor>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 151 | private boolean mCompleted; |
| 152 | private Monitor mCurrentMonitor; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 153 | private long mStartTime; |
Zimuzo | 2a05039 | 2019-05-09 12:51:33 +0100 | [diff] [blame] | 154 | private int mPauseCount; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 155 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 156 | HandlerChecker(Handler handler, String name, long waitMaxMillis) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 157 | mHandler = handler; |
| 158 | mName = name; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 159 | mWaitMax = waitMaxMillis; |
| 160 | mCompleted = true; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 163 | void addMonitorLocked(Monitor monitor) { |
Zimuzo | b065b15 | 2019-04-16 13:33:01 +0100 | [diff] [blame] | 164 | // We don't want to update mMonitors when the Handler is in the middle of checking |
| 165 | // all monitors. We will update mMonitors on the next schedule if it is safe |
| 166 | mMonitorQueue.add(monitor); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | public void scheduleCheckLocked() { |
Zimuzo | b065b15 | 2019-04-16 13:33:01 +0100 | [diff] [blame] | 170 | if (mCompleted) { |
| 171 | // Safe to update monitors in queue, Handler is not in the middle of work |
| 172 | mMonitors.addAll(mMonitorQueue); |
| 173 | mMonitorQueue.clear(); |
| 174 | } |
Zimuzo | 2a05039 | 2019-05-09 12:51:33 +0100 | [diff] [blame] | 175 | if ((mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) |
| 176 | || (mPauseCount > 0)) { |
| 177 | // Don't schedule until after resume OR |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 178 | // If the target looper has recently been polling, then |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 179 | // there is no reason to enqueue our checker on it since that |
| 180 | // is as good as it not being deadlocked. This avoid having |
Zimuzo | 2a05039 | 2019-05-09 12:51:33 +0100 | [diff] [blame] | 181 | // to do a context switch to check the thread. Note that we |
| 182 | // only do this if we have no monitors since those would need to |
| 183 | // be executed at this point. |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 184 | mCompleted = true; |
| 185 | return; |
| 186 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 187 | if (!mCompleted) { |
| 188 | // we already have a check in flight, so no need |
| 189 | return; |
| 190 | } |
| 191 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 192 | mCompleted = false; |
| 193 | mCurrentMonitor = null; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 194 | mStartTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 195 | mHandler.postAtFrontOfQueue(this); |
| 196 | } |
| 197 | |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 198 | boolean isOverdueLocked() { |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 199 | return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax); |
| 200 | } |
| 201 | |
| 202 | public int getCompletionStateLocked() { |
| 203 | if (mCompleted) { |
| 204 | return COMPLETED; |
| 205 | } else { |
| 206 | long latency = SystemClock.uptimeMillis() - mStartTime; |
| 207 | if (latency < mWaitMax/2) { |
| 208 | return WAITING; |
| 209 | } else if (latency < mWaitMax) { |
| 210 | return WAITED_HALF; |
| 211 | } |
| 212 | } |
| 213 | return OVERDUE; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 216 | public Thread getThread() { |
| 217 | return mHandler.getLooper().getThread(); |
| 218 | } |
| 219 | |
| 220 | public String getName() { |
| 221 | return mName; |
| 222 | } |
| 223 | |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 224 | String describeBlockedStateLocked() { |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 225 | if (mCurrentMonitor == null) { |
| 226 | return "Blocked in handler on " + mName + " (" + getThread().getName() + ")"; |
| 227 | } else { |
| 228 | return "Blocked in monitor " + mCurrentMonitor.getClass().getName() |
| 229 | + " on " + mName + " (" + getThread().getName() + ")"; |
| 230 | } |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 231 | } |
| 232 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | @Override |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 234 | public void run() { |
Zimuzo | b065b15 | 2019-04-16 13:33:01 +0100 | [diff] [blame] | 235 | // Once we get here, we ensure that mMonitors does not change even if we call |
| 236 | // #addMonitorLocked because we first add the new monitors to mMonitorQueue and |
| 237 | // move them to mMonitors on the next schedule when mCompleted is true, at which |
| 238 | // point we have completed execution of this method. |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 239 | final int size = mMonitors.size(); |
| 240 | for (int i = 0 ; i < size ; i++) { |
| 241 | synchronized (Watchdog.this) { |
| 242 | mCurrentMonitor = mMonitors.get(i); |
| 243 | } |
| 244 | mCurrentMonitor.monitor(); |
| 245 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 247 | synchronized (Watchdog.this) { |
| 248 | mCompleted = true; |
| 249 | mCurrentMonitor = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
Zimuzo | 2a05039 | 2019-05-09 12:51:33 +0100 | [diff] [blame] | 252 | |
| 253 | /** Pause the HandlerChecker. */ |
| 254 | public void pauseLocked(String reason) { |
| 255 | mPauseCount++; |
| 256 | // Mark as completed, because there's a chance we called this after the watchog |
| 257 | // thread loop called Object#wait after 'WAITED_HALF'. In that case we want to ensure |
| 258 | // the next call to #getCompletionStateLocked for this checker returns 'COMPLETED' |
| 259 | mCompleted = true; |
| 260 | Slog.i(TAG, "Pausing HandlerChecker: " + mName + " for reason: " |
| 261 | + reason + ". Pause count: " + mPauseCount); |
| 262 | } |
| 263 | |
| 264 | /** Resume the HandlerChecker from the last {@link #pauseLocked}. */ |
| 265 | public void resumeLocked(String reason) { |
| 266 | if (mPauseCount > 0) { |
| 267 | mPauseCount--; |
| 268 | Slog.i(TAG, "Resuming HandlerChecker: " + mName + " for reason: " |
| 269 | + reason + ". Pause count: " + mPauseCount); |
| 270 | } else { |
| 271 | Slog.wtf(TAG, "Already resumed HandlerChecker: " + mName); |
| 272 | } |
| 273 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | } |
| 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | final class RebootRequestReceiver extends BroadcastReceiver { |
| 277 | @Override |
| 278 | public void onReceive(Context c, Intent intent) { |
Dianne Hackborn | f6438b1 | 2013-05-09 18:53:48 -0700 | [diff] [blame] | 279 | if (intent.getIntExtra("nowait", 0) != 0) { |
| 280 | rebootSystem("Received ACTION_REBOOT broadcast"); |
| 281 | return; |
| 282 | } |
| 283 | Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Wale Ogunwale | 517daec | 2015-04-15 10:27:24 -0700 | [diff] [blame] | 287 | /** Monitor for checking the availability of binder threads. The monitor will block until |
| 288 | * there is a binder thread available to process in coming IPCs to make sure other processes |
| 289 | * can still communicate with the service. |
| 290 | */ |
| 291 | private static final class BinderThreadMonitor implements Watchdog.Monitor { |
| 292 | @Override |
| 293 | public void monitor() { |
| 294 | Binder.blockUntilThreadAvailable(); |
| 295 | } |
| 296 | } |
| 297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | public interface Monitor { |
| 299 | void monitor(); |
| 300 | } |
| 301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | public static Watchdog getInstance() { |
| 303 | if (sWatchdog == null) { |
| 304 | sWatchdog = new Watchdog(); |
| 305 | } |
| 306 | |
| 307 | return sWatchdog; |
| 308 | } |
| 309 | |
| 310 | private Watchdog() { |
| 311 | super("watchdog"); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 312 | // Initialize handler checkers for each common thread we want to check. Note |
| 313 | // that we are not currently checking the background thread, since it can |
| 314 | // potentially hold longer running operations with no guarantees about the timeliness |
| 315 | // of operations there. |
| 316 | |
| 317 | // The shared foreground thread is the main checker. It is where we |
| 318 | // will also dispatch monitor checks and do other work. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 319 | mMonitorChecker = new HandlerChecker(FgThread.getHandler(), |
| 320 | "foreground thread", DEFAULT_TIMEOUT); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 321 | mHandlerCheckers.add(mMonitorChecker); |
| 322 | // Add checker for main thread. We only do a quick check since there |
| 323 | // can be UI running on the thread. |
| 324 | mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()), |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 325 | "main thread", DEFAULT_TIMEOUT)); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 326 | // Add checker for shared UI thread. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 327 | mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(), |
| 328 | "ui thread", DEFAULT_TIMEOUT)); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 329 | // And also check IO thread. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 330 | mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(), |
| 331 | "i/o thread", DEFAULT_TIMEOUT)); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 332 | // And the display thread. |
| 333 | mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(), |
| 334 | "display thread", DEFAULT_TIMEOUT)); |
Wale Ogunwale | 1f5e53d | 2018-11-05 05:12:46 -0800 | [diff] [blame] | 335 | // And the animation thread. |
| 336 | mHandlerCheckers.add(new HandlerChecker(AnimationThread.getHandler(), |
| 337 | "animation thread", DEFAULT_TIMEOUT)); |
| 338 | // And the surface animation thread. |
| 339 | mHandlerCheckers.add(new HandlerChecker(SurfaceAnimationThread.getHandler(), |
| 340 | "surface animation thread", DEFAULT_TIMEOUT)); |
Wale Ogunwale | 517daec | 2015-04-15 10:27:24 -0700 | [diff] [blame] | 341 | |
| 342 | // Initialize monitor for Binder threads. |
| 343 | addMonitor(new BinderThreadMonitor()); |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 344 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 345 | mOpenFdMonitor = OpenFdMonitor.create(); |
| 346 | |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 347 | mInterestingJavaPids.add(Process.myPid()); |
| 348 | |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 349 | // See the notes on DEFAULT_TIMEOUT. |
| 350 | assert DB || |
| 351 | DEFAULT_TIMEOUT > ZygoteConnectionConstants.WRAPPED_PID_TIMEOUT_MILLIS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Zimuzo | b065b15 | 2019-04-16 13:33:01 +0100 | [diff] [blame] | 354 | /** |
| 355 | * Registers a {@link BroadcastReceiver} to listen to reboot broadcasts and trigger reboot. |
| 356 | * Should be called during boot after the ActivityManagerService is up and registered |
| 357 | * as a system service so it can handle registration of a {@link BroadcastReceiver}. |
| 358 | */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 359 | public void init(Context context, ActivityManagerService activity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | mActivity = activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | context.registerReceiver(new RebootRequestReceiver(), |
| 362 | new IntentFilter(Intent.ACTION_REBOOT), |
| 363 | android.Manifest.permission.REBOOT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 366 | private static boolean isInterestingJavaProcess(String processName) { |
Zim | 9ce0a7c | 2020-03-04 07:34:27 +0000 | [diff] [blame] | 367 | return processName.equals(StorageManagerService.sMediaStoreAuthorityProcessName) |
| 368 | || processName.equals("com.android.phone"); |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Notifies the watchdog when a Java process with {@code pid} is started. |
| 373 | * This process may have its stack trace dumped during an ANR. |
| 374 | */ |
| 375 | public void processStarted(String processName, int pid) { |
| 376 | if (isInterestingJavaProcess(processName)) { |
| 377 | Slog.i(TAG, "Interesting Java process " + processName + " started. Pid " + pid); |
| 378 | synchronized (this) { |
| 379 | mInterestingJavaPids.add(pid); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Notifies the watchdog when a Java process with {@code pid} dies. |
| 386 | */ |
| 387 | public void processDied(String processName, int pid) { |
| 388 | if (isInterestingJavaProcess(processName)) { |
| 389 | Slog.i(TAG, "Interesting Java process " + processName + " died. Pid " + pid); |
| 390 | synchronized (this) { |
| 391 | mInterestingJavaPids.remove(Integer.valueOf(pid)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 396 | public void setActivityController(IActivityController controller) { |
| 397 | synchronized (this) { |
| 398 | mController = controller; |
| 399 | } |
| 400 | } |
| 401 | |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 402 | public void setAllowRestart(boolean allowRestart) { |
| 403 | synchronized (this) { |
| 404 | mAllowRestart = allowRestart; |
| 405 | } |
| 406 | } |
| 407 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | public void addMonitor(Monitor monitor) { |
| 409 | synchronized (this) { |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 410 | mMonitorChecker.addMonitorLocked(monitor); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 414 | public void addThread(Handler thread) { |
| 415 | addThread(thread, DEFAULT_TIMEOUT); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 418 | public void addThread(Handler thread, long timeoutMillis) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 419 | synchronized (this) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 420 | final String name = thread.getLooper().getThread().getName(); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 421 | mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | /** |
Zimuzo | 2a05039 | 2019-05-09 12:51:33 +0100 | [diff] [blame] | 426 | * Pauses Watchdog action for the currently running thread. Useful before executing long running |
| 427 | * operations that could falsely trigger the watchdog. Each call to this will require a matching |
| 428 | * call to {@link #resumeWatchingCurrentThread}. |
| 429 | * |
| 430 | * <p>If the current thread has not been added to the Watchdog, this call is a no-op. |
| 431 | * |
| 432 | * <p>If the Watchdog is already paused for the current thread, this call adds |
| 433 | * adds another pause and will require an additional {@link #resumeCurrentThread} to resume. |
| 434 | * |
| 435 | * <p>Note: Use with care, as any deadlocks on the current thread will be undetected until all |
| 436 | * pauses have been resumed. |
| 437 | */ |
| 438 | public void pauseWatchingCurrentThread(String reason) { |
| 439 | synchronized (this) { |
| 440 | for (HandlerChecker hc : mHandlerCheckers) { |
| 441 | if (Thread.currentThread().equals(hc.getThread())) { |
| 442 | hc.pauseLocked(reason); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Resumes the last pause from {@link #pauseWatchingCurrentThread} for the currently running |
| 450 | * thread. |
| 451 | * |
| 452 | * <p>If the current thread has not been added to the Watchdog, this call is a no-op. |
| 453 | * |
| 454 | * <p>If the Watchdog action for the current thread is already resumed, this call logs a wtf. |
| 455 | * |
| 456 | * <p>If all pauses have been resumed, the Watchdog action is finally resumed, otherwise, |
| 457 | * the Watchdog action for the current thread remains paused until resume is called at least |
| 458 | * as many times as the calls to pause. |
| 459 | */ |
| 460 | public void resumeWatchingCurrentThread(String reason) { |
| 461 | synchronized (this) { |
| 462 | for (HandlerChecker hc : mHandlerCheckers) { |
| 463 | if (Thread.currentThread().equals(hc.getThread())) { |
| 464 | hc.resumeLocked(reason); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | * Perform a full reboot of the system. |
| 472 | */ |
| 473 | void rebootSystem(String reason) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 474 | Slog.i(TAG, "Rebooting system because: " + reason); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 475 | IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE); |
| 476 | try { |
| 477 | pms.reboot(false, reason, false); |
| 478 | } catch (RemoteException ex) { |
| 479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 482 | private int evaluateCheckerCompletionLocked() { |
| 483 | int state = COMPLETED; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 484 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 485 | HandlerChecker hc = mHandlerCheckers.get(i); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 486 | state = Math.max(state, hc.getCompletionStateLocked()); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 487 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 488 | return state; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 491 | private ArrayList<HandlerChecker> getBlockedCheckersLocked() { |
| 492 | ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 493 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 494 | HandlerChecker hc = mHandlerCheckers.get(i); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 495 | if (hc.isOverdueLocked()) { |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 496 | checkers.add(hc); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 497 | } |
| 498 | } |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 499 | return checkers; |
| 500 | } |
| 501 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 502 | private String describeCheckersLocked(List<HandlerChecker> checkers) { |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 503 | StringBuilder builder = new StringBuilder(128); |
| 504 | for (int i=0; i<checkers.size(); i++) { |
| 505 | if (builder.length() > 0) { |
| 506 | builder.append(", "); |
| 507 | } |
| 508 | builder.append(checkers.get(i).describeBlockedStateLocked()); |
| 509 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 510 | return builder.toString(); |
| 511 | } |
| 512 | |
Hui Yu | 7e65c3f | 2019-04-16 17:03:16 -0700 | [diff] [blame] | 513 | private static ArrayList<Integer> getInterestingHalPids() { |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 514 | try { |
| 515 | IServiceManager serviceManager = IServiceManager.getService(); |
| 516 | ArrayList<IServiceManager.InstanceDebugInfo> dump = |
| 517 | serviceManager.debugDump(); |
| 518 | HashSet<Integer> pids = new HashSet<>(); |
| 519 | for (IServiceManager.InstanceDebugInfo info : dump) { |
| 520 | if (info.pid == IServiceManager.PidConstant.NO_PID) { |
| 521 | continue; |
| 522 | } |
| 523 | |
| 524 | if (!HAL_INTERFACES_OF_INTEREST.contains(info.interfaceName)) { |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | pids.add(info.pid); |
| 529 | } |
| 530 | return new ArrayList<Integer>(pids); |
| 531 | } catch (RemoteException e) { |
| 532 | return new ArrayList<Integer>(); |
| 533 | } |
| 534 | } |
| 535 | |
Hui Yu | 7e65c3f | 2019-04-16 17:03:16 -0700 | [diff] [blame] | 536 | static ArrayList<Integer> getInterestingNativePids() { |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 537 | ArrayList<Integer> pids = getInterestingHalPids(); |
| 538 | |
| 539 | int[] nativePids = Process.getPidsForCommands(NATIVE_STACKS_OF_INTEREST); |
| 540 | if (nativePids != null) { |
| 541 | pids.ensureCapacity(pids.size() + nativePids.length); |
| 542 | for (int i : nativePids) { |
| 543 | pids.add(i); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | return pids; |
| 548 | } |
| 549 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | @Override |
| 551 | public void run() { |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 552 | boolean waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | while (true) { |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 554 | final List<HandlerChecker> blockedCheckers; |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 555 | final String subject; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 556 | final boolean allowRestart; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 557 | int debuggerWasConnected = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | synchronized (this) { |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 559 | long timeout = CHECK_INTERVAL; |
| 560 | // Make sure we (re)spin the checkers that have become idle within |
| 561 | // this wait-and-check interval |
| 562 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 563 | HandlerChecker hc = mHandlerCheckers.get(i); |
| 564 | hc.scheduleCheckLocked(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 565 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 567 | if (debuggerWasConnected > 0) { |
| 568 | debuggerWasConnected--; |
| 569 | } |
| 570 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | // NOTE: We use uptimeMillis() here because we do not want to increment the time we |
| 572 | // wait while asleep. If the device is asleep then the thing that we are waiting |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 573 | // to timeout on is asleep as well and won't have a chance to run, causing a false |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | // positive on when to kill things. |
| 575 | long start = SystemClock.uptimeMillis(); |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 576 | while (timeout > 0) { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 577 | if (Debug.isDebuggerConnected()) { |
| 578 | debuggerWasConnected = 2; |
| 579 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | try { |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 581 | wait(timeout); |
Zimuzo | b065b15 | 2019-04-16 13:33:01 +0100 | [diff] [blame] | 582 | // Note: mHandlerCheckers and mMonitorChecker may have changed after waiting |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | } catch (InterruptedException e) { |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 584 | Log.wtf(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | } |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 586 | if (Debug.isDebuggerConnected()) { |
| 587 | debuggerWasConnected = 2; |
| 588 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 589 | timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 590 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 592 | boolean fdLimitTriggered = false; |
| 593 | if (mOpenFdMonitor != null) { |
| 594 | fdLimitTriggered = mOpenFdMonitor.monitor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | } |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 596 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 597 | if (!fdLimitTriggered) { |
| 598 | final int waitState = evaluateCheckerCompletionLocked(); |
| 599 | if (waitState == COMPLETED) { |
| 600 | // The monitors have returned; reset |
| 601 | waitedHalf = false; |
| 602 | continue; |
| 603 | } else if (waitState == WAITING) { |
| 604 | // still waiting but within their configured intervals; back off and recheck |
| 605 | continue; |
| 606 | } else if (waitState == WAITED_HALF) { |
| 607 | if (!waitedHalf) { |
Makoto Onuki | 729c41e | 2019-05-30 09:47:10 -0700 | [diff] [blame] | 608 | Slog.i(TAG, "WAITED_HALF"); |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 609 | // We've waited half the deadlock-detection interval. Pull a stack |
| 610 | // trace and wait another half. |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 611 | ArrayList<Integer> pids = new ArrayList<>(mInterestingJavaPids); |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 612 | ActivityManagerService.dumpStackTraces(pids, null, null, |
Simon MacMullen | 46755e2 | 2020-02-04 14:03:37 +0000 | [diff] [blame] | 613 | getInterestingNativePids(), null); |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 614 | waitedHalf = true; |
| 615 | } |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | // something is overdue! |
| 620 | blockedCheckers = getBlockedCheckersLocked(); |
| 621 | subject = describeCheckersLocked(blockedCheckers); |
| 622 | } else { |
| 623 | blockedCheckers = Collections.emptyList(); |
| 624 | subject = "Open FD high water mark reached"; |
| 625 | } |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 626 | allowRestart = mAllowRestart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // If we got here, that means that the system is most likely hung. |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 630 | // First collect stack traces from all threads of the system process. |
| 631 | // Then kill this process so that the system will restart. |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 632 | EventLog.writeEvent(EventLogTags.WATCHDOG, subject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | |
Zim | c0c1541 | 2020-03-04 07:29:07 +0000 | [diff] [blame] | 634 | ArrayList<Integer> pids = new ArrayList<>(mInterestingJavaPids); |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 635 | |
Simon MacMullen | dfc56be | 2020-01-07 18:31:43 +0000 | [diff] [blame] | 636 | long anrTime = SystemClock.uptimeMillis(); |
Simon MacMullen | 30f04a7 | 2020-02-05 10:57:40 +0000 | [diff] [blame] | 637 | StringBuilder report = new StringBuilder(); |
| 638 | report.append(MemoryPressureUtil.currentPsiState()); |
Simon MacMullen | dfc56be | 2020-01-07 18:31:43 +0000 | [diff] [blame] | 639 | ProcessCpuTracker processCpuTracker = new ProcessCpuTracker(false); |
Simon MacMullen | 46755e2 | 2020-02-04 14:03:37 +0000 | [diff] [blame] | 640 | StringWriter tracesFileException = new StringWriter(); |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 641 | final File stack = ActivityManagerService.dumpStackTraces( |
Simon MacMullen | 46755e2 | 2020-02-04 14:03:37 +0000 | [diff] [blame] | 642 | pids, processCpuTracker, new SparseArray<>(), getInterestingNativePids(), |
| 643 | tracesFileException); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 644 | |
| 645 | // Give some extra time to make sure the stack traces get written. |
| 646 | // The system's been hanging for a minute, another second or two won't hurt much. |
Makoto Onuki | e276b44 | 2018-08-30 09:38:44 -0700 | [diff] [blame] | 647 | SystemClock.sleep(5000); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 648 | |
Simon MacMullen | dfc56be | 2020-01-07 18:31:43 +0000 | [diff] [blame] | 649 | processCpuTracker.update(); |
Simon MacMullen | 46755e2 | 2020-02-04 14:03:37 +0000 | [diff] [blame] | 650 | report.append(processCpuTracker.printCurrentState(anrTime)); |
| 651 | report.append(tracesFileException.getBuffer()); |
Simon MacMullen | dfc56be | 2020-01-07 18:31:43 +0000 | [diff] [blame] | 652 | |
Guang Zhu | 0620c45 | 2014-10-29 14:31:48 -0700 | [diff] [blame] | 653 | // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log |
| 654 | doSysRq('w'); |
| 655 | doSysRq('l'); |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 656 | |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 657 | // Try to add the error to the dropbox, but assuming that the ActivityManager |
| 658 | // itself may be deadlocked. (which has happened, causing this statement to |
| 659 | // deadlock and the watchdog as a whole to be ineffective) |
| 660 | Thread dropboxThread = new Thread("watchdogWriteToDropbox") { |
| 661 | public void run() { |
Amith Yamasani | fa7c3689 | 2019-05-15 17:32:27 -0700 | [diff] [blame] | 662 | // If a watched thread hangs before init() is called, we don't have a |
| 663 | // valid mActivity. So we can't log the error to dropbox. |
| 664 | if (mActivity != null) { |
| 665 | mActivity.addErrorToDropBox( |
| 666 | "watchdog", null, "system_server", null, null, null, |
Simon MacMullen | 46755e2 | 2020-02-04 14:03:37 +0000 | [diff] [blame] | 667 | subject, report.toString(), stack, null); |
Amith Yamasani | fa7c3689 | 2019-05-15 17:32:27 -0700 | [diff] [blame] | 668 | } |
Muhammad Qureshi | e2b2432 | 2020-01-28 10:54:17 -0800 | [diff] [blame] | 669 | FrameworkStatsLog.write(FrameworkStatsLog.SYSTEM_SERVER_WATCHDOG_OCCURRED, |
| 670 | subject); |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 671 | } |
| 672 | }; |
| 673 | dropboxThread.start(); |
| 674 | try { |
| 675 | dropboxThread.join(2000); // wait up to 2 seconds for it to return. |
| 676 | } catch (InterruptedException ignored) {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 678 | IActivityController controller; |
| 679 | synchronized (this) { |
| 680 | controller = mController; |
| 681 | } |
| 682 | if (controller != null) { |
| 683 | Slog.i(TAG, "Reporting stuck state to activity controller"); |
| 684 | try { |
| 685 | Binder.setDumpDisabled("Service dumps disabled due to hung system process."); |
| 686 | // 1 = keep waiting, -1 = kill system |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 687 | int res = controller.systemNotResponding(subject); |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 688 | if (res >= 0) { |
| 689 | Slog.i(TAG, "Activity controller requested to coninue to wait"); |
| 690 | waitedHalf = false; |
| 691 | continue; |
| 692 | } |
| 693 | } catch (RemoteException e) { |
| 694 | } |
| 695 | } |
| 696 | |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 697 | // Only kill the process if the debugger is not attached. |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 698 | if (Debug.isDebuggerConnected()) { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 699 | debuggerWasConnected = 2; |
| 700 | } |
| 701 | if (debuggerWasConnected >= 2) { |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 702 | Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process"); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 703 | } else if (debuggerWasConnected > 0) { |
| 704 | Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process"); |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 705 | } else if (!allowRestart) { |
| 706 | Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process"); |
| 707 | } else { |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 708 | Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject); |
Andreas Gampe | 544416e | 2018-01-26 11:39:46 -0800 | [diff] [blame] | 709 | WatchdogDiagnostics.diagnoseCheckers(blockedCheckers); |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 710 | Slog.w(TAG, "*** GOODBYE!"); |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 711 | Process.killProcess(Process.myPid()); |
| 712 | System.exit(10); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | } |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 714 | |
| 715 | waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | } |
| 717 | } |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 718 | |
Guang Zhu | 0620c45 | 2014-10-29 14:31:48 -0700 | [diff] [blame] | 719 | private void doSysRq(char c) { |
| 720 | try { |
| 721 | FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger"); |
| 722 | sysrq_trigger.write(c); |
| 723 | sysrq_trigger.close(); |
| 724 | } catch (IOException e) { |
| 725 | Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e); |
| 726 | } |
| 727 | } |
| 728 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 729 | public static final class OpenFdMonitor { |
| 730 | /** |
| 731 | * Number of FDs below the soft limit that we trigger a runtime restart at. This was |
| 732 | * chosen arbitrarily, but will need to be at least 6 in order to have a sufficient number |
| 733 | * of FDs in reserve to complete a dump. |
| 734 | */ |
| 735 | private static final int FD_HIGH_WATER_MARK = 12; |
| 736 | |
| 737 | private final File mDumpDir; |
| 738 | private final File mFdHighWaterMark; |
| 739 | |
| 740 | public static OpenFdMonitor create() { |
| 741 | // Only run the FD monitor on debuggable builds (such as userdebug and eng builds). |
| 742 | if (!Build.IS_DEBUGGABLE) { |
| 743 | return null; |
| 744 | } |
| 745 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 746 | final StructRlimit rlimit; |
| 747 | try { |
| 748 | rlimit = android.system.Os.getrlimit(OsConstants.RLIMIT_NOFILE); |
| 749 | } catch (ErrnoException errno) { |
| 750 | Slog.w(TAG, "Error thrown from getrlimit(RLIMIT_NOFILE)", errno); |
| 751 | return null; |
| 752 | } |
| 753 | |
| 754 | // The assumption we're making here is that FD numbers are allocated (more or less) |
| 755 | // sequentially, which is currently (and historically) true since open is currently |
| 756 | // specified to always return the lowest-numbered non-open file descriptor for the |
| 757 | // current process. |
| 758 | // |
| 759 | // We do this to avoid having to enumerate the contents of /proc/self/fd in order to |
| 760 | // count the number of descriptors open in the process. |
| 761 | final File fdThreshold = new File("/proc/self/fd/" + (rlimit.rlim_cur - FD_HIGH_WATER_MARK)); |
Elliott Hughes | 4e4caa7 | 2018-03-23 11:06:36 -0700 | [diff] [blame] | 762 | return new OpenFdMonitor(new File("/data/anr"), fdThreshold); |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | OpenFdMonitor(File dumpDir, File fdThreshold) { |
| 766 | mDumpDir = dumpDir; |
| 767 | mFdHighWaterMark = fdThreshold; |
| 768 | } |
| 769 | |
Nandana Dutt | 6647ef5 | 2018-07-12 17:02:57 +0100 | [diff] [blame] | 770 | /** |
| 771 | * Dumps open file descriptors and their full paths to a temporary file in {@code mDumpDir}. |
| 772 | */ |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 773 | private void dumpOpenDescriptors() { |
Nandana Dutt | 6647ef5 | 2018-07-12 17:02:57 +0100 | [diff] [blame] | 774 | // We cannot exec lsof to get more info about open file descriptors because a newly |
| 775 | // forked process will not have the permissions to readlink. Instead list all open |
| 776 | // descriptors from /proc/pid/fd and resolve them. |
| 777 | List<String> dumpInfo = new ArrayList<>(); |
| 778 | String fdDirPath = String.format("/proc/%d/fd/", Process.myPid()); |
| 779 | File[] fds = new File(fdDirPath).listFiles(); |
| 780 | if (fds == null) { |
| 781 | dumpInfo.add("Unable to list " + fdDirPath); |
| 782 | } else { |
| 783 | for (File f : fds) { |
| 784 | String fdSymLink = f.getAbsolutePath(); |
| 785 | String resolvedPath = ""; |
| 786 | try { |
| 787 | resolvedPath = Os.readlink(fdSymLink); |
| 788 | } catch (ErrnoException ex) { |
| 789 | resolvedPath = ex.getMessage(); |
| 790 | } |
| 791 | dumpInfo.add(fdSymLink + "\t" + resolvedPath); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | // Dump the fds & paths to a temp file. |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 796 | try { |
| 797 | File dumpFile = File.createTempFile("anr_fd_", "", mDumpDir); |
Nandana Dutt | 6647ef5 | 2018-07-12 17:02:57 +0100 | [diff] [blame] | 798 | Path out = Paths.get(dumpFile.getAbsolutePath()); |
| 799 | Files.write(out, dumpInfo, StandardCharsets.UTF_8); |
| 800 | } catch (IOException ex) { |
| 801 | Slog.w(TAG, "Unable to write open descriptors to file: " + ex); |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * @return {@code true} if the high water mark was breached and a dump was written, |
| 807 | * {@code false} otherwise. |
| 808 | */ |
| 809 | public boolean monitor() { |
| 810 | if (mFdHighWaterMark.exists()) { |
| 811 | dumpOpenDescriptors(); |
| 812 | return true; |
| 813 | } |
| 814 | |
| 815 | return false; |
| 816 | } |
| 817 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | } |