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